GitLabgitlab-org

cli

main
ply8 git calls35 ms99 KiB7 cachedprotocol v2
148 lines4.5 KiBRaw
1package run_trig
3import (
4 "errors"
5 "fmt"
6 "os"
7 "strings"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/spf13/cobra"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/ci/ciutils"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19var envVariables = []string{}
21func parseVarArg(s string) (string, string, error) {
22 // From https://pkg.go.dev/strings#Split:
23 //
24 // > If s does not contain sep and sep is not empty,
25 // > Split returns a slice of length 1 whose only element is s.
26 //
27 // Therefore, the function will always return a slice of min length 1.
28 v := strings.SplitN(s, ":", 2)
29 if len(v) == 1 {
30 return "", "", fmt.Errorf("invalid argument structure")
31 }
32 key := strings.TrimSpace(v[0])
33 if key == "" {
34 return "", "", fmt.Errorf("variable key cannot be empty")
35 }
36 return key, v[1], nil
39func NewCmdRunTrig(f cmdutils.Factory) *cobra.Command {
40 pipelineRunCmd := &cobra.Command{
41 Use: "run-trig [flags]",
42 Short: `Run a CI/CD pipeline trigger.`,
43 Aliases: []string{},
44 Long: heredoc.Docf(`
45 Use %[1]s--token%[1]s to provide a pipeline trigger token. If omitted, the
46 %[1]sCI_JOB_TOKEN%[1]s environment variable must be set.
48 Use %[1]s--branch%[1]s to specify a branch or reference. Defaults to the current branch.
49 `, "`") + "\n" + cmdutils.PipelineInputsDescription,
50 Example: heredoc.Doc(`
51 glab ci run-trig -t xxxx
52 glab ci run-trig -t xxxx -b main
54 # Specify CI variables
55 glab ci run-trig -t xxxx -b main --variables key1:val1
56 glab ci run-trig -t xxxx -b main --variables key1:val1,key2:val2
57 glab ci run-trig -t xxxx -b main --variables key1:val1 --variables key2:val2
59 # Specify CI inputs
60 glab ci run-trig -t xxxx -b main --input key1:val1 --input key2:val2
61 glab ci run-trig -t xxxx -b main --input "replicas:int(3)" --input "debug:bool(false)" --input "regions:array(us-east,eu-west)"`),
62 Args: cobra.NoArgs,
63 ValidArgsFunction: cobra.NoFileCompletions,
64 Annotations: map[string]string{
65 mcpannotations.Destructive: "true",
66 },
67 RunE: func(cmd *cobra.Command, args []string) error {
68 var err error
70 client, err := f.GitLabClient()
71 if err != nil {
72 return err
73 }
75 repo, err := f.BaseRepo()
76 if err != nil {
77 return err
78 }
80 pipelineVariables := make(map[string]string)
82 if customPipelineVars, _ := cmd.Flags().GetStringSlice("variables"); len(customPipelineVars) > 0 {
83 for _, v := range customPipelineVars {
84 key, val, err := parseVarArg(v)
85 if err != nil {
86 return fmt.Errorf("parsing pipeline variable. Expected format KEY:VALUE: %w", err)
87 }
88 pipelineVariables[key] = val
89 }
90 }
92 pipelineInputs, err := cmdutils.PipelineInputsFromFlags(cmd)
93 if err != nil {
94 return err
95 }
97 c := &gitlab.RunPipelineTriggerOptions{
98 Inputs: pipelineInputs,
99 }
101 if len(pipelineVariables) != 0 {
102 c.Variables = pipelineVariables
103 }
105 branch, err := cmd.Flags().GetString("branch")
106 if err != nil {
107 return err
108 }
109 if branch != "" {
110 c.Ref = new(branch)
111 } else if currentBranch, err := f.Branch(); err == nil {
112 c.Ref = new(currentBranch)
113 } else {
114 // `ci run-trig` is running out of a git repo
115 f.IO().LogInfo("not in a Git repository. Using repository argument.")
116 c.Ref = new(ciutils.GetDefaultBranch(repo, client))
117 }
119 token, err := cmd.Flags().GetString("token")
120 if err != nil {
121 return err
122 }
123 if token == "" {
124 token = os.Getenv("CI_JOB_TOKEN")
125 }
126 if token == "" {
127 return errors.New("`--token` parameter can be omitted only if `CI_JOB_TOKEN` environment variable is set")
128 }
129 c.Token = new(token)
131 pipe, _, err := client.PipelineTriggers.RunPipelineTrigger(repo.FullName(), c)
132 if err != nil {
133 return err
134 }
136 output := fmt.Sprintf("Created pipeline (ID: %d), status: %s, ref: %s, weburl: %s", pipe.ID, pipe.Status, pipe.Ref, pipe.WebURL)
137 f.IO().LogInfo(output)
139 return nil
140 },
141 }
142 pipelineRunCmd.Flags().StringP("token", "t", "", "Pipeline trigger token. Can be omitted only if the 'CI_JOB_TOKEN' environment variable is set.")
143 pipelineRunCmd.Flags().StringP("branch", "b", "", "Create pipeline on branch or reference <string>.")
144 pipelineRunCmd.Flags().StringSliceVarP(&envVariables, "variables", "", []string{}, "Pass variables to pipeline in the format <key>:<value>. Multiple variables can be comma-separated or specified by repeating the flag.")
145 cmdutils.AddPipelineInputsFlag(pipelineRunCmd)
147 return pipelineRunCmd
Diagnostics8 git calls · 35 ms
CallStatusSizeTime035 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=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 · 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 · 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=3
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: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=3
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=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaa
Open pack map
fetchtree:0200140 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=20
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=162ed9061bb0c0014a90f249c2efe150cdfa6c89
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=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=704a25849d431d46eab038a54255e921b0401b4d
Open pack map
fetch2001.8 KiB · hit20 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1853
git.response.cache=HIT
git.response.duration_ms=20
git.fetch.wants=d8b69e34aef4a67e5fb246d66c1bfff77e75bb9d
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=1853
git.pkt.payload.max=1826
git.sideband.band1.bytes=1826
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
a5e2fe853f94f3c9
Request
003e7c73…
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