GitLabgitlab-org

cli

main
ply9 git calls314 ms99 KiB7 cachedprotocol v2
143 lines4.8 KiBRaw
1// Package send implements `glab autoflow workflow send`.
2package send
4import (
5 "context"
6 "crypto/rand"
7 "errors"
8 "fmt"
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 autoflowv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/v1"
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"
24type options struct {
25 io *iostreams.IOStreams
26 apiClient func(repoHost string) (*api.Client, error)
28 conn autoflowutils.Connection
29 workflowToken *cmdutils.SecretFile
30 channelToken *cmdutils.SecretFile
31 value string
32 idempotencyKey string
34 parsedValue *autoflowv1.Value
37// NewCmd returns the `glab autoflow workflow send` 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: "send",
46 Short: "Send a value to a channel of an AutoFlow workflow. (EXPERIMENTAL)",
47 Long: heredoc.Docf(`
48 Send a value to a channel of a workflow on the AutoFlow service of GitLab Relay.
50 The channel token addresses the channel: %[1]sglab autoflow workflow start --channel%[1]s prints one for each channel it creates, and modules hand out more while the workflow runs. The workflow token of the same workflow authorizes the send. Pass each token in a file with %[1]s--channel-token-file%[1]s and %[1]s--workflow-token-file%[1]s, or in their environment variables.
52 %[1]s--value%[1]s is JSON, converted like the keyword arguments of %[1]sglab autoflow workflow start%[1]s. Sensitive values are not supported yet.
54 AutoFlow delivers a value at most once per idempotency key. To retry a send without delivering the value twice, pass the same %[1]s--idempotency-key%[1]s.
55 `, "`") + text.ExperimentalString,
56 Example: heredoc.Doc(`
57 # Send a string to a channel of a workflow on Runway staging
58 glab autoflow workflow send --hostname staging.gitlab.com \
59 --address staging.autoflow-gke.svc.gitlab.net:443 \
60 --workflow-token-file workflow-token.txt --channel-token-file inbox-token.txt \
61 --value '"hello"'
62 `),
63 Args: cobra.NoArgs,
64 ValidArgsFunction: cobra.NoFileCompletions,
65 Annotations: map[string]string{
66 mcpannotations.Exclude: "true",
67 },
68 RunE: func(cmd *cobra.Command, _ []string) error {
69 if err := opts.complete(); err != nil {
70 return err
71 }
72 if err := opts.validate(); err != nil {
73 return err
74 }
75 return opts.run(cmd.Context())
76 },
77 }
79 opts.conn.AddFlags(cmd)
80 opts.workflowToken = autoflowutils.NewWorkflowTokenFlag(cmd)
81 opts.channelToken = autoflowutils.NewChannelTokenFlag(cmd)
82 fl := cmd.Flags()
83 fl.StringVar(&opts.value, "value", "", "Value to send, as JSON.")
84 fl.StringVar(&opts.idempotencyKey, "idempotency-key", "", "Key that deduplicates deliveries to the workflow. Defaults to a random key.")
85 cobra.CheckErr(cmd.MarkFlagRequired("value"))
87 return cmd
90func (o *options) complete() error {
91 if err := o.conn.Complete(); err != nil {
92 return err
93 }
94 value, err := autoflow.ParseValue([]byte(o.value))
95 if err != nil {
96 return &cmdutils.FlagError{Err: fmt.Errorf("error parsing --value: %w", err)}
97 }
98 o.parsedValue = value
99 if o.idempotencyKey == "" {
100 o.idempotencyKey = "glab-" + rand.Text()
101 }
102 return nil
105func (o *options) validate() error {
106 if o.workflowToken.FromStdin() && o.channelToken.FromStdin() {
107 return &cmdutils.FlagError{Err: errors.New("only one of --workflow-token-file and --channel-token-file can read from standard input")}
108 }
109 return nil
112func (o *options) run(ctx context.Context) error {
113 workflowToken, err := o.workflowToken.Read(o.io.In)
114 if err != nil {
115 return err
116 }
117 channelToken, err := o.channelToken.Read(o.io.In)
118 if err != nil {
119 return err
120 }
122 client, conn, err := o.conn.Dial(ctx, o.io, o.apiClient)
123 if err != nil {
124 return err
125 }
126 defer conn.Close()
128 ctx, cancel := context.WithTimeout(ctx, autoflowutils.CallTimeout)
129 defer cancel()
130 _, err = client.SendToWorkflowChannel(ctx, &autoflowrpcv1.SendToWorkflowChannelRequest{
131 IdempotencyKey: o.idempotencyKey,
132 ChannelToken: channelToken,
133 Value: o.parsedValue,
134 WorkflowToken: &workflowToken,
135 NamespaceId: o.conn.NamespaceID[:],
136 })
137 if err != nil {
138 // AutoFlow may have delivered the value before the call failed.
139 return fmt.Errorf("failed to send the value (to retry without delivering it twice, pass --idempotency-key %s): %w", o.idempotencyKey, err)
140 }
141 o.io.LogInfof("Sent the value with idempotency key %s.\n", o.idempotencyKey)
142 return nil
Diagnostics9 git calls · 314 ms
CallStatusSizeTime0314 ms
discover200185 B2 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 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
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=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:0200173 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=173
git.response.cache=HIT
git.response.duration_ms=2
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=257bc2fe070f60e712099d7a0548db758c6c9692
Open pack map
fetchtree:0200227 B · 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=227
git.response.cache=HIT
git.response.duration_ms=2
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=19b86a461d3d30a55c16a56ca10faa7dc7d8b30d
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=301
git.response.bytes=136
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=47c4058afc901fedecae27c99fedc3c3d778ea59
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=2db11d9facdd0c0469edfbd7d98d4852bf607e09
Open pack map
fetch2001.9 KiB301 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1906
git.response.cache=MISS
git.response.duration_ms=301
git.fetch.wants=f6d9ec3f152be455ce776e60a62afa61d9c3a403
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=1906
git.pkt.payload.max=1879
git.sideband.band1.bytes=1879
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
a5e2d337def6cf3a
Request
c033eb1a…
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