2019-07-09 23:56:38 +03:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2019-08-13 20:51:14 +03:00
|
|
|
"context"
|
2019-07-17 01:06:42 +03:00
|
|
|
"fmt"
|
2019-07-17 01:09:02 +03:00
|
|
|
"strconv"
|
2019-07-09 23:56:38 +03:00
|
|
|
"time"
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
|
2019-07-09 23:56:38 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-07-17 01:06:42 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-07-09 23:56:38 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2019-07-17 01:06:42 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2019-07-09 23:56:38 +03:00
|
|
|
)
|
|
|
|
|
2019-07-22 01:13:47 +03:00
|
|
|
// gitlabImporter implement the Importer interface
|
2019-07-09 23:56:38 +03:00
|
|
|
type gitlabImporter struct {
|
|
|
|
conf core.Configuration
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
// iterator
|
|
|
|
iterator *iterator
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
// send only channel
|
|
|
|
out chan<- core.ImportResult
|
2019-07-09 23:56:38 +03:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
func (gi *gitlabImporter) Init(conf core.Configuration) error {
|
|
|
|
gi.conf = conf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 01:13:47 +03:00
|
|
|
// ImportAll iterate over all the configured repository issues (notes) and ensure the creation
|
|
|
|
// of the missing issues / comments / label events / title changes ...
|
2019-08-13 20:51:14 +03:00
|
|
|
func (gi *gitlabImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
|
|
|
|
gi.iterator = NewIterator(ctx, 10, gi.conf[keyProjectID], gi.conf[keyToken], since)
|
|
|
|
out := make(chan core.ImportResult)
|
|
|
|
gi.out = out
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(gi.out)
|
|
|
|
|
|
|
|
// Loop over all matching issues
|
|
|
|
for gi.iterator.NextIssue() {
|
|
|
|
issue := gi.iterator.IssueValue()
|
|
|
|
|
|
|
|
// create issue
|
|
|
|
b, err := gi.ensureIssue(repo, issue)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("issue creation: %v", err)
|
|
|
|
out <- core.NewImportError(err, "")
|
|
|
|
return
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
// Loop over all notes
|
|
|
|
for gi.iterator.NextNote() {
|
|
|
|
note := gi.iterator.NoteValue()
|
|
|
|
if err := gi.ensureNote(repo, b, note); err != nil {
|
|
|
|
err := fmt.Errorf("note creation: %v", err)
|
|
|
|
out <- core.NewImportError(err, entity.Id(strconv.Itoa(note.ID)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
// Loop over all label events
|
|
|
|
for gi.iterator.NextLabelEvent() {
|
|
|
|
labelEvent := gi.iterator.LabelEventValue()
|
|
|
|
if err := gi.ensureLabelEvent(repo, b, labelEvent); err != nil {
|
|
|
|
err := fmt.Errorf("label event creation: %v", err)
|
|
|
|
out <- core.NewImportError(err, entity.Id(strconv.Itoa(labelEvent.ID)))
|
|
|
|
return
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
// commit bug state
|
|
|
|
if err := b.CommitAsNeeded(); err != nil {
|
|
|
|
err := fmt.Errorf("bug commit: %v", err)
|
|
|
|
out <- core.NewImportError(err, "")
|
|
|
|
return
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
if err := gi.iterator.Error(); err != nil {
|
2019-08-13 20:51:14 +03:00
|
|
|
out <- core.NewImportError(err, "")
|
2019-07-23 19:40:10 +03:00
|
|
|
}
|
2019-08-13 20:51:14 +03:00
|
|
|
}()
|
2019-07-23 19:40:10 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
return out, nil
|
2019-07-09 23:56:38 +03:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue) (*cache.BugCache, error) {
|
|
|
|
// ensure issue author
|
|
|
|
author, err := gi.ensurePerson(repo, issue.Author.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve bug
|
|
|
|
b, err := repo.ResolveBugCreateMetadata(keyGitlabUrl, issue.WebURL)
|
2019-07-23 19:40:10 +03:00
|
|
|
if err == nil {
|
2019-08-18 00:46:10 +03:00
|
|
|
gi.out <- core.NewImportNothing("", "bug already imported")
|
2019-07-23 19:40:10 +03:00
|
|
|
return b, nil
|
|
|
|
}
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != bug.ErrBugNotExist {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
// if bug was never imported
|
|
|
|
cleanText, err := text.Cleanup(issue.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
// create bug
|
|
|
|
b, _, err = repo.NewBugRaw(
|
|
|
|
author,
|
|
|
|
issue.CreatedAt.Unix(),
|
|
|
|
issue.Title,
|
|
|
|
cleanText,
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
|
|
|
core.KeyOrigin: target,
|
2019-08-19 15:09:31 +03:00
|
|
|
keyGitlabId: parseID(issue.IID),
|
2019-07-23 19:40:10 +03:00
|
|
|
keyGitlabUrl: issue.WebURL,
|
|
|
|
keyGitlabProject: gi.conf[keyProjectID],
|
|
|
|
},
|
|
|
|
)
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
// importing a new bug
|
2019-08-13 20:51:14 +03:00
|
|
|
gi.out <- core.NewImportBug(b.Id())
|
2019-07-23 19:40:10 +03:00
|
|
|
|
2019-07-19 20:39:15 +03:00
|
|
|
return b, nil
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gi *gitlabImporter) ensureNote(repo *cache.RepoCache, b *cache.BugCache, note *gitlab.Note) error {
|
2019-08-07 20:14:12 +03:00
|
|
|
gitlabID := parseID(note.ID)
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
id, errResolve := b.ResolveOperationWithMetadata(keyGitlabId, gitlabID)
|
2019-07-31 00:51:37 +03:00
|
|
|
if errResolve != nil && errResolve != cache.ErrNoMatchingOp {
|
|
|
|
return errResolve
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
// ensure issue author
|
|
|
|
author, err := gi.ensurePerson(repo, note.Author.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
noteType, body := GetNoteType(note)
|
|
|
|
switch noteType {
|
|
|
|
case NOTE_CLOSED:
|
2019-07-31 00:51:37 +03:00
|
|
|
if errResolve == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.CloseRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.CreatedAt.Unix(),
|
|
|
|
map[string]string{
|
2019-08-07 20:14:12 +03:00
|
|
|
keyGitlabId: gitlabID,
|
2019-07-17 01:06:42 +03:00
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gi.out <- core.NewImportStatusChange(op.Id())
|
2019-07-17 01:06:42 +03:00
|
|
|
|
|
|
|
case NOTE_REOPENED:
|
2019-07-31 00:51:37 +03:00
|
|
|
if errResolve == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.OpenRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.CreatedAt.Unix(),
|
|
|
|
map[string]string{
|
2019-08-07 20:14:12 +03:00
|
|
|
keyGitlabId: gitlabID,
|
2019-07-17 01:06:42 +03:00
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gi.out <- core.NewImportStatusChange(op.Id())
|
2019-07-17 01:06:42 +03:00
|
|
|
|
|
|
|
case NOTE_DESCRIPTION_CHANGED:
|
|
|
|
issue := gi.iterator.IssueValue()
|
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
firstComment := b.Snapshot().Comments[0]
|
2019-07-17 01:06:42 +03:00
|
|
|
// since gitlab doesn't provide the issue history
|
|
|
|
// we should check for "changed the description" notes and compare issue texts
|
2019-07-19 19:56:58 +03:00
|
|
|
// TODO: Check only one time and ignore next 'description change' within one issue
|
2019-07-31 00:51:37 +03:00
|
|
|
if errResolve == cache.ErrNoMatchingOp && issue.Description != firstComment.Message {
|
2019-07-17 01:06:42 +03:00
|
|
|
// comment edition
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.EditCommentRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.UpdatedAt.Unix(),
|
2019-08-07 20:14:12 +03:00
|
|
|
firstComment.Id(),
|
2019-07-17 01:06:42 +03:00
|
|
|
issue.Description,
|
|
|
|
map[string]string{
|
2019-08-07 20:14:12 +03:00
|
|
|
keyGitlabId: gitlabID,
|
2019-07-17 01:06:42 +03:00
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
gi.out <- core.NewImportTitleEdition(op.Id())
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case NOTE_COMMENT:
|
|
|
|
cleanText, err := text.Cleanup(body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't import the comment
|
2019-07-17 19:54:19 +03:00
|
|
|
if errResolve == cache.ErrNoMatchingOp {
|
2019-07-17 01:06:42 +03:00
|
|
|
|
|
|
|
// add comment operation
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.AddCommentRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.CreatedAt.Unix(),
|
|
|
|
cleanText,
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
2019-08-07 20:14:12 +03:00
|
|
|
keyGitlabId: gitlabID,
|
2019-07-17 01:06:42 +03:00
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gi.out <- core.NewImportComment(op.Id())
|
|
|
|
return nil
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if comment was already exported
|
|
|
|
|
|
|
|
// search for last comment update
|
2019-08-07 20:14:12 +03:00
|
|
|
comment, err := b.Snapshot().SearchComment(id)
|
2019-07-17 01:06:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare local bug comment with the new note body
|
2019-07-17 19:54:19 +03:00
|
|
|
if comment.Message != cleanText {
|
2019-07-17 01:06:42 +03:00
|
|
|
// comment edition
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.EditCommentRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.UpdatedAt.Unix(),
|
2019-08-07 20:14:12 +03:00
|
|
|
comment.Id(),
|
2019-07-17 01:06:42 +03:00
|
|
|
cleanText,
|
2019-07-23 19:40:10 +03:00
|
|
|
nil,
|
2019-07-17 01:06:42 +03:00
|
|
|
)
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gi.out <- core.NewImportCommentEdition(op.Id())
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case NOTE_TITLE_CHANGED:
|
2019-07-17 19:54:19 +03:00
|
|
|
// title change events are given new notes
|
2019-07-31 00:51:37 +03:00
|
|
|
if errResolve == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
op, err := b.SetTitleRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
author,
|
|
|
|
note.CreatedAt.Unix(),
|
|
|
|
body,
|
|
|
|
map[string]string{
|
2019-08-07 20:14:12 +03:00
|
|
|
keyGitlabId: gitlabID,
|
2019-07-17 01:06:42 +03:00
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
gi.out <- core.NewImportTitleEdition(op.Id())
|
2019-07-17 01:06:42 +03:00
|
|
|
|
2019-07-26 04:19:49 +03:00
|
|
|
case NOTE_UNKNOWN,
|
|
|
|
NOTE_ASSIGNED,
|
|
|
|
NOTE_UNASSIGNED,
|
|
|
|
NOTE_CHANGED_MILESTONE,
|
|
|
|
NOTE_REMOVED_MILESTONE,
|
|
|
|
NOTE_CHANGED_DUEDATE,
|
|
|
|
NOTE_REMOVED_DUEDATE,
|
|
|
|
NOTE_LOCKED,
|
2019-08-11 01:08:26 +03:00
|
|
|
NOTE_UNLOCKED,
|
|
|
|
NOTE_MENTIONED_IN_ISSUE,
|
|
|
|
NOTE_MENTIONED_IN_MERGE_REQUEST:
|
2019-08-13 20:51:14 +03:00
|
|
|
|
|
|
|
reason := fmt.Sprintf("unsupported note type: %s", noteType.String())
|
|
|
|
gi.out <- core.NewImportNothing("", reason)
|
2019-07-17 01:06:42 +03:00
|
|
|
return nil
|
2019-07-22 19:56:14 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unhandled note type")
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 23:56:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-17 01:06:42 +03:00
|
|
|
|
|
|
|
func (gi *gitlabImporter) ensureLabelEvent(repo *cache.RepoCache, b *cache.BugCache, labelEvent *gitlab.LabelEvent) error {
|
|
|
|
_, err := b.ResolveOperationWithMetadata(keyGitlabId, parseID(labelEvent.ID))
|
|
|
|
if err != cache.ErrNoMatchingOp {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure issue author
|
|
|
|
author, err := gi.ensurePerson(repo, labelEvent.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch labelEvent.Action {
|
|
|
|
case "add":
|
|
|
|
_, err = b.ForceChangeLabelsRaw(
|
|
|
|
author,
|
|
|
|
labelEvent.CreatedAt.Unix(),
|
|
|
|
[]string{labelEvent.Label.Name},
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
|
|
|
keyGitlabId: parseID(labelEvent.ID),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
case "remove":
|
|
|
|
_, err = b.ForceChangeLabelsRaw(
|
|
|
|
author,
|
|
|
|
labelEvent.CreatedAt.Unix(),
|
|
|
|
nil,
|
|
|
|
[]string{labelEvent.Label.Name},
|
|
|
|
map[string]string{
|
|
|
|
keyGitlabId: parseID(labelEvent.ID),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
2019-07-23 19:40:10 +03:00
|
|
|
err = fmt.Errorf("unexpected label event action")
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gi *gitlabImporter) ensurePerson(repo *cache.RepoCache, id int) (*cache.IdentityCache, error) {
|
2019-07-17 01:09:02 +03:00
|
|
|
// Look first in the cache
|
|
|
|
i, err := repo.ResolveIdentityImmutableMetadata(keyGitlabId, strconv.Itoa(id))
|
|
|
|
if err == nil {
|
|
|
|
return i, nil
|
|
|
|
}
|
2019-08-12 17:12:14 +03:00
|
|
|
if _, ok := err.(entity.ErrMultipleMatch); ok {
|
2019-07-17 01:09:02 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:06:42 +03:00
|
|
|
client := buildClient(gi.conf["token"])
|
|
|
|
|
|
|
|
user, _, err := client.Users.GetUser(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
i, err = repo.NewIdentityRaw(
|
2019-07-17 01:06:42 +03:00
|
|
|
user.Name,
|
|
|
|
user.PublicEmail,
|
|
|
|
user.Username,
|
|
|
|
user.AvatarURL,
|
|
|
|
map[string]string{
|
2019-07-23 20:16:52 +03:00
|
|
|
// because Gitlab
|
2019-07-17 01:09:02 +03:00
|
|
|
keyGitlabId: strconv.Itoa(id),
|
2019-07-17 01:06:42 +03:00
|
|
|
keyGitlabLogin: user.Username,
|
|
|
|
},
|
|
|
|
)
|
2019-08-13 20:51:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gi.out <- core.NewImportIdentity(i.Id())
|
|
|
|
return i, nil
|
2019-07-17 01:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseID(id int) string {
|
|
|
|
return fmt.Sprintf("%d", id)
|
|
|
|
}
|