diff --git a/commands/mr/note/mr_note_create.go b/commands/mr/note/mr_note_create.go index 3836a329..8664fa3e 100644 --- a/commands/mr/note/mr_note_create.go +++ b/commands/mr/note/mr_note_create.go @@ -44,6 +44,22 @@ func NewCmdNote(f *cmdutils.Factory) *cobra.Command { return fmt.Errorf("aborted... Note has an empty message") } + uniqueNoteEnabled, _ := cmd.Flags().GetBool("unique") + + if uniqueNoteEnabled { + opts := &gitlab.ListMergeRequestNotesOptions{} + notes, err := api.ListMRNotes(apiClient, repo.FullName(), mr.IID, opts) + if err != nil { + return fmt.Errorf("running mr note deduplication: %v", err) + } + for _, noteInfo := range notes { + if noteInfo.Body == body { + fmt.Fprintf(f.IO.StdOut, "%s#note_%d\n", mr.WebURL, noteInfo.ID) + return nil + } + } + } + noteInfo, err := api.CreateMRNote(apiClient, repo.FullName(), mr.IID, &gitlab.CreateMergeRequestNoteOptions{ Body: &body, }) @@ -57,5 +73,6 @@ func NewCmdNote(f *cmdutils.Factory) *cobra.Command { } mrCreateNoteCmd.Flags().StringP("message", "m", "", "Comment/Note message") + mrCreateNoteCmd.Flags().Bool("unique", false, "Don't create a comment/note if it already exists") return mrCreateNoteCmd } diff --git a/commands/mr/note/mr_note_create_test.go b/commands/mr/note/mr_note_create_test.go index 2f79d182..93d8ba0d 100644 --- a/commands/mr/note/mr_note_create_test.go +++ b/commands/mr/note/mr_note_create_test.go @@ -218,3 +218,43 @@ func Test_mrNoteCreate_prompt(t *testing.T) { assert.Equal(t, err.Error(), "aborted... Note has an empty message") }) } + +func Test_mrNoteCreate_no_duplicate(t *testing.T) { + fakeHTTP := httpmock.New() + defer fakeHTTP.Verify(t) + + t.Run("message provided", func(t *testing.T) { + + fakeHTTP.RegisterResponder("GET", "/projects/OWNER/REPO/merge_requests/1", + httpmock.NewStringResponse(200, ` + { + "id": 1, + "iid": 1, + "web_url": "https://gitlab.com/OWNER/REPO/merge_requests/1" + } + `)) + + fakeHTTP.RegisterResponder("GET", "/projects/OWNER/REPO/merge_requests/1/notes", + httpmock.NewStringResponse(200, ` + [ + {"id": 0, "body": "aaa"}, + {"id": 111, "body": "bbb"}, + {"id": 222, "body": "some note message"}, + {"id": 333, "body": "ccc"} + ] + `)) + as, teardown := prompt.InitAskStubber() + defer teardown() + as.StubOne("some note message") + + // glab mr note 1 + output, err := runCommand(fakeHTTP, true, `1 --unique`) + if err != nil { + t.Error(err) + return + } + println(output.String()) + assert.Equal(t, output.Stderr(), "") + assert.Equal(t, output.String(), "https://gitlab.com/OWNER/REPO/merge_requests/1#note_222\n") + }) +} diff --git a/docs/source/mr/note.md b/docs/source/mr/note.md index a5d74b7e..c1505f21 100644 --- a/docs/source/mr/note.md +++ b/docs/source/mr/note.md @@ -21,6 +21,7 @@ glab mr note [ | ] [flags] ```plaintext -m, --message string Comment/Note message + --unique Don't create a comment/note if it already exists ``` ## Options inherited from parent commands