GitLabgitlab-org

cli

main
ply6 git calls53 ms97 KiB5 cachedprotocol v2
177 lines4.8 KiBRaw
1// Package upload uploads local files to a project and returns the markdown
2// references that embed them in an issue, merge request, or note.
3package upload
5import (
6 "bufio"
7 "context"
8 "errors"
9 "fmt"
10 "io"
11 "mime"
12 "net/http"
13 "os"
14 "path/filepath"
15 "strings"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
19 "gitlab.com/gitlab-org/cli/internal/iostreams"
22const StdinPath = "-"
24const stdinName = "upload"
26// sniffLen matches the window http.DetectContentType reads.
27const sniffLen = 512
29// imageExtensions maps the sniffable content types GitLab renders inline to the
30// extension its filename needs to carry. mime.ExtensionsByType is unusable
31// here because its result is sorted: image/jpeg yields ".jfif" first.
32var imageExtensions = map[string]string{
33 "image/png": ".png",
34 "image/jpeg": ".jpg",
35 "image/gif": ".gif",
36 "image/webp": ".webp",
39// Attachment is a local file to upload and reference from a markdown body.
40type Attachment struct {
41 Name string
42 Open func() (io.ReadCloser, error)
45// Attachments resolves attachment paths, reading stdin for [StdinPath]. It
46// fails before any upload starts, so a bad path leaves nothing uploaded.
47func Attachments(stdin io.Reader, paths []string) ([]Attachment, error) {
48 attachments := make([]Attachment, 0, len(paths))
49 stdinUsed := false
51 for _, path := range paths {
52 if path != StdinPath {
53 attachment, err := fileAttachment(path)
54 if err != nil {
55 return nil, err
56 }
57 attachments = append(attachments, attachment)
58 continue
59 }
61 if stdinUsed {
62 return nil, fmt.Errorf("attachment %q can only be given once", StdinPath)
63 }
64 stdinUsed = true
66 attachment, err := stdinAttachment(stdin)
67 if err != nil {
68 return nil, err
69 }
70 attachments = append(attachments, attachment)
71 }
73 return attachments, nil
76func fileAttachment(path string) (Attachment, error) {
77 info, err := os.Stat(path)
78 if err != nil {
79 return Attachment{}, fmt.Errorf("failed to read attachment: %w", err)
80 }
81 if info.IsDir() {
82 return Attachment{}, fmt.Errorf("attachment %q is a directory", path)
83 }
85 return Attachment{
86 Name: filepath.Base(path),
87 Open: func() (io.ReadCloser, error) { return os.Open(path) },
88 }, nil
91func stdinAttachment(stdin io.Reader) (Attachment, error) {
92 buffered := bufio.NewReaderSize(stdin, sniffLen)
94 head, err := buffered.Peek(sniffLen)
95 if err != nil && !errors.Is(err, io.EOF) {
96 return Attachment{}, fmt.Errorf("failed to read attachment from standard input: %w", err)
97 }
98 if len(head) == 0 {
99 return Attachment{}, errors.New("attachment on standard input is empty")
100 }
102 return Attachment{
103 Name: stdinName + sniffExtension(head),
104 Open: func() (io.ReadCloser, error) { return io.NopCloser(buffered), nil },
105 }, nil
108// sniffExtension returns the extension GitLab needs to render head inline.
109func sniffExtension(head []byte) string {
110 contentType, _, err := mime.ParseMediaType(http.DetectContentType(head))
111 if err != nil {
112 return ""
113 }
114 return imageExtensions[contentType]
117// Upload uploads each attachment and returns a markdown reference to each, in
118// order. On failure the error names the ones that did upload, so a retry can
119// reuse them.
120func Upload(ctx context.Context, ios *iostreams.IOStreams, client *gitlab.Client, projectPath string, attachments []Attachment) ([]string, error) {
121 color := ios.Color()
122 references := make([]string, 0, len(attachments))
124 for _, attachment := range attachments {
125 // Progress goes to stderr so it cannot corrupt piped command output.
126 ios.LogErrorf("%s Uploading %s\n", color.ProgressIcon(), color.Blue(attachment.Name))
128 reference, err := uploadOne(ctx, client, projectPath, attachment)
129 if err != nil {
130 return nil, uploadError(attachment.Name, err, references)
131 }
132 references = append(references, reference)
133 }
135 return references, nil
138func uploadOne(ctx context.Context, client *gitlab.Client, projectPath string, attachment Attachment) (_ string, err error) {
139 content, err := attachment.Open()
140 if err != nil {
141 return "", err
142 }
143 defer func() { err = errors.Join(err, content.Close()) }()
145 uploaded, _, err := client.ProjectMarkdownUploads.UploadProjectMarkdown(
146 projectPath,
147 content,
148 attachment.Name,
149 gitlab.WithContext(ctx),
150 )
151 if err != nil {
152 return "", err
153 }
155 return uploaded.Markdown, nil
158func uploadError(name string, err error, uploaded []string) error {
159 wrapped := fmt.Errorf("failed to upload %s: %w", name, err)
160 if len(uploaded) == 0 {
161 return wrapped
162 }
163 return fmt.Errorf("%w\n\nAlready uploaded, reusable without uploading again:\n%s", wrapped, strings.Join(uploaded, "\n"))
166// Append returns body with references appended after a blank line.
167func Append(body string, references []string) string {
168 if len(references) == 0 {
169 return body
170 }
172 joined := strings.Join(references, "\n")
173 if strings.TrimSpace(body) == "" {
174 return joined
175 }
176 return strings.TrimRight(body, "\n") + "\n\n" + joined
Diagnostics6 git calls · 53 ms
CallStatusSizeTime053 ms
discover200185 B4 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 KiB · hit3 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=HIT
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 · 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=4273
git.response.cache=HIT
git.response.duration_ms=1
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=44
Open 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=45
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=d49ba2eccc7edfaff7ed3e62032e49b6b478cea7
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=04984e458394c157b6d8250a04d09c8e72b3716a
Open pack map
fetch2001.8 KiB · hit45 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1891
git.response.cache=HIT
git.response.duration_ms=45
git.fetch.wants=5ff4855cee0a02668b6f3a4c6ff07e49f3a5248a
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=1891
git.pkt.payload.max=1864
git.sideband.band1.bytes=1864
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
a5e203f8792070b8
Request
4ab5a2d4…
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