2019-11-07 19:01:08 +03:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-15 18:01:15 +03:00
|
|
|
"os"
|
2019-11-07 19:01:08 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2020-02-15 18:01:15 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core/auth"
|
2019-11-07 19:01:08 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2019-11-07 19:01:08 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2022-07-25 14:16:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity/dag"
|
2019-11-07 19:01:08 +03:00
|
|
|
)
|
|
|
|
|
2019-11-23 09:34:19 +03:00
|
|
|
var (
|
|
|
|
ErrMissingCredentials = errors.New("missing credentials")
|
|
|
|
)
|
|
|
|
|
2019-11-07 19:01:08 +03:00
|
|
|
// jiraExporter implement the Exporter interface
|
|
|
|
type jiraExporter struct {
|
|
|
|
conf core.Configuration
|
|
|
|
|
|
|
|
// cache identities clients
|
|
|
|
identityClient map[entity.Id]*Client
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
// the mapping from git-bug "status" to JIRA "status" id
|
|
|
|
statusMap map[string]string
|
|
|
|
|
2019-11-07 19:01:08 +03:00
|
|
|
// cache identifiers used to speed up exporting operations
|
|
|
|
// cleared for each bug
|
|
|
|
cachedOperationIDs map[entity.Id]string
|
|
|
|
|
|
|
|
// cache labels used to speed up exporting labels events
|
|
|
|
cachedLabels map[string]string
|
|
|
|
|
|
|
|
// store JIRA project information
|
|
|
|
project *Project
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init .
|
2020-02-15 18:01:15 +03:00
|
|
|
func (je *jiraExporter) Init(ctx context.Context, repo *cache.RepoCache, conf core.Configuration) error {
|
2020-02-09 23:08:47 +03:00
|
|
|
je.conf = conf
|
|
|
|
je.identityClient = make(map[entity.Id]*Client)
|
|
|
|
je.cachedOperationIDs = make(map[entity.Id]string)
|
|
|
|
je.cachedLabels = make(map[string]string)
|
2019-11-07 19:01:08 +03:00
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
statusMap, err := getStatusMap(je.conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
je.statusMap = statusMap
|
|
|
|
|
|
|
|
// preload all clients
|
|
|
|
err = je.cacheAllClient(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
if len(je.identityClient) == 0 {
|
|
|
|
return fmt.Errorf("no credentials for this bridge")
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *Client
|
|
|
|
for _, c := range je.identityClient {
|
|
|
|
client = c
|
|
|
|
break
|
|
|
|
}
|
2019-11-23 09:34:19 +03:00
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
if client == nil {
|
|
|
|
panic("nil client")
|
2019-11-23 09:34:19 +03:00
|
|
|
}
|
2020-02-15 18:01:15 +03:00
|
|
|
|
|
|
|
je.project, err = client.GetProject(je.conf[confKeyProject])
|
2019-11-23 09:34:19 +03:00
|
|
|
if err != nil {
|
2020-02-15 18:01:15 +03:00
|
|
|
return err
|
2019-11-23 09:34:19 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
return nil
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
func (je *jiraExporter) cacheAllClient(ctx context.Context, repo *cache.RepoCache) error {
|
|
|
|
creds, err := auth.List(repo,
|
|
|
|
auth.WithTarget(target),
|
|
|
|
auth.WithKind(auth.KindLoginPassword), auth.WithKind(auth.KindLogin),
|
|
|
|
auth.WithMeta(auth.MetaKeyBaseURL, je.conf[confKeyBaseUrl]),
|
|
|
|
)
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
2020-02-15 18:01:15 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
for _, cred := range creds {
|
|
|
|
login, ok := cred.GetMetadata(auth.MetaKeyLogin)
|
|
|
|
if !ok {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "credential %s is not tagged with a Jira login\n", cred.ID().Human())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := repo.ResolveIdentityImmutableMetadata(metaKeyJiraLogin, login)
|
2022-11-19 13:33:12 +03:00
|
|
|
if entity.IsErrNotFound(err) {
|
2020-02-15 18:01:15 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := je.identityClient[user.Id()]; !ok {
|
2022-03-10 20:41:54 +03:00
|
|
|
client, err := buildClient(ctx, je.conf[confKeyBaseUrl], je.conf[confKeyCredentialType], cred)
|
2020-02-15 18:01:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
je.identityClient[user.Id()] = client
|
|
|
|
}
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getClientForIdentity return an API client configured with the credentials
|
|
|
|
// of the given identity. If no client were found it will initialize it from
|
|
|
|
// the known credentials and cache it for next use.
|
|
|
|
func (je *jiraExporter) getClientForIdentity(userId entity.Id) (*Client, error) {
|
|
|
|
client, ok := je.identityClient[userId]
|
|
|
|
if ok {
|
|
|
|
return client, nil
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 18:01:15 +03:00
|
|
|
return nil, ErrMissingCredentials
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportAll export all event made by the current user to Jira
|
|
|
|
func (je *jiraExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
|
|
|
|
out := make(chan core.ExportResult)
|
|
|
|
|
2019-11-07 19:01:08 +03:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
var allIdentitiesIds []entity.Id
|
2020-02-09 23:08:47 +03:00
|
|
|
for id := range je.identityClient {
|
2019-11-07 19:01:08 +03:00
|
|
|
allIdentitiesIds = append(allIdentitiesIds, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
allBugsIds := repo.AllBugsIds()
|
|
|
|
|
|
|
|
for _, id := range allBugsIds {
|
|
|
|
b, err := repo.ResolveBug(id)
|
|
|
|
if err != nil {
|
|
|
|
out <- core.NewExportError(errors.Wrap(err, "can't load bug"), id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
// stop iterating if context cancel function is called
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
snapshot := b.Snapshot()
|
|
|
|
|
|
|
|
// ignore issues whose last modification date is before the query 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-11-07 19:01:08 +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 18:01:15 +03:00
|
|
|
err := je.exportBug(ctx, b, out)
|
2019-11-23 09:34:19 +03:00
|
|
|
if err != nil {
|
|
|
|
out <- core.NewExportError(errors.Wrap(err, "can't export bug"), id)
|
|
|
|
return
|
|
|
|
}
|
2019-11-07 19:01:08 +03:00
|
|
|
} else {
|
|
|
|
out <- core.NewExportNothing(id, "not an actor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportBug publish bugs and related events
|
2020-02-15 18:01:15 +03:00
|
|
|
func (je *jiraExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) error {
|
2019-11-07 19:01:08 +03:00
|
|
|
snapshot := b.Snapshot()
|
|
|
|
|
|
|
|
var bugJiraID string
|
|
|
|
|
|
|
|
// Special case:
|
|
|
|
// if a user try to export a bug that is not already exported to jira (or
|
|
|
|
// imported from jira) and we do not have the token of the bug author,
|
|
|
|
// there is nothing we can do.
|
|
|
|
|
|
|
|
// first operation is always createOp
|
|
|
|
createOp := snapshot.Operations[0].(*bug.CreateOperation)
|
|
|
|
author := snapshot.Author
|
|
|
|
|
|
|
|
// skip bug if it was imported from some other bug system
|
2019-12-18 18:49:49 +03:00
|
|
|
origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
|
2019-11-07 19:01:08 +03:00
|
|
|
if ok && origin != target {
|
|
|
|
out <- core.NewExportNothing(
|
|
|
|
b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
|
2019-11-23 09:34:19 +03:00
|
|
|
return nil
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip bug if it is a jira bug but is associated with another project
|
|
|
|
// (one bridge per JIRA project)
|
2020-02-15 18:01:15 +03:00
|
|
|
project, ok := snapshot.GetCreateMetadata(metaKeyJiraProject)
|
2020-02-09 23:08:47 +03:00
|
|
|
if ok && !stringInSlice(project, []string{je.project.ID, je.project.Key}) {
|
2019-11-07 19:01:08 +03:00
|
|
|
out <- core.NewExportNothing(
|
|
|
|
b.Id(), fmt.Sprintf("issue tagged with project: %s", project))
|
2019-11-23 09:34:19 +03:00
|
|
|
return nil
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get jira bug ID
|
2020-02-15 18:01:15 +03:00
|
|
|
jiraID, ok := snapshot.GetCreateMetadata(metaKeyJiraId)
|
2019-11-07 19:01:08 +03:00
|
|
|
if ok {
|
|
|
|
// will be used to mark operation related to a bug as exported
|
|
|
|
bugJiraID = jiraID
|
|
|
|
} else {
|
|
|
|
// check that we have credentials for operation author
|
2020-02-15 18:01:15 +03:00
|
|
|
client, err := je.getClientForIdentity(author.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
|
|
|
// if bug is not yet exported and we do not have the author's credentials
|
|
|
|
// then there is nothing we can do, so just skip this bug
|
|
|
|
out <- core.NewExportNothing(
|
2020-02-15 18:01:15 +03:00
|
|
|
b.Id(), fmt.Sprintf("missing author credentials for user %.8s",
|
2019-11-07 19:01:08 +03:00
|
|
|
author.Id().String()))
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load any custom fields required to create an issue from the git
|
|
|
|
// config file.
|
|
|
|
fields := make(map[string]interface{})
|
2020-02-15 18:01:15 +03:00
|
|
|
defaultFields, hasConf := je.conf[confKeyCreateDefaults]
|
2019-11-07 19:01:08 +03:00
|
|
|
if hasConf {
|
2019-11-15 02:56:32 +03:00
|
|
|
err = json.Unmarshal([]byte(defaultFields), &fields)
|
|
|
|
if err != nil {
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-15 02:56:32 +03:00
|
|
|
}
|
2019-11-07 19:01:08 +03:00
|
|
|
} else {
|
|
|
|
// If there is no configuration provided, at the very least the
|
|
|
|
// "issueType" field is always required. 10001 is "story" which I'm
|
|
|
|
// pretty sure is standard/default on all JIRA instances.
|
2019-11-23 09:34:19 +03:00
|
|
|
fields["issuetype"] = map[string]interface{}{
|
|
|
|
"id": "10001",
|
|
|
|
}
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2020-02-15 18:01:15 +03:00
|
|
|
bugIDField, hasConf := je.conf[confKeyCreateGitBug]
|
2019-11-07 19:01:08 +03:00
|
|
|
if hasConf {
|
|
|
|
// If the git configuration also indicates it, we can assign the git-bug
|
|
|
|
// id to a custom field to assist in integrations
|
|
|
|
fields[bugIDField] = b.Id().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// create bug
|
|
|
|
result, err := client.CreateIssue(
|
2020-02-09 23:08:47 +03:00
|
|
|
je.project.ID, createOp.Title, createOp.Message, fields)
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "exporting jira issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id := result.ID
|
|
|
|
out <- core.NewExportBug(b.Id())
|
|
|
|
// mark bug creation operation as exported
|
|
|
|
err = markOperationAsExported(
|
2020-02-09 23:08:47 +03:00
|
|
|
b, createOp.Id(), id, je.project.Key, time.Time{})
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// commit operation to avoid creating multiple issues with multiple pushes
|
|
|
|
err = b.CommitAsNeeded()
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache bug jira ID
|
|
|
|
bugJiraID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache operation jira id
|
2020-02-09 23:08:47 +03:00
|
|
|
je.cachedOperationIDs[createOp.Id()] = bugJiraID
|
2019-11-07 19:01:08 +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-11-07 19:01:08 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore operations already existing in jira (due to import or export)
|
|
|
|
// cache the ID of already exported or imported issues and events from
|
|
|
|
// Jira
|
2020-02-15 18:01:15 +03:00
|
|
|
if id, ok := op.GetMetadata(metaKeyJiraId); ok {
|
2020-02-09 23:08:47 +03:00
|
|
|
je.cachedOperationIDs[op.Id()] = id
|
2019-11-07 19:01:08 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-14 13:36:32 +03:00
|
|
|
opAuthor := op.Author()
|
2020-02-15 18:01:15 +03:00
|
|
|
client, err := je.getClientForIdentity(opAuthor.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
2019-11-23 09:34:19 +03:00
|
|
|
out <- core.NewExportError(
|
|
|
|
fmt.Errorf("missing operation author credentials for user %.8s",
|
2022-11-13 14:31:38 +03:00
|
|
|
author.Id().String()), b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
|
|
|
var exportTime time.Time
|
2019-11-15 02:56:32 +03:00
|
|
|
switch opr := op.(type) {
|
2019-11-07 19:01:08 +03:00
|
|
|
case *bug.AddCommentOperation:
|
|
|
|
comment, err := client.AddComment(bugJiraID, opr.Message)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "adding comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
id = comment.ID
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportComment(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
|
|
|
|
// cache comment id
|
2020-02-09 23:08:47 +03:00
|
|
|
je.cachedOperationIDs[op.Id()] = id
|
2019-11-07 19:01:08 +03:00
|
|
|
|
|
|
|
case *bug.EditCommentOperation:
|
|
|
|
if opr.Target == createOp.Id() {
|
|
|
|
// An EditCommentOpreation with the Target set to the create operation
|
|
|
|
// encodes a modification to the long-description/summary.
|
|
|
|
exportTime, err = client.UpdateIssueBody(bugJiraID, opr.Message)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "editing issue")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportCommentEdition(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
id = bugJiraID
|
|
|
|
} else {
|
|
|
|
// Otherwise it's an edit to an actual comment. A comment cannot be
|
|
|
|
// edited before it was created, so it must be the case that we have
|
|
|
|
// already observed and cached the AddCommentOperation.
|
2020-02-09 23:08:47 +03:00
|
|
|
commentID, ok := je.cachedOperationIDs[opr.Target]
|
2019-11-07 19:01:08 +03:00
|
|
|
if !ok {
|
|
|
|
// Since an edit has to come after the creation, we expect we would
|
|
|
|
// have cached the creation id.
|
|
|
|
panic("unexpected error: comment id not found")
|
|
|
|
}
|
|
|
|
comment, err := client.UpdateComment(bugJiraID, commentID, opr.Message)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "editing comment")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportCommentEdition(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
// JIRA doesn't track all comment edits, they will only tell us about
|
|
|
|
// the most recent one. We must invent a consistent id for the operation
|
|
|
|
// so we use the comment ID plus the timestamp of the update, as
|
|
|
|
// reported by JIRA. Note that this must be consistent with the importer
|
|
|
|
// during ensureComment()
|
2019-12-05 09:50:35 +03:00
|
|
|
id = getTimeDerivedID(comment.ID, comment.Updated)
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case *bug.SetStatusOperation:
|
2020-02-15 18:01:15 +03:00
|
|
|
jiraStatus, hasStatus := je.statusMap[opr.Status.String()]
|
2019-11-07 19:01:08 +03:00
|
|
|
if hasStatus {
|
|
|
|
exportTime, err = UpdateIssueStatus(client, bugJiraID, jiraStatus)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "editing status")
|
2019-12-14 00:17:26 +03:00
|
|
|
out <- core.NewExportWarning(err, b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
// Failure to update status isn't necessarily a big error. It's
|
|
|
|
// possible that we just don't have enough information to make that
|
|
|
|
// update. In this case, just don't export the operation.
|
|
|
|
continue
|
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportStatusChange(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
id = bugJiraID
|
|
|
|
} else {
|
2019-11-23 09:34:19 +03:00
|
|
|
out <- core.NewExportError(fmt.Errorf(
|
|
|
|
"No jira status mapped for %.8s", opr.Status.String()), b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case *bug.SetTitleOperation:
|
|
|
|
exportTime, err = client.UpdateIssueTitle(bugJiraID, opr.Title)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "editing title")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportTitleEdition(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
id = bugJiraID
|
|
|
|
|
|
|
|
case *bug.LabelChangeOperation:
|
|
|
|
exportTime, err = client.UpdateLabels(
|
|
|
|
bugJiraID, opr.Added, opr.Removed)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "updating labels")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
out <- core.NewExportLabelChange(b.Id())
|
2019-11-07 19:01:08 +03:00
|
|
|
id = bugJiraID
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unhandled operation type case")
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark operation as exported
|
|
|
|
err = markOperationAsExported(
|
2020-02-09 23:08:47 +03:00
|
|
|
b, op.Id(), id, je.project.Key, exportTime)
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "marking operation as exported")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// commit at each operation export to avoid exporting same events multiple
|
|
|
|
// times
|
|
|
|
err = b.CommitAsNeeded()
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "bug commit")
|
|
|
|
out <- core.NewExportError(err, b.Id())
|
2019-11-23 09:34:19 +03:00
|
|
|
return err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 09:34:19 +03:00
|
|
|
|
|
|
|
return nil
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-09 23:08:47 +03:00
|
|
|
func markOperationAsExported(b *cache.BugCache, target entity.Id, jiraID, jiraProject string, exportTime time.Time) error {
|
2019-11-07 19:01:08 +03:00
|
|
|
newMetadata := map[string]string{
|
2020-02-15 18:01:15 +03:00
|
|
|
metaKeyJiraId: jiraID,
|
|
|
|
metaKeyJiraProject: jiraProject,
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
if !exportTime.IsZero() {
|
2020-02-15 18:01:15 +03:00
|
|
|
newMetadata[metaKeyJiraExportTime] = exportTime.Format(http.TimeFormat)
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.SetMetadata(target, newMetadata)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueStatus attempts to change the "status" field by finding a
|
|
|
|
// transition which achieves the desired state and then performing that
|
|
|
|
// transition
|
2020-02-09 23:08:47 +03:00
|
|
|
func UpdateIssueStatus(client *Client, issueKeyOrID string, desiredStateNameOrID string) (time.Time, error) {
|
2019-11-07 19:01:08 +03:00
|
|
|
var responseTime time.Time
|
|
|
|
|
|
|
|
tlist, err := client.GetTransitions(issueKeyOrID)
|
|
|
|
if err != nil {
|
|
|
|
return responseTime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transition := getTransitionTo(tlist, desiredStateNameOrID)
|
|
|
|
if transition == nil {
|
|
|
|
return responseTime, errTransitionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
responseTime, err = client.DoTransition(issueKeyOrID, transition.ID)
|
|
|
|
if err != nil {
|
|
|
|
return responseTime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseTime, nil
|
|
|
|
}
|