GitLabgitlab-org

cli

main
ply8 git calls210 ms99 KiB6 cachedprotocol v2
158 lines4.4 KiBRaw
1package issueutils
3import (
4 "context"
5 "fmt"
6 "net/url"
7 "strconv"
8 "strings"
10 "golang.org/x/sync/errgroup"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/api"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/config"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/tableprinter"
20 "gitlab.com/gitlab-org/cli/internal/utils"
23func DisplayIssueList(streams *iostreams.IOStreams, issues []*gitlab.Issue, projectID string) string {
24 c := streams.Color()
25 table := tableprinter.NewTablePrinter()
26 table.SetIsTTY(streams.IsOutputTTY())
28 if len(issues) > 0 {
29 table.AddRow("ID", "Title", "Labels", "Created at")
30 }
32 for _, issue := range issues {
33 table.AddCell(streams.Hyperlink(IssueState(c, issue), issue.WebURL))
34 table.AddCell(issue.Title)
36 if len(issue.Labels) > 0 {
37 table.AddCellf("(%s)", c.Cyan(strings.Trim(strings.Join(issue.Labels, ", "), ",")))
38 } else {
39 table.AddCell("")
40 }
42 if issue.CreatedAt != nil {
43 table.AddCell(c.Gray(utils.TimeToPrettyTimeAgo(*issue.CreatedAt)))
44 } else {
45 table.AddCell("")
46 }
47 table.EndRow()
48 }
50 return table.Render()
53func DisplayIssue(c *iostreams.ColorPalette, i *gitlab.Issue, isTTY bool) string {
54 var duration string
55 if i.CreatedAt != nil {
56 duration = utils.TimeToPrettyTimeAgo(*i.CreatedAt)
57 }
58 issueID := IssueState(c, i)
60 if isTTY {
61 if duration != "" {
62 return fmt.Sprintf("%s %s (%s)\n %s\n", issueID, i.Title, duration, i.WebURL)
63 }
64 return fmt.Sprintf("%s %s\n %s\n", issueID, i.Title, i.WebURL)
65 } else {
66 return i.WebURL
67 }
70func IssueState(c *iostreams.ColorPalette, i *gitlab.Issue) string {
71 switch {
72 case i.State == "opened":
73 return c.Green(fmt.Sprintf("#%d", i.IID))
74 default:
75 return c.Red(fmt.Sprintf("#%d", i.IID))
76 }
79func IssuesFromArgs(ctx context.Context, apiClientFunc func(repoHost string) (*api.Client, error), gitlabClient *gitlab.Client, baseRepoFn func() (glrepo.Interface, error), defaultHostname string, cfg config.Config, args []string) ([]*gitlab.Issue, glrepo.Interface, error) {
80 var baseRepo glrepo.Interface
82 if len(args) <= 1 {
83 if len(args) == 1 {
84 args = strings.Split(args[0], ",")
85 }
86 if len(args) <= 1 {
87 issue, repo, err := IssueFromArg(apiClientFunc, gitlabClient, baseRepoFn, defaultHostname, cfg, args[0])
88 if err != nil {
89 return nil, nil, err
90 }
91 baseRepo = repo
92 return []*gitlab.Issue{issue}, baseRepo, err
93 }
94 }
96 errGroup, _ := errgroup.WithContext(ctx)
97 errGroup.SetLimit(api.MaxConcurrentRequests)
98 issues := make([]*gitlab.Issue, len(args))
99 repos := make([]glrepo.Interface, len(args))
100 for i, arg := range args {
101 errGroup.Go(func() error {
102 issue, repo, err := IssueFromArg(apiClientFunc, gitlabClient, baseRepoFn, defaultHostname, cfg, arg)
103 if err != nil {
104 return err
105 }
106 repos[i] = repo
107 issues[i] = issue
108 return nil
109 })
110 }
111 if err := errGroup.Wait(); err != nil {
112 return nil, nil, err
113 }
114 return issues, repos[len(repos)-1], nil
117func IssueFromArg(apiClientFunc func(repoHost string) (*api.Client, error), client *gitlab.Client, baseRepoFn func() (glrepo.Interface, error), defaultHostname string, cfg config.Config, arg string) (*gitlab.Issue, glrepo.Interface, error) {
118 issueIID, baseRepo := issueMetadataFromURL(arg, defaultHostname, cfg)
119 if issueIID == 0 {
120 var err error
121 issueIIDInt, err := strconv.Atoi(strings.TrimPrefix(arg, "#"))
122 if err != nil {
123 return nil, nil, fmt.Errorf("invalid issue format: %q", arg)
124 }
125 issueIID = int64(issueIIDInt)
126 }
128 if baseRepo == nil {
129 var err error
130 baseRepo, err = baseRepoFn()
131 if err != nil {
132 return nil, nil, fmt.Errorf("could not determine base repository: %w", err)
133 }
134 } else {
135 a, err := apiClientFunc(baseRepo.RepoHost())
136 if err != nil {
137 return nil, nil, err
138 }
139 client = a.Lab()
140 }
142 issue, err := issueFromIID(client, baseRepo, issueIID)
143 return issue, baseRepo, err
146func issueMetadataFromURL(s, defaultHostname string, cfg config.Config) (int64, glrepo.Interface) {
147 u, err := url.Parse(s)
148 if err != nil || (u.Scheme != "https" && u.Scheme != "http") {
149 return 0, nil
150 }
152 issueIID, repo := cmdutils.ParseIssueFromURL(s, defaultHostname, cfg)
153 return int64(issueIID), repo
156func issueFromIID(apiClient *gitlab.Client, repo glrepo.Interface, issueIID int64) (*gitlab.Issue, error) {
157 return api.GetIssue(apiClient, repo.FullName(), issueIID)
Diagnostics8 git calls · 210 ms
CallStatusSizeTime0210 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 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
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=4
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=44
Open 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=5
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 · 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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200492 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=492
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=409e7038e253494f94691a22a250deee77ba03bf
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=492
git.pkt.payload.max=465
git.sideband.band1.bytes=465
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=465
git.pack.trailer=c8a67aa26d573734cf0c8f302f8c1dd08e47d76a
Open pack map
fetchtree:0200137 B · 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=137
git.response.cache=HIT
git.response.duration_ms=6
git.fetch.filter=tree:0
git.fetch.wants=1e3b6b810ea02e45ee91727cb626467da691d44a
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=137
git.pkt.payload.max=110
git.sideband.band1.bytes=110
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=110
git.pack.trailer=601fda9373d934d98367af3929640db9a95bed5a
Open pack map
fetch2001.5 KiB189 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=189
git.response.bytes=1537
git.response.cache=MISS
git.response.duration_ms=189
git.fetch.wants=d9839ace51d865ae46c50946899b3152d47e7c3a
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=1537
git.pkt.payload.max=1510
git.sideband.band1.bytes=1510
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
a5e215bb282b984a
Request
ac2c0fff…
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