diff options
Diffstat (limited to 'bridge/launchpad/import.go')
-rw-r--r-- | bridge/launchpad/import.go | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/bridge/launchpad/import.go b/bridge/launchpad/import.go index 074b4bf3..10d25e6c 100644 --- a/bridge/launchpad/import.go +++ b/bridge/launchpad/import.go @@ -44,15 +44,18 @@ func (li *launchpadImporter) ImportAll(repo *cache.RepoCache) error { } for _, lpBug := range lpBugs { + var b *cache.BugCache + var err error + lpBugID := fmt.Sprintf("%d", lpBug.ID) - _, err := repo.ResolveBugCreateMetadata(keyLaunchpadID, lpBugID) + b, err = repo.ResolveBugCreateMetadata(keyLaunchpadID, lpBugID) if err != nil && err != bug.ErrBugNotExist { return err } if err == bug.ErrBugNotExist { createdAt, _ := time.Parse(time.RFC3339, lpBug.CreatedAt) - _, err := repo.NewBugRaw( + b, err = repo.NewBugRaw( li.makePerson(lpBug.Owner), createdAt.Unix(), lpBug.Title, @@ -70,6 +73,45 @@ func (li *launchpadImporter) ImportAll(repo *cache.RepoCache) error { fmt.Println("TODO: Update bug") } + /* Handle messages */ + if len(lpBug.Messages) == 0 { + return errors.Wrapf(err, "failed to fetch comments for bug #%s", lpBugID) + } + + // The Launchpad API returns the bug description as the first + // comment, so skip it. + for _, lpMessage := range lpBug.Messages[1:] { + _, err := b.ResolveTargetWithMetadata(keyLaunchpadID, lpMessage.ID) + if err != nil && err != cache.ErrNoMatchingOp { + return errors.Wrapf(err, "failed to fetch comments for bug #%s", lpBugID) + } + + // 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 + } + + // This is a new comment, we can add it. + createdAt, _ := time.Parse(time.RFC3339, lpMessage.CreatedAt) + err = b.AddCommentRaw( + li.makePerson(lpMessage.Owner), + createdAt.Unix(), + lpMessage.Content, + nil, + map[string]string{ + keyLaunchpadID: lpMessage.ID, + }) + if err != nil { + return errors.Wrapf(err, "failed to add comment to bug #%s", lpBugID) + } + } + err = b.CommitAsNeeded() + if err != nil { + return err + } } return nil } |