2019-06-09 00:14:59 +03:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2019-06-22 21:08:51 +03:00
|
|
|
"bytes"
|
2019-06-14 21:28:53 +03:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-01-12 18:38:16 +03:00
|
|
|
"os"
|
2019-08-06 00:27:08 +03:00
|
|
|
"strings"
|
2019-06-09 00:14:59 +03:00
|
|
|
"time"
|
|
|
|
|
2019-06-22 18:10:23 +03:00
|
|
|
"github.com/pkg/errors"
|
2019-06-15 03:50:09 +03:00
|
|
|
"github.com/shurcooL/githubv4"
|
2019-08-18 00:45:31 +03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-15 03:50:09 +03:00
|
|
|
|
2019-06-09 00:14:59 +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-06-09 00:14:59 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/bug"
|
2022-08-20 11:52:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/common"
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2022-07-25 14:16:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity/dag"
|
2019-06-09 00:14:59 +03:00
|
|
|
)
|
|
|
|
|
2019-06-22 18:10:23 +03:00
|
|
|
var (
|
|
|
|
ErrMissingIdentityToken = errors.New("missing identity token")
|
|
|
|
)
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// githubExporter implement the Exporter interface
|
2019-06-09 00:14:59 +03:00
|
|
|
type githubExporter struct {
|
2019-06-20 19:08:29 +03:00
|
|
|
conf core.Configuration
|
|
|
|
|
2019-06-22 00:25:23 +03:00
|
|
|
// cache identities clients
|
2021-09-14 23:22:28 +03:00
|
|
|
identityClient map[entity.Id]*rateLimitHandlerClient
|
2019-06-21 01:25:43 +03:00
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
// the client to use for non user-specific queries
|
2020-02-23 16:05:03 +03:00
|
|
|
// it's the client associated to the "default-login" config
|
|
|
|
// used for the github V4 API (graphql)
|
2021-09-14 23:22:28 +03:00
|
|
|
defaultClient *rateLimitHandlerClient
|
2020-01-24 02:30:13 +03:00
|
|
|
|
|
|
|
// the token of the default user
|
2020-02-23 16:05:03 +03:00
|
|
|
// it's the token associated to the "default-login" config
|
|
|
|
// used for the github V3 API (REST)
|
2020-01-24 02:30:13 +03:00
|
|
|
defaultToken *auth.Token
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// github repository ID
|
|
|
|
repositoryID string
|
|
|
|
|
|
|
|
// cache identifiers used to speed up exporting operations
|
|
|
|
// cleared for each bug
|
2019-08-12 17:12:14 +03:00
|
|
|
cachedOperationIDs map[entity.Id]string
|
2019-06-20 19:08:29 +03:00
|
|
|
|
|
|
|
// cache labels used to speed up exporting labels events
|
|
|
|
cachedLabels map[string]string
|
2021-09-14 23:22:28 +03:00
|
|
|
|
|
|
|
// channel to send export results
|
|
|
|
out chan<- core.ExportResult
|
2019-06-09 00:14:59 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 21:28:53 +03:00
|
|
|
// Init .
|
2020-02-15 17:39:49 +03:00
|
|
|
func (ge *githubExporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
|
2019-06-09 00:14:59 +03:00
|
|
|
ge.conf = conf
|
2021-09-14 23:22:28 +03:00
|
|
|
ge.identityClient = make(map[entity.Id]*rateLimitHandlerClient)
|
2019-08-12 17:12:14 +03:00
|
|
|
ge.cachedOperationIDs = make(map[entity.Id]string)
|
2019-06-20 19:08:29 +03:00
|
|
|
ge.cachedLabels = make(map[string]string)
|
2019-12-08 23:15:06 +03:00
|
|
|
|
|
|
|
// preload all clients
|
2020-02-23 16:05:03 +03:00
|
|
|
err := ge.cacheAllClient(repo)
|
2019-12-08 23:15:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-09 00:14:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:38:16 +03:00
|
|
|
func (ge *githubExporter) cacheAllClient(repo *cache.RepoCache) error {
|
2019-12-08 23:15:06 +03:00
|
|
|
creds, err := auth.List(repo, auth.WithTarget(target), auth.WithKind(auth.KindToken))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-21 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
for _, cred := range creds {
|
2020-01-24 02:30:13 +03:00
|
|
|
login, ok := cred.GetMetadata(auth.MetaKeyLogin)
|
2020-01-12 18:38:16 +03:00
|
|
|
if !ok {
|
2020-02-04 02:25:27 +03:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "credential %s is not tagged with a Github login\n", cred.ID().Human())
|
2020-01-12 18:38:16 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
user, err := repo.Identities().ResolveIdentityImmutableMetadata(metaKeyGithubLogin, login)
|
2022-11-19 13:33:12 +03:00
|
|
|
if entity.IsErrNotFound(err) {
|
2020-01-12 18:38:16 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-02-04 02:25:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-12 18:38:16 +03:00
|
|
|
|
2020-02-23 16:05:03 +03:00
|
|
|
if _, ok := ge.identityClient[user.Id()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
client := buildClient(creds[0].(*auth.Token))
|
|
|
|
ge.identityClient[user.Id()] = client
|
|
|
|
|
|
|
|
// assign the default client and token as well
|
|
|
|
if ge.defaultClient == nil && login == ge.conf[confKeyDefaultLogin] {
|
|
|
|
ge.defaultClient = client
|
|
|
|
ge.defaultToken = creds[0].(*auth.Token)
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
2019-06-21 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 16:05:03 +03:00
|
|
|
if ge.defaultClient == nil {
|
|
|
|
return fmt.Errorf("no token found for the default login \"%s\"", ge.conf[confKeyDefaultLogin])
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getClientForIdentity return a githubv4 API client configured with the access token of the given identity.
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) getClientForIdentity(userId entity.Id) (*rateLimitHandlerClient, error) {
|
2019-12-08 23:15:06 +03:00
|
|
|
client, ok := ge.identityClient[userId]
|
|
|
|
if ok {
|
|
|
|
return client, nil
|
|
|
|
}
|
2019-06-22 14:06:51 +03:00
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
return nil, ErrMissingIdentityToken
|
2019-06-21 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-06-09 00:14:59 +03:00
|
|
|
// ExportAll export all event made by the current user to Github
|
2019-08-13 20:51:14 +03:00
|
|
|
func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
|
2019-06-25 02:33:48 +03:00
|
|
|
out := make(chan core.ExportResult)
|
2021-09-14 23:22:28 +03:00
|
|
|
ge.out = out
|
2019-06-09 00:14:59 +03:00
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
var err error
|
2019-07-05 19:32:51 +03:00
|
|
|
// get repository node id
|
|
|
|
ge.repositoryID, err = getRepositoryNodeID(
|
2019-08-13 20:51:14 +03:00
|
|
|
ctx,
|
2019-12-08 23:15:06 +03:00
|
|
|
ge.defaultToken,
|
2020-02-15 04:55:19 +03:00
|
|
|
ge.conf[confKeyOwner],
|
|
|
|
ge.conf[confKeyProject],
|
2019-07-05 19:32:51 +03:00
|
|
|
)
|
2019-08-06 00:27:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2019-06-09 00:14:59 +03:00
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
// query all labels
|
|
|
|
err = ge.cacheGithubLabels(ctx, ge.defaultClient)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewExportError(errors.Wrap(err, "can't obtain Github labels"), "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
allIdentitiesIds := make([]entity.Id, 0, len(ge.identityClient))
|
|
|
|
for id := range ge.identityClient {
|
2019-06-25 02:33:48 +03:00
|
|
|
allIdentitiesIds = append(allIdentitiesIds, id)
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
allBugsIds := repo.Bugs().AllIds()
|
2019-06-25 02:33:48 +03:00
|
|
|
|
|
|
|
for _, id := range allBugsIds {
|
2022-12-21 23:54:36 +03:00
|
|
|
b, err := repo.Bugs().Resolve(id)
|
2019-06-25 02:33:48 +03:00
|
|
|
if err != nil {
|
2019-08-12 17:12:14 +03:00
|
|
|
out <- core.NewExportError(errors.Wrap(err, "can't load bug"), id)
|
2019-06-25 02:33:48 +03:00
|
|
|
return
|
2019-06-09 00:14:59 +03:00
|
|
|
}
|
2019-06-23 20:20:10 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
select {
|
2019-06-09 00:14:59 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
case <-ctx.Done():
|
|
|
|
// stop iterating if context cancel function is called
|
|
|
|
return
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
default:
|
|
|
|
snapshot := b.Snapshot()
|
|
|
|
|
|
|
|
// ignore issues created before since date
|
|
|
|
// TODO: compare the Lamport time instead of using the unix time
|
2020-06-26 00:18:17 +03:00
|
|
|
if snapshot.CreateTime.Before(since) {
|
2019-08-13 20:51:14 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), "bug created before the since date")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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-13 20:51:14 +03:00
|
|
|
}
|
2019-06-25 02:33:48 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-05 19:32:51 +03:00
|
|
|
}()
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
return out, nil
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// exportBug publish bugs and related events
|
2020-02-15 04:55:19 +03:00
|
|
|
func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) {
|
2019-06-20 19:08:29 +03:00
|
|
|
snapshot := b.Snapshot()
|
2019-11-19 21:51:43 +03:00
|
|
|
var bugUpdated bool
|
2019-06-20 19:08:29 +03:00
|
|
|
|
|
|
|
var bugGithubID string
|
|
|
|
var bugGithubURL string
|
|
|
|
|
|
|
|
// Special case:
|
|
|
|
// if a user try to export a bug that is not already exported to Github (or imported
|
2019-06-23 18:54:38 +03:00
|
|
|
// from Github) and we do not have the token of the bug author, there is nothing we can do.
|
2019-06-20 19:08:29 +03:00
|
|
|
|
|
|
|
// first operation is always createOp
|
|
|
|
createOp := snapshot.Operations[0].(*bug.CreateOperation)
|
2019-06-23 20:20:10 +03:00
|
|
|
author := snapshot.Author
|
2019-06-20 19:08:29 +03:00
|
|
|
|
2019-06-22 14:06:51 +03:00
|
|
|
// skip bug if origin is not allowed
|
2019-11-10 19:48:13 +03:00
|
|
|
origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
|
2019-07-06 01:23:19 +03:00
|
|
|
if ok && origin != target {
|
2019-07-05 19:32:51 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
|
2019-06-25 02:33:48 +03:00
|
|
|
return
|
2019-06-22 14:06:51 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// get github bug ID
|
2019-11-10 19:48:13 +03:00
|
|
|
githubID, ok := snapshot.GetCreateMetadata(metaKeyGithubId)
|
2019-06-20 19:08:29 +03:00
|
|
|
if ok {
|
2019-11-10 19:48:13 +03:00
|
|
|
githubURL, ok := snapshot.GetCreateMetadata(metaKeyGithubUrl)
|
2019-06-20 19:08:29 +03:00
|
|
|
if !ok {
|
|
|
|
// if we find github ID, github URL must be found too
|
2019-08-12 17:12:14 +03:00
|
|
|
err := fmt.Errorf("incomplete Github metadata: expected to find issue URL")
|
2019-07-05 19:32:51 +03:00
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
2019-06-23 18:54:38 +03:00
|
|
|
|
2019-07-06 01:23:19 +03:00
|
|
|
// extract owner and project
|
|
|
|
owner, project, err := splitURL(githubURL)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("bad project url: %v", err)
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-19 21:51:43 +03:00
|
|
|
// ignore issue coming from other repositories
|
2020-02-15 04:55:19 +03:00
|
|
|
if owner != ge.conf[confKeyOwner] && project != ge.conf[confKeyProject] {
|
2019-07-06 01:23:19 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("skipping issue from url:%s", githubURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// will be used to mark operation related to a bug as exported
|
|
|
|
bugGithubID = githubID
|
|
|
|
bugGithubURL = githubURL
|
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
} else {
|
2019-06-22 00:25:23 +03:00
|
|
|
// check that we have a token for operation author
|
2019-12-08 23:15:06 +03:00
|
|
|
client, err := ge.getClientForIdentity(author.Id())
|
2019-06-21 01:25:43 +03:00
|
|
|
if err != nil {
|
2019-06-22 18:10:23 +03:00
|
|
|
// if bug is still not exported and we do not have the author stop the execution
|
2019-06-25 02:33:48 +03:00
|
|
|
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
|
|
|
|
return
|
2019-06-21 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// create bug
|
2021-09-14 23:22:28 +03:00
|
|
|
id, url, err := ge.createGithubIssue(ctx, client, ge.repositoryID, createOp.Title, createOp.Message)
|
2019-06-20 19:08:29 +03:00
|
|
|
if err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "exporting github issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
2019-06-25 02:33:48 +03:00
|
|
|
out <- core.NewExportBug(b.Id())
|
2019-06-23 01:52:30 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// mark bug creation operation as exported
|
2019-08-12 17:12:14 +03:00
|
|
|
if err := markOperationAsExported(b, createOp.Id(), id, url); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-22 18:10:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// commit operation to avoid creating multiple issues with multiple pushes
|
|
|
|
if err := b.CommitAsNeeded(); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache bug github ID and URL
|
|
|
|
bugGithubID = id
|
|
|
|
bugGithubURL = url
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache operation github id
|
2019-08-12 17:12:14 +03:00
|
|
|
ge.cachedOperationIDs[createOp.Id()] = bugGithubID
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
for _, op := range snapshot.Operations[1:] {
|
|
|
|
// ignore SetMetadata operations
|
2022-07-25 14:16:16 +03:00
|
|
|
if _, ok := op.(dag.OperationDoesntChangeSnapshot); ok {
|
2019-06-20 19:08:29 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-07-05 19:32:51 +03:00
|
|
|
// ignore operations already existing in github (due to import or export)
|
2019-06-20 19:08:29 +03:00
|
|
|
// cache the ID of already exported or imported issues and events from Github
|
2019-11-10 19:48:13 +03:00
|
|
|
if id, ok := op.GetMetadata(metaKeyGithubId); ok {
|
2019-08-12 17:12:14 +03:00
|
|
|
ge.cachedOperationIDs[op.Id()] = id
|
2019-06-20 19:08:29 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-14 13:36:32 +03:00
|
|
|
opAuthor := op.Author()
|
2019-12-08 23:15:06 +03:00
|
|
|
client, err := ge.getClientForIdentity(opAuthor.Id())
|
2019-06-21 01:25:43 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var id, url string
|
2019-11-19 21:51:43 +03:00
|
|
|
switch op := op.(type) {
|
2019-06-20 19:08:29 +03:00
|
|
|
case *bug.AddCommentOperation:
|
|
|
|
// send operation to github
|
2021-09-14 23:22:28 +03:00
|
|
|
id, url, err = ge.addCommentGithubIssue(ctx, client, bugGithubID, op.Message)
|
2019-06-20 19:08:29 +03:00
|
|
|
if err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "adding comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportComment(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// cache comment id
|
2019-08-12 17:12:14 +03:00
|
|
|
ge.cachedOperationIDs[op.Id()] = id
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
case *bug.EditCommentOperation:
|
|
|
|
// Since github doesn't consider the issue body as a comment
|
2019-11-19 21:51:43 +03:00
|
|
|
if op.Target == createOp.Id() {
|
2019-06-22 14:40:17 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// case bug creation operation: we need to edit the Github issue
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := ge.updateGithubIssueBody(ctx, client, bugGithubID, op.Message); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "editing issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportCommentEdition(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
id = bugGithubID
|
|
|
|
url = bugGithubURL
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
} else {
|
2019-06-22 14:40:17 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// case comment edition operation: we need to edit the Github comment
|
2019-11-19 21:51:43 +03:00
|
|
|
commentID, ok := ge.cachedOperationIDs[op.Target]
|
2019-06-20 19:08:29 +03:00
|
|
|
if !ok {
|
|
|
|
panic("unexpected error: comment id not found")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
eid, eurl, err := ge.editCommentGithubIssue(ctx, client, commentID, op.Message)
|
2019-06-20 19:08:29 +03:00
|
|
|
if err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "editing comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportCommentEdition(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
// use comment id/url instead of issue id/url
|
|
|
|
id = eid
|
|
|
|
url = eurl
|
|
|
|
}
|
|
|
|
|
|
|
|
case *bug.SetStatusOperation:
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := ge.updateGithubIssueStatus(ctx, client, bugGithubID, op.Status); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "editing status")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportStatusChange(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
id = bugGithubID
|
|
|
|
url = bugGithubURL
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
case *bug.SetTitleOperation:
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := ge.updateGithubIssueTitle(ctx, client, bugGithubID, op.Title); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "editing title")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportTitleEdition(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
id = bugGithubID
|
|
|
|
url = bugGithubURL
|
2019-06-20 19:08:29 +03:00
|
|
|
|
|
|
|
case *bug.LabelChangeOperation:
|
2019-11-19 21:51:43 +03:00
|
|
|
if err := ge.updateGithubIssueLabels(ctx, client, bugGithubID, op.Added, op.Removed); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "updating labels")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-20 19:08:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportLabelChange(b.Id())
|
2019-06-25 02:33:48 +03:00
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
id = bugGithubID
|
|
|
|
url = bugGithubURL
|
2019-06-20 19:08:29 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unhandled operation type case")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 01:25:43 +03:00
|
|
|
// mark operation as exported
|
2019-08-12 17:12:14 +03:00
|
|
|
if err := markOperationAsExported(b, op.Id(), id, url); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-21 01:25:43 +03:00
|
|
|
}
|
2019-06-20 19:08:29 +03:00
|
|
|
|
2019-06-23 14:50:45 +03:00
|
|
|
// commit at each operation export to avoid exporting same events multiple times
|
2019-06-22 18:10:23 +03:00
|
|
|
if err := b.CommitAsNeeded(); err != nil {
|
2019-06-25 02:33:48 +03:00
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
|
|
|
return
|
2019-06-22 18:10:23 +03:00
|
|
|
}
|
2019-11-19 21:51:43 +03:00
|
|
|
|
|
|
|
bugUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bugUpdated {
|
|
|
|
out <- core.NewExportNothing(b.Id(), "nothing has been exported")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRepositoryNodeID request github api v3 to get repository node id
|
2019-12-08 23:15:06 +03:00
|
|
|
func getRepositoryNodeID(ctx context.Context, token *auth.Token, owner, project string) (string, error) {
|
2019-06-14 21:28:53 +03:00
|
|
|
url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
|
2019-08-13 20:51:14 +03:00
|
|
|
client := &http.Client{}
|
2019-06-14 21:28:53 +03:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// need the token for private repositories
|
2019-12-08 23:15:06 +03:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("token %s", token.Value))
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-06-14 21:28:53 +03:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-07-05 19:32:51 +03:00
|
|
|
return "", fmt.Errorf("HTTP error %v retrieving repository node id", resp.StatusCode)
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aux := struct {
|
|
|
|
NodeID string `json:"node_id"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
data, _ := ioutil.ReadAll(resp.Body)
|
2019-06-25 02:33:48 +03:00
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-06-14 21:28:53 +03:00
|
|
|
|
|
|
|
err = json.Unmarshal(data, &aux)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aux.NodeID, nil
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func markOperationAsExported(b *cache.BugCache, target entity.Id, githubID, githubURL string) error {
|
2019-06-20 19:08:29 +03:00
|
|
|
_, err := b.SetMetadata(
|
|
|
|
target,
|
|
|
|
map[string]string{
|
2019-11-10 19:48:13 +03:00
|
|
|
metaKeyGithubId: githubID,
|
|
|
|
metaKeyGithubUrl: githubURL,
|
2019-06-20 19:08:29 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) cacheGithubLabels(ctx context.Context, gc *rateLimitHandlerClient) error {
|
2019-06-22 23:28:04 +03:00
|
|
|
variables := map[string]interface{}{
|
2020-02-15 04:55:19 +03:00
|
|
|
"owner": githubv4.String(ge.conf[confKeyOwner]),
|
|
|
|
"name": githubv4.String(ge.conf[confKeyProject]),
|
2019-08-06 00:27:08 +03:00
|
|
|
"first": githubv4.Int(10),
|
|
|
|
"after": (*githubv4.String)(nil),
|
2019-06-22 23:28:04 +03:00
|
|
|
}
|
2019-06-22 20:46:42 +03:00
|
|
|
|
2019-08-06 00:27:08 +03:00
|
|
|
q := labelsQuery{}
|
2019-06-22 20:46:42 +03:00
|
|
|
|
2019-08-06 00:27:08 +03:00
|
|
|
hasNextPage := true
|
|
|
|
for hasNextPage {
|
2022-06-05 16:01:08 +03:00
|
|
|
if err := gc.queryExport(ctx, &q, variables, ge.out); err != nil {
|
2019-08-06 00:27:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range q.Repository.Labels.Nodes {
|
|
|
|
ge.cachedLabels[label.Name] = label.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
hasNextPage = q.Repository.Labels.PageInfo.HasNextPage
|
|
|
|
variables["after"] = q.Repository.Labels.PageInfo.EndCursor
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 00:27:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:07:45 +03:00
|
|
|
func (ge *githubExporter) getLabelID(label string) (string, error) {
|
2019-08-06 00:27:08 +03:00
|
|
|
label = strings.ToLower(label)
|
|
|
|
for cachedLabel, ID := range ge.cachedLabels {
|
|
|
|
if label == strings.ToLower(cachedLabel) {
|
|
|
|
return ID, nil
|
|
|
|
}
|
2019-06-22 23:28:04 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 00:27:08 +03:00
|
|
|
return "", fmt.Errorf("didn't find label id in cache")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 20:20:10 +03:00
|
|
|
// create a new label and return it github id
|
|
|
|
// NOTE: since createLabel mutation is still in preview mode we use github api v3 to create labels
|
|
|
|
// see https://developer.github.com/v4/mutation/createlabel/ and https://developer.github.com/v4/previews/#labels-preview
|
2019-08-13 20:51:14 +03:00
|
|
|
func (ge *githubExporter) createGithubLabel(ctx context.Context, label, color string) (string, error) {
|
2020-02-15 04:55:19 +03:00
|
|
|
url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[confKeyOwner], ge.conf[confKeyProject])
|
2019-08-13 20:51:14 +03:00
|
|
|
client := &http.Client{}
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-06-22 21:08:51 +03:00
|
|
|
params := struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}{
|
|
|
|
Name: label,
|
2019-06-23 18:54:38 +03:00
|
|
|
Color: color,
|
2019-06-22 21:08:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
2019-06-14 21:28:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-06-14 21:28:53 +03:00
|
|
|
// need the token for private repositories
|
2019-12-08 23:15:06 +03:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.defaultToken.Value))
|
2019-06-14 21:28:53 +03:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
|
|
return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
aux := struct {
|
2019-06-22 21:08:51 +03:00
|
|
|
ID int `json:"id"`
|
2019-06-14 21:28:53 +03:00
|
|
|
NodeID string `json:"node_id"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
}{}
|
|
|
|
|
2019-06-22 21:08:51 +03:00
|
|
|
data, _ = ioutil.ReadAll(resp.Body)
|
2019-06-14 21:28:53 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &aux)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aux.NodeID, nil
|
|
|
|
}
|
|
|
|
|
2019-06-23 20:20:10 +03:00
|
|
|
/**
|
2019-06-22 18:10:23 +03:00
|
|
|
// create github label using api v4
|
|
|
|
func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelColor string) (string, error) {
|
2019-06-22 23:28:04 +03:00
|
|
|
m := createLabelMutation{}
|
2019-06-22 18:10:23 +03:00
|
|
|
input := createLabelInput{
|
|
|
|
RepositoryID: ge.repositoryID,
|
|
|
|
Name: githubv4.String(label),
|
|
|
|
Color: githubv4.String(labelColor),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
ctx := context.Background()
|
2019-06-22 20:46:42 +03:00
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, &m, input, nil); err != nil {
|
2019-06-22 18:10:23 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.CreateLabel.Label.ID, nil
|
|
|
|
}
|
2019-06-23 20:20:10 +03:00
|
|
|
*/
|
2019-06-22 18:10:23 +03:00
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) getOrCreateGithubLabelID(ctx context.Context, gc *rateLimitHandlerClient, repositoryID string, label bug.Label) (string, error) {
|
2019-08-06 00:27:08 +03:00
|
|
|
// try to get label id from cache
|
2021-04-09 14:07:45 +03:00
|
|
|
labelID, err := ge.getLabelID(string(label))
|
2019-06-14 21:28:53 +03:00
|
|
|
if err == nil {
|
|
|
|
return labelID, nil
|
|
|
|
}
|
|
|
|
|
2019-06-23 18:54:38 +03:00
|
|
|
// RGBA to hex color
|
2019-08-28 22:13:45 +03:00
|
|
|
rgba := label.Color().RGBA()
|
2019-06-22 21:08:51 +03:00
|
|
|
hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
labelID, err = ge.createGithubLabel(ctx, string(label), hexColor)
|
2019-06-14 21:28:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return labelID, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) getLabelsIDs(ctx context.Context, gc *rateLimitHandlerClient, repositoryID string, labels []bug.Label) ([]githubv4.ID, error) {
|
2019-06-14 21:28:53 +03:00
|
|
|
ids := make([]githubv4.ID, 0, len(labels))
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// check labels ids
|
2019-06-22 21:08:51 +03:00
|
|
|
for _, label := range labels {
|
|
|
|
id, ok := ge.cachedLabels[string(label)]
|
2019-06-14 21:28:53 +03:00
|
|
|
if !ok {
|
|
|
|
// try to query label id
|
2019-08-13 20:51:14 +03:00
|
|
|
id, err = ge.getOrCreateGithubLabelID(ctx, gc, repositoryID, label)
|
2019-06-14 21:28:53 +03:00
|
|
|
if err != nil {
|
2019-06-22 18:10:23 +03:00
|
|
|
return nil, errors.Wrap(err, "get or create github label")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache label id
|
2019-06-22 21:08:51 +03:00
|
|
|
ge.cachedLabels[string(label)] = id
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ids = append(ids, githubv4.ID(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a github issue and return it ID
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) createGithubIssue(ctx context.Context, gc *rateLimitHandlerClient, repositoryID, title, body string) (string, string, error) {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &createIssueMutation{}
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.CreateIssueInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
RepositoryID: repositoryID,
|
|
|
|
Title: githubv4.String(title),
|
|
|
|
Body: (*githubv4.String)(&body),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-20 19:08:29 +03:00
|
|
|
return "", "", err
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
issue := m.CreateIssue.Issue
|
|
|
|
return issue.ID, issue.URL, nil
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
// add a comment to an issue and return its ID
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) addCommentGithubIssue(ctx context.Context, gc *rateLimitHandlerClient, subjectID string, body string) (string, string, error) {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &addCommentToIssueMutation{}
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.AddCommentInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
SubjectID: subjectID,
|
|
|
|
Body: githubv4.String(body),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-20 19:08:29 +03:00
|
|
|
return "", "", err
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:08:29 +03:00
|
|
|
node := m.AddComment.CommentEdge.Node
|
|
|
|
return node.ID, node.URL, nil
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) editCommentGithubIssue(ctx context.Context, gc *rateLimitHandlerClient, commentID, body string) (string, string, error) {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &updateIssueCommentMutation{}
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.UpdateIssueCommentInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
ID: commentID,
|
|
|
|
Body: githubv4.String(body),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-20 19:08:29 +03:00
|
|
|
return "", "", err
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:28:04 +03:00
|
|
|
return commentID, m.UpdateIssueComment.IssueComment.URL, nil
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 11:52:11 +03:00
|
|
|
func (ge *githubExporter) updateGithubIssueStatus(ctx context.Context, gc *rateLimitHandlerClient, id string, status common.Status) error {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &updateIssueMutation{}
|
|
|
|
|
|
|
|
// set state
|
2019-07-05 19:32:51 +03:00
|
|
|
var state githubv4.IssueState
|
|
|
|
|
|
|
|
switch status {
|
2022-08-20 11:52:11 +03:00
|
|
|
case common.OpenStatus:
|
2019-07-05 19:32:51 +03:00
|
|
|
state = githubv4.IssueStateOpen
|
2022-08-20 11:52:11 +03:00
|
|
|
case common.ClosedStatus:
|
2019-07-05 21:55:24 +03:00
|
|
|
state = githubv4.IssueStateClosed
|
2019-07-05 19:32:51 +03:00
|
|
|
default:
|
|
|
|
panic("unknown bug state")
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.UpdateIssueInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
ID: id,
|
|
|
|
State: &state,
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-14 21:28:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) updateGithubIssueBody(ctx context.Context, gc *rateLimitHandlerClient, id string, body string) error {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &updateIssueMutation{}
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.UpdateIssueInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
ID: id,
|
|
|
|
Body: (*githubv4.String)(&body),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-14 21:28:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) updateGithubIssueTitle(ctx context.Context, gc *rateLimitHandlerClient, id, title string) error {
|
2019-06-14 21:28:53 +03:00
|
|
|
m := &updateIssueMutation{}
|
2019-06-22 14:40:17 +03:00
|
|
|
input := githubv4.UpdateIssueInput{
|
2019-06-14 21:28:53 +03:00
|
|
|
ID: id,
|
|
|
|
Title: (*githubv4.String)(&title),
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, input, nil, ge.out); err != nil {
|
2019-06-14 21:28:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// update github issue labels
|
2021-09-14 23:22:28 +03:00
|
|
|
func (ge *githubExporter) updateGithubIssueLabels(ctx context.Context, gc *rateLimitHandlerClient, labelableID string, added, removed []bug.Label) error {
|
2019-06-22 20:46:42 +03:00
|
|
|
|
2019-08-18 00:45:31 +03:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2019-07-05 21:55:24 +03:00
|
|
|
if len(added) > 0 {
|
2019-08-18 00:45:31 +03:00
|
|
|
wg.Go(func() error {
|
2019-08-13 20:51:14 +03:00
|
|
|
addedIDs, err := ge.getLabelsIDs(ctx, gc, labelableID, added)
|
2019-07-05 21:55:24 +03:00
|
|
|
if err != nil {
|
2019-08-18 00:45:31 +03:00
|
|
|
return err
|
2019-07-05 21:55:24 +03:00
|
|
|
}
|
2019-06-22 23:28:04 +03:00
|
|
|
|
2019-07-05 21:55:24 +03:00
|
|
|
m := &addLabelsToLabelableMutation{}
|
|
|
|
inputAdd := githubv4.AddLabelsToLabelableInput{
|
|
|
|
LabelableID: labelableID,
|
|
|
|
LabelIDs: addedIDs,
|
|
|
|
}
|
|
|
|
|
|
|
|
// add labels
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m, inputAdd, nil, ge.out); err != nil {
|
2019-08-18 00:45:31 +03:00
|
|
|
return err
|
2019-07-05 21:55:24 +03:00
|
|
|
}
|
2019-08-18 00:45:31 +03:00
|
|
|
return nil
|
|
|
|
})
|
2019-06-23 20:20:10 +03:00
|
|
|
}
|
2019-06-14 21:28:53 +03:00
|
|
|
|
2019-07-05 21:55:24 +03:00
|
|
|
if len(removed) > 0 {
|
2019-08-18 00:45:31 +03:00
|
|
|
wg.Go(func() error {
|
2019-08-13 20:51:14 +03:00
|
|
|
removedIDs, err := ge.getLabelsIDs(ctx, gc, labelableID, removed)
|
2019-07-05 21:55:24 +03:00
|
|
|
if err != nil {
|
2019-08-18 00:45:31 +03:00
|
|
|
return err
|
2019-07-05 21:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m2 := &removeLabelsFromLabelableMutation{}
|
|
|
|
inputRemove := githubv4.RemoveLabelsFromLabelableInput{
|
|
|
|
LabelableID: labelableID,
|
|
|
|
LabelIDs: removedIDs,
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove label labels
|
2021-09-14 23:22:28 +03:00
|
|
|
if err := gc.mutate(ctx, m2, inputRemove, nil, ge.out); err != nil {
|
2019-08-18 00:45:31 +03:00
|
|
|
return err
|
2019-07-05 21:55:24 +03:00
|
|
|
}
|
2019-08-18 00:45:31 +03:00
|
|
|
return nil
|
|
|
|
})
|
2019-06-14 21:28:53 +03:00
|
|
|
}
|
2019-06-09 00:14:59 +03:00
|
|
|
|
2019-08-18 00:45:31 +03:00
|
|
|
return wg.Wait()
|
2019-06-09 00:14:59 +03:00
|
|
|
}
|