GitLabgitlab-org

cli

main
ply10 git calls339 ms101 KiB7 cachedprotocol v2
188 lines6.3 KiBRaw
1package publish
3import (
4 "context"
5 "errors"
6 "fmt"
7 "slices"
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/dbg"
18 "gitlab.com/gitlab-org/cli/internal/glrepo"
19 "gitlab.com/gitlab-org/cli/internal/iostreams"
20 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21 "gitlab.com/gitlab-org/cli/internal/text"
22 "gitlab.com/gitlab-org/cli/internal/utils"
25type options struct {
26 io *iostreams.IOStreams
27 factory cmdutils.Factory
28 gitlabClient func() (*gitlab.Client, error)
30 // Flags.
31 message string
32 internal bool
33 reviewerState string
34 yes bool
36 // Populated in complete.
37 client *gitlab.Client
38 mr *gitlab.MergeRequest
39 repo glrepo.Interface
42func NewCmd(f cmdutils.Factory) *cobra.Command {
43 opts := &options{
44 io: f.IO(),
45 factory: f,
46 gitlabClient: f.GitLabClient,
47 }
49 cmd := &cobra.Command{
50 Use: "publish [<id> | <branch>]",
51 Short: "Publish all your pending review comments on a merge request. (EXPERIMENTAL)",
52 Long: heredoc.Docf(`
53 Publish every pending review comment you created on a merge request, as when you submit a review from the merge request page. Only your own pending comments are published; other reviewers' pending comments are unaffected. Check what is pending with %[1]sglab mr note draft list%[1]s first.
55 Use %[1]s--message%[1]s to add a summary note to the merge request when publishing, and %[1]s--internal%[1]s to restrict that summary to project members with at least the Reporter role.
57 Use %[1]s--reviewer-state%[1]s to set your review state on the merge request. Neither state records a formal approval; use %[1]sglab mr approve%[1]s to approve. If you are the merge request author, GitLab ignores this flag unless you are also listed as one of the reviewers.
59 Unless you pass %[1]s--yes%[1]s, the command shows the number of pending comments and prompts you to confirm. When not running interactively, %[1]s--yes%[1]s is required. If there are no pending comments, the command exits with an error.
60 `, "`") + text.ExperimentalString,
61 Example: heredoc.Doc(`
62 # Publish your pending review comments on merge request 123
63 glab mr note draft publish 123
65 # Publish the current branch's pending review comments
66 glab mr note draft publish
68 # Publish with a summary note and request changes
69 glab mr note draft publish 123 -m "A few blockers, see the comments." --reviewer-state requested_changes
71 # Publish without confirmation
72 glab mr note draft publish 123 --yes
73 `),
74 Args: cobra.MaximumNArgs(1),
75 Annotations: map[string]string{
76 mcpannotations.Destructive: "true",
77 },
78 RunE: func(cmd *cobra.Command, args []string) error {
79 if err := opts.validate(); err != nil {
80 return err
81 }
82 if err := opts.complete(cmd, args); err != nil {
83 return err
84 }
85 return opts.run(cmd.Context())
86 },
87 }
89 fl := cmd.Flags()
90 fl.StringVarP(&opts.message, "message", "m", "", "Summary note to add to the merge request when publishing.")
91 fl.BoolVar(&opts.internal, "internal", false, "Mark the summary note as internal. Requires --message.")
92 fl.Var(
93 cmdutils.NewEnumValue([]string{"requested_changes", "reviewed"}, "", &opts.reviewerState),
94 "reviewer-state", "Set the review state after publishing: requested_changes, reviewed. Does not record a formal approval.",
95 )
96 fl.BoolVarP(&opts.yes, "yes", "y", false, "Skip confirmation prompt.")
98 return cmd
101// validate checks only flag values, so it runs before complete to reject bad
102// input without any API calls.
103func (o *options) validate() error {
104 if o.message != "" && strings.TrimSpace(o.message) == "" {
105 return errors.New("--message cannot be empty")
106 }
107 if o.internal && strings.TrimSpace(o.message) == "" {
108 return errors.New("--internal requires --message")
109 }
110 if !o.yes && !o.io.PromptEnabled() {
111 return cmdutils.FlagError{Err: errors.New("--yes required when not running interactively")}
112 }
113 return nil
116func (o *options) complete(cmd *cobra.Command, args []string) error {
117 client, err := o.gitlabClient()
118 if err != nil {
119 return err
120 }
121 o.client = client
123 mr, repo, err := mrutils.MRFromArgs(cmd.Context(), o.factory, args, "any")
124 if err != nil {
125 return err
126 }
127 o.mr = mr
128 o.repo = repo
130 return nil
133func (o *options) run(ctx context.Context) error {
134 drafts, err := mrutils.ListAllDraftNotes(ctx, o.client, o.repo.FullName(), o.mr.IID)
135 if err != nil {
136 return err
137 }
138 if len(drafts) == 0 {
139 return fmt.Errorf("no pending review comments on !%d", o.mr.IID)
140 }
142 if !o.yes {
143 var confirmed bool
144 if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Publish %s?", utils.Pluralize(len(drafts), "pending review comment"))); err != nil {
145 return err
146 }
147 if !confirmed {
148 o.io.LogInfo("Aborted.")
149 return nil
150 }
151 }
153 publishOpts := &gitlab.PublishAllDraftNotesOptions{}
154 if message := strings.TrimSpace(o.message); message != "" {
155 publishOpts.Note = new(message)
156 }
157 if o.internal {
158 publishOpts.Internal = new(true)
159 }
160 if o.reviewerState != "" {
161 publishOpts.ReviewerState = new(o.reviewerState)
162 }
164 //nolint:staticcheck // PublishAllDraftNotesWithOptions is the only way to pass note/internal/reviewer_state in SDK v3; options merge into PublishAllDraftNotes in 4.0.
165 _, err = o.client.DraftNotes.PublishAllDraftNotesWithOptions(
166 o.repo.FullName(),
167 o.mr.IID,
168 publishOpts,
169 gitlab.WithContext(ctx),
170 )
171 if err != nil {
172 return fmt.Errorf("failed to publish pending review comments: %w", err)
173 }
175 o.io.LogInfof("✓ Published %s. %s\n", utils.Pluralize(len(drafts), "pending review comment"), o.mr.WebURL)
177 // bulk_publish reports success but drops reviewer_state when the caller
178 // authored the MR and is not one of its reviewers.
179 if o.reviewerState != "" && o.mr.Author != nil {
180 user, _, err := o.client.Users.CurrentUser(gitlab.WithContext(ctx))
181 if err != nil {
182 dbg.Debugf("skipping --reviewer-state author check, current user lookup failed: %v", err)
183 } else if user.ID == o.mr.Author.ID && !slices.ContainsFunc(o.mr.Reviewers, func(r *gitlab.BasicUser) bool { return r.ID == user.ID }) {
184 o.io.LogErrorf("%s GitLab did not set your review state: you authored this merge request and are not one of its reviewers.\n", o.io.Color().WarnIcon())
185 }
186 }
187 return nil
Diagnostics10 git calls · 339 ms
CallStatusSizeTime0339 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
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 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
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
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=3
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
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:02001.8 KiB · hit4 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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 · 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=6c82398efd91c9e023d583b948fde889bd4991b2
Open pack map
fetchtree:0200259 B · hit2 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=259
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=17c60670b892d93213e07c56d27bf8c78b2f6eee
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=259
git.pkt.payload.max=232
git.sideband.band1.bytes=232
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=232
git.pack.trailer=a86d2eccaf7aae3b0fec29dbfe655131faf5a4ad
Open pack map
fetchtree:0200176 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=321
git.response.bytes=176
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=f4803b475319c7928519f6199427aeb534eec830
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=176
git.pkt.payload.max=149
git.sideband.band1.bytes=149
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=149
git.pack.trailer=9a9a2a79859324a1daf1ee0f70d8289fbb901820
Open pack map
fetch2002.4 KiB321 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2458
git.response.cache=MISS
git.response.duration_ms=321
git.fetch.wants=8041baee67d2258d74bcb2b8efd2fe4241e6275a
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=2458
git.pkt.payload.max=2431
git.sideband.band1.bytes=2431
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
a5e2f56c0f5e3a24
Request
ae0c7cce…
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