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		lpBugID := fmt.Sprintf("%d", lpBug.ID)
48		_, err := repo.ResolveBugCreateMetadata(keyLaunchpadID, lpBugID)
49		if err != nil && err != bug.ErrBugNotExist {
50			return err
51		}
52
53		if err == bug.ErrBugNotExist {
54			createdAt, _ := time.Parse(time.RFC3339, lpBug.CreatedAt)
55			_, err := repo.NewBugRaw(
56				li.makePerson(lpBug.Owner),
57				createdAt.Unix(),
58				lpBug.Title,
59				lpBug.Description,
60				nil,
61				map[string]string{
62					keyLaunchpadID: lpBugID,
63				},
64			)
65			if err != nil {
66				return errors.Wrapf(err, "failed to add bug id #%s", lpBugID)
67			}
68		} else {
69			/* TODO: Update bug */
70			fmt.Println("TODO: Update bug")
71		}
72
73	}
74	return nil
75}
76
77func (li *launchpadImporter) Import(repo *cache.RepoCache, id string) error {
78	fmt.Println("IMPORT")
79	return nil
80}