Merge pull request #187 from A-Hilaly/gitlab-support

Amine created

Gitlab bridge exporter

Change summary

bridge/core/export.go        |   5 
bridge/gitlab/export.go      | 485 +++++++++++++++++++++++++++++++++++++
bridge/gitlab/export_test.go | 295 +++++++++++++++++++++++
bridge/gitlab/gitlab.go      |   2 
bridge/gitlab/import.go      |   2 
commands/bridge_push.go      |   1 
6 files changed, 782 insertions(+), 8 deletions(-)

Detailed changes

bridge/core/export.go 🔗

@@ -62,8 +62,9 @@ func (er ExportResult) String() string {
 
 func NewExportError(err error, id entity.Id) ExportResult {
 	return ExportResult{
-		ID:  id,
-		Err: err,
+		ID:    id,
+		Err:   err,
+		Event: ExportEventError,
 	}
 }
 

bridge/gitlab/export.go 🔗

@@ -1,12 +1,18 @@
 package gitlab
 
 import (
+	"context"
+	"fmt"
+	"strconv"
 	"time"
 
 	"github.com/pkg/errors"
+	"github.com/xanzy/go-gitlab"
 
 	"github.com/MichaelMure/git-bug/bridge/core"
+	"github.com/MichaelMure/git-bug/bug"
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/entity"
 )
 
 var (
@@ -14,14 +20,487 @@ var (
 )
 
 // gitlabExporter implement the Exporter interface
-type gitlabExporter struct{}
+type gitlabExporter struct {
+	conf core.Configuration
+
+	// cache identities clients
+	identityClient map[string]*gitlab.Client
+
+	// map identities with their tokens
+	identityToken map[string]string
+
+	// gitlab repository ID
+	repositoryID string
+
+	// cache identifiers used to speed up exporting operations
+	// cleared for each bug
+	cachedOperationIDs map[string]string
+}
 
 // Init .
 func (ge *gitlabExporter) Init(conf core.Configuration) error {
+	ge.conf = conf
+	//TODO: initialize with multiple tokens
+	ge.identityToken = make(map[string]string)
+	ge.identityClient = make(map[string]*gitlab.Client)
+	ge.cachedOperationIDs = make(map[string]string)
+
 	return nil
 }
 
+// getIdentityClient return a gitlab v4 API client configured with the access token of the given identity.
+// if no client were found it will initialize it from the known tokens map and cache it for next use
+func (ge *gitlabExporter) getIdentityClient(id entity.Id) (*gitlab.Client, error) {
+	client, ok := ge.identityClient[id.String()]
+	if ok {
+		return client, nil
+	}
+
+	// get token
+	token, ok := ge.identityToken[id.String()]
+	if !ok {
+		return nil, ErrMissingIdentityToken
+	}
+
+	// create client
+	client = buildClient(token)
+	// cache client
+	ge.identityClient[id.String()] = client
+
+	return client, nil
+}
+
 // ExportAll export all event made by the current user to Gitlab
-func (ge *gitlabExporter) ExportAll(repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
-	return nil, nil
+func (ge *gitlabExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
+	out := make(chan core.ExportResult)
+
+	user, err := repo.GetUserIdentity()
+	if err != nil {
+		return nil, err
+	}
+
+	ge.identityToken[user.Id().String()] = ge.conf[keyToken]
+
+	// get repository node id
+	ge.repositoryID = ge.conf[keyProjectID]
+
+	go func() {
+		defer close(out)
+
+		allIdentitiesIds := make([]entity.Id, 0, len(ge.identityToken))
+		for id := range ge.identityToken {
+			allIdentitiesIds = append(allIdentitiesIds, entity.Id(id))
+		}
+
+		allBugsIds := repo.AllBugsIds()
+
+		for _, id := range allBugsIds {
+			select {
+			case <-ctx.Done():
+				return
+			default:
+				b, err := repo.ResolveBug(id)
+				if err != nil {
+					out <- core.NewExportError(err, id)
+					return
+				}
+
+				snapshot := b.Snapshot()
+
+				// ignore issues created before since date
+				// TODO: compare the Lamport time instead of using the unix time
+				if snapshot.CreatedAt.Before(since) {
+					out <- core.NewExportNothing(b.Id(), "bug created before the since date")
+					continue
+				}
+
+				if snapshot.HasAnyActor(allIdentitiesIds...) {
+					// try to export the bug and it associated events
+					ge.exportBug(ctx, b, since, out)
+				} else {
+					out <- core.NewExportNothing(id, "not an actor")
+				}
+			}
+		}
+	}()
+
+	return out, nil
+}
+
+// exportBug publish bugs and related events
+func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
+	snapshot := b.Snapshot()
+
+	var err error
+	var bugGitlabID int
+	var bugGitlabIDString string
+	var bugCreationId string
+
+	// Special case:
+	// if a user try to export a bug that is not already exported to Gitlab (or imported
+	// from Gitlab) and we do not have the token of the bug author, there is nothing we can do.
+
+	// skip bug if origin is not allowed
+	origin, ok := snapshot.GetCreateMetadata(core.KeyOrigin)
+	if ok && origin != target {
+		out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
+		return
+	}
+
+	// first operation is always createOp
+	createOp := snapshot.Operations[0].(*bug.CreateOperation)
+	author := snapshot.Author
+
+	// get gitlab bug ID
+	gitlabID, ok := snapshot.GetCreateMetadata(keyGitlabId)
+	if ok {
+		projectID, ok := snapshot.GetCreateMetadata(keyGitlabProject)
+		if !ok {
+			err := fmt.Errorf("expected to find gitlab project id")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+
+		if projectID != ge.conf[keyProjectID] {
+			out <- core.NewExportNothing(b.Id(), "skipping issue imported from another repository")
+			return
+		}
+
+		out <- core.NewExportNothing(b.Id(), "bug already exported")
+
+		// will be used to mark operation related to a bug as exported
+		bugGitlabIDString = gitlabID
+		bugGitlabID, err = strconv.Atoi(bugGitlabIDString)
+		if err != nil {
+			out <- core.NewExportError(fmt.Errorf("unexpected gitlab id format: %s", bugGitlabIDString), b.Id())
+			return
+		}
+
+	} else {
+		// check that we have a token for operation author
+		client, err := ge.getIdentityClient(author.Id())
+		if err != nil {
+			// if bug is still not exported and we do not have the author stop the execution
+			out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
+			return
+		}
+
+		// create bug
+		_, id, url, err := createGitlabIssue(ctx, client, ge.repositoryID, createOp.Title, createOp.Message)
+		if err != nil {
+			err := errors.Wrap(err, "exporting gitlab issue")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+
+		idString := strconv.Itoa(id)
+		out <- core.NewExportBug(b.Id())
+
+		_, err = b.SetMetadata(
+			createOp.Id(),
+			map[string]string{
+				keyGitlabId:      idString,
+				keyGitlabUrl:     url,
+				keyGitlabProject: ge.repositoryID,
+			},
+		)
+		if err != nil {
+			err := errors.Wrap(err, "marking operation as exported")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+
+		// commit operation to avoid creating multiple issues with multiple pushes
+		if err := b.CommitAsNeeded(); err != nil {
+			err := errors.Wrap(err, "bug commit")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+
+		// cache bug gitlab ID and URL
+		bugGitlabID = id
+		bugGitlabIDString = idString
+	}
+
+	bugCreationId = createOp.Id().String()
+	// cache operation gitlab id
+	ge.cachedOperationIDs[bugCreationId] = bugGitlabIDString
+
+	labelSet := make(map[string]struct{})
+	for _, op := range snapshot.Operations[1:] {
+		// ignore SetMetadata operations
+		if _, ok := op.(*bug.SetMetadataOperation); ok {
+			continue
+		}
+
+		// ignore operations already existing in gitlab (due to import or export)
+		// cache the ID of already exported or imported issues and events from Gitlab
+		if id, ok := op.GetMetadata(keyGitlabId); ok {
+			ge.cachedOperationIDs[op.Id().String()] = id
+			out <- core.NewExportNothing(op.Id(), "already exported operation")
+			continue
+		}
+
+		opAuthor := op.GetAuthor()
+		client, err := ge.getIdentityClient(opAuthor.Id())
+		if err != nil {
+			out <- core.NewExportNothing(op.Id(), "missing operation author token")
+			continue
+		}
+
+		var id int
+		var idString, url string
+		switch op := op.(type) {
+		case *bug.AddCommentOperation:
+
+			// send operation to gitlab
+			id, err = addCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, op.Message)
+			if err != nil {
+				err := errors.Wrap(err, "adding comment")
+				out <- core.NewExportError(err, b.Id())
+				return
+			}
+
+			out <- core.NewExportComment(op.Id())
+
+			idString = strconv.Itoa(id)
+			// cache comment id
+			ge.cachedOperationIDs[op.Id().String()] = idString
+
+		case *bug.EditCommentOperation:
+			targetId := op.Target.String()
+
+			// Since gitlab doesn't consider the issue body as a comment
+			if targetId == bugCreationId {
+
+				// case bug creation operation: we need to edit the Gitlab issue
+				if err := updateGitlabIssueBody(ctx, client, ge.repositoryID, bugGitlabID, op.Message); err != nil {
+					err := errors.Wrap(err, "editing issue")
+					out <- core.NewExportError(err, b.Id())
+					return
+				}
+
+				out <- core.NewExportCommentEdition(op.Id())
+				id = bugGitlabID
+
+			} else {
+
+				// case comment edition operation: we need to edit the Gitlab comment
+				commentID, ok := ge.cachedOperationIDs[targetId]
+				if !ok {
+					out <- core.NewExportError(fmt.Errorf("unexpected error: comment id not found"), op.Target)
+					return
+				}
+
+				commentIDint, err := strconv.Atoi(commentID)
+				if err != nil {
+					out <- core.NewExportError(fmt.Errorf("unexpected comment id format"), op.Target)
+					return
+				}
+
+				if err := editCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, commentIDint, op.Message); err != nil {
+					err := errors.Wrap(err, "editing comment")
+					out <- core.NewExportError(err, b.Id())
+					return
+				}
+
+				out <- core.NewExportCommentEdition(op.Id())
+				id = commentIDint
+			}
+
+		case *bug.SetStatusOperation:
+			if err := updateGitlabIssueStatus(ctx, client, ge.repositoryID, bugGitlabID, op.Status); err != nil {
+				err := errors.Wrap(err, "editing status")
+				out <- core.NewExportError(err, b.Id())
+				return
+			}
+
+			out <- core.NewExportStatusChange(op.Id())
+			id = bugGitlabID
+
+		case *bug.SetTitleOperation:
+			if err := updateGitlabIssueTitle(ctx, client, ge.repositoryID, bugGitlabID, op.Title); err != nil {
+				err := errors.Wrap(err, "editing title")
+				out <- core.NewExportError(err, b.Id())
+				return
+			}
+
+			out <- core.NewExportTitleEdition(op.Id())
+			id = bugGitlabID
+
+		case *bug.LabelChangeOperation:
+			// we need to set the actual list of labels at each label change operation
+			// because gitlab update issue requests need directly the latest list of the verison
+
+			for _, label := range op.Added {
+				labelSet[label.String()] = struct{}{}
+			}
+
+			for _, label := range op.Removed {
+				delete(labelSet, label.String())
+			}
+
+			labels := make([]string, 0, len(labelSet))
+			for key := range labelSet {
+				labels = append(labels, key)
+			}
+
+			if err := updateGitlabIssueLabels(ctx, client, ge.repositoryID, bugGitlabID, labels); err != nil {
+				err := errors.Wrap(err, "updating labels")
+				out <- core.NewExportError(err, b.Id())
+				return
+			}
+
+			out <- core.NewExportLabelChange(op.Id())
+			id = bugGitlabID
+		default:
+			panic("unhandled operation type case")
+		}
+
+		idString = strconv.Itoa(id)
+		// mark operation as exported
+		if err := markOperationAsExported(b, op.Id(), idString, url); err != nil {
+			err := errors.Wrap(err, "marking operation as exported")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+
+		// commit at each operation export to avoid exporting same events multiple times
+		if err := b.CommitAsNeeded(); err != nil {
+			err := errors.Wrap(err, "bug commit")
+			out <- core.NewExportError(err, b.Id())
+			return
+		}
+	}
+}
+
+func markOperationAsExported(b *cache.BugCache, target entity.Id, gitlabID, gitlabURL string) error {
+	_, err := b.SetMetadata(
+		target,
+		map[string]string{
+			keyGitlabId:  gitlabID,
+			keyGitlabUrl: gitlabURL,
+		},
+	)
+
+	return err
+}
+
+// create a gitlab. issue and return it ID
+func createGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID, title, body string) (int, int, string, error) {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	issue, _, err := gc.Issues.CreateIssue(
+		repositoryID,
+		&gitlab.CreateIssueOptions{
+			Title:       &title,
+			Description: &body,
+		},
+		gitlab.WithContext(ctx),
+	)
+	if err != nil {
+		return 0, 0, "", err
+	}
+
+	return issue.ID, issue.IID, issue.WebURL, nil
+}
+
+// add a comment to an issue and return it ID
+func addCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) (int, error) {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	note, _, err := gc.Notes.CreateIssueNote(
+		repositoryID, issueID,
+		&gitlab.CreateIssueNoteOptions{
+			Body: &body,
+		},
+		gitlab.WithContext(ctx),
+	)
+	if err != nil {
+		return 0, err
+	}
+
+	return note.ID, nil
+}
+
+func editCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID, noteID int, body string) error {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	_, _, err := gc.Notes.UpdateIssueNote(
+		repositoryID, issueID, noteID,
+		&gitlab.UpdateIssueNoteOptions{
+			Body: &body,
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return err
+}
+
+func updateGitlabIssueStatus(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, status bug.Status) error {
+	var state string
+
+	switch status {
+	case bug.OpenStatus:
+		state = "reopen"
+	case bug.ClosedStatus:
+		state = "close"
+	default:
+		panic("unknown bug state")
+	}
+
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	_, _, err := gc.Issues.UpdateIssue(
+		repositoryID, issueID,
+		&gitlab.UpdateIssueOptions{
+			StateEvent: &state,
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return err
+}
+
+func updateGitlabIssueBody(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) error {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	_, _, err := gc.Issues.UpdateIssue(
+		repositoryID, issueID,
+		&gitlab.UpdateIssueOptions{
+			Description: &body,
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return err
+}
+
+func updateGitlabIssueTitle(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, title string) error {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	_, _, err := gc.Issues.UpdateIssue(
+		repositoryID, issueID,
+		&gitlab.UpdateIssueOptions{
+			Title: &title,
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return err
+}
+
+// update gitlab. issue labels
+func updateGitlabIssueLabels(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, labels []string) error {
+	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
+	defer cancel()
+	_, _, err := gc.Issues.UpdateIssue(
+		repositoryID, issueID,
+		&gitlab.UpdateIssueOptions{
+			Labels: labels,
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return err
 }

bridge/gitlab/export_test.go 🔗

@@ -1 +1,296 @@
 package gitlab
+
+import (
+	"context"
+	"fmt"
+	"math/rand"
+	"os"
+	"strconv"
+	"testing"
+	"time"
+
+	"github.com/xanzy/go-gitlab"
+
+	"github.com/stretchr/testify/require"
+
+	"github.com/MichaelMure/git-bug/bridge/core"
+	"github.com/MichaelMure/git-bug/bug"
+	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/repository"
+	"github.com/MichaelMure/git-bug/util/interrupt"
+)
+
+const (
+	testRepoBaseName = "git-bug-test-gitlab-exporter"
+)
+
+type testCase struct {
+	name     string
+	bug      *cache.BugCache
+	numOp    int // number of original operations
+	numOpExp int // number of operations after export
+	numOpImp int // number of operations after import
+}
+
+func testCases(t *testing.T, repo *cache.RepoCache, identity *cache.IdentityCache) []*testCase {
+	// simple bug
+	simpleBug, _, err := repo.NewBug("simple bug", "new bug")
+	require.NoError(t, err)
+
+	// bug with comments
+	bugWithComments, _, err := repo.NewBug("bug with comments", "new bug")
+	require.NoError(t, err)
+
+	_, err = bugWithComments.AddComment("new comment")
+	require.NoError(t, err)
+
+	// bug with label changes
+	bugLabelChange, _, err := repo.NewBug("bug label change", "new bug")
+	require.NoError(t, err)
+
+	_, _, err = bugLabelChange.ChangeLabels([]string{"bug"}, nil)
+	require.NoError(t, err)
+
+	_, _, err = bugLabelChange.ChangeLabels([]string{"core"}, nil)
+	require.NoError(t, err)
+
+	_, _, err = bugLabelChange.ChangeLabels(nil, []string{"bug"})
+	require.NoError(t, err)
+
+	// bug with comments editions
+	bugWithCommentEditions, createOp, err := repo.NewBug("bug with comments editions", "new bug")
+	require.NoError(t, err)
+
+	_, err = bugWithCommentEditions.EditComment(createOp.Id(), "first comment edited")
+	require.NoError(t, err)
+
+	commentOp, err := bugWithCommentEditions.AddComment("first comment")
+	require.NoError(t, err)
+
+	_, err = bugWithCommentEditions.EditComment(commentOp.Id(), "first comment edited")
+	require.NoError(t, err)
+
+	// bug status changed
+	bugStatusChanged, _, err := repo.NewBug("bug status changed", "new bug")
+	require.NoError(t, err)
+
+	_, err = bugStatusChanged.Close()
+	require.NoError(t, err)
+
+	_, err = bugStatusChanged.Open()
+	require.NoError(t, err)
+
+	// bug title changed
+	bugTitleEdited, _, err := repo.NewBug("bug title edited", "new bug")
+	require.NoError(t, err)
+
+	_, err = bugTitleEdited.SetTitle("bug title edited again")
+	require.NoError(t, err)
+
+	return []*testCase{
+		&testCase{
+			name:     "simple bug",
+			bug:      simpleBug,
+			numOp:    1,
+			numOpExp: 2,
+			numOpImp: 1,
+		},
+		&testCase{
+			name:     "bug with comments",
+			bug:      bugWithComments,
+			numOp:    2,
+			numOpExp: 4,
+			numOpImp: 2,
+		},
+		&testCase{
+			name:     "bug label change",
+			bug:      bugLabelChange,
+			numOp:    4,
+			numOpExp: 8,
+			numOpImp: 4,
+		},
+		&testCase{
+			name:     "bug with comment editions",
+			bug:      bugWithCommentEditions,
+			numOp:    4,
+			numOpExp: 8,
+			numOpImp: 2,
+		},
+		&testCase{
+			name:     "bug changed status",
+			bug:      bugStatusChanged,
+			numOp:    3,
+			numOpExp: 6,
+			numOpImp: 3,
+		},
+		&testCase{
+			name:     "bug title edited",
+			bug:      bugTitleEdited,
+			numOp:    2,
+			numOpExp: 4,
+			numOpImp: 2,
+		},
+	}
+}
+
+func TestPushPull(t *testing.T) {
+	// token must have 'repo' and 'delete_repo' scopes
+	token := os.Getenv("GITLAB_API_TOKEN")
+	if token == "" {
+		t.Skip("Env var GITLAB_API_TOKEN missing")
+	}
+
+	// create repo backend
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
+
+	backend, err := cache.NewRepoCache(repo)
+	require.NoError(t, err)
+
+	// set author identity
+	author, err := backend.NewIdentity("test identity", "test@test.org")
+	require.NoError(t, err)
+
+	err = backend.SetUserIdentity(author)
+	require.NoError(t, err)
+
+	defer backend.Close()
+	interrupt.RegisterCleaner(backend.Close)
+
+	tests := testCases(t, backend, author)
+
+	// generate project name
+	projectName := generateRepoName()
+
+	// create target Gitlab repository
+	projectID, err := createRepository(context.TODO(), projectName, token)
+	require.NoError(t, err)
+
+	fmt.Println("created repository", projectName)
+
+	// Make sure to remove the Gitlab repository when the test end
+	defer func(t *testing.T) {
+		if err := deleteRepository(context.TODO(), projectID, token); err != nil {
+			t.Fatal(err)
+		}
+		fmt.Println("deleted repository:", projectName)
+	}(t)
+
+	interrupt.RegisterCleaner(func() error {
+		return deleteRepository(context.TODO(), projectID, token)
+	})
+
+	// initialize exporter
+	exporter := &gitlabExporter{}
+	err = exporter.Init(core.Configuration{
+		keyProjectID: strconv.Itoa(projectID),
+		keyToken:     token,
+	})
+	require.NoError(t, err)
+
+	ctx := context.Background()
+	start := time.Now()
+
+	// export all bugs
+	exportEvents, err := exporter.ExportAll(ctx, backend, time.Time{})
+	require.NoError(t, err)
+
+	for result := range exportEvents {
+		require.NoError(t, result.Err)
+	}
+	require.NoError(t, err)
+
+	fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
+
+	repoTwo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repoTwo)
+
+	// create a second backend
+	backendTwo, err := cache.NewRepoCache(repoTwo)
+	require.NoError(t, err)
+
+	importer := &gitlabImporter{}
+	err = importer.Init(core.Configuration{
+		keyProjectID: strconv.Itoa(projectID),
+		keyToken:     token,
+	})
+	require.NoError(t, err)
+
+	// import all exported bugs to the second backend
+	importEvents, err := importer.ImportAll(ctx, backendTwo, time.Time{})
+	require.NoError(t, err)
+
+	for result := range importEvents {
+		require.NoError(t, result.Err)
+	}
+
+	require.Len(t, backendTwo.AllBugsIds(), len(tests))
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			// for each operation a SetMetadataOperation will be added
+			// so number of operations should double
+			require.Len(t, tt.bug.Snapshot().Operations, tt.numOpExp)
+
+			// verify operation have correct metadata
+			for _, op := range tt.bug.Snapshot().Operations {
+				// Check if the originals operations (*not* SetMetadata) are tagged properly
+				if _, ok := op.(*bug.SetMetadataOperation); !ok {
+					_, haveIDMetadata := op.GetMetadata(keyGitlabId)
+					require.True(t, haveIDMetadata)
+
+					_, haveURLMetada := op.GetMetadata(keyGitlabUrl)
+					require.True(t, haveURLMetada)
+				}
+			}
+
+			// get bug gitlab ID
+			bugGitlabID, ok := tt.bug.Snapshot().GetCreateMetadata(keyGitlabId)
+			require.True(t, ok)
+
+			// retrieve bug from backendTwo
+			importedBug, err := backendTwo.ResolveBugCreateMetadata(keyGitlabId, bugGitlabID)
+			require.NoError(t, err)
+
+			// verify bug have same number of original operations
+			require.Len(t, importedBug.Snapshot().Operations, tt.numOpImp)
+
+			// verify bugs are taged with origin=gitlab
+			issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(core.KeyOrigin)
+			require.True(t, ok)
+			require.Equal(t, issueOrigin, target)
+
+			//TODO: maybe more tests to ensure bug final state
+		})
+	}
+}
+
+func generateRepoName() string {
+	rand.Seed(time.Now().UnixNano())
+	var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
+	b := make([]rune, 8)
+	for i := range b {
+		b[i] = letterRunes[rand.Intn(len(letterRunes))]
+	}
+	return fmt.Sprintf("%s-%s", testRepoBaseName, string(b))
+}
+
+// create repository need a token with scope 'repo'
+func createRepository(ctx context.Context, name, token string) (int, error) {
+	client := buildClient(token)
+	project, _, err := client.Projects.CreateProject(
+		&gitlab.CreateProjectOptions{
+			Name: gitlab.String(name),
+		},
+		gitlab.WithContext(ctx),
+	)
+
+	return project.ID, err
+}
+
+// delete repository need a token with scope 'delete_repo'
+func deleteRepository(ctx context.Context, project int, token string) error {
+	client := buildClient(token)
+	_, err := client.Projects.DeleteProject(project, gitlab.WithContext(ctx))
+	return err
+}

bridge/gitlab/gitlab.go 🔗

@@ -38,7 +38,7 @@ func (*Gitlab) NewImporter() core.Importer {
 }
 
 func (*Gitlab) NewExporter() core.Exporter {
-	return nil
+	return &gitlabExporter{}
 }
 
 func buildClient(token string) *gitlab.Client {

bridge/gitlab/import.go 🔗

@@ -121,7 +121,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
 		nil,
 		map[string]string{
 			core.KeyOrigin:   target,
-			keyGitlabId:      parseID(issue.ID),
+			keyGitlabId:      parseID(issue.IID),
 			keyGitlabUrl:     issue.WebURL,
 			keyGitlabProject: gi.conf[keyProjectID],
 		},

commands/bridge_push.go 🔗

@@ -85,7 +85,6 @@ func runBridgePush(cmd *cobra.Command, args []string) error {
 	close(done)
 
 	fmt.Printf("Successfully exported %d issues with %s bridge\n", exportedIssues, b.Name)
-
 	return nil
 }