Merge branch 'check-duplicates' into 'main'

feat: add no-duplicate flag

Closes #1071

See merge request https://gitlab.com/gitlab-org/cli/-/merge_requests/1049

Merged-by: Gary Holtz <gholtz@gitlab.com>
Approved-by: Oscar Tovar <otovar@gitlab.com>
Approved-by: Gary Holtz <gholtz@gitlab.com>
Co-authored-by: Camilla Conte <cconte@redhat.com>
This commit is contained in:
Gary Holtz 2022-11-15 17:34:56 +00:00
commit 5ddf81ee69
3 changed files with 58 additions and 0 deletions

View File

@ -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
}

View File

@ -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")
})
}

View File

@ -21,6 +21,7 @@ glab mr note [<id> | <branch>] [flags]
```plaintext
-m, --message string Comment/Note message
--unique Don't create a comment/note if it already exists
```
## Options inherited from parent commands