GitLabgitlab-org

cli

main
ply9 git calls262 ms100 KiB6 cachedprotocol v2
263 lines8.8 KiBRaw
1package delete
3import (
4 "context"
5 "fmt"
6 "regexp"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/container_registry/registryutils"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20type options struct {
21 repositoryID int64
22 tagName string
23 nameRegexDelete string
24 nameRegexKeep string
25 keepN int
26 olderThan string
27 keepNSet bool
28 bulkDelete bool
29 forceDelete bool
31 io *iostreams.IOStreams
32 gitlabClient func() (*gitlab.Client, error)
33 baseRepo func() (glrepo.Interface, error)
36func NewCmd(f cmdutils.Factory) *cobra.Command {
37 opts := &options{
38 io: f.IO(),
39 gitlabClient: f.GitLabClient,
40 baseRepo: f.BaseRepo,
41 }
43 cmd := &cobra.Command{
44 Use: "delete <repository-id> [<tag-name>] [flags]",
45 Short: "Delete container registry tags.",
46 Long: heredoc.Doc(`
47 Delete one container registry tag by name, or delete matching tags in
48 bulk by filter.
50 Deleting a single tag is synchronous. Bulk deletion is scheduled
51 asynchronously by GitLab; matching tags may remain visible until the
52 background deletion job has completed.
54 To bulk delete tags, omit <tag-name> and provide at least one bulk
55 deletion flag: --name-regex-delete, --name-regex-keep, --keep-n, or
56 --older-than.
58 The repository ID must belong to the selected project. Use -R/--repo
59 to specify the owning project when running this command outside that
60 project's Git checkout.
61 `),
62 Aliases: []string{"del"},
63 Args: cobra.RangeArgs(1, 2),
64 Example: heredoc.Doc(`
65 # Delete a container registry tag with a confirmation prompt
66 glab container-registry tag delete 123 latest
68 # Skip the confirmation prompt
69 glab container-registry tag delete 123 latest --yes
71 # Schedule tags matching a regular expression for deletion
72 glab container-registry tag delete 123 --name-regex-delete '^release-.*' --yes
74 # Schedule old tags for deletion, but keep the 10 most recent matching tags
75 glab container-registry tag delete 123 --name-regex-delete '.*' --keep-n 10 --older-than 30d --yes
77 # Delete a container registry tag in another project
78 glab container-registry tag delete 123 latest -R gitlab-org/cli`),
79 Annotations: map[string]string{
80 mcpannotations.Destructive: "true",
81 },
82 RunE: func(cmd *cobra.Command, args []string) error {
83 if err := opts.complete(cmd, args); err != nil {
84 return err
85 }
86 if err := opts.validate(); err != nil {
87 return err
88 }
90 return opts.run(cmd.Context())
91 },
92 }
94 fl := cmd.Flags()
95 fl.StringVar(&opts.nameRegexDelete, "name-regex-delete", "", "Regular expression for tag names to delete. Bulk deletion only; scheduled asynchronously.")
96 fl.StringVar(&opts.nameRegexKeep, "name-regex-keep", "", "Regular expression for tag names to keep. Bulk deletion only; scheduled asynchronously.")
97 fl.IntVar(&opts.keepN, "keep-n", 0, "Keep the latest N matching tags. Bulk deletion only; scheduled asynchronously.")
98 fl.StringVar(&opts.olderThan, "older-than", "", "Delete tags older than the given duration, such as 7d or 1month. Bulk deletion only; scheduled asynchronously.")
99 fl.BoolVarP(&opts.forceDelete, "yes", "y", false, "Skip the confirmation prompt.")
101 return cmd
104func (o *options) complete(cmd *cobra.Command, args []string) error {
105 repositoryID, err := cmdutils.ParseID(args[0], "repository ID")
106 if err != nil {
107 return &cmdutils.FlagError{Err: err}
108 }
109 o.repositoryID = repositoryID
110 if len(args) == 2 {
111 o.tagName = args[1]
112 }
113 o.bulkDelete = cmd.Flags().Changed("name-regex-delete") ||
114 cmd.Flags().Changed("name-regex-keep") ||
115 cmd.Flags().Changed("keep-n") ||
116 cmd.Flags().Changed("older-than")
117 o.keepNSet = cmd.Flags().Changed("keep-n")
119 return nil
122func (o *options) validate() error {
123 if o.tagName == "" && !o.bulkDelete {
124 return &cmdutils.FlagError{Err: fmt.Errorf("either a tag name or at least one bulk deletion flag is required")}
125 }
126 if o.tagName != "" && o.bulkDelete {
127 return &cmdutils.FlagError{Err: fmt.Errorf("either a tag name or bulk deletion flags must be passed, but not both")}
128 }
129 if o.bulkDelete && o.nameRegexDelete == "" && o.nameRegexKeep == "" && !o.keepNSet && o.olderThan == "" {
130 return &cmdutils.FlagError{Err: fmt.Errorf("at least one bulk deletion flag must be non-empty")}
131 }
132 if o.nameRegexDelete != "" {
133 if _, err := regexp.Compile(o.nameRegexDelete); err != nil {
134 return &cmdutils.FlagError{Err: fmt.Errorf("--name-regex-delete is not a valid regular expression: %w", err)}
135 }
136 }
137 if o.nameRegexKeep != "" {
138 if _, err := regexp.Compile(o.nameRegexKeep); err != nil {
139 return &cmdutils.FlagError{Err: fmt.Errorf("--name-regex-keep is not a valid regular expression: %w", err)}
140 }
141 }
142 if o.keepN < 0 {
143 return &cmdutils.FlagError{Err: fmt.Errorf("--keep-n must be zero or a positive integer")}
144 }
145 if !o.forceDelete && !o.io.PromptEnabled() {
146 return &cmdutils.FlagError{Err: fmt.Errorf("--yes or -y flag is required when not running interactively")}
147 }
149 return nil
152func (o *options) run(ctx context.Context) error {
153 client, err := o.gitlabClient()
154 if err != nil {
155 return err
156 }
158 repo, err := o.baseRepo()
159 if err != nil {
160 return err
161 }
163 if o.bulkDelete {
164 return o.runBulkDelete(ctx, client, repo)
165 }
167 return o.runSingleDelete(ctx, client, repo)
170func (o *options) runSingleDelete(ctx context.Context, client *gitlab.Client, repo glrepo.Interface) error {
171 tagPath := fmt.Sprintf("%s:%s", repo.FullName(), o.tagName)
173 if !o.forceDelete && o.io.PromptEnabled() {
174 o.io.LogErrorf("This action will permanently delete container registry tag %q.\n\n", tagPath)
175 err := o.io.Confirm(ctx, &o.forceDelete, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to delete container registry tag %q?", tagPath))
176 if err != nil {
177 return cmdutils.WrapError(err, "could not prompt")
178 }
179 }
181 if !o.forceDelete {
182 return cmdutils.CancelError()
183 }
185 c := o.io.Color()
186 o.io.LogInfof("%s Deleting container registry tag %s\n",
187 c.ProgressIcon(),
188 tagPath)
190 _, err := client.ContainerRegistry.DeleteRegistryRepositoryTag(repo.FullName(), o.repositoryID, o.tagName)
191 if err != nil {
192 return cmdutils.WrapError(err, registryutils.ProjectScopedTagError("failed to delete container registry", o.tagName, o.repositoryID, repo.FullName())+".")
193 }
195 o.io.LogInfof(c.Bold("%s Container registry tag %q deleted.\n"), c.RedCheck(), o.tagName)
196 return nil
199func (o *options) runBulkDelete(ctx context.Context, client *gitlab.Client, repo glrepo.Interface) error {
200 if !o.forceDelete && o.io.PromptEnabled() {
201 o.io.LogErrorf("%s", bulkDeleteConfirmationMessage(o.repositoryID, repo.FullName(), o.nameRegexDelete, o.nameRegexKeep, o.keepN, o.olderThan))
202 err := o.io.Confirm(ctx, &o.forceDelete, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to schedule matching container registry tags for deletion from repository %d?", o.repositoryID))
203 if err != nil {
204 return cmdutils.WrapError(err, "could not prompt")
205 }
206 }
208 if !o.forceDelete {
209 return cmdutils.CancelError()
210 }
212 deleteOpts := &gitlab.DeleteRegistryRepositoryTagsOptions{}
213 if o.nameRegexDelete != "" {
214 deleteOpts.NameRegexpDelete = new(o.nameRegexDelete)
215 }
216 if o.nameRegexKeep != "" {
217 deleteOpts.NameRegexpKeep = new(o.nameRegexKeep)
218 }
219 if o.keepNSet {
220 keepN := int64(o.keepN)
221 deleteOpts.KeepN = new(keepN)
222 }
223 if o.olderThan != "" {
224 deleteOpts.OlderThan = new(o.olderThan)
225 }
227 c := o.io.Color()
228 o.io.LogInfof("%s Scheduling container registry tags for deletion %s=%s %s=%d\n",
229 c.ProgressIcon(),
230 c.Blue("repo"), repo.FullName(),
231 c.Blue("repository"), o.repositoryID)
233 _, err := client.ContainerRegistry.DeleteRegistryRepositoryTags(repo.FullName(), o.repositoryID, deleteOpts)
234 if err != nil {
235 return cmdutils.WrapError(err, registryutils.ProjectScopedRepositoryError("failed to delete container registry tags from", o.repositoryID, repo.FullName())+".")
236 }
238 o.io.LogInfof(c.Bold("%s Container registry tags scheduled for deletion. They may remain visible until GitLab finishes the background deletion job.\n"), c.RedCheck())
239 return nil
242func bulkDeleteConfirmationMessage(repositoryID int64, repoName string, nameRegexDelete string, nameRegexKeep string, keepN int, olderThan string) string {
243 return fmt.Sprintf(heredoc.Doc(`
244 This action schedules container registry tags for deletion from repository %d on %s.
246 Filters:
247 name regex delete: %s
248 name regex keep: %s
249 keep latest: %d
250 older than: %s
252 The matching tags may remain visible until the background deletion job has completed.
254 `), repositoryID, repoName, emptyValue(nameRegexDelete), emptyValue(nameRegexKeep), keepN, emptyValue(olderThan))
257func emptyValue(value string) string {
258 if value == "" {
259 return "<unset>"
260 }
262 return value
Diagnostics9 git calls · 262 ms
CallStatusSizeTime0262 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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=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
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=3
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=4
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:0200243 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=243
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a517df7bd3c1e8ee5cd02bc33418664d7dab246f
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=243
git.pkt.payload.max=216
git.sideband.band1.bytes=216
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=216
git.pack.trailer=50fe37a7b0ca9579b3ab7da0b785d0006b143110
Open pack map
fetchtree:0200224 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=224
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=8b9b7c6ea42df140f2c89ead4e15052403738eb0
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=224
git.pkt.payload.max=197
git.sideband.band1.bytes=197
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=197
git.pack.trailer=80648207117f05e5e688025c86dccdb4da2296b2
Open pack map
fetchtree:0200138 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=138
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=6034aa6ef2d91f47f3e854fc2c8458c4ddfaf860
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=138
git.pkt.payload.max=111
git.sideband.band1.bytes=111
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=111
git.pack.trailer=a0e880721321eb92512c53abd8983b98e60d6f98
Open pack map
fetch2002.6 KiB244 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=244
git.response.bytes=2631
git.response.cache=MISS
git.response.duration_ms=244
git.fetch.wants=55e9f445175dfb100d9f1ced3e088501179b75c3
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=2631
git.pkt.payload.max=2604
git.sideband.band1.bytes=2604
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
a5e2e5e599e92947
Request
f327a78b…
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