Skip to content

Commit

Permalink
edit: fix labels update for issue and MR
Browse files Browse the repository at this point in the history
Since the label/unlabel code was written the go-gitlab module was
updated and received got the ability to add and remove labels explicitly
instead of the specifying all labels through a single struct member.

With that, we can use these new members in the UpdateLabel struct to
get the job done and solve the issue where it's not possible to unlabel
an issue or MR with a single label, since the array would be ignore for
being "empty".

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Jun 11, 2021
1 parent c2cbf46 commit 71df609
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
22 changes: 13 additions & 9 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,31 @@ lab issue edit <id>:<comment_id> # update a comment on MR`,
return
}

var labelsChanged bool
// get the labels to add
labelTerms, err := cmd.Flags().GetStringSlice("label")
addLabelTerms, err := cmd.Flags().GetStringSlice("label")
if err != nil {
log.Fatal(err)
}
labels, err := MapLabels(rn, labelTerms)
addLabels, err := MapLabels(rn, addLabelTerms)
if err != nil {
log.Fatal(err)
}
if len(addLabels) > 0 {
labelsChanged = true
}

// get the labels to remove
unlabelTerms, err := cmd.Flags().GetStringSlice("unlabel")
rmLabelTerms, err := cmd.Flags().GetStringSlice("unlabel")
if err != nil {
log.Fatal(err)
}
unlabels, err := MapLabels(rn, unlabelTerms)
rmLabels, err := MapLabels(rn, rmLabelTerms)
if err != nil {
log.Fatal(err)
}

labels, labelsChanged, err := editGetLabels(issue.Labels, labels, unlabels)
if err != nil {
log.Fatal(err)
if len(rmLabels) > 0 {
labelsChanged = true
}

// get the assignees to add
Expand Down Expand Up @@ -152,7 +154,9 @@ lab issue edit <id>:<comment_id> # update a comment on MR`,
}

if labelsChanged {
opts.Labels = labels
// empty arrays are just ignored
opts.AddLabels = addLabels
opts.RemoveLabels = rmLabels
}

if assigneesChanged {
Expand Down
22 changes: 13 additions & 9 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,31 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
return
}

var labelsChanged bool
// get the labels to add
labelTerms, err := cmd.Flags().GetStringSlice("label")
addLabelTerms, err := cmd.Flags().GetStringSlice("label")
if err != nil {
log.Fatal(err)
}
labels, err := MapLabels(rn, labelTerms)
addLabels, err := MapLabels(rn, addLabelTerms)
if err != nil {
log.Fatal(err)
}
if len(addLabels) > 0 {
labelsChanged = true
}

// get the labels to remove
unlabelTerms, err := cmd.Flags().GetStringSlice("unlabel")
rmLabelTerms, err := cmd.Flags().GetStringSlice("unlabel")
if err != nil {
log.Fatal(err)
}
unlabels, err := MapLabels(rn, unlabelTerms)
rmLabels, err := MapLabels(rn, rmLabelTerms)
if err != nil {
log.Fatal(err)
}

labels, labelsChanged, err := editGetLabels(mr.Labels, labels, unlabels)
if err != nil {
log.Fatal(err)
if len(rmLabels) > 0 {
labelsChanged = true
}

// get the assignees to add
Expand Down Expand Up @@ -243,7 +245,9 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
}

if labelsChanged {
opts.Labels = labels
// empty arrays are just ignored
opts.AddLabels = addLabels
opts.RemoveLabels = rmLabels
}

if assigneesChanged {
Expand Down

0 comments on commit 71df609

Please sign in to comment.