Skip to content

Commit

Permalink
mr note: Add --resolve option
Browse files Browse the repository at this point in the history
Currently lab does not have functionality to resolve threads.  Add a
--resolve option to the 'mr note' command that will result in the thread
being resolved.  Empty comments are allowed for the --resolve option.

Add a --resolve option to the 'mr note' command.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Dec 6, 2020
1 parent 465b8a4 commit ae4b111
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ lab issue edit <id>:<comment_id> # update a comment on MR`,

// Edit a comment on the Issue
if commentNum != 0 {
replyNote(rn, false, issueNum, commentNum, true, false, "", linebreak)
replyNote(rn, false, issueNum, commentNum, true, false, "", linebreak, false)
return
}

Expand Down
19 changes: 15 additions & 4 deletions cmd/issue_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ func NoteRunFn(cmd *cobra.Command, args []string) {
}

if reply != 0 {
resolve, err := cmd.Flags().GetBool("resolve")
if err != nil {
log.Fatal(err)
}

quote, err := cmd.Flags().GetBool("quote")
if err != nil {
log.Fatal(err)
}
replyNote(rn, isMR, int(idNum), reply, quote, false, filename, linebreak)
replyNote(rn, isMR, int(idNum), reply, quote, false, filename, linebreak, resolve)
return
}

Expand Down Expand Up @@ -207,7 +212,7 @@ func noteText(body string) (string, error) {
return b.String(), nil
}

func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bool, filename string, linebreak bool) {
func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bool, filename string, linebreak bool, resolve bool) {

var (
discussions []*gitlab.Discussion
Expand Down Expand Up @@ -260,7 +265,7 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo
}
}

if body == "" {
if body == "" && !resolve {
log.Fatal("aborting note due to empty note msg")
}

Expand All @@ -276,7 +281,12 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo
}
} else {
if isMR {
NoteURL, err = lab.AddMRDiscussionNote(rn, idNum, discussion.ID, body)
if body != "" {
NoteURL, err = lab.AddMRDiscussionNote(rn, idNum, discussion.ID, body)
}
if resolve {
NoteURL, err = lab.ResolveMRDiscussion(rn, idNum, discussion.ID, reply)
}
} else {
NoteURL, err = lab.AddIssueDiscussionNote(rn, idNum, discussion.ID, body)
}
Expand All @@ -294,6 +304,7 @@ func init() {
issueNoteCmd.Flags().StringP("file", "F", "", "use the given file as the message")
issueNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
issueNoteCmd.Flags().Bool("quote", false, "quote note in reply (used with --reply only)")
issueNoteCmd.Flags().Bool("resolve", false, "[unused in issue note command]")

issueCmd.AddCommand(issueNoteCmd)
carapace.Gen(issueNoteCmd).PositionalCompletion(
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,

// Edit a comment on the MR
if commentNum != 0 {
replyNote(rn, true, mrNum, commentNum, true, true, "", linebreak)
replyNote(rn, true, mrNum, commentNum, true, true, "", linebreak, false)
return
}

Expand Down
1 change: 1 addition & 0 deletions cmd/mr_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
mrNoteCmd.Flags().StringP("file", "F", "", "use the given file as the message")
mrNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
mrNoteCmd.Flags().Bool("quote", false, "quote note in reply (used with --reply only)")
mrNoteCmd.Flags().Bool("resolve", false, "mark thread resolved (used with --reply only)")

mrCmd.AddCommand(mrNoteCmd)
carapace.Gen(mrNoteCmd).PositionalCompletion(
Expand Down
17 changes: 17 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,20 @@ func GetMRApprovedBys(project string, mrNum int) ([]string, error) {

return retArray, err
}

func ResolveMRDiscussion(project string, mrNum int, discussionID string, noteID int) (string, error) {
p, err := FindProject(project)
if err != nil {
return "", err
}

opts := &gitlab.ResolveMergeRequestDiscussionOptions{
Resolved: gitlab.Bool(true),
}

discussion, _, err := lab.Discussions.ResolveMergeRequestDiscussion(p.ID, mrNum, discussionID, opts)
if err != nil {
return discussion.ID, err
}
return fmt.Sprintf("Resolved %s/merge_requests/%d#note_%d", p.WebURL, mrNum, noteID), nil
}

0 comments on commit ae4b111

Please sign in to comment.