2018-12-08 05:58:51 +03:00
|
|
|
package launchpad
|
|
|
|
|
|
|
|
import (
|
2019-08-13 20:51:14 +03:00
|
|
|
"context"
|
2018-12-08 05:58:51 +03:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-12-08 05:58:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type launchpadImporter struct {
|
|
|
|
conf core.Configuration
|
|
|
|
}
|
|
|
|
|
2020-02-15 17:39:49 +03:00
|
|
|
func (li *launchpadImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
|
2018-12-08 05:58:51 +03:00
|
|
|
li.conf = conf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (li *launchpadImporter) ensurePerson(repo *cache.RepoCache, owner LPPerson) (*cache.IdentityCache, error) {
|
2019-01-19 18:01:06 +03:00
|
|
|
// Look first in the cache
|
2019-11-10 19:48:13 +03:00
|
|
|
i, err := repo.ResolveIdentityImmutableMetadata(metaKeyLaunchpadLogin, owner.Login)
|
2019-01-19 18:01:06 +03:00
|
|
|
if err == nil {
|
|
|
|
return i, nil
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|
2019-12-08 23:15:06 +03:00
|
|
|
if entity.IsErrMultipleMatch(err) {
|
2019-01-19 18:01:06 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.NewIdentityRaw(
|
|
|
|
owner.Name,
|
|
|
|
"",
|
2020-02-25 23:35:57 +03:00
|
|
|
owner.Login,
|
2019-01-19 18:01:06 +03:00
|
|
|
"",
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
metaKeyLaunchpadLogin: owner.Login,
|
2019-01-19 18:01:06 +03:00
|
|
|
},
|
|
|
|
)
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
func (li *launchpadImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
|
|
|
|
out := make(chan core.ImportResult)
|
2018-12-08 05:58:51 +03:00
|
|
|
lpAPI := new(launchpadAPI)
|
|
|
|
|
|
|
|
err := lpAPI.Init()
|
|
|
|
if err != nil {
|
2019-08-13 20:51:14 +03:00
|
|
|
return nil, err
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
lpBugs, err := lpAPI.SearchTasks(ctx, li.conf["project"])
|
2018-12-08 05:58:51 +03:00
|
|
|
if err != nil {
|
2019-08-13 20:51:14 +03:00
|
|
|
return nil, err
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
go func() {
|
|
|
|
for _, lpBug := range lpBugs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
lpBugID := fmt.Sprintf("%d", lpBug.ID)
|
2020-03-28 19:08:58 +03:00
|
|
|
b, err := repo.ResolveBugMatcher(func(excerpt *cache.BugExcerpt) bool {
|
|
|
|
return excerpt.CreateMetadata[core.MetaKeyOrigin] == target &&
|
|
|
|
excerpt.CreateMetadata[metaKeyLaunchpadID] == lpBugID
|
|
|
|
})
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil && err != bug.ErrBugNotExist {
|
|
|
|
out <- core.NewImportError(err, entity.Id(lpBugID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, err := li.ensurePerson(repo, lpBug.Owner)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewImportError(err, entity.Id(lpBugID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == bug.ErrBugNotExist {
|
|
|
|
createdAt, _ := time.Parse(time.RFC3339, lpBug.CreatedAt)
|
|
|
|
b, _, err = repo.NewBugRaw(
|
|
|
|
owner,
|
|
|
|
createdAt.Unix(),
|
|
|
|
lpBug.Title,
|
|
|
|
lpBug.Description,
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
core.MetaKeyOrigin: target,
|
|
|
|
metaKeyLaunchpadID: lpBugID,
|
2019-08-13 20:51:14 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewImportError(err, entity.Id(lpBugID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out <- core.NewImportBug(b.Id())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle messages */
|
|
|
|
if len(lpBug.Messages) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Launchpad API returns the bug description as the first
|
|
|
|
// comment, so skip it.
|
|
|
|
for _, lpMessage := range lpBug.Messages[1:] {
|
2019-11-10 19:48:13 +03:00
|
|
|
_, err := b.ResolveOperationWithMetadata(metaKeyLaunchpadID, lpMessage.ID)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil && err != cache.ErrNoMatchingOp {
|
|
|
|
out <- core.NewImportError(err, entity.Id(lpMessage.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this comment already exists, we are probably
|
|
|
|
// updating an existing bug. We do not want to duplicate
|
|
|
|
// the comments, so let us just skip this one.
|
|
|
|
// TODO: Can Launchpad comments be edited?
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, err := li.ensurePerson(repo, lpMessage.Owner)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewImportError(err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a new comment, we can add it.
|
|
|
|
createdAt, _ := time.Parse(time.RFC3339, lpMessage.CreatedAt)
|
|
|
|
op, err := b.AddCommentRaw(
|
|
|
|
owner,
|
|
|
|
createdAt.Unix(),
|
|
|
|
lpMessage.Content,
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
metaKeyLaunchpadID: lpMessage.ID,
|
2019-08-13 20:51:14 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-03-28 20:41:14 +03:00
|
|
|
out <- core.NewImportError(err, b.Id())
|
2019-08-13 20:51:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out <- core.NewImportComment(op.Id())
|
|
|
|
}
|
|
|
|
|
2019-11-19 22:12:07 +03:00
|
|
|
if !b.NeedCommit() {
|
|
|
|
out <- core.NewImportNothing(b.Id(), "no imported operation")
|
|
|
|
} else if err := b.Commit(); err != nil {
|
2019-08-13 20:51:14 +03:00
|
|
|
out <- core.NewImportError(err, "")
|
|
|
|
return
|
|
|
|
}
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-13 20:51:14 +03:00
|
|
|
}()
|
2018-12-08 05:58:51 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
return out, nil
|
2018-12-08 05:58:51 +03:00
|
|
|
}
|