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/commands/service-account/serviceaccountutils"
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)
21type options struct {
22 io *iostreams.IOStreams
23 apiClient func(repoHost string) (*api.Client, error)
24 baseRepo func() (glrepo.Interface, error)
26 account string
27 group string
28 yes bool
29}
31func NewCmd(f cmdutils.Factory) *cobra.Command {
32 opts := &options{
33 io: f.IO(),
34 apiClient: f.ApiClient,
35 baseRepo: f.BaseRepo,
36 }
38 cmd := &cobra.Command{
39 Use: "delete <id | username> [flags]",
40 Short: "Delete a service account.",
41 Aliases: []string{"del"},
42 Args: cobra.ExactArgs(1),
43 ValidArgsFunction: cobra.NoFileCompletions,
44 Long: heredoc.Docf(`
45 Delete a service account of a project or a group, by its ID or username.
47 The account's contributions, such as merge requests and comments, are moved to the ghost user. Anything authenticating with the account's tokens stops working.
49 GitLab deletes the account in the background, so it can still appear in %[1]sglab service-account list%[1]s for a short time.
51 A numeric argument is always treated as an ID. IDs and usernames come from %[1]sglab service-account list%[1]s.
52 `, "`"),
53 Example: heredoc.Doc(`
54 # Delete service account 42 of the current project, after confirming
55 glab service-account delete 42
57 # Delete it without a confirmation prompt
58 glab service-account delete 42 --yes
60 # Delete a service account by its username
61 glab service-account delete release-bot
63 # Delete a service account of a specific project
64 glab service-account delete 42 --repo user/my-repo
66 # Delete a group's service account
67 glab service-account delete 42 --group my-group`),
68 Annotations: map[string]string{
69 mcpannotations.Destructive: "true",
70 },
71 RunE: func(cmd *cobra.Command, args []string) error {
72 if err := opts.complete(cmd, args); err != nil {
73 return err
74 }
75 if err := opts.validate(); err != nil {
76 return err
77 }
79 return opts.run(cmd.Context())
80 },
81 }
83 cmdutils.EnableRepoOverride(cmd, f)
85 fl := cmd.Flags()
86 fl.StringVarP(&opts.group, "group", "g", "", "Delete a service account of a `group` instead of a project. Accepts a group ID or full path.")
87 fl.BoolVarP(&opts.yes, "yes", "y", false, "Skip the confirmation prompt.")
89 cmd.MarkFlagsMutuallyExclusive("repo", "group")
91 return cmd
92}
94func (o *options) complete(cmd *cobra.Command, args []string) error {
95 o.account = args[0]
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 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
116 if err != nil {
117 return err
118 }
120 // Resolved before the prompt, so the prompt names the account by ID.
121 id, err := serviceaccountutils.ResolveID(ctx, client, scope, o.account)
122 if err != nil {
123 return cmdutils.WrapError(err, "failed to delete service account")
124 }
126 if err := o.confirmDeletion(ctx, id, scope); err != nil {
127 return err
128 }
130 // hard_delete stays unset: a soft delete keeps the account's contributions.
131 if scope.Group != "" {
132 _, err = client.Groups.DeleteServiceAccount(scope.Group, id, &gitlab.DeleteServiceAccountOptions{}, gitlab.WithContext(ctx))
133 } else {
134 _, err = client.Projects.DeleteProjectServiceAccount(scope.Project, id, &gitlab.DeleteProjectServiceAccountOptions{}, gitlab.WithContext(ctx))
135 }
136 if err != nil {
137 return cmdutils.WrapError(err, "failed to delete service account")
138 }
140 o.io.LogInfof("%s Scheduled service account %d of %s for deletion.\n", o.io.Color().RedCheck(), id, scope)
142 return nil
143}
145func (o *options) confirmDeletion(ctx context.Context, id int64, scope serviceaccountutils.Scope) error {
146 if o.yes {
147 return nil
148 }
150 o.io.LogErrorf("This deletes service account %d of %s. Its contributions move to the ghost user, and anything authenticating with its tokens stops working.\n\n", id, scope)
152 var confirmed bool
153 if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to delete service account %d?", id)); err != nil {
154 return cmdutils.WrapError(err, "could not prompt")
155 }
156 if !confirmed {
157 return cmdutils.CancelError()
158 }
160 return nil
161}
Diagnostics8 git calls · 213 ms
CallStatusSizeTime0213 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=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 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=5 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 · 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:0200322 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=322 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=ba2caccf6c55211c6b29b98f40c2358479d91b1f 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=322 git.pkt.payload.max=295 git.sideband.band1.bytes=295 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=295 git.pack.trailer=3488e558d4708096e879feba968e084c249dbe22Open pack map
fetchtree:0200138 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=197 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=2a83c32378dd81a74f1a4c91ddfdaeb92b60b4d2 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=80dbef3c92c8a6ae4f8cf148bfcf64dd19aed5b5Open pack map
fetch2001.8 KiB197 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1858 git.response.cache=MISS git.response.duration_ms=197 git.fetch.wants=77b8553c5997378a555c92390d70d8b4ff92d22a 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=1858 git.pkt.payload.max=1831 git.sideband.band1.bytes=1831 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
- a5e2ff757978f3c9
- Request
- 160bd003…
- 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