GitLabgitlab-org
cli
main
1package upload
3import (
4 "context"
5 "errors"
6 "net/http"
7 "os"
8 "strconv"
9 "time"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/spf13/cobra"
13 "github.com/spf13/pflag"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/commands/release/releaseutils"
19 "gitlab.com/gitlab-org/cli/internal/commands/release/releaseutils/upload"
20 "gitlab.com/gitlab-org/cli/internal/glrepo"
21 "gitlab.com/gitlab-org/cli/internal/iostreams"
22 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
23)
25type options struct {
26 tagName string
27 assetLinksAsJSON string
29 assetLinks []*upload.ReleaseAsset
30 assetFiles []*upload.ReleaseFile
32 usePackageRegistry bool
33 packageName string
35 io *iostreams.IOStreams
36 gitlabClient func() (*gitlab.Client, error)
37 baseRepo func() (glrepo.Interface, error)
38}
40func NewCmdUpload(f cmdutils.Factory) *cobra.Command {
41 opts := &options{
42 io: f.IO(),
43 gitlabClient: f.GitLabClient,
44 baseRepo: f.BaseRepo,
45 }
47 cmd := &cobra.Command{
48 Use: "upload <tag> [<files>...]",
49 Short: "Upload release asset files or links to a GitLab release.",
50 Long: heredoc.Doc(`Upload release assets to a GitLab release.
52 Define the display name by appending '#' after the filename.
53 The link type comes after the display name, like this: 'myfile.tar.gz#My display name#package'
54 `),
55 Args: func() cobra.PositionalArgs {
56 return func(cmd *cobra.Command, args []string) error {
57 if len(args) < 1 {
58 return &cmdutils.FlagError{Err: errors.New("no tag name provided")}
59 }
60 if len(args) < 2 && opts.assetLinksAsJSON == "" {
61 return cmdutils.FlagError{Err: errors.New("no files specified")}
62 }
63 return nil
64 }
65 }(),
66 Example: heredoc.Doc(`
67 # Upload a release asset with a display name. 'Type' defaults to 'other'.
68 glab release upload v1.0.1 '/path/to/asset.zip#My display label'
70 # Upload a release asset with a display name and type.
71 glab release upload v1.0.1 '/path/to/asset.png#My display label#image'
73 # Upload all assets in a specified folder. 'Type' defaults to 'other'.
74 glab release upload v1.0.1 ./dist/*
76 # Upload all tarballs in a specified folder. 'Type' defaults to 'other'.
77 glab release upload v1.0.1 ./dist/*.tar.gz
79 # Upload release assets links specified as JSON string
80 glab release upload v1.0.1 --assets-links='
81 [
82 {
83 "name": "Asset1",
84 "url":"https://<domain>/some/location/1",
85 "link_type": "other",
86 "direct_asset_path": "path/to/file"
87 }
88 ]'`),
89 Annotations: map[string]string{
90 mcpannotations.Destructive: "true",
91 },
92 RunE: func(cmd *cobra.Command, args []string) error {
93 if err := opts.complete(cmd.Flags(), args); err != nil {
94 return err
95 }
97 if err := opts.validate(); err != nil {
98 return err
99 }
101 return opts.run(cmd.Context())
102 },
103 }
105 fl := cmd.Flags()
106 fl.StringVarP(&opts.assetLinksAsJSON, "assets-links", "a", "", "`JSON` string representation of assets links, like: `--assets-links='[{\"name\": \"Asset1\", \"url\":\"https://<domain>/some/location/1\", \"link_type\": \"other\", \"direct_asset_path\": \"path/to/file\"}]'`.")
107 fl.BoolVar(&opts.usePackageRegistry, "use-package-registry", false, "Upload release assets to the generic package registry of the project. Alternatively to this flag you may also set the GITLAB_RELEASE_ASSETS_USE_PACKAGE_REGISTRY environment variable to either the value true or 1. The flag takes precedence over this environment variable.")
108 fl.StringVar(&opts.packageName, "package-name", upload.DefaultReleasePackageName, "The package name to use when uploading the assets to the generic package release with --use-package-registry.")
110 return cmd
111}
113func (o *options) complete(flags *pflag.FlagSet, args []string) error {
114 o.tagName = args[0]
116 assetFiles, err := releaseutils.AssetsFromArgs(args[1:])
117 if err != nil {
118 return err
119 }
120 o.assetFiles = assetFiles
122 if !flags.Changed("use-package-registry") {
123 if usePackageRegistry, err := strconv.ParseBool(os.Getenv("GITLAB_RELEASE_ASSETS_USE_PACKAGE_REGISTRY")); err == nil {
124 o.usePackageRegistry = usePackageRegistry
125 }
126 }
128 return nil
129}
131func (o *options) validate() error {
132 if o.assetFiles == nil && o.assetLinksAsJSON == "" {
133 return cmdutils.FlagError{Err: errors.New("no files specified")}
134 }
136 if o.assetLinksAsJSON != "" {
137 var err error
138 o.assetLinks, err = releaseutils.AssetLinksFromJSON(o.assetLinksAsJSON)
139 if err != nil {
140 return err
141 }
142 }
144 return nil
145}
147func (o *options) run(ctx context.Context) error {
148 start := time.Now()
150 client, err := o.gitlabClient()
151 if err != nil {
152 return err
153 }
155 repo, err := o.baseRepo()
156 if err != nil {
157 return err
158 }
159 color := o.io.Color()
160 var resp *gitlab.Response
162 o.io.LogInfof("%s Validating tag %s=%s %s=%s\n",
163 color.ProgressIcon(),
164 color.Blue("repo"), repo.FullName(),
165 color.Blue("tag"), o.tagName)
167 release, resp, err := client.Releases.GetRelease(repo.FullName(), o.tagName, gitlab.WithContext(ctx))
168 if err != nil {
169 if resp != nil && (resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden) {
170 return cmdutils.WrapError(err, "release does not exist. Create a new release with `glab release create "+o.tagName+"`.")
171 }
172 return cmdutils.WrapError(err, "failed to fetch release.")
173 }
175 // upload files and create asset links
176 err = releaseutils.CreateReleaseAssets(ctx, o.io, client, o.assetFiles, o.assetLinks, repo.FullName(), release.TagName, o.packageName, o.usePackageRegistry)
177 if err != nil {
178 return cmdutils.WrapError(err, "creating release assets failed.")
179 }
181 o.io.LogInfof(color.Bold("%s Upload succeeded after %0.2fs.\n"), color.GreenCheck(), time.Since(start).Seconds())
182 return nil
183}
Diagnostics8 git calls · 240 ms
CallStatusSizeTime0240 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=4 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 KiB · hit2 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=HIT 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 · hit4 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=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=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=3 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:0200350 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=350 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=b2a837e646ba305993f35203fafafb109e57a7ca 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=350 git.pkt.payload.max=323 git.sideband.band1.bytes=323 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=323 git.pack.trailer=1232131042f2ba4c9c4dbd99382667a1f0849e15Open pack map
fetchtree:0200170 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=223 git.response.bytes=170 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=bf82235aa6ec4e87a691af6db75446f20b210b48 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=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=9d90a84ccb7108242bb8bc9cc50e207dd5587939Open pack map
fetch2002.1 KiB223 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2162 git.response.cache=MISS git.response.duration_ms=223 git.fetch.wants=c2317a36ac135afa4d4f01ca6ee7d06ebc772239 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=2162 git.pkt.payload.max=2135 git.sideband.band1.bytes=2135 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
- a5e2bd825c59cfc3
- Request
- a49c2b78…
- 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