2019-07-09 23:58:15 +03:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2019-08-19 14:50:48 +03:00
|
|
|
"context"
|
2019-07-26 04:22:07 +03:00
|
|
|
"fmt"
|
2020-02-04 02:25:27 +03:00
|
|
|
"os"
|
2019-07-26 04:22:07 +03:00
|
|
|
"strconv"
|
2019-07-09 23:58:15 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-26 04:22:07 +03:00
|
|
|
"github.com/xanzy/go-gitlab"
|
2019-07-09 23:58:15 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-12-08 23:15:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core/auth"
|
2019-07-26 04:22:07 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-07-09 23:58:15 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2019-08-13 21:51:39 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2020-02-04 02:25:27 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2019-07-09 23:58:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrMissingIdentityToken = errors.New("missing identity token")
|
|
|
|
)
|
|
|
|
|
|
|
|
// gitlabExporter implement the Exporter interface
|
2019-07-26 04:22:07 +03:00
|
|
|
type gitlabExporter struct {
|
|
|
|
conf core.Configuration
|
|
|
|
|
|
|
|
// cache identities clients
|
2019-12-08 23:15:06 +03:00
|
|
|
identityClient map[entity.Id]*gitlab.Client
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
// gitlab repository ID
|
|
|
|
repositoryID string
|
|
|
|
|
|
|
|
// cache identifiers used to speed up exporting operations
|
|
|
|
// cleared for each bug
|
|
|
|
cachedOperationIDs map[string]string
|
|
|
|
}
|
2019-07-09 23:58:15 +03:00
|
|
|
|
|
|
|
// Init .
|
2020-02-15 17:39:49 +03:00
|
|
|
func (ge *gitlabExporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
|
2019-07-26 04:22:07 +03:00
|
|
|
ge.conf = conf
|
2019-12-08 23:15:06 +03:00
|
|
|
ge.identityClient = make(map[entity.Id]*gitlab.Client)
|
2019-07-26 04:22:07 +03:00
|
|
|
ge.cachedOperationIDs = make(map[string]string)
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
// get repository node id
|
2020-02-15 04:55:19 +03:00
|
|
|
ge.repositoryID = ge.conf[confKeyProjectID]
|
2019-12-08 23:15:06 +03:00
|
|
|
|
|
|
|
// preload all clients
|
2020-02-15 04:55:19 +03:00
|
|
|
err := ge.cacheAllClient(repo, ge.conf[confKeyGitlabBaseUrl])
|
2019-12-08 23:15:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:58:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
func (ge *gitlabExporter) cacheAllClient(repo *cache.RepoCache, baseURL string) error {
|
|
|
|
creds, err := auth.List(repo,
|
|
|
|
auth.WithTarget(target),
|
|
|
|
auth.WithKind(auth.KindToken),
|
|
|
|
auth.WithMeta(auth.MetaKeyBaseURL, baseURL),
|
|
|
|
)
|
2019-12-08 23:15:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
for _, cred := range creds {
|
2020-02-04 02:25:27 +03:00
|
|
|
login, ok := cred.GetMetadata(auth.MetaKeyLogin)
|
|
|
|
if !ok {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "credential %s is not tagged with a Gitlab login\n", cred.ID().Human())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := repo.ResolveIdentityImmutableMetadata(metaKeyGitlabLogin, login)
|
|
|
|
if err == identity.ErrIdentityNotExist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ge.identityClient[user.Id()]; !ok {
|
2020-02-15 04:55:19 +03:00
|
|
|
client, err := buildClient(ge.conf[confKeyGitlabBaseUrl], creds[0].(*auth.Token))
|
2019-12-10 22:30:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-04 02:25:27 +03:00
|
|
|
ge.identityClient[user.Id()] = client
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getIdentityClient return a gitlab v4 API client configured with the access token of the given identity.
|
|
|
|
func (ge *gitlabExporter) getIdentityClient(userId entity.Id) (*gitlab.Client, error) {
|
|
|
|
client, ok := ge.identityClient[userId]
|
|
|
|
if ok {
|
|
|
|
return client, nil
|
|
|
|
}
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
return nil, ErrMissingIdentityToken
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 23:58:15 +03:00
|
|
|
// ExportAll export all event made by the current user to Gitlab
|
2019-08-19 14:50:48 +03:00
|
|
|
func (ge *gitlabExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
|
2019-07-26 04:22:07 +03:00
|
|
|
out := make(chan core.ExportResult)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
allIdentitiesIds := make([]entity.Id, 0, len(ge.identityClient))
|
|
|
|
for id := range ge.identityClient {
|
|
|
|
allIdentitiesIds = append(allIdentitiesIds, id)
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
allBugsIds := repo.AllBugsIds()
|
|
|
|
|
|
|
|
for _, id := range allBugsIds {
|
2019-08-19 15:09:31 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-07-26 04:22:07 +03:00
|
|
|
return
|
2019-08-19 15:09:31 +03:00
|
|
|
default:
|
|
|
|
b, err := repo.ResolveBug(id)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewExportError(err, id)
|
|
|
|
return
|
|
|
|
}
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-08-19 15:09:31 +03:00
|
|
|
snapshot := b.Snapshot()
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-08-19 15:09:31 +03:00
|
|
|
// ignore issues created before since date
|
|
|
|
// TODO: compare the Lamport time instead of using the unix time
|
|
|
|
if snapshot.CreatedAt.Before(since) {
|
|
|
|
out <- core.NewExportNothing(b.Id(), "bug created before the since date")
|
|
|
|
continue
|
|
|
|
}
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-08-19 15:09:31 +03:00
|
|
|
if snapshot.HasAnyActor(allIdentitiesIds...) {
|
|
|
|
// try to export the bug and it associated events
|
2020-02-15 04:55:19 +03:00
|
|
|
ge.exportBug(ctx, b, out)
|
2019-08-19 15:09:31 +03:00
|
|
|
}
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportBug publish bugs and related events
|
2020-02-15 04:55:19 +03:00
|
|
|
func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) {
|
2019-07-26 04:22:07 +03:00
|
|
|
snapshot := b.Snapshot()
|
|
|
|
|
2019-11-19 22:09:29 +03:00
|
|
|
var bugUpdated bool
|
2019-07-26 04:22:07 +03:00
|
|
|
var err error
|
|
|
|
var bugGitlabID int
|
|
|
|
var bugGitlabIDString string
|
2019-12-10 22:30:29 +03:00
|
|
|
var GitlabBaseUrl string
|
2019-08-13 21:51:39 +03:00
|
|
|
var bugCreationId string
|
|
|
|
|
2019-07-26 04:22:07 +03:00
|
|
|
// Special case:
|
|
|
|
// if a user try to export a bug that is not already exported to Gitlab (or imported
|
|
|
|
// from Gitlab) and we do not have the token of the bug author, there is nothing we can do.
|
|
|
|
|
|
|
|
// skip bug if origin is not allowed
|
2019-11-10 19:48:13 +03:00
|
|
|
origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
|
2019-07-26 04:22:07 +03:00
|
|
|
if ok && origin != target {
|
|
|
|
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// first operation is always createOp
|
|
|
|
createOp := snapshot.Operations[0].(*bug.CreateOperation)
|
|
|
|
author := snapshot.Author
|
|
|
|
|
|
|
|
// get gitlab bug ID
|
2019-11-10 19:48:13 +03:00
|
|
|
gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
|
2019-07-26 04:22:07 +03:00
|
|
|
if ok {
|
2019-12-10 22:30:29 +03:00
|
|
|
gitlabBaseUrl, ok := snapshot.GetCreateMetadata(metaKeyGitlabBaseUrl)
|
2020-02-15 04:55:19 +03:00
|
|
|
if ok && gitlabBaseUrl != ge.conf[confKeyGitlabBaseUrl] {
|
2019-12-10 22:30:29 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), "skipping issue imported from another Gitlab instance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-10 19:48:13 +03:00
|
|
|
projectID, ok := snapshot.GetCreateMetadata(metaKeyGitlabProject)
|
2019-08-23 15:57:34 +03:00
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("expected to find gitlab project id")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
if projectID != ge.conf[confKeyProjectID] {
|
2019-08-23 15:57:34 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), "skipping issue imported from another repository")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:22:07 +03:00
|
|
|
// will be used to mark operation related to a bug as exported
|
|
|
|
bugGitlabIDString = gitlabID
|
|
|
|
bugGitlabID, err = strconv.Atoi(bugGitlabIDString)
|
|
|
|
if err != nil {
|
2019-08-23 15:57:34 +03:00
|
|
|
out <- core.NewExportError(fmt.Errorf("unexpected gitlab id format: %s", bugGitlabIDString), b.Id())
|
|
|
|
return
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// check that we have a token for operation author
|
|
|
|
client, err := ge.getIdentityClient(author.Id())
|
|
|
|
if err != nil {
|
|
|
|
// if bug is still not exported and we do not have the author stop the execution
|
|
|
|
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create bug
|
2019-08-19 15:09:31 +03:00
|
|
|
_, id, url, err := createGitlabIssue(ctx, client, ge.repositoryID, createOp.Title, createOp.Message)
|
2019-07-26 04:22:07 +03:00
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "exporting gitlab issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
idString := strconv.Itoa(id)
|
|
|
|
out <- core.NewExportBug(b.Id())
|
|
|
|
|
2019-08-23 15:57:34 +03:00
|
|
|
_, err = b.SetMetadata(
|
|
|
|
createOp.Id(),
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
metaKeyGitlabId: idString,
|
|
|
|
metaKeyGitlabUrl: url,
|
|
|
|
metaKeyGitlabProject: ge.repositoryID,
|
2019-12-10 22:30:29 +03:00
|
|
|
metaKeyGitlabBaseUrl: GitlabBaseUrl,
|
2019-08-23 15:57:34 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit operation to avoid creating multiple issues with multiple pushes
|
|
|
|
if err := b.CommitAsNeeded(); err != nil {
|
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache bug gitlab ID and URL
|
|
|
|
bugGitlabID = id
|
|
|
|
bugGitlabIDString = idString
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
bugCreationId = createOp.Id().String()
|
2019-07-26 04:22:07 +03:00
|
|
|
// cache operation gitlab id
|
2019-08-13 21:51:39 +03:00
|
|
|
ge.cachedOperationIDs[bugCreationId] = bugGitlabIDString
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
labelSet := make(map[string]struct{})
|
2019-07-26 04:22:07 +03:00
|
|
|
for _, op := range snapshot.Operations[1:] {
|
|
|
|
// ignore SetMetadata operations
|
|
|
|
if _, ok := op.(*bug.SetMetadataOperation); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore operations already existing in gitlab (due to import or export)
|
|
|
|
// cache the ID of already exported or imported issues and events from Gitlab
|
2019-11-10 19:48:13 +03:00
|
|
|
if id, ok := op.GetMetadata(metaKeyGitlabId); ok {
|
2019-08-13 21:51:39 +03:00
|
|
|
ge.cachedOperationIDs[op.Id().String()] = id
|
2019-07-26 04:22:07 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opAuthor := op.GetAuthor()
|
|
|
|
client, err := ge.getIdentityClient(opAuthor.Id())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var id int
|
|
|
|
var idString, url string
|
2019-08-23 02:54:02 +03:00
|
|
|
switch op := op.(type) {
|
2019-07-26 04:22:07 +03:00
|
|
|
case *bug.AddCommentOperation:
|
|
|
|
|
|
|
|
// send operation to gitlab
|
2019-08-23 02:54:02 +03:00
|
|
|
id, err = addCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, op.Message)
|
2019-07-26 04:22:07 +03:00
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "adding comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportComment(op.Id())
|
2019-07-26 04:22:07 +03:00
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
idString = strconv.Itoa(id)
|
2019-07-26 04:22:07 +03:00
|
|
|
// cache comment id
|
2019-08-13 21:51:39 +03:00
|
|
|
ge.cachedOperationIDs[op.Id().String()] = idString
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
case *bug.EditCommentOperation:
|
2019-08-23 02:54:02 +03:00
|
|
|
targetId := op.Target.String()
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
// Since gitlab doesn't consider the issue body as a comment
|
2019-08-13 21:51:39 +03:00
|
|
|
if targetId == bugCreationId {
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
// case bug creation operation: we need to edit the Gitlab issue
|
2019-08-23 02:54:02 +03:00
|
|
|
if err := updateGitlabIssueBody(ctx, client, ge.repositoryID, bugGitlabID, op.Message); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "editing issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportCommentEdition(op.Id())
|
2019-07-26 04:22:07 +03:00
|
|
|
id = bugGitlabID
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// case comment edition operation: we need to edit the Gitlab comment
|
2019-08-13 21:51:39 +03:00
|
|
|
commentID, ok := ge.cachedOperationIDs[targetId]
|
2019-07-26 04:22:07 +03:00
|
|
|
if !ok {
|
2019-08-23 02:54:02 +03:00
|
|
|
out <- core.NewExportError(fmt.Errorf("unexpected error: comment id not found"), op.Target)
|
|
|
|
return
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
commentIDint, err := strconv.Atoi(commentID)
|
2019-07-26 04:22:07 +03:00
|
|
|
if err != nil {
|
2019-08-23 02:54:02 +03:00
|
|
|
out <- core.NewExportError(fmt.Errorf("unexpected comment id format"), op.Target)
|
|
|
|
return
|
2019-08-13 21:51:39 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
if err := editCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, commentIDint, op.Message); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "editing comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportCommentEdition(op.Id())
|
|
|
|
id = commentIDint
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case *bug.SetStatusOperation:
|
2019-08-23 02:54:02 +03:00
|
|
|
if err := updateGitlabIssueStatus(ctx, client, ge.repositoryID, bugGitlabID, op.Status); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "editing status")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportStatusChange(op.Id())
|
2019-07-26 04:22:07 +03:00
|
|
|
id = bugGitlabID
|
|
|
|
|
|
|
|
case *bug.SetTitleOperation:
|
2019-08-23 02:54:02 +03:00
|
|
|
if err := updateGitlabIssueTitle(ctx, client, ge.repositoryID, bugGitlabID, op.Title); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "editing title")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportTitleEdition(op.Id())
|
2019-07-26 04:22:07 +03:00
|
|
|
id = bugGitlabID
|
|
|
|
|
|
|
|
case *bug.LabelChangeOperation:
|
2019-08-19 14:50:48 +03:00
|
|
|
// we need to set the actual list of labels at each label change operation
|
|
|
|
// because gitlab update issue requests need directly the latest list of the verison
|
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
for _, label := range op.Added {
|
|
|
|
labelSet[label.String()] = struct{}{}
|
2019-08-19 14:50:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
for _, label := range op.Removed {
|
|
|
|
delete(labelSet, label.String())
|
|
|
|
}
|
2019-08-19 14:50:48 +03:00
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
labels := make([]string, 0, len(labelSet))
|
|
|
|
for key := range labelSet {
|
|
|
|
labels = append(labels, key)
|
2019-08-19 14:50:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 02:54:02 +03:00
|
|
|
if err := updateGitlabIssueLabels(ctx, client, ge.repositoryID, bugGitlabID, labels); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "updating labels")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
out <- core.NewExportLabelChange(op.Id())
|
2019-07-26 04:22:07 +03:00
|
|
|
id = bugGitlabID
|
|
|
|
default:
|
|
|
|
panic("unhandled operation type case")
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:09:31 +03:00
|
|
|
idString = strconv.Itoa(id)
|
2019-07-26 04:22:07 +03:00
|
|
|
// mark operation as exported
|
2019-08-13 21:51:39 +03:00
|
|
|
if err := markOperationAsExported(b, op.Id(), idString, url); err != nil {
|
2019-07-26 04:22:07 +03:00
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit at each operation export to avoid exporting same events multiple times
|
|
|
|
if err := b.CommitAsNeeded(); err != nil {
|
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
2019-11-19 22:09:29 +03:00
|
|
|
|
|
|
|
bugUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bugUpdated {
|
|
|
|
out <- core.NewExportNothing(b.Id(), "nothing has been exported")
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:51:39 +03:00
|
|
|
func markOperationAsExported(b *cache.BugCache, target entity.Id, gitlabID, gitlabURL string) error {
|
2019-07-26 04:22:07 +03:00
|
|
|
_, err := b.SetMetadata(
|
|
|
|
target,
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
metaKeyGitlabId: gitlabID,
|
|
|
|
metaKeyGitlabUrl: gitlabURL,
|
2019-07-26 04:22:07 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a gitlab. issue and return it ID
|
2019-08-19 15:09:31 +03:00
|
|
|
func createGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID, title, body string) (int, int, string, error) {
|
2019-08-19 14:50:48 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
issue, _, err := gc.Issues.CreateIssue(
|
|
|
|
repositoryID,
|
|
|
|
&gitlab.CreateIssueOptions{
|
|
|
|
Title: &title,
|
|
|
|
Description: &body,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
if err != nil {
|
2019-08-19 15:09:31 +03:00
|
|
|
return 0, 0, "", err
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-19 15:09:31 +03:00
|
|
|
return issue.ID, issue.IID, issue.WebURL, nil
|
2019-07-26 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a comment to an issue and return it ID
|
2019-08-19 14:50:48 +03:00
|
|
|
func addCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) (int, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
note, _, err := gc.Notes.CreateIssueNote(
|
|
|
|
repositoryID, issueID,
|
|
|
|
&gitlab.CreateIssueNoteOptions{
|
|
|
|
Body: &body,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return note.ID, nil
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:50:48 +03:00
|
|
|
func editCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID, noteID int, body string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := gc.Notes.UpdateIssueNote(
|
|
|
|
repositoryID, issueID, noteID,
|
|
|
|
&gitlab.UpdateIssueNoteOptions{
|
|
|
|
Body: &body,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:50:48 +03:00
|
|
|
func updateGitlabIssueStatus(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, status bug.Status) error {
|
2019-07-26 04:22:07 +03:00
|
|
|
var state string
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case bug.OpenStatus:
|
|
|
|
state = "reopen"
|
|
|
|
case bug.ClosedStatus:
|
|
|
|
state = "close"
|
|
|
|
default:
|
|
|
|
panic("unknown bug state")
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:50:48 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := gc.Issues.UpdateIssue(
|
|
|
|
repositoryID, issueID,
|
|
|
|
&gitlab.UpdateIssueOptions{
|
|
|
|
StateEvent: &state,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:50:48 +03:00
|
|
|
func updateGitlabIssueBody(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := gc.Issues.UpdateIssue(
|
|
|
|
repositoryID, issueID,
|
|
|
|
&gitlab.UpdateIssueOptions{
|
|
|
|
Description: &body,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:50:48 +03:00
|
|
|
func updateGitlabIssueTitle(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, title string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := gc.Issues.UpdateIssue(
|
|
|
|
repositoryID, issueID,
|
|
|
|
&gitlab.UpdateIssueOptions{
|
|
|
|
Title: &title,
|
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update gitlab. issue labels
|
2019-08-19 14:50:48 +03:00
|
|
|
func updateGitlabIssueLabels(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, labels []string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
2019-10-24 22:08:31 +03:00
|
|
|
gitlabLabels := gitlab.Labels(labels)
|
2019-08-19 14:50:48 +03:00
|
|
|
_, _, err := gc.Issues.UpdateIssue(
|
|
|
|
repositoryID, issueID,
|
|
|
|
&gitlab.UpdateIssueOptions{
|
2019-10-24 22:08:31 +03:00
|
|
|
Labels: &gitlabLabels,
|
2019-08-19 14:50:48 +03:00
|
|
|
},
|
|
|
|
gitlab.WithContext(ctx),
|
|
|
|
)
|
2019-07-26 04:22:07 +03:00
|
|
|
|
|
|
|
return err
|
2019-07-09 23:58:15 +03:00
|
|
|
}
|