From 01c0f644b2b94589f4f597a90d6245d5e2c0ad17 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Tue, 9 Jul 2019 22:56:38 +0200 Subject: bridge/gitlab: init new bridge --- bridge/gitlab/import.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bridge/gitlab/import.go (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go new file mode 100644 index 00000000..dec90a6c --- /dev/null +++ b/bridge/gitlab/import.go @@ -0,0 +1,30 @@ +package gitlab + +import ( + "time" + + "github.com/MichaelMure/git-bug/bridge/core" + "github.com/MichaelMure/git-bug/cache" +) + +const ( + keyGitlabLogin = "gitlab-login" +) + +type gitlabImporter struct { + conf core.Configuration + + // number of imported issues + importedIssues int + + // number of imported identities + importedIdentities int +} + +func (*gitlabImporter) Init(conf core.Configuration) error { + return nil +} + +func (*gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) error { + return nil +} -- cgit From 8b6c896369bc48599bc97181a3f3a85a9425af87 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Wed, 17 Jul 2019 00:06:42 +0200 Subject: bridge/gitlab: complete importer --- bridge/gitlab/import.go | 311 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 309 insertions(+), 2 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index dec90a6c..1ac8eaf3 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -1,10 +1,15 @@ package gitlab import ( + "fmt" "time" + "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/util/text" ) const ( @@ -14,6 +19,9 @@ const ( type gitlabImporter struct { conf core.Configuration + // iterator + iterator *iterator + // number of imported issues importedIssues int @@ -21,10 +29,309 @@ type gitlabImporter struct { importedIdentities int } -func (*gitlabImporter) Init(conf core.Configuration) error { +func (gi *gitlabImporter) Init(conf core.Configuration) error { + gi.conf = conf + return nil +} + +func (gi *gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) error { + gi.iterator = NewIterator(gi.conf[keyProjectID], gi.conf[keyToken], since) + + // Loop over all matching issues + for gi.iterator.NextIssue() { + issue := gi.iterator.IssueValue() + fmt.Printf("importing issue: %v\n", issue.Title) + + // create issue + b, err := gi.ensureIssue(repo, issue) + if err != nil { + return fmt.Errorf("issue creation: %v", err) + } + + // Loop over all notes + for gi.iterator.NextNote() { + note := gi.iterator.NoteValue() + if err := gi.ensureNote(repo, b, note); err != nil { + return fmt.Errorf("note creation: %v", err) + } + } + + // Loop over all label events + for gi.iterator.NextLabelEvent() { + labelEvent := gi.iterator.LabelEventValue() + if err := gi.ensureLabelEvent(repo, b, labelEvent); err != nil { + return fmt.Errorf("label event creation: %v", err) + } + + } + + // commit bug state + if err := b.CommitAsNeeded(); err != nil { + return fmt.Errorf("bug commit: %v", err) + } + } + + if err := gi.iterator.Error(); err != nil { + fmt.Printf("import error: %v\n", err) + return err + } + + fmt.Printf("Successfully imported %d issues and %d identities from Gitlab\n", gi.importedIssues, gi.importedIdentities) return nil } -func (*gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) error { +func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue) (*cache.BugCache, error) { + // ensure issue author + author, err := gi.ensurePerson(repo, issue.Author.ID) + if err != nil { + return nil, err + } + + // resolve bug + b, err := repo.ResolveBugCreateMetadata(keyGitlabUrl, issue.WebURL) + if err != nil && err != bug.ErrBugNotExist { + return nil, err + } + + if err == bug.ErrBugNotExist { + cleanText, err := text.Cleanup(string(issue.Description)) + if err != nil { + return nil, err + } + + // create bug + b, _, err = repo.NewBugRaw( + author, + issue.CreatedAt.Unix(), + issue.Title, + cleanText, + nil, + map[string]string{ + keyOrigin: target, + keyGitlabId: parseID(issue.ID), + keyGitlabUrl: issue.WebURL, + }, + ) + + if err != nil { + return nil, err + } + + // importing a new bug + gi.importedIssues++ + + return b, nil + } + + return nil, nil +} + +func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error { + id := parseID(note.ID) + + hash, err := b.ResolveOperationWithMetadata(keyGitlabId, id) + if err != cache.ErrNoMatchingOp { + return err + } + + // ensure issue author + author, err := gi.ensurePerson(repo, note.Author.ID) + if err != nil { + return err + } + + noteType, body := GetNoteType(note) + switch noteType { + case NOTE_CLOSED: + _, err = b.CloseRaw( + author, + note.CreatedAt.Unix(), + map[string]string{ + keyGitlabId: id, + }, + ) + return err + + case NOTE_REOPENED: + _, err = b.OpenRaw( + author, + note.CreatedAt.Unix(), + map[string]string{ + keyGitlabId: id, + }, + ) + return err + + case NOTE_DESCRIPTION_CHANGED: + issue := gi.iterator.IssueValue() + + // since gitlab doesn't provide the issue history + // we should check for "changed the description" notes and compare issue texts + + if issue.Description != b.Snapshot().Comments[0].Message { + // comment edition + _, err = b.EditCommentRaw( + author, + note.UpdatedAt.Unix(), + target, + issue.Description, + map[string]string{ + keyGitlabId: id, + keyGitlabUrl: "", + }, + ) + + return err + + } + + case NOTE_COMMENT: + + cleanText, err := text.Cleanup(body) + if err != nil { + return err + } + + // if we didn't import the comment + if err == cache.ErrNoMatchingOp { + + // add comment operation + _, err = b.AddCommentRaw( + author, + note.CreatedAt.Unix(), + cleanText, + nil, + map[string]string{ + keyGitlabId: id, + keyGitlabUrl: "", + }, + ) + + return err + } + + // if comment was already exported + + // if note wasn't updated + if note.UpdatedAt.Equal(*note.CreatedAt) { + return nil + } + + // search for last comment update + timeline, err := b.Snapshot().SearchTimelineItem(hash) + if err != nil { + return err + } + + item, ok := timeline.(*bug.AddCommentTimelineItem) + if !ok { + return fmt.Errorf("expected add comment time line") + } + + // compare local bug comment with the new note body + if item.Message != cleanText { + // comment edition + _, err = b.EditCommentRaw( + author, + note.UpdatedAt.Unix(), + target, + cleanText, + map[string]string{ + // no metadata unique metadata to store + keyGitlabId: "", + keyGitlabUrl: "", + }, + ) + + return err + } + + return nil + + case NOTE_TITLE_CHANGED: + + _, err = b.SetTitleRaw( + author, + note.CreatedAt.Unix(), + body, + map[string]string{ + keyGitlabId: id, + keyGitlabUrl: "", + }, + ) + + return err + + default: + // non handled note types + + return nil + } + return nil } + +func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCache, labelEvent *gitlab.LabelEvent) error { + _, err := b.ResolveOperationWithMetadata(keyGitlabId, parseID(labelEvent.ID)) + if err != cache.ErrNoMatchingOp { + return err + } + + // ensure issue author + author, err := gi.ensurePerson(repo, labelEvent.User.ID) + if err != nil { + return err + } + + switch labelEvent.Action { + case "add": + _, err = b.ForceChangeLabelsRaw( + author, + labelEvent.CreatedAt.Unix(), + []string{labelEvent.Label.Name}, + nil, + map[string]string{ + keyGitlabId: parseID(labelEvent.ID), + }, + ) + + case "remove": + _, err = b.ForceChangeLabelsRaw( + author, + labelEvent.CreatedAt.Unix(), + nil, + []string{labelEvent.Label.Name}, + map[string]string{ + keyGitlabId: parseID(labelEvent.ID), + }, + ) + + default: + panic("unexpected label event action") + } + + return err +} + +func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.IdentityCache, error) { + client := buildClient(gi.conf["token"]) + + user, _, err := client.Users.GetUser(id) + if err != nil { + return nil, err + } + + return repo.NewIdentityRaw( + user.Name, + user.PublicEmail, + user.Username, + user.AvatarURL, + map[string]string{ + keyGitlabLogin: user.Username, + }, + ) +} + +func parseID(id int) string { + return fmt.Sprintf("%d", id) +} -- cgit From ffb8d34e4f04b678b3f4785a8247762ca7c06c4a Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Wed, 17 Jul 2019 00:09:02 +0200 Subject: bridge/gitlab: check identity cache in ensurePerson --- bridge/gitlab/import.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 1ac8eaf3..4eeb3813 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -2,6 +2,7 @@ package gitlab import ( "fmt" + "strconv" "time" "github.com/xanzy/go-gitlab" @@ -9,6 +10,7 @@ import ( "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/util/text" ) @@ -314,6 +316,18 @@ func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCa } func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.IdentityCache, error) { + // Look first in the cache + i, err := repo.ResolveIdentityImmutableMetadata(keyGitlabId, strconv.Itoa(id)) + if err == nil { + return i, nil + } + if _, ok := err.(identity.ErrMultipleMatch); ok { + return nil, err + } + + // importing a new identity + gi.importedIdentities++ + client := buildClient(gi.conf["token"]) user, _, err := client.Users.GetUser(id) @@ -327,6 +341,7 @@ func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.Id user.Username, user.AvatarURL, map[string]string{ + keyGitlabId: strconv.Itoa(id), keyGitlabLogin: user.Username, }, ) -- cgit From 76a389c93da284c4178789d378b4a2bbd8214934 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Wed, 17 Jul 2019 18:54:19 +0200 Subject: bridge/gitlab: make resolve error unique within the importer --- bridge/gitlab/import.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 4eeb3813..d94dbe2e 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -64,7 +64,6 @@ func (gi *gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) erro if err := gi.ensureLabelEvent(repo, b, labelEvent); err != nil { return fmt.Errorf("label event creation: %v", err) } - } // commit bug state @@ -131,17 +130,17 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error { id := parseID(note.ID) - hash, err := b.ResolveOperationWithMetadata(keyGitlabId, id) - if err != cache.ErrNoMatchingOp { - return err - } - // ensure issue author author, err := gi.ensurePerson(repo, note.Author.ID) if err != nil { return err } + hash, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, id) + if err != nil && err != cache.ErrNoMatchingOp { + return err + } + noteType, body := GetNoteType(note) switch noteType { case NOTE_CLOSED: @@ -149,7 +148,8 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n author, note.CreatedAt.Unix(), map[string]string{ - keyGitlabId: id, + keyGitlabId: id, + keyGitlabUrl: "", }, ) return err @@ -159,7 +159,8 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n author, note.CreatedAt.Unix(), map[string]string{ - keyGitlabId: id, + keyGitlabId: id, + keyGitlabUrl: "", }, ) return err @@ -171,6 +172,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n // we should check for "changed the description" notes and compare issue texts if issue.Description != b.Snapshot().Comments[0].Message { + // comment edition _, err = b.EditCommentRaw( author, @@ -195,7 +197,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n } // if we didn't import the comment - if err == cache.ErrNoMatchingOp { + if errResolve == cache.ErrNoMatchingOp { // add comment operation _, err = b.AddCommentRaw( @@ -220,18 +222,13 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n } // search for last comment update - timeline, err := b.Snapshot().SearchTimelineItem(hash) + comment, err := b.Snapshot().SearchComment(hash) if err != nil { return err } - item, ok := timeline.(*bug.AddCommentTimelineItem) - if !ok { - return fmt.Errorf("expected add comment time line") - } - // compare local bug comment with the new note body - if item.Message != cleanText { + if comment.Message != cleanText { // comment edition _, err = b.EditCommentRaw( author, @@ -251,7 +248,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n return nil case NOTE_TITLE_CHANGED: - + // title change events are given new notes _, err = b.SetTitleRaw( author, note.CreatedAt.Unix(), @@ -265,8 +262,8 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n return err default: - // non handled note types - + // non handled note types, this is not an error + //TODO: send warning via channel return nil } -- cgit From ce3a2788ab3bc9f205bcc27b03355155d641c2f4 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Wed, 17 Jul 2019 22:41:42 +0200 Subject: bridge/gitlab: fix note error handling bug bridge/gitlab: remove unused functions --- bridge/gitlab/import.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index d94dbe2e..93ad8570 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -137,7 +137,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n } hash, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, id) - if err != nil && err != cache.ErrNoMatchingOp { + if errResolve != cache.ErrNoMatchingOp { return err } -- cgit From b9a533804940989617890b84d5008e8bb7c8e15b Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Fri, 19 Jul 2019 18:56:58 +0200 Subject: bridge/gitlab: move constants to gitlab.go --- bridge/gitlab/import.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 93ad8570..6227767b 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -14,10 +14,6 @@ import ( "github.com/MichaelMure/git-bug/util/text" ) -const ( - keyGitlabLogin = "gitlab-login" -) - type gitlabImporter struct { conf core.Configuration @@ -170,7 +166,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n // since gitlab doesn't provide the issue history // we should check for "changed the description" notes and compare issue texts - + // TODO: Check only one time and ignore next 'description change' within one issue if issue.Description != b.Snapshot().Comments[0].Message { // comment edition -- cgit From b18507836cd1716ba842e82eb8d42c82b8d62d71 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Fri, 19 Jul 2019 19:39:15 +0200 Subject: bridge/gitlab: add gitlab client default timeout bridge/gitlab: fix import bug --- bridge/gitlab/import.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 6227767b..6869a103 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -120,7 +120,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue return b, nil } - return nil, nil + return b, nil } func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error { -- cgit From b27647c7a0dd95fdbbe4d22962129615fc5c9325 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Mon, 22 Jul 2019 00:13:47 +0200 Subject: bridge/gitlab: Fix test project path bridge/gitlab: update comments --- bridge/gitlab/import.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 6869a103..b2db13d0 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -14,6 +14,7 @@ import ( "github.com/MichaelMure/git-bug/util/text" ) +// gitlabImporter implement the Importer interface type gitlabImporter struct { conf core.Configuration @@ -32,6 +33,8 @@ func (gi *gitlabImporter) Init(conf core.Configuration) error { return nil } +// ImportAll iterate over all the configured repository issues (notes) and ensure the creation +// of the missing issues / comments / label events / title changes ... func (gi *gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) error { gi.iterator = NewIterator(gi.conf[keyProjectID], gi.conf[keyToken], since) @@ -90,6 +93,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue return nil, err } + // if bug was never imported if err == bug.ErrBugNotExist { cleanText, err := text.Cleanup(string(issue.Description)) if err != nil { -- cgit From ece2cb126293361212d7673fea976876af7b811b Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Mon, 22 Jul 2019 18:56:14 +0200 Subject: bridge/gitlab: improve tests and errors bridge/gitlab: global fixes --- bridge/gitlab/import.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index b2db13d0..67d9aa25 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -95,7 +95,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue // if bug was never imported if err == bug.ErrBugNotExist { - cleanText, err := text.Cleanup(string(issue.Description)) + cleanText, err := text.Cleanup(issue.Description) if err != nil { return nil, err } @@ -261,10 +261,12 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n return err - default: - // non handled note types, this is not an error + case NOTE_UNKNOWN: //TODO: send warning via channel return nil + + default: + panic("unhandled note type") } return nil @@ -322,9 +324,6 @@ func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.Id return nil, err } - // importing a new identity - gi.importedIdentities++ - client := buildClient(gi.conf["token"]) user, _, err := client.Users.GetUser(id) @@ -332,6 +331,9 @@ func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.Id return nil, err } + // importing a new identity + gi.importedIdentities++ + return repo.NewIdentityRaw( user.Name, user.PublicEmail, -- cgit From 0329bfdf440ec48c5c5c5c6dbe2ca8519d99b706 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Tue, 23 Jul 2019 17:29:53 +0200 Subject: bridge/gitlab: change validateProjectURL signature bridge/gitlab: code cleanup --- bridge/gitlab/import.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 67d9aa25..b19587a9 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -108,9 +108,9 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue cleanText, nil, map[string]string{ - keyOrigin: target, - keyGitlabId: parseID(issue.ID), - keyGitlabUrl: issue.WebURL, + core.KeyOrigin: target, + keyGitlabId: parseID(issue.ID), + keyGitlabUrl: issue.WebURL, }, ) -- cgit From 0c8f1c3a58c4707284cc6368af26715520a220c0 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Tue, 23 Jul 2019 18:40:10 +0200 Subject: bridge/gitlab: fix comment edition target hash in the import bridge/gitlab: global changes, typo fixes, comments addition --- bridge/gitlab/import.go | 103 +++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 58 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index b19587a9..20c586a7 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -11,6 +11,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/util/git" "github.com/MichaelMure/git-bug/util/text" ) @@ -65,17 +66,17 @@ func (gi *gitlabImporter) ImportAll(repo *cache.RepoCache, since time.Time) erro } } + if err := gi.iterator.Error(); err != nil { + fmt.Printf("import error: %v\n", err) + return err + } + // commit bug state if err := b.CommitAsNeeded(); err != nil { return fmt.Errorf("bug commit: %v", err) } } - if err := gi.iterator.Error(); err != nil { - fmt.Printf("import error: %v\n", err) - return err - } - fmt.Printf("Successfully imported %d issues and %d identities from Gitlab\n", gi.importedIssues, gi.importedIdentities) return nil } @@ -93,37 +94,38 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue return nil, err } - // if bug was never imported - if err == bug.ErrBugNotExist { - cleanText, err := text.Cleanup(issue.Description) - if err != nil { - return nil, err - } - - // create bug - b, _, err = repo.NewBugRaw( - author, - issue.CreatedAt.Unix(), - issue.Title, - cleanText, - nil, - map[string]string{ - core.KeyOrigin: target, - keyGitlabId: parseID(issue.ID), - keyGitlabUrl: issue.WebURL, - }, - ) + if err == nil { + return b, nil + } - if err != nil { - return nil, err - } + // if bug was never imported + cleanText, err := text.Cleanup(issue.Description) + if err != nil { + return nil, err + } - // importing a new bug - gi.importedIssues++ + // create bug + b, _, err = repo.NewBugRaw( + author, + issue.CreatedAt.Unix(), + issue.Title, + cleanText, + nil, + map[string]string{ + core.KeyOrigin: target, + keyGitlabId: parseID(issue.ID), + keyGitlabUrl: issue.WebURL, + keyGitlabProject: gi.conf[keyProjectID], + }, + ) - return b, nil + if err != nil { + return nil, err } + // importing a new bug + gi.importedIssues++ + return b, nil } @@ -138,7 +140,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n hash, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, id) if errResolve != cache.ErrNoMatchingOp { - return err + return errResolve } noteType, body := GetNoteType(note) @@ -148,8 +150,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n author, note.CreatedAt.Unix(), map[string]string{ - keyGitlabId: id, - keyGitlabUrl: "", + keyGitlabId: id, }, ) return err @@ -159,8 +160,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n author, note.CreatedAt.Unix(), map[string]string{ - keyGitlabId: id, - keyGitlabUrl: "", + keyGitlabId: id, }, ) return err @@ -168,25 +168,24 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n case NOTE_DESCRIPTION_CHANGED: issue := gi.iterator.IssueValue() + firstComment := b.Snapshot().Comments[0] // since gitlab doesn't provide the issue history // we should check for "changed the description" notes and compare issue texts // TODO: Check only one time and ignore next 'description change' within one issue - if issue.Description != b.Snapshot().Comments[0].Message { + if issue.Description != firstComment.Message { // comment edition _, err = b.EditCommentRaw( author, note.UpdatedAt.Unix(), - target, + git.Hash(firstComment.Id()), issue.Description, map[string]string{ - keyGitlabId: id, - keyGitlabUrl: "", + keyGitlabId: id, }, ) return err - } case NOTE_COMMENT: @@ -206,8 +205,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n cleanText, nil, map[string]string{ - keyGitlabId: id, - keyGitlabUrl: "", + keyGitlabId: id, }, ) @@ -216,11 +214,6 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n // if comment was already exported - // if note wasn't updated - if note.UpdatedAt.Equal(*note.CreatedAt) { - return nil - } - // search for last comment update comment, err := b.Snapshot().SearchComment(hash) if err != nil { @@ -233,13 +226,9 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n _, err = b.EditCommentRaw( author, note.UpdatedAt.Unix(), - target, + hash, cleanText, - map[string]string{ - // no metadata unique metadata to store - keyGitlabId: "", - keyGitlabUrl: "", - }, + nil, ) return err @@ -254,15 +243,13 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n note.CreatedAt.Unix(), body, map[string]string{ - keyGitlabId: id, - keyGitlabUrl: "", + keyGitlabId: id, }, ) return err case NOTE_UNKNOWN: - //TODO: send warning via channel return nil default: @@ -308,7 +295,7 @@ func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCa ) default: - panic("unexpected label event action") + err = fmt.Errorf("unexpected label event action") } return err -- cgit From 29fdd37ce69b48aa9fc3c1b829ff67818041068f Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Tue, 23 Jul 2019 19:16:52 +0200 Subject: bridge/github: add getNewTitle tests --- bridge/gitlab/import.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'bridge/gitlab/import.go') diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go index 20c586a7..8f4ceec9 100644 --- a/bridge/gitlab/import.go +++ b/bridge/gitlab/import.go @@ -138,11 +138,6 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n return err } - hash, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, id) - if errResolve != cache.ErrNoMatchingOp { - return errResolve - } - noteType, body := GetNoteType(note) switch noteType { case NOTE_CLOSED: @@ -189,6 +184,10 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n } case NOTE_COMMENT: + hash, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, id) + if errResolve != cache.ErrNoMatchingOp { + return errResolve + } cleanText, err := text.Cleanup(body) if err != nil { @@ -226,7 +225,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n _, err = b.EditCommentRaw( author, note.UpdatedAt.Unix(), - hash, + git.Hash(comment.Id()), cleanText, nil, ) @@ -327,6 +326,7 @@ func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.Id user.Username, user.AvatarURL, map[string]string{ + // because Gitlab keyGitlabId: strconv.Itoa(id), keyGitlabLogin: user.Username, }, -- cgit