GitLabgitlab-org
cli
main
1package note
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strconv"
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 "gitlab.com/gitlab-org/cli/internal/glrepo"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19 "gitlab.com/gitlab-org/cli/internal/text"
20)
22type deleteOptions struct {
23 io *iostreams.IOStreams
24 factory cmdutils.Factory
25 gitlabClient func() (*gitlab.Client, error)
27 // Flags.
28 yes bool
30 // Populated in complete.
31 mrArgs []string
32 client *gitlab.Client
33 mr *gitlab.MergeRequest
34 repo glrepo.Interface
35 discussionID string
36 noteID int64
37 note *gitlab.Note
38}
40func NewCmdDelete(f cmdutils.Factory) *cobra.Command {
41 opts := &deleteOptions{
42 io: f.IO(),
43 factory: f,
44 gitlabClient: f.GitLabClient,
45 }
47 cmd := &cobra.Command{
48 Use: "delete [<id> | <branch>] <note-id>",
49 Short: "Delete a note from a merge request. (EXPERIMENTAL)",
50 Long: heredoc.Docf(`
51 Permanently delete a note from a merge request.
53 %[1]s<note-id>%[1]s is a numeric note ID, not a hex discussion ID.
54 You can find note IDs with:
56 - %[1]sglab mr note list -F json%[1]s (the %[1]s.id%[1]s field)
57 - Note URLs: %[1]s.../merge_requests/1#note_12345%[1]s
59 Deletion is permanent and cannot be undone. Unless you pass %[1]s--yes%[1]s,
60 the command prompts you to confirm.
61 `, "`") + text.ExperimentalString,
62 Example: heredoc.Doc(`
63 # Delete note 12345 from merge request 1
64 glab mr note delete 1 12345
66 # Delete without confirmation
67 glab mr note delete 1 12345 --yes
69 # Delete a note on the current branch's merge request
70 glab mr note delete 12345
71 `),
72 Args: cobra.RangeArgs(1, 2),
73 Annotations: map[string]string{
74 mcpannotations.Destructive: "true",
75 },
76 RunE: func(cmd *cobra.Command, args []string) error {
77 if err := opts.complete(cmd.Context(), args); err != nil {
78 return err
79 }
80 return opts.run(cmd.Context())
81 },
82 }
84 cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, "Skip confirmation prompt.")
86 return cmd
87}
89func (o *deleteOptions) complete(ctx context.Context, args []string) error {
90 // Last arg is always the note ID.
91 noteIDStr := args[len(args)-1]
92 noteID, err := strconv.ParseInt(noteIDStr, 10, 64)
93 if err != nil {
94 return fmt.Errorf("invalid note ID %q: must be a numeric note ID, not a discussion ID prefix", noteIDStr)
95 }
96 o.noteID = noteID
98 if len(args) == 2 {
99 o.mrArgs = args[:1]
100 }
102 client, err := o.gitlabClient()
103 if err != nil {
104 return err
105 }
106 o.client = client
108 mr, repo, err := mrutils.MRFromArgs(ctx, o.factory, o.mrArgs, "any")
109 if err != nil {
110 return err
111 }
112 o.mr = mr
113 o.repo = repo
115 // Find the parent discussion and note.
116 o.discussionID, o.note, err = mrutils.FindNoteInDiscussionPaginated(ctx, client, repo.FullName(), mr.IID, noteID)
117 if err != nil {
118 if errors.Is(err, mrutils.ErrNoteNotFound) {
119 return fmt.Errorf("note %d not found in merge request !%d", noteID, mr.IID)
120 }
121 return fmt.Errorf("failed to look up note %d in merge request !%d: %w", noteID, mr.IID, err)
122 }
124 return nil
125}
127func (o *deleteOptions) run(ctx context.Context) error {
128 if !o.yes && !o.io.PromptEnabled() {
129 return cmdutils.FlagError{Err: fmt.Errorf("--yes required when not running interactively")}
130 }
132 if !o.yes && o.io.PromptEnabled() {
133 author := ""
134 if o.note.Author.Username != "" {
135 author = fmt.Sprintf(" by @%s", o.note.Author.Username)
136 }
138 o.io.LogInfof("Note %d%s: %s\n", o.noteID, author, mrutils.NotePreview(o.note.Body))
140 var confirmed bool
141 if err := o.io.Confirm(ctx, &confirmed, "Are you sure you want to delete this note?"); err != nil {
142 return err
143 }
144 if !confirmed {
145 o.io.LogInfo("Aborted.")
146 return nil
147 }
148 }
150 _, err := o.client.Discussions.DeleteMergeRequestDiscussionNote(
151 o.repo.FullName(),
152 o.mr.IID,
153 o.discussionID,
154 o.noteID,
155 gitlab.WithContext(ctx),
156 )
157 if err != nil {
158 return fmt.Errorf("failed to delete note: %w", err)
159 }
161 o.io.LogInfof("✓ Deleted note %d from !%d\n", o.noteID, o.mr.IID)
162 return nil
163}
Diagnostics8 git calls · 211 ms
CallStatusSizeTime0211 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 · hit5 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=5 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=2 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 · hit4 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=746 git.response.cache=HIT git.response.duration_ms=4 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.8 KiB198 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=198 git.response.bytes=1811 git.response.cache=MISS git.response.duration_ms=198 git.fetch.wants=745515689fab7acce2c54958bd8944917209ec7a 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=1811 git.pkt.payload.max=1784 git.sideband.band1.bytes=1784 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
- a5e29693287d2407
- Request
- 531b5b46…
- 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