GitLabgitlab-org
cli
main
1// Package get implements `glab autoflow workflow get`.
2package get
4import (
5 "context"
6 "encoding/json"
7 "fmt"
8 "time"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
12 autoflowrpcv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/rpc/v1"
13 "google.golang.org/protobuf/types/known/timestamppb"
15 "gitlab.com/gitlab-org/cli/internal/api"
16 "gitlab.com/gitlab-org/cli/internal/api/autoflow"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/commands/autoflow/autoflowutils"
19 "gitlab.com/gitlab-org/cli/internal/iostreams"
20 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21 "gitlab.com/gitlab-org/cli/internal/text"
22)
24type options struct {
25 io *iostreams.IOStreams
26 apiClient func(repoHost string) (*api.Client, error)
28 conn autoflowutils.Connection
29 workflowToken *cmdutils.SecretFile
30 wait autoflowutils.Wait
31 valueFormat string
32 outputFormat string
34 workflowKey string
35}
37// NewCmd returns the `glab autoflow workflow get` command.
38func NewCmd(f cmdutils.Factory) *cobra.Command {
39 opts := &options{
40 io: f.IO(),
41 apiClient: f.ApiClient,
42 }
44 cmd := &cobra.Command{
45 Use: "get <workflow-key>",
46 Short: "Get the state and result of an AutoFlow workflow. (EXPERIMENTAL)",
47 Long: heredoc.Docf(`
48 Get the state of a workflow on the AutoFlow service of GitLab Relay and, once it has finished, its result.
50 The command needs the workflow token that %[1]sglab autoflow workflow start%[1]s printed. Pass it in a file with %[1]s--workflow-token-file%[1]s or in its environment variable.
52 With %[1]s--wait%[1]s, the command polls until the workflow has finished and exits with an error unless it completed.
54 The result value is printed as JSON: %[1]sNone%[1]s becomes %[1]snull%[1]s, an integer or a float a number, a list an array and a dict with string keys an object. A value without a JSON equivalent, such as a tuple, bytes or a channel, is printed in the protobuf JSON form of %[1]sgitlab.autoflow.v1.Value%[1]s. %[1]s--value-format protojson%[1]s prints the whole value in that form.
55 `, "`") + text.ExperimentalString,
56 Example: heredoc.Doc(`
57 # Wait until a workflow on Runway staging has finished
58 glab autoflow workflow get "$WORKFLOW_KEY" --hostname staging.gitlab.com \
59 --address staging.autoflow-gke.svc.gitlab.net:443 \
60 --workflow-token-file workflow-token.txt --wait
61 `),
62 Args: cobra.ExactArgs(1),
63 ValidArgsFunction: cobra.NoFileCompletions,
64 Annotations: map[string]string{
65 mcpannotations.Exclude: "true",
66 },
67 RunE: func(cmd *cobra.Command, args []string) error {
68 if err := opts.complete(args); err != nil {
69 return err
70 }
71 if err := opts.validate(); err != nil {
72 return err
73 }
74 return opts.run(cmd.Context())
75 },
76 }
78 opts.conn.AddFlags(cmd)
79 opts.workflowToken = autoflowutils.NewWorkflowTokenFlag(cmd)
80 opts.wait.AddFlags(cmd)
81 cmd.Flags().Var(
82 cmdutils.NewEnumValue([]string{"json", "protojson"}, "json", &opts.valueFormat),
83 "value-format", "Format of the result value: json, protojson.",
84 )
85 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
87 return cmd
88}
90func (o *options) complete(args []string) error {
91 o.workflowKey = args[0]
92 return o.conn.Complete()
93}
95func (o *options) validate() error {
96 return o.wait.Validate()
97}
99func (o *options) run(ctx context.Context) error {
100 token, err := o.workflowToken.Read(o.io.In)
101 if err != nil {
102 return err
103 }
105 client, conn, err := o.conn.Dial(ctx, o.io, o.apiClient)
106 if err != nil {
107 return err
108 }
109 defer conn.Close()
111 req := &autoflowrpcv1.GetWorkflowRequest{
112 WorkflowKey: o.workflowKey,
113 WorkflowToken: &token,
114 NamespaceId: o.conn.NamespaceID[:],
115 }
116 var resp *autoflowrpcv1.GetWorkflowResponse
117 if o.wait.Enabled {
118 resp, err = o.wait.Until(ctx, client, req)
119 } else {
120 resp, err = autoflowutils.GetWorkflow(ctx, client, req)
121 }
122 if err != nil {
123 return err
124 }
126 if err := o.print(resp); err != nil {
127 return err
128 }
129 if o.wait.Enabled && resp.GetState() != autoflowrpcv1.WorkflowState_WORKFLOW_STATE_COMPLETED {
130 return fmt.Errorf("workflow finished in state %s", autoflowutils.State(resp.GetState()))
131 }
132 return nil
133}
135type workflow struct {
136 WorkflowKey string `json:"workflow_key"`
137 Name string `json:"name"`
138 IdempotencyKey string `json:"idempotency_key"`
139 State string `json:"state"`
140 CreatedAt *time.Time `json:"created_at,omitempty"`
141 UpdatedAt *time.Time `json:"updated_at,omitempty"`
142 Value json.RawMessage `json:"value,omitempty"`
143 Error string `json:"error,omitempty"`
144}
146func (o *options) print(resp *autoflowrpcv1.GetWorkflowResponse) error {
147 w := workflow{
148 WorkflowKey: resp.GetWorkflowKey(),
149 Name: resp.GetName(),
150 IdempotencyKey: resp.GetIdempotencyKey(),
151 State: autoflowutils.State(resp.GetState()),
152 CreatedAt: timestamp(resp.GetCreatedAt()),
153 UpdatedAt: timestamp(resp.GetUpdatedAt()),
154 Error: resp.GetResult().GetError().GetMessage(),
155 }
156 if v := resp.GetResult().GetValue(); v != nil {
157 marshal := autoflow.MarshalValue
158 if o.valueFormat == "protojson" {
159 marshal = autoflow.MarshalProtoValue
160 }
161 b, err := marshal(v)
162 if err != nil {
163 return fmt.Errorf("failed to encode the workflow result: %w", err)
164 }
165 w.Value = b
166 }
168 switch o.outputFormat {
169 case "json":
170 return o.io.PrintJSON(w)
171 default:
172 o.printText(w)
173 return nil
174 }
175}
177func (o *options) printText(w workflow) {
178 o.io.LogInfof("workflow_key: %s\nname: %s\nidempotency_key: %s\nstate: %s\n", w.WorkflowKey, w.Name, w.IdempotencyKey, w.State)
179 if w.CreatedAt != nil {
180 o.io.LogInfof("created_at: %s\n", w.CreatedAt.Format(time.RFC3339Nano))
181 }
182 if w.UpdatedAt != nil {
183 o.io.LogInfof("updated_at: %s\n", w.UpdatedAt.Format(time.RFC3339Nano))
184 }
185 if w.Value != nil {
186 o.io.LogInfof("value: %s\n", w.Value)
187 }
188 if w.Error != "" {
189 o.io.LogInfof("error: %s\n", w.Error)
190 }
191}
193func timestamp(ts *timestamppb.Timestamp) *time.Time {
194 if ts == nil {
195 return nil
196 }
197 t := ts.AsTime()
198 return &t
199}
Diagnostics9 git calls · 237 ms
CallStatusSizeTime0237 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=2 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=44Open pack map
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=4 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:02001.8 KiB · hit4 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=4 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:0200173 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=173 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=9842465ba0bef0fe35148002069a98d0492349fa 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=173 git.pkt.payload.max=146 git.sideband.band1.bytes=146 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=146 git.pack.trailer=257bc2fe070f60e712099d7a0548db758c6c9692Open pack map
fetchtree:0200227 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=227 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=d9a7c66bd00e9fc74c41d266132b2171a7a82cdd 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=227 git.pkt.payload.max=200 git.sideband.band1.bytes=200 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=200 git.pack.trailer=19b86a461d3d30a55c16a56ca10faa7dc7d8b30dOpen pack map
fetchtree:0200134 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=220 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=e699c957590e8574d1181448e231e5ffdf182f2e 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=5c9b6f12321aeb06c6347ee48ab720cceaff3c5aOpen pack map
fetch2002.3 KiB220 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2325 git.response.cache=MISS git.response.duration_ms=220 git.fetch.wants=5f18de15fb52df811dcf1383aa2412a5993abb1b 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=2325 git.pkt.payload.max=2298 git.sideband.band1.bytes=2298 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
- a5e2e31dff3e4af4
- Request
- 47cd411c…
- 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