GitLabgitlab-org
cli
main
1package remove
3import (
4 "fmt"
5 "strconv"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/glrepo"
14 "gitlab.com/gitlab-org/cli/internal/iostreams"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16)
18type options struct {
19 userID int64
20 username string
22 baseRepo func() (glrepo.Interface, error)
23 gitlabClient func() (*gitlab.Client, error)
24 io *iostreams.IOStreams
25}
27func newOptions(f cmdutils.Factory) *options {
28 return &options{
29 io: f.IO(),
30 baseRepo: f.BaseRepo,
31 gitlabClient: f.GitLabClient,
32 }
33}
35func NewCmd(f cmdutils.Factory) *cobra.Command {
36 opts := newOptions(f)
38 cmd := &cobra.Command{
39 Use: "remove [flags]",
40 Short: `Remove a member from the project.`,
41 Long: heredoc.Doc(`
42 Remove a member from the project.
43 `),
44 Example: heredoc.Doc(`
45 # Remove a user by username
46 glab repo members remove --username=john.doe
48 # Remove a user by ID
49 glab repo members remove --user-id=123`),
50 Args: cobra.NoArgs,
51 ValidArgsFunction: cobra.NoFileCompletions,
52 Annotations: map[string]string{
53 mcpannotations.Destructive: "true",
54 },
55 RunE: func(cmd *cobra.Command, args []string) error {
56 if err := opts.validate(); err != nil {
57 return err
58 }
59 return opts.run()
60 },
61 }
63 cmdutils.EnableRepoOverride(cmd, f)
65 fl := cmd.Flags()
66 fl.Int64VarP(&opts.userID, "user-id", "u", 0, "User ID instead of username.")
67 fl.StringVarP(&opts.username, "username", "", "", "Username instead of user-id.")
68 cmd.MarkFlagsMutuallyExclusive("username", "user-id")
70 return cmd
71}
73func (o *options) validate() error {
74 if o.username == "" && o.userID == 0 {
75 return fmt.Errorf("either username or user-id must be specified")
76 }
78 if o.username != "" && o.userID != 0 {
79 return fmt.Errorf("cannot specify both username and user-id")
80 }
82 return nil
83}
85func (o *options) run() error {
86 client, err := o.gitlabClient()
87 if err != nil {
88 return err
89 }
91 repo, err := o.baseRepo()
92 if err != nil {
93 return err
94 }
96 var userIDToRemove int64
97 var userIdentifier string
99 switch {
100 case o.userID != 0:
101 userIDToRemove = o.userID
102 userIdentifier = strconv.FormatInt(o.userID, 10)
103 case o.username != "":
104 // Get user ID from username
105 users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{
106 Username: new(o.username),
107 })
108 if err != nil {
109 return fmt.Errorf("failed to find user %s: %w", o.username, err)
110 }
111 if len(users) == 0 {
112 return fmt.Errorf("user %s not found", o.username)
113 }
114 if len(users) > 1 {
115 return fmt.Errorf("multiple users found with username %s", o.username)
116 }
118 userIDToRemove = users[0].ID
119 userIdentifier = o.username
120 }
122 _, err = client.ProjectMembers.DeleteProjectMember(repo.FullName(), userIDToRemove)
123 if err != nil {
124 return fmt.Errorf("failed to remove member %s: %w", userIdentifier, err)
125 }
127 c := o.io.Color()
128 o.io.LogInfof("%s Successfully removed %s from %s\n",
129 c.GreenCheck(),
130 c.Bold(userIdentifier),
131 c.Bold(repo.FullName()))
133 return nil
134}
Diagnostics10 git calls · 764 ms
CallStatusSizeTime0764 ms
discover200185 B166 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 KiB447 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=265 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=281 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 · hit7 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=7 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
fetchtree:02001.1 KiB · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=1112 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=tree:0 git.fetch.wants=fed5065f0120a857fec6fa567dcdacfd34c3187b 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=1112 git.pkt.payload.max=1085 git.sideband.band1.bytes=1085 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1085 git.pack.trailer=792a8e6885c3db50fcc6be7a28de09363c6fb5e4Open pack map
fetchtree:0200890 B · hit37 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=26 git.response.bytes=890 git.response.cache=HIT git.response.duration_ms=37 git.fetch.filter=tree:0 git.fetch.wants=11daffb8433226c87be08ba8cfc35154e4957d77 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=890 git.pkt.payload.max=863 git.sideband.band1.bytes=863 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=863 git.pack.trailer=b909e7b7573f11b0bb74a32781c8c5e4e19277f4Open pack map
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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200614 B · hit6 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=614 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=tree:0 git.fetch.wants=3a89d0cfc8e81880c982180388e42e561a0b7eee 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=614 git.pkt.payload.max=587 git.sideband.band1.bytes=587 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=587 git.pack.trailer=eb8250f8e8340ff2fafc22c5aba90db34fd906a9Open pack map
fetchtree:0200198 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=198 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=ae4de5db0d32551153085c68d34eb0d395b7df5a 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=198 git.pkt.payload.max=171 git.sideband.band1.bytes=171 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=171 git.pack.trailer=b44c92e9fa6892fac61a2b92ee59f8711c1238cfOpen pack map
fetchtree:0200138 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=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=5b0ed1814140098db3cc27dd087a146e1ef755a2 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=ea76fb4e2d6426283e8bec82ad217dcdb1782bf7Open pack map
fetch2001.3 KiB250 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=250 git.response.bytes=1317 git.response.cache=MISS git.response.duration_ms=250 git.fetch.wants=9955641cd238843de729c15d50e8f662adf45132 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=1317 git.pkt.payload.max=1290 git.sideband.band1.bytes=1290 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
- a5ea61fbddb3ecd4
- Request
- 7df5987a…
- 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