GitLabgitlab-org
cli
main
1package update
3import (
4 "fmt"
5 "strings"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
9 "github.com/spf13/pflag"
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/config"
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)
21const (
22 archiveFlag = "archive"
23 defaultBranchFlag = "defaultBranch"
24 descriptionFlag = "description"
25)
27type options struct {
28 apiClient func(repoHost string) (*api.Client, error)
29 io *iostreams.IOStreams
30 baseRepo func() (glrepo.Interface, error)
31 gitlabClient func() (*gitlab.Client, error)
32 defaultHostname func() string
33 config config.Config
35 archive *bool
36 defaultBranch *string
37 description *string
38 projectID string
39}
41func NewCmdUpdate(f cmdutils.Factory) *cobra.Command {
42 opts := &options{
43 apiClient: f.ApiClient,
44 io: f.IO(),
45 baseRepo: f.BaseRepo,
46 gitlabClient: f.GitLabClient,
47 defaultHostname: f.DefaultHostname,
48 config: f.Config(),
49 }
51 cmd := &cobra.Command{
52 Use: "update [path] [flags]",
53 Short: `Update an existing GitLab project or repository.`,
54 Long: heredoc.Docf(`
55 Change project settings such as the description or default branch, or
56 archive and unarchive the project with %[1]s--archive%[1]s.
57 `, "`"),
58 Args: cobra.MaximumNArgs(1),
59 Example: heredoc.Doc(`
60 # Update the description for my-project.
61 glab repo update my-project --description "This project is cool."
63 # Update the default branch for my-project.
64 glab repo update my-project --defaultBranch main
66 # Archive my-project.
67 glab repo update my-project --archive
68 glab repo update my-project --archive=true
70 # Unarchive my-project.
71 glab repo update my-project --archive=false`),
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.Flags(), args); err != nil {
77 return err
78 }
79 return opts.run()
80 },
81 }
83 cmd.Flags().Bool(archiveFlag, false, "Whether the project should be archived.")
84 cmd.Flags().String(defaultBranchFlag, "", "New default branch for the project.")
85 cmd.Flags().StringP(descriptionFlag, "d", "", "New description for the project.")
86 cmd.MarkFlagsOneRequired(archiveFlag, defaultBranchFlag, descriptionFlag)
87 return cmd
88}
90func (o *options) complete(flags *pflag.FlagSet, args []string) error {
91 if flags.Changed(archiveFlag) {
92 archive, err := flags.GetBool(archiveFlag)
93 if err != nil {
94 return err
95 }
96 o.archive = &archive
97 }
98 if flags.Changed(defaultBranchFlag) {
99 defaultBranch, err := flags.GetString(defaultBranchFlag)
100 if err != nil {
101 return err
102 }
103 o.defaultBranch = &defaultBranch
104 }
105 if flags.Changed(descriptionFlag) {
106 description, err := flags.GetString(descriptionFlag)
107 if err != nil {
108 return err
109 }
110 o.description = &description
111 }
113 if len(args) == 1 {
114 o.projectID = args[0]
115 }
116 return nil
117}
119func (o *options) run() error {
120 repo, err := o.getRepoFromProjectID()
121 if err != nil {
122 return err
123 }
125 client, err := o.apiClient(repo.RepoHost())
126 if err != nil {
127 return err
128 }
129 apiClient := client.Lab()
131 var project *gitlab.Project
132 if o.settingsChanged() {
133 project, _, err = apiClient.Projects.EditProject(repo.FullName(), &gitlab.EditProjectOptions{
134 DefaultBranch: o.defaultBranch,
135 Description: o.description,
136 })
137 if err != nil {
138 return fmt.Errorf("updating project: %w", err)
139 }
140 }
142 // Handle archive flag separately - this uses a separate API endpoint
143 if o.archive != nil {
144 if *o.archive {
145 project, _, err = apiClient.Projects.ArchiveProject(repo.FullName())
146 if err != nil {
147 return fmt.Errorf("archiving project: %w", err)
148 }
149 } else {
150 project, _, err = apiClient.Projects.UnarchiveProject(repo.FullName())
151 if err != nil {
152 return fmt.Errorf("unarchiving project: %w", err)
153 }
154 }
155 }
157 greenCheck := o.io.Color().Green("✓")
158 o.io.LogInfof("%s Updated repository %s on GitLab: %s\n", greenCheck, project.NameWithNamespace, project.WebURL)
159 return nil
160}
162// getRepoFromProjectID wrangles the various ways of specifying a project into
163// a repository object. The accepted input formats are:
164// - no project specified - assume the current dir is a Git repository and
165// look up the remote
166// - just project name - need to look up user's username and prefix it with a
167// slash
168// - user/project - can be passed verbatim to the API
169// - fully qualified GitLab URL
170func (o *options) getRepoFromProjectID() (glrepo.Interface, error) {
171 projectID := o.projectID
172 // No project argument - use current repository
173 if projectID == "" {
174 // Configure client to have host of current repository
175 repo, err := o.baseRepo()
176 if err != nil {
177 return nil, cmdutils.WrapError(err, "`repository` is required when not running in a Git repository.")
178 }
179 return repo, nil
180 } else {
181 // If the ProjectID is a single token, use current user's namespace
182 if !strings.Contains(projectID, "/") {
183 client, err := o.gitlabClient()
184 if err != nil {
185 return nil, err
186 }
187 currentUser, _, err := client.Users.CurrentUser()
188 if err != nil {
189 return nil, cmdutils.WrapError(err, "Failed to retrieve your current user.")
190 }
192 projectID = currentUser.Username + "/" + projectID
193 }
195 // Get the repo full name from the ProjectID which can be a full URL or a group/repo format
196 return glrepo.FromFullName(projectID, o.defaultHostname(), o.config)
197 }
198}
200// settingsChanged returns true if a "settings" flag has been changed. These
201// are the flags that require a call to the EditProject API to change. They
202// include, for example, the description and default branch, but do not include
203// the "archive" flag.
204func (o *options) settingsChanged() bool {
205 return o.defaultBranch != nil || o.description != nil
206}
Diagnostics8 git calls · 197 ms
CallStatusSizeTime0197 ms
discover200185 B3 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 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 · 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: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=3 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
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:0200614 B · 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=614 git.response.cache=HIT git.response.duration_ms=3 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: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=182 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=524ce9f9559bc45f83bb7cfe1e7ca3d9c84411ed 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=9a34ddcf7449a6e2281e77ba48ca19ec6c77155cOpen pack map
fetch2002.1 KiB182 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2174 git.response.cache=MISS git.response.duration_ms=182 git.fetch.wants=2eda63a26fc430ae96dfa218093fe8f8ef25f328 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=2174 git.pkt.payload.max=2147 git.sideband.band1.bytes=2147 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
- a5ea56622e937089
- Request
- e69d5f2b…
- 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