GitLabgitlab-org

cli

main
ply8 git calls225 ms100 KiB6 cachedprotocol v2
143 lines4.8 KiBRaw
1package note
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strings"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/spf13/cobra"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
16 draftCmd "gitlab.com/gitlab-org/cli/internal/commands/mr/note/draft"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21func NewCmdNote(f cmdutils.Factory) *cobra.Command {
22 mrCreateNoteCmd := &cobra.Command{
23 Use: "note [<id> | <branch>]",
24 Aliases: []string{"comment"},
25 Short: "Manage comments and discussions on a merge request.",
26 Long: heredoc.Docf(`
27 Creates a comment by default. Use %[1]sglab mr note resolve%[1]s to resolve a discussion or %[1]sglab mr note reopen%[1]s to reopen one.
28 `, "`"),
29 Args: cobra.MaximumNArgs(1),
30 Annotations: map[string]string{
31 mcpannotations.Destructive: "true",
32 },
33 RunE: func(cmd *cobra.Command, args []string) error {
34 client, err := f.GitLabClient()
35 if err != nil {
36 return err
37 }
39 mr, repo, err := mrutils.MRFromArgs(cmd.Context(), f, args, "any")
40 if err != nil {
41 return err
42 }
44 // Check if we're resolving or unresolving
45 resolveNoteID, _ := cmd.Flags().GetInt64("resolve")
46 unresolveNoteID, _ := cmd.Flags().GetInt64("unresolve")
48 if resolveNoteID != 0 {
49 return resolveDiscussion(cmd.Context(), client, f, mr, repo, resolveNoteID, true)
50 }
52 if unresolveNoteID != 0 {
53 return resolveDiscussion(cmd.Context(), client, f, mr, repo, unresolveNoteID, false)
54 }
56 // Create note (existing behavior)
57 body, _ := cmd.Flags().GetString("message")
58 if strings.TrimSpace(body) == "" {
59 body, err = mrutils.NoteBodyFromStdinOrEditor(cmd.Context(), f.IO(), f.Config)
60 if err != nil {
61 return err
62 }
63 }
64 if strings.TrimSpace(body) == "" {
65 return fmt.Errorf("aborted: note has an empty message")
66 }
68 uniqueNoteEnabled, _ := cmd.Flags().GetBool("unique")
69 if uniqueNoteEnabled {
70 found, err := deduplicateNote(f.IO(), client, repo.FullName(), mr.IID, body, mr.WebURL)
71 if err != nil {
72 return err
73 }
74 if found {
75 return nil
76 }
77 }
79 noteInfo, _, err := client.Notes.CreateMergeRequestNote(repo.FullName(), mr.IID, &gitlab.CreateMergeRequestNoteOptions{Body: &body})
80 if err != nil {
81 return err
82 }
84 f.IO().LogInfof("%s#note_%d\n", mr.WebURL, noteInfo.ID)
85 return nil
86 },
87 }
89 mrCreateNoteCmd.Flags().StringP("message", "m", "", "Comment or note message.")
90 mrCreateNoteCmd.Flags().Bool("unique", false, "Do not create a comment if a comment with the same body already exists.")
91 mrCreateNoteCmd.Flags().Int64("resolve", 0, "Resolve the discussion containing the specified note ID.")
92 mrCreateNoteCmd.Flags().Int64("unresolve", 0, "Unresolve the discussion containing the specified note ID.")
94 mrCreateNoteCmd.MarkFlagsMutuallyExclusive("message", "resolve")
95 mrCreateNoteCmd.MarkFlagsMutuallyExclusive("message", "unresolve")
96 mrCreateNoteCmd.MarkFlagsMutuallyExclusive("resolve", "unresolve")
98 cobra.CheckErr(mrCreateNoteCmd.Flags().MarkDeprecated("resolve", "use `glab mr note resolve` instead."))
99 cobra.CheckErr(mrCreateNoteCmd.Flags().MarkDeprecated("unresolve", "use `glab mr note reopen` instead."))
100 cobra.CheckErr(mrCreateNoteCmd.Flags().MarkDeprecated("message", "use `glab mr note create` instead."))
101 cobra.CheckErr(mrCreateNoteCmd.Flags().MarkDeprecated("unique", "use `glab mr note create` instead."))
103 mrCreateNoteCmd.AddCommand(NewCmdCreate(f))
104 mrCreateNoteCmd.AddCommand(draftCmd.NewCmd(f))
105 mrCreateNoteCmd.AddCommand(NewCmdList(f))
106 mrCreateNoteCmd.AddCommand(NewCmdResolve(f))
107 mrCreateNoteCmd.AddCommand(NewCmdReopen(f))
108 mrCreateNoteCmd.AddCommand(NewCmdUpdate(f))
109 mrCreateNoteCmd.AddCommand(NewCmdDelete(f))
111 return mrCreateNoteCmd
114func resolveDiscussion(ctx context.Context, client *gitlab.Client, f cmdutils.Factory, mr *gitlab.MergeRequest, repo glrepo.Interface, noteID int64, resolve bool) error {
115 targetDiscussionID, _, err := mrutils.FindNoteInDiscussionPaginated(ctx, client, repo.FullName(), mr.IID, noteID)
116 if err != nil {
117 if errors.Is(err, mrutils.ErrNoteNotFound) {
118 return fmt.Errorf("note %d not found in merge request !%d", noteID, mr.IID)
119 }
120 return fmt.Errorf("failed to look up note %d in merge request !%d: %w", noteID, mr.IID, err)
121 }
123 action := "resolve"
124 if !resolve {
125 action = "unresolve"
126 }
128 _, _, err = client.Discussions.ResolveMergeRequestDiscussion(
129 repo.FullName(),
130 mr.IID,
131 targetDiscussionID,
132 &gitlab.ResolveMergeRequestDiscussionOptions{
133 Resolved: &resolve,
134 },
135 gitlab.WithContext(ctx),
136 )
137 if err != nil {
138 return fmt.Errorf("failed to %s discussion: %w", action, err)
139 }
141 f.IO().LogInfof("✓ Discussion %sd (note #%d in !%d)\n", action, noteID, mr.IID)
142 return nil
Diagnostics8 git calls · 225 ms
CallStatusSizeTime0225 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200746 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=746
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=746
git.pkt.payload.max=719
git.sideband.band1.bytes=719
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=719
git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790
Open pack map
fetchtree:0200540 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=540
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=d1be7f980a856805fa31e30e01fff4db335cbe1b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=540
git.pkt.payload.max=513
git.sideband.band1.bytes=513
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=513
git.pack.trailer=6c82398efd91c9e023d583b948fde889bd4991b2
Open pack map
fetch2001.6 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=1656
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=44081b901293564fcfa31667c6e3bfe8e8a6e76b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1656
git.pkt.payload.max=1629
git.sideband.band1.bytes=1629
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2c735e89f4afc
Request
454695c4…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format