GitLabgitlab-org
cli
main
1package delete
3import (
4 "context"
5 "errors"
6 "fmt"
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/api"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
18)
20type options struct {
21 io *iostreams.IOStreams
22 apiClient func(repoHost string) (*api.Client, error)
23 baseRepo func() (glrepo.Interface, error)
25 tokenID int64
26 group string
27 yes bool
28}
30func NewCmd(f cmdutils.Factory) *cobra.Command {
31 opts := &options{
32 io: f.IO(),
33 apiClient: f.ApiClient,
34 baseRepo: f.BaseRepo,
35 }
37 cmd := &cobra.Command{
38 Use: "delete <token-id> [flags]",
39 Short: "Delete a deploy token.",
40 Aliases: []string{"del"},
41 Args: cobra.ExactArgs(1),
42 ValidArgsFunction: cobra.NoFileCompletions,
43 Long: heredoc.Docf(`
44 Delete a deploy token of a project or a group, by its ID.
46 The deletion is permanent and takes effect immediately. Anything authenticating with the token stops working.
48 Deleting is not the revoke action from the GitLab UI, which the REST API does not expose. A revoked token stays listed and is reported as revoked, while a deleted token is gone from %[1]sglab deploy-token list%[1]s entirely.
50 Token IDs come from %[1]sglab deploy-token list%[1]s.
51 `, "`"),
52 Example: heredoc.Doc(`
53 # Delete deploy token 42 of the current project, after confirming
54 glab deploy-token delete 42
56 # Delete it without a confirmation prompt
57 glab deploy-token delete 42 --yes
59 # Delete a deploy token of a specific project
60 glab deploy-token delete 42 --repo user/my-repo
62 # Delete a group's deploy token
63 glab deploy-token delete 42 --group group/sub-group`),
64 Annotations: map[string]string{
65 mcpannotations.Destructive: "true",
66 },
67 RunE: func(cmd *cobra.Command, args []string) error {
68 if err := opts.complete(cmd, args); err != nil {
69 return err
70 }
71 if err := opts.validate(); err != nil {
72 return err
73 }
75 return opts.run(cmd.Context())
76 },
77 }
79 cmdutils.EnableRepoOverride(cmd, f)
81 fl := cmd.Flags()
82 fl.StringVarP(&opts.group, "group", "g", "", "Delete a deploy token of a `group` instead of a project. Accepts a group ID or full path.")
83 fl.BoolVarP(&opts.yes, "yes", "y", false, "Skip the confirmation prompt.")
85 cmd.MarkFlagsMutuallyExclusive("repo", "group")
87 return cmd
88}
90func (o *options) complete(cmd *cobra.Command, args []string) error {
91 tokenID, err := cmdutils.ParseID(args[0], "deploy token ID")
92 if err != nil {
93 return err
94 }
95 o.tokenID = tokenID
97 group, err := cmdutils.GroupOverride(cmd)
98 if err != nil {
99 return err
100 }
101 o.group = group
103 return nil
104}
106func (o *options) validate() error {
107 if !o.yes && !o.io.PromptEnabled() {
108 return &cmdutils.FlagError{Err: errors.New("--yes required when not running interactively")}
109 }
111 return nil
112}
114func (o *options) run(ctx context.Context) error {
115 // The group scope works outside a repository, so a missing base repo is only
116 // fatal for the project scope. Fail before building a client that would be
117 // discarded, so the caller sees why the repository was needed.
118 repo, repoErr := o.baseRepo()
119 if o.group == "" && repoErr != nil {
120 return repoErr
121 }
123 var repoHost string
124 if repoErr == nil {
125 repoHost = repo.RepoHost()
126 }
128 apiClient, err := o.apiClient(repoHost)
129 if err != nil {
130 return err
131 }
132 client := apiClient.Lab()
134 // Naming the target is what makes a GITLAB_GROUP redirect visible before
135 // anything is destroyed. The group branch comes first because repo is nil
136 // whenever a group was given outside a repository.
137 var target string
138 switch {
139 case o.group != "":
140 target = "group " + o.group
141 default:
142 target = "project " + repo.FullName()
143 }
145 if err := o.confirmDeletion(ctx, target); err != nil {
146 return err
147 }
149 switch {
150 case o.group != "":
151 _, err = client.DeployTokens.DeleteGroupDeployToken(o.group, o.tokenID, gitlab.WithContext(ctx))
152 default:
153 _, err = client.DeployTokens.DeleteProjectDeployToken(repo.FullName(), o.tokenID, gitlab.WithContext(ctx))
154 }
155 if err != nil {
156 return cmdutils.WrapError(err, "failed to delete deploy token")
157 }
159 c := o.io.Color()
160 o.io.LogInfof("%s Deleted deploy token %d of %s.\n", c.RedCheck(), o.tokenID, target)
162 return nil
163}
165func (o *options) confirmDeletion(ctx context.Context, target string) error {
166 if o.yes {
167 return nil
168 }
170 o.io.LogErrorf("This permanently deletes deploy token %d of %s. Anything authenticating with it stops working.\n\n", o.tokenID, target)
172 var confirmed bool
173 if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to delete deploy token %d?", o.tokenID)); err != nil {
174 return cmdutils.WrapError(err, "could not prompt")
175 }
176 if !confirmed {
177 return cmdutils.CancelError()
178 }
180 return nil
181}
Diagnostics8 git calls · 217 ms
CallStatusSizeTime0217 ms
discover200185 B4 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 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
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=4 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=4 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200263 B · hit2 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=263 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=aff3dd5052d75f0afe2383713d669f993c82b2f9 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=263 git.pkt.payload.max=236 git.sideband.band1.bytes=236 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=236 git.pack.trailer=4076aed8a003d312c41b730611843784ab33181fOpen pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=199 git.response.bytes=139 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=d6784907a69994a056e6857dd1ae5d611178e54a 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=139 git.pkt.payload.max=112 git.sideband.band1.bytes=112 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=112 git.pack.trailer=5ea7b6af50711cdef8e8c010ded7a8ba292a8997Open pack map
fetch2002.0 KiB199 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2012 git.response.cache=MISS git.response.duration_ms=199 git.fetch.wants=beedeaa4cabc59a1f7f739bada88dd2d6a6cd252 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=2012 git.pkt.payload.max=1985 git.sideband.band1.bytes=1985 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
- a5e29d517a91c465
- Request
- b36c96b9…
- 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