diff options
author | Amine Hilaly <hilalyamine@gmail.com> | 2019-07-05 20:55:24 +0200 |
---|---|---|
committer | Amine Hilaly <hilalyamine@gmail.com> | 2019-07-05 22:03:00 +0200 |
commit | 3e181168eadb1d6a13e67583c16219200d052fcc (patch) | |
tree | 74fdc5e4e6c511e457919f53d9d51c75e505de3c /bridge | |
parent | 93e731fd0d365c0cf49dbb7aea371e48f46e1f11 (diff) | |
download | git-bug-3e181168eadb1d6a13e67583c16219200d052fcc.tar.gz |
[bridge/github] improve export label change function
[bridge/core] rename `EventStatus` to `ExportEvent`
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/core/export.go | 48 | ||||
-rw-r--r-- | bridge/github/export.go | 91 |
2 files changed, 81 insertions, 58 deletions
diff --git a/bridge/core/export.go b/bridge/core/export.go index 51149430..080ced80 100644 --- a/bridge/core/export.go +++ b/bridge/core/export.go @@ -2,41 +2,41 @@ package core import "fmt" -type EventStatus int +type ExportEvent int const ( - _ EventStatus = iota - EventStatusBug - EventStatusComment - EventStatusCommentEdition - EventStatusStatusChange - EventStatusTitleEdition - EventStatusLabelChange - EventStatusNothing + _ ExportEvent = iota + ExportEventBug + ExportEventComment + ExportEventCommentEdition + ExportEventStatusChange + ExportEventTitleEdition + ExportEventLabelChange + ExportEventNothing ) type ExportResult struct { Err error - Event EventStatus + Event ExportEvent ID string Reason string } func (er ExportResult) String() string { switch er.Event { - case EventStatusBug: + case ExportEventBug: return "new issue" - case EventStatusComment: + case ExportEventComment: return "new comment" - case EventStatusCommentEdition: + case ExportEventCommentEdition: return "updated comment" - case EventStatusStatusChange: + case ExportEventStatusChange: return "changed status" - case EventStatusTitleEdition: + case ExportEventTitleEdition: return "changed title" - case EventStatusLabelChange: + case ExportEventLabelChange: return "changed label" - case EventStatusNothing: + case ExportEventNothing: return fmt.Sprintf("no event: %v", er.Reason) default: panic("unknown export result") @@ -54,48 +54,48 @@ func NewExportNothing(id string, reason string) ExportResult { return ExportResult{ ID: id, Reason: reason, - Event: EventStatusNothing, + Event: ExportEventNothing, } } func NewExportBug(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusBug, + Event: ExportEventBug, } } func NewExportComment(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusComment, + Event: ExportEventComment, } } func NewExportCommentEdition(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusCommentEdition, + Event: ExportEventCommentEdition, } } func NewExportStatusChange(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusStatusChange, + Event: ExportEventStatusChange, } } func NewExportLabelChange(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusLabelChange, + Event: ExportEventLabelChange, } } func NewExportTitleEdition(id string) ExportResult { return ExportResult{ ID: id, - Event: EventStatusTitleEdition, + Event: ExportEventTitleEdition, } } diff --git a/bridge/github/export.go b/bridge/github/export.go index 5e5b1a21..c4ca42da 100644 --- a/bridge/github/export.go +++ b/bridge/github/export.go @@ -7,6 +7,8 @@ import ( "fmt" "io/ioutil" "net/http" + "strings" + "sync" "time" "github.com/pkg/errors" @@ -671,7 +673,7 @@ func updateGithubIssueStatus(gc *githubv4.Client, id string, status bug.Status) case bug.OpenStatus: state = githubv4.IssueStateOpen case bug.ClosedStatus: - state = githubv4.IssueStateOpen + state = githubv4.IssueStateClosed default: panic("unknown bug state") } @@ -730,49 +732,70 @@ func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error { // update github issue labels func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelableID string, added, removed []bug.Label) error { - addedIDs, err := ge.getLabelsIDs(gc, labelableID, added) - if err != nil { - return errors.Wrap(err, "getting added labels ids") - } - - m := &addLabelsToLabelableMutation{} - inputAdd := githubv4.AddLabelsToLabelableInput{ - LabelableID: labelableID, - LabelIDs: addedIDs, - } + var errs []string + var wg sync.WaitGroup parentCtx := context.Background() - ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout) - // add labels - if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil { - cancel() - return err - } - cancel() + if len(added) > 0 { + wg.Add(1) + go func() { + defer wg.Done() - if len(removed) == 0 { - return nil - } + addedIDs, err := ge.getLabelsIDs(gc, labelableID, added) + if err != nil { + errs = append(errs, errors.Wrap(err, "getting added labels ids").Error()) + return + } - removedIDs, err := ge.getLabelsIDs(gc, labelableID, removed) - if err != nil { - return errors.Wrap(err, "getting added labels ids") + m := &addLabelsToLabelableMutation{} + inputAdd := githubv4.AddLabelsToLabelableInput{ + LabelableID: labelableID, + LabelIDs: addedIDs, + } + + ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout) + defer cancel() + + // add labels + if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil { + errs = append(errs, err.Error()) + } + }() } - m2 := &removeLabelsFromLabelableMutation{} - inputRemove := githubv4.RemoveLabelsFromLabelableInput{ - LabelableID: labelableID, - LabelIDs: removedIDs, + if len(removed) > 0 { + wg.Add(1) + go func() { + defer wg.Done() + + removedIDs, err := ge.getLabelsIDs(gc, labelableID, removed) + if err != nil { + errs = append(errs, errors.Wrap(err, "getting added labels ids").Error()) + return + } + + m2 := &removeLabelsFromLabelableMutation{} + inputRemove := githubv4.RemoveLabelsFromLabelableInput{ + LabelableID: labelableID, + LabelIDs: removedIDs, + } + + ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout) + defer cancel() + + // remove label labels + if err := gc.Mutate(ctx, m2, inputRemove, nil); err != nil { + errs = append(errs, err.Error()) + } + }() } - ctx, cancel = context.WithTimeout(parentCtx, defaultTimeout) - defer cancel() + wg.Wait() - // remove label labels - if err := gc.Mutate(ctx, m2, inputRemove, nil); err != nil { - return err + if len(errs) == 0 { + return nil } - return nil + return fmt.Errorf("label change error: %v", strings.Join(errs, "\n")) } |