Merge pull request #247 from MichaelMure/bridge-origin

bridge: move keyOrigin to core package
This commit is contained in:
Michael Muré 2019-11-19 19:25:43 +01:00 committed by GitHub
commit e01cefff98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 103 additions and 101 deletions

View File

@ -20,8 +20,8 @@ var ErrImportNotSupported = errors.New("import is not supported")
var ErrExportNotSupported = errors.New("export is not supported") var ErrExportNotSupported = errors.New("export is not supported")
const ( const (
KeyTarget = "target" ConfigKeyTarget = "target"
KeyOrigin = "origin" MetaKeyOrigin = "origin"
bridgeConfigKeyPrefix = "git-bug.bridge" bridgeConfigKeyPrefix = "git-bug.bridge"
) )
@ -102,7 +102,7 @@ func LoadBridge(repo *cache.RepoCache, name string) (*Bridge, error) {
return nil, err return nil, err
} }
target := conf[KeyTarget] target := conf[ConfigKeyTarget]
bridge, err := NewBridge(repo, target, name) bridge, err := NewBridge(repo, target, name)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -114,7 +114,7 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams)
return nil, fmt.Errorf("project doesn't exist or authentication token has an incorrect scope") return nil, fmt.Errorf("project doesn't exist or authentication token has an incorrect scope")
} }
conf[core.KeyTarget] = target conf[core.ConfigKeyTarget] = target
conf[keyToken] = token conf[keyToken] = token
conf[keyOwner] = owner conf[keyOwner] = owner
conf[keyProject] = project conf[keyProject] = project
@ -128,8 +128,8 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams)
} }
func (*Github) ValidateConfig(conf core.Configuration) error { func (*Github) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.KeyTarget]; !ok { if v, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.KeyTarget) return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
} else if v != target { } else if v != target {
return fmt.Errorf("unexpected target name: %v", v) return fmt.Errorf("unexpected target name: %v", v)
} }

View File

@ -174,16 +174,16 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
author := snapshot.Author author := snapshot.Author
// skip bug if origin is not allowed // skip bug if origin is not allowed
origin, ok := snapshot.GetCreateMetadata(keyOrigin) origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
if ok && origin != target { if ok && origin != target {
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin)) out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
return return
} }
// get github bug ID // get github bug ID
githubID, ok := snapshot.GetCreateMetadata(keyGithubId) githubID, ok := snapshot.GetCreateMetadata(metaKeyGithubId)
if ok { if ok {
githubURL, ok := snapshot.GetCreateMetadata(keyGithubUrl) githubURL, ok := snapshot.GetCreateMetadata(metaKeyGithubUrl)
if !ok { if !ok {
// if we find github ID, github URL must be found too // if we find github ID, github URL must be found too
err := fmt.Errorf("incomplete Github metadata: expected to find issue URL") err := fmt.Errorf("incomplete Github metadata: expected to find issue URL")
@ -258,7 +258,7 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
// ignore operations already existing in github (due to import or export) // ignore operations already existing in github (due to import or export)
// cache the ID of already exported or imported issues and events from Github // cache the ID of already exported or imported issues and events from Github
if id, ok := op.GetMetadata(keyGithubId); ok { if id, ok := op.GetMetadata(metaKeyGithubId); ok {
ge.cachedOperationIDs[op.Id()] = id ge.cachedOperationIDs[op.Id()] = id
out <- core.NewExportNothing(op.Id(), "already exported operation") out <- core.NewExportNothing(op.Id(), "already exported operation")
continue continue
@ -437,8 +437,8 @@ func markOperationAsExported(b *cache.BugCache, target entity.Id, githubID, gith
_, err := b.SetMetadata( _, err := b.SetMetadata(
target, target,
map[string]string{ map[string]string{
keyGithubId: githubID, metaKeyGithubId: githubID,
keyGithubUrl: githubURL, metaKeyGithubUrl: githubURL,
}, },
) )

View File

@ -233,27 +233,27 @@ func TestPushPull(t *testing.T) {
for _, op := range tt.bug.Snapshot().Operations { for _, op := range tt.bug.Snapshot().Operations {
// Check if the originals operations (*not* SetMetadata) are tagged properly // Check if the originals operations (*not* SetMetadata) are tagged properly
if _, ok := op.(*bug.SetMetadataOperation); !ok { if _, ok := op.(*bug.SetMetadataOperation); !ok {
_, haveIDMetadata := op.GetMetadata(keyGithubId) _, haveIDMetadata := op.GetMetadata(metaKeyGithubId)
require.True(t, haveIDMetadata) require.True(t, haveIDMetadata)
_, haveURLMetada := op.GetMetadata(keyGithubUrl) _, haveURLMetada := op.GetMetadata(metaKeyGithubUrl)
require.True(t, haveURLMetada) require.True(t, haveURLMetada)
} }
} }
// get bug github ID // get bug github ID
bugGithubID, ok := tt.bug.Snapshot().GetCreateMetadata(keyGithubId) bugGithubID, ok := tt.bug.Snapshot().GetCreateMetadata(metaKeyGithubId)
require.True(t, ok) require.True(t, ok)
// retrieve bug from backendTwo // retrieve bug from backendTwo
importedBug, err := backendTwo.ResolveBugCreateMetadata(keyGithubId, bugGithubID) importedBug, err := backendTwo.ResolveBugCreateMetadata(metaKeyGithubId, bugGithubID)
require.NoError(t, err) require.NoError(t, err)
// verify bug have same number of original operations // verify bug have same number of original operations
require.Len(t, importedBug.Snapshot().Operations, tt.numOrOp) require.Len(t, importedBug.Snapshot().Operations, tt.numOrOp)
// verify bugs are taged with origin=github // verify bugs are taged with origin=github
issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(keyOrigin) issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(core.MetaKeyOrigin)
require.True(t, ok) require.True(t, ok)
require.Equal(t, issueOrigin, target) require.Equal(t, issueOrigin, target)

View File

@ -15,10 +15,9 @@ import (
) )
const ( const (
keyOrigin = "origin" metaKeyGithubId = "github-id"
keyGithubId = "github-id" metaKeyGithubUrl = "github-url"
keyGithubUrl = "github-url" metaKeyGithubLogin = "github-login"
keyGithubLogin = "github-login"
) )
// githubImporter implement the Importer interface // githubImporter implement the Importer interface
@ -92,7 +91,7 @@ func (gi *githubImporter) ensureIssue(repo *cache.RepoCache, issue issueTimeline
} }
// resolve bug // resolve bug
b, err := repo.ResolveBugCreateMetadata(keyGithubUrl, issue.Url.String()) b, err := repo.ResolveBugCreateMetadata(metaKeyGithubUrl, issue.Url.String())
if err != nil && err != bug.ErrBugNotExist { if err != nil && err != bug.ErrBugNotExist {
return nil, err return nil, err
} }
@ -119,9 +118,9 @@ func (gi *githubImporter) ensureIssue(repo *cache.RepoCache, issue issueTimeline
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
keyOrigin: target, core.MetaKeyOrigin: target,
keyGithubId: parseId(issue.Id), metaKeyGithubId: parseId(issue.Id),
keyGithubUrl: issue.Url.String(), metaKeyGithubUrl: issue.Url.String(),
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -157,9 +156,9 @@ func (gi *githubImporter) ensureIssue(repo *cache.RepoCache, issue issueTimeline
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
keyOrigin: target, core.MetaKeyOrigin: target,
keyGithubId: parseId(issue.Id), metaKeyGithubId: parseId(issue.Id),
keyGithubUrl: issue.Url.String(), metaKeyGithubUrl: issue.Url.String(),
}, },
) )
@ -173,7 +172,7 @@ func (gi *githubImporter) ensureIssue(repo *cache.RepoCache, issue issueTimeline
} }
// other edits will be added as CommentEdit operations // other edits will be added as CommentEdit operations
target, err := b.ResolveOperationWithMetadata(keyGithubId, parseId(issue.Id)) target, err := b.ResolveOperationWithMetadata(metaKeyGithubId, parseId(issue.Id))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -206,7 +205,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
case "LabeledEvent": case "LabeledEvent":
id := parseId(item.LabeledEvent.Id) id := parseId(item.LabeledEvent.Id)
_, err := b.ResolveOperationWithMetadata(keyGithubId, id) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, id)
if err == nil { if err == nil {
reason := fmt.Sprintf("operation already imported: %v", item.Typename) reason := fmt.Sprintf("operation already imported: %v", item.Typename)
gi.out <- core.NewImportNothing("", reason) gi.out <- core.NewImportNothing("", reason)
@ -227,7 +226,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
string(item.LabeledEvent.Label.Name), string(item.LabeledEvent.Label.Name),
}, },
nil, nil,
map[string]string{keyGithubId: id}, map[string]string{metaKeyGithubId: id},
) )
if err != nil { if err != nil {
return err return err
@ -238,7 +237,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
case "UnlabeledEvent": case "UnlabeledEvent":
id := parseId(item.UnlabeledEvent.Id) id := parseId(item.UnlabeledEvent.Id)
_, err := b.ResolveOperationWithMetadata(keyGithubId, id) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, id)
if err == nil { if err == nil {
reason := fmt.Sprintf("operation already imported: %v", item.Typename) reason := fmt.Sprintf("operation already imported: %v", item.Typename)
gi.out <- core.NewImportNothing("", reason) gi.out <- core.NewImportNothing("", reason)
@ -259,7 +258,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
[]string{ []string{
string(item.UnlabeledEvent.Label.Name), string(item.UnlabeledEvent.Label.Name),
}, },
map[string]string{keyGithubId: id}, map[string]string{metaKeyGithubId: id},
) )
if err != nil { if err != nil {
return err return err
@ -270,7 +269,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
case "ClosedEvent": case "ClosedEvent":
id := parseId(item.ClosedEvent.Id) id := parseId(item.ClosedEvent.Id)
_, err := b.ResolveOperationWithMetadata(keyGithubId, id) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, id)
if err != cache.ErrNoMatchingOp { if err != cache.ErrNoMatchingOp {
return err return err
} }
@ -286,7 +285,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
op, err := b.CloseRaw( op, err := b.CloseRaw(
author, author,
item.ClosedEvent.CreatedAt.Unix(), item.ClosedEvent.CreatedAt.Unix(),
map[string]string{keyGithubId: id}, map[string]string{metaKeyGithubId: id},
) )
if err != nil { if err != nil {
@ -298,7 +297,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
case "ReopenedEvent": case "ReopenedEvent":
id := parseId(item.ReopenedEvent.Id) id := parseId(item.ReopenedEvent.Id)
_, err := b.ResolveOperationWithMetadata(keyGithubId, id) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, id)
if err != cache.ErrNoMatchingOp { if err != cache.ErrNoMatchingOp {
return err return err
} }
@ -314,7 +313,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
op, err := b.OpenRaw( op, err := b.OpenRaw(
author, author,
item.ReopenedEvent.CreatedAt.Unix(), item.ReopenedEvent.CreatedAt.Unix(),
map[string]string{keyGithubId: id}, map[string]string{metaKeyGithubId: id},
) )
if err != nil { if err != nil {
@ -326,7 +325,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
case "RenamedTitleEvent": case "RenamedTitleEvent":
id := parseId(item.RenamedTitleEvent.Id) id := parseId(item.RenamedTitleEvent.Id)
_, err := b.ResolveOperationWithMetadata(keyGithubId, id) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, id)
if err != cache.ErrNoMatchingOp { if err != cache.ErrNoMatchingOp {
return err return err
} }
@ -343,7 +342,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
author, author,
item.RenamedTitleEvent.CreatedAt.Unix(), item.RenamedTitleEvent.CreatedAt.Unix(),
string(item.RenamedTitleEvent.CurrentTitle), string(item.RenamedTitleEvent.CurrentTitle),
map[string]string{keyGithubId: id}, map[string]string{metaKeyGithubId: id},
) )
if err != nil { if err != nil {
return err return err
@ -367,7 +366,7 @@ func (gi *githubImporter) ensureTimelineComment(repo *cache.RepoCache, b *cache.
return err return err
} }
targetOpID, err := b.ResolveOperationWithMetadata(keyGithubId, parseId(item.Id)) targetOpID, err := b.ResolveOperationWithMetadata(metaKeyGithubId, parseId(item.Id))
if err == nil { if err == nil {
gi.out <- core.NewImportNothing("", "comment already imported") gi.out <- core.NewImportNothing("", "comment already imported")
} else if err != cache.ErrNoMatchingOp { } else if err != cache.ErrNoMatchingOp {
@ -390,8 +389,8 @@ func (gi *githubImporter) ensureTimelineComment(repo *cache.RepoCache, b *cache.
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
keyGithubId: parseId(item.Id), metaKeyGithubId: parseId(item.Id),
keyGithubUrl: parseId(item.Url.String()), metaKeyGithubUrl: parseId(item.Url.String()),
}, },
) )
if err != nil { if err != nil {
@ -428,8 +427,8 @@ func (gi *githubImporter) ensureTimelineComment(repo *cache.RepoCache, b *cache.
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
keyGithubId: parseId(item.Id), metaKeyGithubId: parseId(item.Id),
keyGithubUrl: item.Url.String(), metaKeyGithubUrl: item.Url.String(),
}, },
) )
if err != nil { if err != nil {
@ -451,7 +450,7 @@ func (gi *githubImporter) ensureTimelineComment(repo *cache.RepoCache, b *cache.
} }
func (gi *githubImporter) ensureCommentEdit(repo *cache.RepoCache, b *cache.BugCache, target entity.Id, edit userContentEdit) error { func (gi *githubImporter) ensureCommentEdit(repo *cache.RepoCache, b *cache.BugCache, target entity.Id, edit userContentEdit) error {
_, err := b.ResolveOperationWithMetadata(keyGithubId, parseId(edit.Id)) _, err := b.ResolveOperationWithMetadata(metaKeyGithubId, parseId(edit.Id))
if err == nil { if err == nil {
gi.out <- core.NewImportNothing(b.Id(), "edition already imported") gi.out <- core.NewImportNothing(b.Id(), "edition already imported")
return nil return nil
@ -485,7 +484,7 @@ func (gi *githubImporter) ensureCommentEdit(repo *cache.RepoCache, b *cache.BugC
target, target,
cleanText, cleanText,
map[string]string{ map[string]string{
keyGithubId: parseId(edit.Id), metaKeyGithubId: parseId(edit.Id),
}, },
) )
@ -508,7 +507,7 @@ func (gi *githubImporter) ensurePerson(repo *cache.RepoCache, actor *actor) (*ca
} }
// Look first in the cache // Look first in the cache
i, err := repo.ResolveIdentityImmutableMetadata(keyGithubLogin, string(actor.Login)) i, err := repo.ResolveIdentityImmutableMetadata(metaKeyGithubLogin, string(actor.Login))
if err == nil { if err == nil {
return i, nil return i, nil
} }
@ -543,7 +542,7 @@ func (gi *githubImporter) ensurePerson(repo *cache.RepoCache, actor *actor) (*ca
string(actor.Login), string(actor.Login),
string(actor.AvatarUrl), string(actor.AvatarUrl),
map[string]string{ map[string]string{
keyGithubLogin: string(actor.Login), metaKeyGithubLogin: string(actor.Login),
}, },
) )
@ -557,7 +556,7 @@ func (gi *githubImporter) ensurePerson(repo *cache.RepoCache, actor *actor) (*ca
func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache, error) { func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache, error) {
// Look first in the cache // Look first in the cache
i, err := repo.ResolveIdentityImmutableMetadata(keyGithubLogin, "ghost") i, err := repo.ResolveIdentityImmutableMetadata(metaKeyGithubLogin, "ghost")
if err == nil { if err == nil {
return i, nil return i, nil
} }
@ -592,7 +591,7 @@ func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache,
string(q.User.Login), string(q.User.Login),
string(q.User.AvatarUrl), string(q.User.AvatarUrl),
map[string]string{ map[string]string{
keyGithubLogin: string(q.User.Login), metaKeyGithubLogin: string(q.User.Login),
}, },
) )
} }

View File

@ -163,7 +163,7 @@ func Test_Importer(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
b, err := backend.ResolveBugCreateMetadata(keyGithubUrl, tt.url) b, err := backend.ResolveBugCreateMetadata(metaKeyGithubUrl, tt.url)
require.NoError(t, err) require.NoError(t, err)
ops := b.Snapshot().Operations ops := b.Snapshot().Operations

View File

@ -80,7 +80,7 @@ func (g *Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams)
conf[keyProjectID] = strconv.Itoa(id) conf[keyProjectID] = strconv.Itoa(id)
conf[keyToken] = token conf[keyToken] = token
conf[core.KeyTarget] = target conf[core.ConfigKeyTarget] = target
err = g.ValidateConfig(conf) err = g.ValidateConfig(conf)
if err != nil { if err != nil {
@ -91,8 +91,8 @@ func (g *Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams)
} }
func (g *Gitlab) ValidateConfig(conf core.Configuration) error { func (g *Gitlab) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.KeyTarget]; !ok { if v, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.KeyTarget) return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
} else if v != target { } else if v != target {
return fmt.Errorf("unexpected target name: %v", v) return fmt.Errorf("unexpected target name: %v", v)
} }

View File

@ -141,7 +141,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
// from Gitlab) and we do not have the token of the bug author, there is nothing we can do. // 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 // skip bug if origin is not allowed
origin, ok := snapshot.GetCreateMetadata(core.KeyOrigin) origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
if ok && origin != target { if ok && origin != target {
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin)) out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
return return
@ -152,9 +152,9 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
author := snapshot.Author author := snapshot.Author
// get gitlab bug ID // get gitlab bug ID
gitlabID, ok := snapshot.GetCreateMetadata(keyGitlabId) gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
if ok { if ok {
projectID, ok := snapshot.GetCreateMetadata(keyGitlabProject) projectID, ok := snapshot.GetCreateMetadata(metaKeyGitlabProject)
if !ok { if !ok {
err := fmt.Errorf("expected to find gitlab project id") err := fmt.Errorf("expected to find gitlab project id")
out <- core.NewExportError(err, b.Id()) out <- core.NewExportError(err, b.Id())
@ -199,9 +199,9 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
_, err = b.SetMetadata( _, err = b.SetMetadata(
createOp.Id(), createOp.Id(),
map[string]string{ map[string]string{
keyGitlabId: idString, metaKeyGitlabId: idString,
keyGitlabUrl: url, metaKeyGitlabUrl: url,
keyGitlabProject: ge.repositoryID, metaKeyGitlabProject: ge.repositoryID,
}, },
) )
if err != nil { if err != nil {
@ -235,7 +235,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
// ignore operations already existing in gitlab (due to import or export) // ignore operations already existing in gitlab (due to import or export)
// cache the ID of already exported or imported issues and events from Gitlab // cache the ID of already exported or imported issues and events from Gitlab
if id, ok := op.GetMetadata(keyGitlabId); ok { if id, ok := op.GetMetadata(metaKeyGitlabId); ok {
ge.cachedOperationIDs[op.Id().String()] = id ge.cachedOperationIDs[op.Id().String()] = id
out <- core.NewExportNothing(op.Id(), "already exported operation") out <- core.NewExportNothing(op.Id(), "already exported operation")
continue continue
@ -378,8 +378,8 @@ func markOperationAsExported(b *cache.BugCache, target entity.Id, gitlabID, gitl
_, err := b.SetMetadata( _, err := b.SetMetadata(
target, target,
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
keyGitlabUrl: gitlabURL, metaKeyGitlabUrl: gitlabURL,
}, },
) )

View File

@ -236,27 +236,27 @@ func TestPushPull(t *testing.T) {
for _, op := range tt.bug.Snapshot().Operations { for _, op := range tt.bug.Snapshot().Operations {
// Check if the originals operations (*not* SetMetadata) are tagged properly // Check if the originals operations (*not* SetMetadata) are tagged properly
if _, ok := op.(*bug.SetMetadataOperation); !ok { if _, ok := op.(*bug.SetMetadataOperation); !ok {
_, haveIDMetadata := op.GetMetadata(keyGitlabId) _, haveIDMetadata := op.GetMetadata(metaKeyGitlabId)
require.True(t, haveIDMetadata) require.True(t, haveIDMetadata)
_, haveURLMetada := op.GetMetadata(keyGitlabUrl) _, haveURLMetada := op.GetMetadata(metaKeyGitlabUrl)
require.True(t, haveURLMetada) require.True(t, haveURLMetada)
} }
} }
// get bug gitlab ID // get bug gitlab ID
bugGitlabID, ok := tt.bug.Snapshot().GetCreateMetadata(keyGitlabId) bugGitlabID, ok := tt.bug.Snapshot().GetCreateMetadata(metaKeyGitlabId)
require.True(t, ok) require.True(t, ok)
// retrieve bug from backendTwo // retrieve bug from backendTwo
importedBug, err := backendTwo.ResolveBugCreateMetadata(keyGitlabId, bugGitlabID) importedBug, err := backendTwo.ResolveBugCreateMetadata(metaKeyGitlabId, bugGitlabID)
require.NoError(t, err) require.NoError(t, err)
// verify bug have same number of original operations // verify bug have same number of original operations
require.Len(t, importedBug.Snapshot().Operations, tt.numOpImp) require.Len(t, importedBug.Snapshot().Operations, tt.numOpImp)
// verify bugs are taged with origin=gitlab // verify bugs are taged with origin=gitlab
issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(core.KeyOrigin) issueOrigin, ok := importedBug.Snapshot().GetCreateMetadata(core.MetaKeyOrigin)
require.True(t, ok) require.True(t, ok)
require.Equal(t, issueOrigin, target) require.Equal(t, issueOrigin, target)

View File

@ -12,10 +12,10 @@ import (
const ( const (
target = "gitlab" target = "gitlab"
keyGitlabId = "gitlab-id" metaKeyGitlabId = "gitlab-id"
keyGitlabUrl = "gitlab-url" metaKeyGitlabUrl = "gitlab-url"
keyGitlabLogin = "gitlab-login" metaKeyGitlabLogin = "gitlab-login"
keyGitlabProject = "gitlab-project-id" metaKeyGitlabProject = "gitlab-project-id"
keyProjectID = "project-id" keyProjectID = "project-id"
keyToken = "token" keyToken = "token"

View File

@ -97,7 +97,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
} }
// resolve bug // resolve bug
b, err := repo.ResolveBugCreateMetadata(keyGitlabUrl, issue.WebURL) b, err := repo.ResolveBugCreateMetadata(metaKeyGitlabUrl, issue.WebURL)
if err == nil { if err == nil {
gi.out <- core.NewImportNothing("", "bug already imported") gi.out <- core.NewImportNothing("", "bug already imported")
return b, nil return b, nil
@ -120,10 +120,10 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
core.KeyOrigin: target, core.MetaKeyOrigin: target,
keyGitlabId: parseID(issue.IID), metaKeyGitlabId: parseID(issue.IID),
keyGitlabUrl: issue.WebURL, metaKeyGitlabUrl: issue.WebURL,
keyGitlabProject: gi.conf[keyProjectID], metaKeyGitlabProject: gi.conf[keyProjectID],
}, },
) )
@ -140,7 +140,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error { func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error {
gitlabID := parseID(note.ID) gitlabID := parseID(note.ID)
id, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, gitlabID) id, errResolve := b.ResolveOperationWithMetadata(metaKeyGitlabId, gitlabID)
if errResolve != nil && errResolve != cache.ErrNoMatchingOp { if errResolve != nil && errResolve != cache.ErrNoMatchingOp {
return errResolve return errResolve
} }
@ -162,7 +162,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
author, author,
note.CreatedAt.Unix(), note.CreatedAt.Unix(),
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
}, },
) )
if err != nil { if err != nil {
@ -180,7 +180,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
author, author,
note.CreatedAt.Unix(), note.CreatedAt.Unix(),
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
}, },
) )
if err != nil { if err != nil {
@ -204,7 +204,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
firstComment.Id(), firstComment.Id(),
issue.Description, issue.Description,
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
}, },
) )
if err != nil { if err != nil {
@ -230,7 +230,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
cleanText, cleanText,
nil, nil,
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
}, },
) )
if err != nil { if err != nil {
@ -278,7 +278,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
note.CreatedAt.Unix(), note.CreatedAt.Unix(),
body, body,
map[string]string{ map[string]string{
keyGitlabId: gitlabID, metaKeyGitlabId: gitlabID,
}, },
) )
if err != nil { if err != nil {
@ -311,7 +311,7 @@ func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, n
} }
func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCache, labelEvent *gitlab.LabelEvent) error { func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCache, labelEvent *gitlab.LabelEvent) error {
_, err := b.ResolveOperationWithMetadata(keyGitlabId, parseID(labelEvent.ID)) _, err := b.ResolveOperationWithMetadata(metaKeyGitlabId, parseID(labelEvent.ID))
if err != cache.ErrNoMatchingOp { if err != cache.ErrNoMatchingOp {
return err return err
} }
@ -330,7 +330,7 @@ func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCa
[]string{labelEvent.Label.Name}, []string{labelEvent.Label.Name},
nil, nil,
map[string]string{ map[string]string{
keyGitlabId: parseID(labelEvent.ID), metaKeyGitlabId: parseID(labelEvent.ID),
}, },
) )
@ -341,7 +341,7 @@ func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCa
nil, nil,
[]string{labelEvent.Label.Name}, []string{labelEvent.Label.Name},
map[string]string{ map[string]string{
keyGitlabId: parseID(labelEvent.ID), metaKeyGitlabId: parseID(labelEvent.ID),
}, },
) )
@ -354,7 +354,7 @@ func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCa
func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.IdentityCache, error) { func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.IdentityCache, error) {
// Look first in the cache // Look first in the cache
i, err := repo.ResolveIdentityImmutableMetadata(keyGitlabId, strconv.Itoa(id)) i, err := repo.ResolveIdentityImmutableMetadata(metaKeyGitlabId, strconv.Itoa(id))
if err == nil { if err == nil {
return i, nil return i, nil
} }
@ -376,8 +376,8 @@ func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.Id
user.AvatarURL, user.AvatarURL,
map[string]string{ map[string]string{
// because Gitlab // because Gitlab
keyGitlabId: strconv.Itoa(id), metaKeyGitlabId: strconv.Itoa(id),
keyGitlabLogin: user.Username, metaKeyGitlabLogin: user.Username,
}, },
) )
if err != nil { if err != nil {

View File

@ -116,7 +116,7 @@ func TestImport(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
b, err := backend.ResolveBugCreateMetadata(keyGitlabUrl, tt.url) b, err := backend.ResolveBugCreateMetadata(metaKeyGitlabUrl, tt.url)
require.NoError(t, err) require.NoError(t, err)
ops := b.Snapshot().Operations ops := b.Snapshot().Operations

View File

@ -62,7 +62,7 @@ func (l *Launchpad) Configure(repo repository.RepoCommon, params core.BridgePara
} }
conf[keyProject] = project conf[keyProject] = project
conf[core.KeyTarget] = target conf[core.ConfigKeyTarget] = target
err = l.ValidateConfig(conf) err = l.ValidateConfig(conf)
if err != nil { if err != nil {
@ -77,8 +77,8 @@ func (*Launchpad) ValidateConfig(conf core.Configuration) error {
return fmt.Errorf("missing %s key", keyProject) return fmt.Errorf("missing %s key", keyProject)
} }
if _, ok := conf[core.KeyTarget]; !ok { if _, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.KeyTarget) return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
} }
return nil return nil

View File

@ -20,12 +20,14 @@ func (li *launchpadImporter) Init(conf core.Configuration) error {
return nil return nil
} }
const keyLaunchpadID = "launchpad-id" const (
const keyLaunchpadLogin = "launchpad-login" metaKeyLaunchpadID = "launchpad-id"
metaKeyLaunchpadLogin = "launchpad-login"
)
func (li *launchpadImporter) ensurePerson(repo *cache.RepoCache, owner LPPerson) (*cache.IdentityCache, error) { func (li *launchpadImporter) ensurePerson(repo *cache.RepoCache, owner LPPerson) (*cache.IdentityCache, error) {
// Look first in the cache // Look first in the cache
i, err := repo.ResolveIdentityImmutableMetadata(keyLaunchpadLogin, owner.Login) i, err := repo.ResolveIdentityImmutableMetadata(metaKeyLaunchpadLogin, owner.Login)
if err == nil { if err == nil {
return i, nil return i, nil
} }
@ -39,7 +41,7 @@ func (li *launchpadImporter) ensurePerson(repo *cache.RepoCache, owner LPPerson)
owner.Login, owner.Login,
"", "",
map[string]string{ map[string]string{
keyLaunchpadLogin: owner.Login, metaKeyLaunchpadLogin: owner.Login,
}, },
) )
} }
@ -65,7 +67,7 @@ func (li *launchpadImporter) ImportAll(ctx context.Context, repo *cache.RepoCach
return return
default: default:
lpBugID := fmt.Sprintf("%d", lpBug.ID) lpBugID := fmt.Sprintf("%d", lpBug.ID)
b, err := repo.ResolveBugCreateMetadata(keyLaunchpadID, lpBugID) b, err := repo.ResolveBugCreateMetadata(metaKeyLaunchpadID, lpBugID)
if err != nil && err != bug.ErrBugNotExist { if err != nil && err != bug.ErrBugNotExist {
out <- core.NewImportError(err, entity.Id(lpBugID)) out <- core.NewImportError(err, entity.Id(lpBugID))
return return
@ -86,7 +88,8 @@ func (li *launchpadImporter) ImportAll(ctx context.Context, repo *cache.RepoCach
lpBug.Description, lpBug.Description,
nil, nil,
map[string]string{ map[string]string{
keyLaunchpadID: lpBugID, core.MetaKeyOrigin: target,
metaKeyLaunchpadID: lpBugID,
}, },
) )
if err != nil { if err != nil {
@ -108,7 +111,7 @@ func (li *launchpadImporter) ImportAll(ctx context.Context, repo *cache.RepoCach
// The Launchpad API returns the bug description as the first // The Launchpad API returns the bug description as the first
// comment, so skip it. // comment, so skip it.
for _, lpMessage := range lpBug.Messages[1:] { for _, lpMessage := range lpBug.Messages[1:] {
_, err := b.ResolveOperationWithMetadata(keyLaunchpadID, lpMessage.ID) _, err := b.ResolveOperationWithMetadata(metaKeyLaunchpadID, lpMessage.ID)
if err != nil && err != cache.ErrNoMatchingOp { if err != nil && err != cache.ErrNoMatchingOp {
out <- core.NewImportError(err, entity.Id(lpMessage.ID)) out <- core.NewImportError(err, entity.Id(lpMessage.ID))
return return
@ -136,7 +139,7 @@ func (li *launchpadImporter) ImportAll(ctx context.Context, repo *cache.RepoCach
lpMessage.Content, lpMessage.Content,
nil, nil,
map[string]string{ map[string]string{
keyLaunchpadID: lpMessage.ID, metaKeyLaunchpadID: lpMessage.ID,
}) })
if err != nil { if err != nil {
out <- core.NewImportError(err, op.Id()) out <- core.NewImportError(err, op.Id())