GitLabgitlab-org
cli
main
1package note
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strconv"
8 "strings"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/text"
21)
23type resolveOptions struct {
24 io *iostreams.IOStreams
25 factory cmdutils.Factory
26 gitlabClient func() (*gitlab.Client, error)
27 resolve bool
28 action string
29 past string
31 // Populated in complete.
32 repoOverride string
33 mrArgs []string
34 discussionPrefix string
36 // Populated in run.
37 client *gitlab.Client
38 mr *gitlab.MergeRequest
39 repo glrepo.Interface
40 discussionID string
41}
43func NewCmdResolve(f cmdutils.Factory) *cobra.Command {
44 return newResolveCmd(f, true)
45}
47func NewCmdReopen(f cmdutils.Factory) *cobra.Command {
48 return newResolveCmd(f, false)
49}
51func newResolveCmd(f cmdutils.Factory, resolve bool) *cobra.Command {
52 action := "resolve"
53 past := "resolved"
54 if !resolve {
55 action = "reopen"
56 past = "reopened"
57 }
59 opts := &resolveOptions{
60 io: f.IO(),
61 factory: f,
62 gitlabClient: f.GitLabClient,
63 resolve: resolve,
64 action: action,
65 past: past,
66 }
68 cmd := &cobra.Command{
69 Use: fmt.Sprintf("%s [<id> | <branch>] <discussion-id>", action),
70 Short: fmt.Sprintf("%s a discussion on a merge request. (EXPERIMENTAL)", capitalize(action)),
71 Long: heredoc.Docf(`
72 %s a discussion on a merge request.
74 The identifier can be one of the following:
76 - Discussion ID: full 40-character hex string or an 8+ character prefix
77 - Note ID: integer note ID (looks up the parent discussion automatically)
79 The merge request comes first, then the discussion. Without a merge request, the merge request for the current branch is used. With --repo, the merge request is required.
81 If a prefix matches multiple discussions, an error is returned with the ambiguous matches.
82 `, capitalize(action)) + text.ExperimentalString,
83 Example: heredoc.Docf(`
84 # %[1]s a discussion on merge request 123 by prefix
85 glab mr note %[2]s 123 abc12345
87 # %[1]s a discussion on merge request 123 in another repository
88 glab mr note %[2]s 123 abc12345 -R OWNER/REPO
90 # %[1]s a discussion by note ID
91 glab mr note %[2]s 3107030349
93 # %[1]s a discussion by prefix (8+ chars, auto-detects merge request from branch)
94 glab mr note %[2]s abc12345
96 # %[1]s a discussion by full ID
97 glab mr note %[2]s abc12345deadbeef1234567890abcdef12345678
98 `, capitalize(action), action),
99 Args: cobra.RangeArgs(1, 2),
100 Annotations: map[string]string{
101 mcpannotations.Destructive: "true",
102 },
103 RunE: func(cmd *cobra.Command, args []string) error {
104 if err := opts.complete(cmd, args); err != nil {
105 return err
106 }
108 if err := opts.validate(); err != nil {
109 return err
110 }
112 return opts.run(cmd.Context())
113 },
114 }
116 return cmd
117}
119func (o *resolveOptions) complete(cmd *cobra.Command, args []string) error {
120 repoOverride, err := cmd.Flags().GetString("repo")
121 if err != nil {
122 return err
123 }
124 o.repoOverride = repoOverride
125 o.discussionPrefix = args[len(args)-1]
126 if len(args) == 2 {
127 o.mrArgs = args[:1]
128 }
130 return nil
131}
133func (o *resolveOptions) validate() error {
134 if o.repoOverride != "" && len(o.mrArgs) == 0 {
135 return &cmdutils.FlagError{Err: fmt.Errorf("a merge request ID or branch is required with --repo: glab mr note %s <id> <discussion-id> -R %s", o.action, o.repoOverride)}
136 }
137 return nil
138}
140func (o *resolveOptions) lookup(ctx context.Context) error {
141 client, err := o.gitlabClient()
142 if err != nil {
143 return err
144 }
145 o.client = client
147 mr, repo, err := mrutils.MRFromArgs(ctx, o.factory, o.mrArgs, "any")
148 if err != nil {
149 return err
150 }
151 o.mr = mr
152 o.repo = repo
154 // Resolve the discussion ID from the prefix or note ID.
155 if noteID, parseErr := strconv.ParseInt(o.discussionPrefix, 10, 64); parseErr == nil {
156 o.discussionID, _, err = mrutils.FindNoteInDiscussionPaginated(ctx, client, repo.FullName(), mr.IID, noteID)
157 if err != nil {
158 if errors.Is(err, mrutils.ErrNoteNotFound) {
159 return fmt.Errorf("note %d not found in merge request !%d", noteID, mr.IID)
160 }
161 return fmt.Errorf("failed to look up note %d in merge request !%d: %w", noteID, mr.IID, err)
162 }
163 } else {
164 o.discussionID, err = mrutils.ResolveDiscussionID(ctx, client, repo.FullName(), mr.IID, o.discussionPrefix)
165 if err != nil {
166 return err
167 }
168 }
170 return nil
171}
173func (o *resolveOptions) run(ctx context.Context) error {
174 if err := o.lookup(ctx); err != nil {
175 return err
176 }
178 _, _, err := o.client.Discussions.ResolveMergeRequestDiscussion(
179 o.repo.FullName(),
180 o.mr.IID,
181 o.discussionID,
182 &gitlab.ResolveMergeRequestDiscussionOptions{
183 Resolved: &o.resolve,
184 },
185 gitlab.WithContext(ctx),
186 )
187 if err != nil {
188 return fmt.Errorf("failed to %s discussion: %w", o.action, err)
189 }
191 o.io.LogInfof("✓ Discussion %s (%s in !%d)\n", o.past, mrutils.TruncateDiscussionID(o.discussionID), o.mr.IID)
192 return nil
193}
195func capitalize(s string) string {
196 if s == "" {
197 return s
198 }
199 return strings.ToUpper(s[:1]) + s[1:]
200}
Diagnostics8 git calls · 202 ms
CallStatusSizeTime0202 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 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 · hit4 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=32926 git.response.cache=HIT git.response.duration_ms=4 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=44Open 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200746 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=746 git.response.cache=HIT git.response.duration_ms=3 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=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200540 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=540 git.response.cache=HIT git.response.duration_ms=2 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=6c82398efd91c9e023d583b948fde889bd4991b2Open pack map
fetch2001.9 KiB190 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=190 git.response.bytes=1973 git.response.cache=MISS git.response.duration_ms=190 git.fetch.wants=437f0fcc5800d2f274af2805449737f24efbb953 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=1973 git.pkt.payload.max=1946 git.sideband.band1.bytes=1946 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
- a5e2c4dfc9e4aedd
- Request
- a3a77cad…
- 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