GitLabgitlab-org

cli

main
ply8 git calls183 ms99 KiB6 cachedprotocol v2
182 lines4.4 KiBRaw
1package jobs
3import (
4 "context"
5 "strings"
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/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/glrepo"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/tableprinter"
18 "gitlab.com/gitlab-org/cli/internal/utils"
21type options struct {
22 io *iostreams.IOStreams
23 baseRepo func() (glrepo.Interface, error)
24 apiClient func(repoHost string) (*api.Client, error)
25 runnerID int64
26 status string
27 orderBy string
28 sort string
29 page int64
30 perPage int64
31 outputFormat string
34func NewCmd(f cmdutils.Factory) *cobra.Command {
35 opts := &options{
36 io: f.IO(),
37 baseRepo: f.BaseRepo,
38 apiClient: f.ApiClient,
39 }
41 cmd := &cobra.Command{
42 Use: "jobs <runner-id> [flags]",
43 Short: "List jobs processed by a runner.",
44 Long: heredoc.Doc(`
45 Lists jobs processed by the specified runner, including jobs that are currently running.
47 Requires the Maintainer or Owner role for the project.
48 `),
49 Example: heredoc.Doc(`
50 # List all jobs for runner 9
51 glab runner jobs 9
53 # List only running jobs
54 glab runner jobs 9 --status running
56 # List jobs as JSON
57 glab runner jobs 9 --output json`),
58 Args: cobra.ExactArgs(1),
59 Annotations: map[string]string{
60 mcpannotations.Safe: "true",
61 },
62 RunE: func(cmd *cobra.Command, args []string) error {
63 if err := opts.complete(args); err != nil {
64 return err
65 }
66 return opts.run(cmd.Context())
67 },
68 }
70 fl := cmd.Flags()
71 fl.StringVar(&opts.status, "status", "", "Filter jobs by status: running, success, failed, canceled.")
72 fl.StringVar(&opts.orderBy, "order-by", "id", "Order jobs by: id.")
73 fl.StringVar(&opts.sort, "sort", "desc", "Sort order: asc or desc.")
74 fl.Int64VarP(&opts.page, "page", "p", 1, "Page number.")
75 fl.Int64VarP(&opts.perPage, "per-page", "P", api.DefaultListLimit, "Number of items to list per page.")
77 cmdutils.EnableRepoOverride(cmd, f)
78 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
80 return cmd
83func (o *options) complete(args []string) error {
84 id, err := cmdutils.ParseID(args[0], "runner ID")
85 if err != nil {
86 return err
87 }
88 o.runnerID = id
89 return nil
92func (o *options) run(ctx context.Context) error {
93 var repoHost string
94 if repo, err := o.baseRepo(); err == nil {
95 repoHost = repo.RepoHost()
96 }
98 apiClient, err := o.apiClient(repoHost)
99 if err != nil {
100 return err
101 }
102 client := apiClient.Lab()
104 listOpts := &gitlab.ListRunnerJobsOptions{
105 ListOptions: gitlab.ListOptions{
106 Page: o.page,
107 PerPage: o.perPage,
108 },
109 }
110 if o.status != "" {
111 listOpts.Status = new(strings.ToLower(o.status))
112 }
113 if o.orderBy != "" {
114 listOpts.OrderBy = new(o.orderBy)
115 }
116 if o.sort != "" {
117 listOpts.Sort = new(strings.ToLower(o.sort))
118 }
120 jobs, resp, err := client.Runners.ListRunnerJobs(o.runnerID, listOpts, gitlab.WithContext(ctx))
121 if err != nil {
122 return cmdutils.WrapError(err, "failed to list runner jobs")
123 }
125 switch o.outputFormat {
126 case "json":
127 return o.io.PrintJSON(jobs)
128 default:
129 return o.printTable(jobs, resp)
130 }
133func (o *options) printTable(jobs []*gitlab.Job, resp *gitlab.Response) error {
134 title := utils.NewListTitle("job")
135 title.Page = int(o.page)
136 title.CurrentPageTotal = len(jobs)
137 if resp != nil {
138 title.Total = int(resp.TotalItems)
139 }
141 if err := o.io.StartPager(); err != nil {
142 return err
143 }
144 defer o.io.StopPager()
146 o.io.LogInfof("%s\n%s\n", title.Describe(), displayJobs(o.io, jobs))
147 return nil
150func displayJobs(io *iostreams.IOStreams, jobs []*gitlab.Job) string {
151 c := io.Color()
152 table := tableprinter.NewTablePrinter()
153 table.AddRow(c.Bold("ID"), c.Bold("Status"), c.Bold("Stage"), c.Bold("Name"), c.Bold("Ref"), c.Bold("Project"))
154 for _, j := range jobs {
155 projectPath := ""
156 if j.Project != nil {
157 projectPath = j.Project.PathWithNamespace
158 }
159 table.AddRow(
160 j.ID,
161 formatJobStatus(c, j.Status),
162 j.Stage,
163 j.Name,
164 j.Ref,
165 projectPath,
166 )
167 }
168 return table.Render()
171func formatJobStatus(c *iostreams.ColorPalette, status string) string {
172 switch strings.ToLower(status) {
173 case "success":
174 return c.Green(status)
175 case "failed", "canceled":
176 return c.Red(status)
177 case "running":
178 return c.Blue(status)
179 default:
180 return status
181 }
Diagnostics8 git calls · 183 ms
CallStatusSizeTime0183 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
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=5
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: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=2
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200314 B · 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=314
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=9182edcf49c51a2d4d26f5f18ad76a93a377699f
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=314
git.pkt.payload.max=287
git.sideband.band1.bytes=287
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=287
git.pack.trailer=d25ce511fa222aedcda012c18364dcb1f012062b
Open pack map
fetchtree:0200136 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=136
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=38f7f95c04f88f1ad745ad11891ebca6c1bde10b
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=136
git.pkt.payload.max=109
git.sideband.band1.bytes=109
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=109
git.pack.trailer=84f92e84cf8c6917e7451046591f286215fca2fb
Open pack map
fetch2001.8 KiB171 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=171
git.response.bytes=1825
git.response.cache=MISS
git.response.duration_ms=171
git.fetch.wants=8eddbda63bcf952b5e32707b83a74dbe3db98924
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=1825
git.pkt.payload.max=1798
git.sideband.band1.bytes=1798
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
a5e34b63097e8a31
Request
94b1ad7e…
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