import.go

  1package launchpad
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"github.com/MichaelMure/git-bug/bridge/core"
  8	"github.com/MichaelMure/git-bug/bug"
  9	"github.com/MichaelMure/git-bug/cache"
 10	"github.com/pkg/errors"
 11)
 12
 13type launchpadImporter struct {
 14	conf core.Configuration
 15}
 16
 17func (li *launchpadImporter) Init(conf core.Configuration) error {
 18	li.conf = conf
 19	return nil
 20}
 21
 22const keyLaunchpadID = "launchpad-id"
 23
 24func (li *launchpadImporter) makePerson(owner LPPerson) bug.Person {
 25	return bug.Person{
 26		Name:      owner.Name,
 27		Email:     "",
 28		Login:     owner.Login,
 29		AvatarUrl: "",
 30	}
 31}
 32
 33func (li *launchpadImporter) ImportAll(repo *cache.RepoCache) error {
 34	lpAPI := new(launchpadAPI)
 35
 36	err := lpAPI.Init()
 37	if err != nil {
 38		return err
 39	}
 40
 41	lpBugs, err := lpAPI.SearchTasks(li.conf["project"])
 42	if err != nil {
 43		return err
 44	}
 45
 46	for _, lpBug := range lpBugs {
 47		var b *cache.BugCache
 48		var err error
 49
 50		lpBugID := fmt.Sprintf("%d", lpBug.ID)
 51		b, err = repo.ResolveBugCreateMetadata(keyLaunchpadID, lpBugID)
 52		if err != nil && err != bug.ErrBugNotExist {
 53			return err
 54		}
 55
 56		if err == bug.ErrBugNotExist {
 57			createdAt, _ := time.Parse(time.RFC3339, lpBug.CreatedAt)
 58			b, err = repo.NewBugRaw(
 59				li.makePerson(lpBug.Owner),
 60				createdAt.Unix(),
 61				lpBug.Title,
 62				lpBug.Description,
 63				nil,
 64				map[string]string{
 65					keyLaunchpadID: lpBugID,
 66				},
 67			)
 68			if err != nil {
 69				return errors.Wrapf(err, "failed to add bug id #%s", lpBugID)
 70			}
 71		} else {
 72			/* TODO: Update bug */
 73			fmt.Println("TODO: Update bug")
 74		}
 75
 76		/* Handle messages */
 77		if len(lpBug.Messages) == 0 {
 78			return errors.Wrapf(err, "failed to fetch comments for bug #%s", lpBugID)
 79		}
 80
 81		// The Launchpad API returns the bug description as the first
 82		// comment, so skip it.
 83		for _, lpMessage := range lpBug.Messages[1:] {
 84			_, err := b.ResolveTargetWithMetadata(keyLaunchpadID, lpMessage.ID)
 85			if err != nil && err != cache.ErrNoMatchingOp {
 86				return errors.Wrapf(err, "failed to fetch comments for bug #%s", lpBugID)
 87			}
 88
 89			// If this comment already exists, we are probably
 90			// updating an existing bug. We do not want to duplicate
 91			// the comments, so let us just skip this one.
 92			// TODO: Can Launchpad comments be edited?
 93			if err == nil {
 94				continue
 95			}
 96
 97			// This is a new comment, we can add it.
 98			createdAt, _ := time.Parse(time.RFC3339, lpMessage.CreatedAt)
 99			err = b.AddCommentRaw(
100				li.makePerson(lpMessage.Owner),
101				createdAt.Unix(),
102				lpMessage.Content,
103				nil,
104				map[string]string{
105					keyLaunchpadID: lpMessage.ID,
106				})
107			if err != nil {
108				return errors.Wrapf(err, "failed to add comment to bug #%s", lpBugID)
109			}
110		}
111		err = b.CommitAsNeeded()
112		if err != nil {
113			return err
114		}
115	}
116	return nil
117}
118
119func (li *launchpadImporter) Import(repo *cache.RepoCache, id string) error {
120	fmt.Println("IMPORT")
121	return nil
122}