GitLabgitlab-org

cli

main
ply8 git calls227 ms99 KiB5 cachedprotocol v2
193 lines5.8 KiBRaw
1package update
3import (
4 "context"
5 "fmt"
6 "strings"
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"
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 name string
28 nameSet bool
29 username string
30 usernameSet bool
31 email string
32 emailSet bool
33 group string
34 outputFormat string
37func NewCmd(f cmdutils.Factory) *cobra.Command {
38 opts := &options{
39 io: f.IO(),
40 apiClient: f.ApiClient,
41 baseRepo: f.BaseRepo,
42 }
44 cmd := &cobra.Command{
45 Use: "update <id | username> [flags]",
46 Short: "Update a service account.",
47 Args: cobra.ExactArgs(1),
48 ValidArgsFunction: cobra.NoFileCompletions,
49 Long: heredoc.Docf(`
50 Update the name, username, or email address of a service account of a project or a group, by its ID or username.
52 - Only the given fields change. At least one of %[1]s--name%[1]s, %[1]s--username%[1]s, or %[1]s--email%[1]s is required.
53 - A field cannot be cleared, so an empty value is rejected.
54 - A numeric argument is always treated as an ID. IDs and usernames come from %[1]sglab service-account list%[1]s.
55 - A new custom email address must be confirmed before the account uses it, unless the top-level group has a matching verified domain or email confirmation is turned off on the instance. Until then, the address is shown as the unconfirmed email.
56 - The username of a service account associated with a composite identity cannot be changed.
58 Updating a group service account requires GitLab 17.10 or later. Changing the email address requires GitLab 18.2 or later.
59 `, "`"),
60 Example: heredoc.Doc(`
61 # Rename service account 42 of the current project
62 glab service-account update 42 --name "Release bot"
64 # Change the username of a service account, found by its current username
65 glab service-account update release-bot --username release-bot-2
67 # Change the email address of a service account of a specific project
68 glab service-account update 42 --email release-bot@example.com --repo user/my-repo
70 # Rename a group's service account
71 glab service-account update 42 --name "Release bot" --group my-group`),
72 Annotations: map[string]string{
73 mcpannotations.Destructive: "true",
74 },
75 RunE: func(cmd *cobra.Command, args []string) error {
76 if err := opts.complete(cmd, args); err != nil {
77 return err
78 }
79 if err := opts.validate(); err != nil {
80 return err
81 }
83 return opts.run(cmd.Context())
84 },
85 }
87 cmdutils.EnableRepoOverride(cmd, f)
89 fl := cmd.Flags()
90 fl.StringVarP(&opts.group, "group", "g", "", "Update a service account of a `group` instead of a project. Accepts a group ID or full path.")
91 fl.StringVar(&opts.name, "name", "", "New name of the service account.")
92 fl.StringVar(&opts.username, "username", "", "New `username` of the service account.")
93 fl.StringVar(&opts.email, "email", "", "New email address of the service account.")
95 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
96 cmd.MarkFlagsOneRequired("name", "username", "email")
97 cmd.MarkFlagsMutuallyExclusive("repo", "group")
99 return cmd
102func (o *options) complete(cmd *cobra.Command, args []string) error {
103 o.account = args[0]
105 fl := cmd.Flags()
106 o.nameSet = fl.Changed("name")
107 o.usernameSet = fl.Changed("username")
108 o.emailSet = fl.Changed("email")
110 group, err := cmdutils.GroupOverride(cmd)
111 if err != nil {
112 return err
113 }
114 o.group = group
116 return nil
119// validate rejects blank values, which GitLab drops without an error, so the
120// update would otherwise report success and change nothing.
121func (o *options) validate() error {
122 for _, f := range []struct {
123 name string
124 set bool
125 value string
126 }{
127 {"name", o.nameSet, o.name},
128 {"username", o.usernameSet, o.username},
129 {"email", o.emailSet, o.email},
130 } {
131 if f.set && strings.TrimSpace(f.value) == "" {
132 return &cmdutils.FlagError{Err: fmt.Errorf("--%s can't be empty", f.name)}
133 }
134 }
136 return nil
139func (o *options) run(ctx context.Context) error {
140 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
141 if err != nil {
142 return err
143 }
145 id, err := serviceaccountutils.ResolveID(ctx, client, scope, o.account)
146 if err != nil {
147 return cmdutils.WrapError(err, "failed to update service account")
148 }
150 var name, username, email *string
151 if o.nameSet {
152 name = &o.name
153 }
154 if o.usernameSet {
155 username = &o.username
156 }
157 if o.emailSet {
158 email = &o.email
159 }
161 var raw any
162 var account serviceaccountutils.Account
163 if scope.Group != "" {
164 a, _, err := client.Groups.UpdateServiceAccount(scope.Group, id, &gitlab.UpdateServiceAccountOptions{
165 Name: name,
166 Username: username,
167 Email: email,
168 }, gitlab.WithContext(ctx))
169 if err != nil {
170 return cmdutils.WrapError(err, "failed to update service account")
171 }
172 raw, account = a, serviceaccountutils.FromGroup(a)
173 } else {
174 a, _, err := client.Projects.UpdateProjectServiceAccount(scope.Project, id, &gitlab.UpdateProjectServiceAccountOptions{
175 Name: name,
176 Username: username,
177 Email: email,
178 }, gitlab.WithContext(ctx))
179 if err != nil {
180 return cmdutils.WrapError(err, "failed to update service account")
181 }
182 raw, account = a, serviceaccountutils.FromProject(a)
183 }
185 if o.outputFormat == "json" {
186 return o.io.PrintJSON(raw)
187 }
189 serviceaccountutils.PrintDetails(o.io, account)
190 o.io.LogErrorf("%s Updated service account %d in %s.\n", o.io.Color().GreenCheck(), account.ID, scope)
192 return nil
Diagnostics8 git calls · 227 ms
CallStatusSizeTime0227 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 KiB3 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=UPDATING
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 · 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:0 · deepen 1020032 KiB · hit4 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=4
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
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=1
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:0200322 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=322
git.response.cache=HIT
git.response.duration_ms=4
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=3488e558d4708096e879feba968e084c249dbe22
Open pack map
fetchtree:0200138 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=138
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=35dba1ca18eda59e3093f7e3f8c83d3541f0fecc
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=189ef6465c8f61b2f35e0fa326a1197ba29398ff
Open pack map
fetch2002.2 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=2211
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=05c1feec691c05a64f3b4d2048dd7603e4f2ad65
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=2211
git.pkt.payload.max=2184
git.sideband.band1.bytes=2184
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
a5e2fe034824cf81
Request
0e8378ed…
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