GitLabgitlab-org
cli
main
1package create
3import (
4 "context"
6 "github.com/MakeNowJust/heredoc/v2"
7 "github.com/spf13/cobra"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/service-account/serviceaccountutils"
14 "gitlab.com/gitlab-org/cli/internal/glrepo"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17)
19type options struct {
20 io *iostreams.IOStreams
21 apiClient func(repoHost string) (*api.Client, error)
22 baseRepo func() (glrepo.Interface, error)
24 name string
25 nameSet bool
26 username string
27 usernameSet bool
28 email string
29 emailSet bool
30 group string
31 outputFormat string
32}
34func NewCmd(f cmdutils.Factory) *cobra.Command {
35 opts := &options{
36 io: f.IO(),
37 apiClient: f.ApiClient,
38 baseRepo: f.BaseRepo,
39 }
41 cmd := &cobra.Command{
42 Use: "create [flags]",
43 Short: "Create a service account.",
44 Args: cobra.NoArgs,
45 ValidArgsFunction: cobra.NoFileCompletions,
46 Long: heredoc.Docf(`
47 Create a service account for a project or a group.
49 - Every flag is optional. Without %[1]s--name%[1]s, the account is named "Service account user". Without %[1]s--username%[1]s or %[1]s--email%[1]s, GitLab generates them.
50 - A 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.
51 - A service account created in a top-level group can be invited to any group or project on the instance. One created in a subgroup can be invited only to that subgroup and its descendants.
53 Creating a service account in a subgroup requires GitLab 18.11 or later. Setting %[1]s--email%[1]s for a group service account requires GitLab 17.11 or later.
54 `, "`"),
55 Example: heredoc.Doc(`
56 # Create a service account for the current project
57 glab service-account create --name "Release bot"
59 # Create it with a chosen username and email address
60 glab service-account create --name "Release bot" --username release-bot --email release-bot@example.com
62 # Create a service account for a specific project
63 glab service-account create --name "Release bot" --repo user/my-repo
65 # Create a group service account
66 glab service-account create --name "Release bot" --group my-group
68 # Create a service account and print its ID from a script
69 glab service-account create --name "Release bot" --output json --jq '.id'`),
70 Annotations: map[string]string{
71 mcpannotations.Destructive: "true",
72 },
73 RunE: func(cmd *cobra.Command, args []string) error {
74 if err := opts.complete(cmd); err != nil {
75 return err
76 }
78 return opts.run(cmd.Context())
79 },
80 }
82 cmdutils.EnableRepoOverride(cmd, f)
84 fl := cmd.Flags()
85 fl.StringVarP(&opts.group, "group", "g", "", "Create a service account for a `group` instead of a project. Accepts a group ID or full path.")
86 fl.StringVar(&opts.name, "name", "", "Name of the service account.")
87 fl.StringVar(&opts.username, "username", "", "The service account's `username`.")
88 fl.StringVar(&opts.email, "email", "", "Email address of the service account.")
90 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
91 cmd.MarkFlagsMutuallyExclusive("repo", "group")
93 return cmd
94}
96func (o *options) complete(cmd *cobra.Command) error {
97 fl := cmd.Flags()
98 o.nameSet = fl.Changed("name")
99 o.usernameSet = fl.Changed("username")
100 o.emailSet = fl.Changed("email")
102 group, err := cmdutils.GroupOverride(cmd)
103 if err != nil {
104 return err
105 }
106 o.group = group
108 return nil
109}
111func (o *options) run(ctx context.Context) error {
112 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
113 if err != nil {
114 return err
115 }
117 var name, username, email *string
118 if o.nameSet {
119 name = &o.name
120 }
121 if o.usernameSet {
122 username = &o.username
123 }
124 if o.emailSet {
125 email = &o.email
126 }
128 var raw any
129 var account serviceaccountutils.Account
130 if scope.Group != "" {
131 a, _, err := client.Groups.CreateServiceAccount(scope.Group, &gitlab.CreateServiceAccountOptions{
132 Name: name,
133 Username: username,
134 Email: email,
135 }, gitlab.WithContext(ctx))
136 if err != nil {
137 return cmdutils.WrapError(err, "failed to create service account")
138 }
139 raw, account = a, serviceaccountutils.FromGroup(a)
140 } else {
141 a, _, err := client.Projects.CreateProjectServiceAccount(scope.Project, &gitlab.CreateProjectServiceAccountOptions{
142 Name: name,
143 Username: username,
144 Email: email,
145 }, gitlab.WithContext(ctx))
146 if err != nil {
147 return cmdutils.WrapError(err, "failed to create service account")
148 }
149 raw, account = a, serviceaccountutils.FromProject(a)
150 }
152 if o.outputFormat == "json" {
153 return o.io.PrintJSON(raw)
154 }
156 serviceaccountutils.PrintDetails(o.io, account)
157 o.io.LogErrorf("%s Created service account %d in %s.\n", o.io.Color().GreenCheck(), account.ID, scope)
159 return nil
160}
Diagnostics8 git calls · 205 ms
CallStatusSizeTime0205 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
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=44Open pack map
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:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=3 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=2 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:0200139 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=139 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c6c2b5498b6622028a7dc0718dab12d4378b30c6 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=59d1f9699d7da3b3468a2f353552de793e65509bOpen pack map
fetch2001.8 KiB192 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=192 git.response.bytes=1864 git.response.cache=MISS git.response.duration_ms=192 git.fetch.wants=34be901b9921fc628fe1e997f4278fb89163dc69 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=1864 git.pkt.payload.max=1837 git.sideband.band1.bytes=1837 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
- a5e2e2355b144af4
- Request
- dcec7996…
- 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