GitLabgitlab-org
cli
main
1package get
3import (
4 "fmt"
5 "strconv"
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/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/ci/ciutils"
14 "gitlab.com/gitlab-org/cli/internal/iostreams"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16 "gitlab.com/gitlab-org/cli/internal/tableprinter"
17)
19const NoVariablesInPipelineMessage = "No variables found in pipeline."
21type PipelineMergedResponse struct {
22 *gitlab.Pipeline
23 Jobs []*gitlab.Job `json:"jobs"`
24 Variables []*gitlab.PipelineVariable `json:"variables"`
25}
27func NewCmdGet(f cmdutils.Factory) *cobra.Command {
28 pipelineGetCmd := &cobra.Command{
29 Use: "get [flags]",
30 Short: `Get the details of a CI/CD pipeline.`,
31 Aliases: []string{"stats"},
32 Long: heredoc.Docf(`
33 Defaults to the current branch. Use %[1]s--pipeline-id%[1]s to specify a pipeline
34 instead of fetching the latest for a branch.
36 Use %[1]s--merge-request%[1]s to target the head pipeline of a specific merge
37 request by IID. This differs from %[1]s--branch%[1]s when the MR's head pipeline
38 diverges from the latest pipeline on its source branch — for example, forks or
39 detached pipelines.
41 Use %[1]s--status%[1]s to filter jobs by state (passed through to the API's
42 %[1]sscope%[1]s parameter).
44 Use %[1]s--output json%[1]s to get the pipeline details as JSON.
45 `, "`"),
46 Example: heredoc.Doc(`
47 # Get the pipeline for the current branch
48 glab ci get
50 # Get a specific pipeline by ID in another project
51 glab ci get -R some/project -p 12345
53 # Show only failed jobs for the head pipeline of MR !42
54 glab ci get --merge-request=42 --status=failed --with-job-details`),
55 Args: cobra.NoArgs,
56 ValidArgsFunction: cobra.NoFileCompletions,
57 Annotations: map[string]string{
58 mcpannotations.Safe: "true",
59 },
60 RunE: func(cmd *cobra.Command, args []string) error {
61 var err error
63 client, err := f.GitLabClient()
64 if err != nil {
65 return err
66 }
68 repo, err := f.BaseRepo()
69 if err != nil {
70 return err
71 }
73 // Parse arguments into local vars
74 branch, _ := cmd.Flags().GetString("branch")
75 pipelineId, err := cmd.Flags().GetInt("pipeline-id")
76 if err != nil {
77 return err
78 }
79 mrIID, err := cmd.Flags().GetInt("merge-request")
80 if err != nil {
81 return err
82 }
84 var msgNotFound string
85 switch {
86 case pipelineId != 0:
87 msgNotFound = fmt.Sprintf("No pipeline with the given ID: %d", pipelineId)
88 case mrIID != 0:
89 mr, _, err := client.MergeRequests.GetMergeRequest(repo.FullName(), int64(mrIID), nil)
90 if err != nil {
91 return fmt.Errorf("failed to get merge request !%d: %w", mrIID, err)
92 }
93 if mr.HeadPipeline == nil || mr.HeadPipeline.ID == 0 {
94 return fmt.Errorf("no pipeline found for merge request !%d", mrIID)
95 }
96 pipelineId = int(mr.HeadPipeline.ID)
97 msgNotFound = fmt.Sprintf("No pipeline found for merge request !%d", mrIID)
98 default:
99 branch = ciutils.GetBranch(branch, func() (string, error) {
100 return f.Branch()
101 }, repo, client)
103 pipeline, err := ciutils.GetPipelineWithFallback(cmd.Context(), client, repo.FullName(), branch, f.IO())
104 if err != nil {
105 return err
106 }
107 pipelineId = int(pipeline.ID)
108 msgNotFound = fmt.Sprintf("No pipelines running or available on branch: %s", branch)
109 }
111 pipeline, _, err := client.Pipelines.GetPipeline(repo.FullName(), int64(pipelineId))
112 if err != nil {
113 return fmt.Errorf("%s: %w", msgNotFound, err)
114 }
116 statusFilter, _ := cmd.Flags().GetString("status")
117 listOpts := &gitlab.ListJobsOptions{ListOptions: gitlab.ListOptions{PerPage: 100}}
118 if statusFilter != "" {
119 listOpts.Scope = &[]gitlab.BuildStateValue{gitlab.BuildStateValue(statusFilter)}
120 }
122 jobs, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.Job, *gitlab.Response, error) {
123 return client.Jobs.ListPipelineJobs(repo.FullName(), int64(pipelineId), listOpts, p)
124 })
125 if err != nil {
126 return err
127 }
129 showVariables, _ := cmd.Flags().GetBool("with-variables")
131 var variables []*gitlab.PipelineVariable
132 if showVariables {
133 variables, _, err = client.Pipelines.GetPipelineVariables(pipeline.ProjectID, int64(pipelineId))
134 if err != nil {
135 return err
136 }
137 }
139 mergedPipelineObject := &PipelineMergedResponse{
140 Pipeline: pipeline,
141 Jobs: jobs,
142 Variables: variables,
143 }
145 outputFormat, _ := cmd.Flags().GetString("output-format")
146 output, _ := cmd.Flags().GetString("output")
147 if output == "json" || outputFormat == "json" {
148 return f.IO().PrintJSON(*mergedPipelineObject)
149 }
151 showJobDetails, _ := cmd.Flags().GetBool("with-job-details")
152 printTable(f.IO(), *mergedPipelineObject, showJobDetails)
153 return nil
154 },
155 }
157 fl := pipelineGetCmd.Flags()
158 fl.StringP("branch", "b", "", "Get the pipeline for a branch. Defaults to the current branch.")
159 fl.IntP("pipeline-id", "p", 0, "Get the pipeline with the given <id>.")
160 fl.Int("merge-request", 0, "Show the pipeline for the given merge request <iid>.")
161 pipelineGetCmd.MarkFlagsMutuallyExclusive("merge-request", "pipeline-id")
162 pipelineGetCmd.MarkFlagsMutuallyExclusive("merge-request", "branch")
163 fl.StringP("output", "F", "text", "Format output. Options: text, json.")
164 fl.StringP("output-format", "o", "text", "Use output.")
165 _ = fl.MarkHidden("output-format")
166 _ = fl.MarkDeprecated("output-format", "Deprecated. Use 'output' instead.")
167 fl.BoolP("with-job-details", "d", false, "Show extended job information.")
168 fl.Bool("with-variables", false, "Show variables in pipeline. Requires the Maintainer role.")
169 fl.StringP("status", "s", "", "Show only jobs in the given state. Passed through to the API's scope parameter.")
171 cmdutils.AddJQFlag(pipelineGetCmd, f.IO())
172 return pipelineGetCmd
173}
175func printTable(io *iostreams.IOStreams, p PipelineMergedResponse, showJobDetails bool) {
176 printPipelineTable(io, p)
178 if showJobDetails {
179 printJobTable(io, p)
180 } else {
181 printJobText(io, p)
182 }
184 printVariables(io, p)
185}
187func printPipelineTable(io *iostreams.IOStreams, p PipelineMergedResponse) {
188 io.LogInfof("%s", "# Pipeline:\n")
189 pipelineTable := tableprinter.NewTablePrinter()
190 pipelineTable.AddRow("id:", strconv.FormatInt(p.ID, 10))
191 pipelineTable.AddRow("status:", p.Status)
192 pipelineTable.AddRow("source:", p.Source)
193 pipelineTable.AddRow("ref:", p.Ref)
194 pipelineTable.AddRow("sha:", p.SHA)
195 pipelineTable.AddRow("tag:", p.Tag)
196 pipelineTable.AddRow("yaml Errors:", p.YamlErrors)
197 pipelineTable.AddRow("user:", p.User.Username)
198 pipelineTable.AddRow("created:", p.CreatedAt)
199 pipelineTable.AddRow("started:", p.StartedAt)
200 pipelineTable.AddRow("updated:", p.UpdatedAt)
201 io.LogInfo(pipelineTable.String())
202}
204func printJobTable(io *iostreams.IOStreams, p PipelineMergedResponse) {
205 io.LogInfof("%s", "# Jobs:\n")
206 jobTable := tableprinter.NewTablePrinter()
207 jobTable.AddRow("ID", "Name", "Stage", "Status", "Duration", "Failure reason", "URL")
208 for _, j := range p.Jobs {
209 jobTable.AddRow(j.ID, j.Name, j.Stage, j.Status, j.Duration, j.FailureReason, j.WebURL)
210 }
211 io.LogInfo(jobTable.String())
212}
214func printJobText(io *iostreams.IOStreams, p PipelineMergedResponse) {
215 io.LogInfof("%s", "# Jobs:\n")
216 jobTable := tableprinter.NewTablePrinter()
217 for _, j := range p.Jobs {
218 jobTable.AddRow(j.Name+":", j.Status)
219 }
220 io.LogInfo(jobTable.String())
221}
223func printVariables(io *iostreams.IOStreams, p PipelineMergedResponse) {
224 if p.Variables != nil {
225 io.LogInfof("%s", "# Variables:\n")
226 if len(p.Variables) == 0 {
227 io.LogInfof("%s", NoVariablesInPipelineMessage)
228 }
230 varTable := tableprinter.NewTablePrinter()
231 for _, v := range p.Variables {
232 varTable.AddRow(v.Key+":", v.Value)
233 }
234 io.LogInfo(varTable.String())
235 }
236}
Diagnostics8 git calls · 347 ms
CallStatusSizeTime0347 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=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 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 · hit3 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=3 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 · 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:0200635 B · hit3 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=635 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03 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=635 git.pkt.payload.max=608 git.sideband.band1.bytes=608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=608 git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200168 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=335 git.response.bytes=168 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=27ab403515eea5ea023c3d05cac03519511924a7 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=168 git.pkt.payload.max=141 git.sideband.band1.bytes=141 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=141 git.pack.trailer=7b9f6926f037354e3f8f733893a53ab7f0c64635Open pack map
fetch2002.6 KiB335 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2629 git.response.cache=MISS git.response.duration_ms=335 git.fetch.wants=e49a40f891bc18f88c5243990fc1e9c93f0aa503 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=2629 git.pkt.payload.max=2602 git.sideband.band1.bytes=2602 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
- a5e2cf3b6ce3efad
- Request
- 84b48455…
- 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