|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/sourcegraph/src-cli/internal/api" |
| 13 | + "github.com/sourcegraph/src-cli/internal/clicompat" |
| 14 | + "github.com/sourcegraph/src-cli/internal/cmderrors" |
| 15 | + "github.com/urfave/cli/v3" |
| 16 | +) |
| 17 | + |
| 18 | +const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkflowInstanceVariables( |
| 19 | + $instanceID: ID!, |
| 20 | + $variables: [AgenticWorkflowInstanceVariableInput!]!, |
| 21 | +) { |
| 22 | + updateAgenticWorkflowInstanceVariables(instanceID: $instanceID, variables: $variables) { |
| 23 | + id |
| 24 | + } |
| 25 | +}` |
| 26 | + |
| 27 | +var abcVariablesSetCommand = clicompat.Wrap(&cli.Command{ |
| 28 | + Name: "set", |
| 29 | + UsageText: "src abc variables set [options] <workflow-instance-id> [<name>=<value> ...]", |
| 30 | + Usage: "Set variables on a workflow instance", |
| 31 | + DisableSliceFlagSeparator: true, |
| 32 | + Description: ` |
| 33 | +Set workflow instance variables |
| 34 | +
|
| 35 | +Examples: |
| 36 | +
|
| 37 | + Set a string variable on a workflow instance: |
| 38 | +
|
| 39 | + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria" |
| 40 | +
|
| 41 | + Set multiple variables in one request: |
| 42 | +
|
| 43 | + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]' |
| 44 | +
|
| 45 | + Set a structured JSON value: |
| 46 | +
|
| 47 | + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]' |
| 48 | +
|
| 49 | +NOTE: Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings. |
| 50 | +`, |
| 51 | + Flags: clicompat.WithAPIFlags( |
| 52 | + &cli.StringSliceFlag{ |
| 53 | + Name: "var", |
| 54 | + Usage: "Variable assignment in <name>=<value> form. Repeat to set multiple variables.", |
| 55 | + }, |
| 56 | + ), |
| 57 | + Action: func(ctx context.Context, cmd *cli.Command) error { |
| 58 | + if !cmd.Args().Present() { |
| 59 | + return cmderrors.Usage("must provide a workflow instance ID") |
| 60 | + } |
| 61 | + |
| 62 | + instanceID := cmd.Args().First() |
| 63 | + client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer) |
| 64 | + abcVariables, err := parseABCVariables(cmd.Args().Tail(), cmd.StringSlice("var")) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return runABCVariablesSet(ctx, client, instanceID, abcVariables, cmd.Writer) |
| 69 | + }, |
| 70 | +}) |
| 71 | + |
| 72 | +func parseABCVariables(positional []string, flagged []string) (map[string]string, error) { |
| 73 | + rawVariables := append(positional, flagged...) |
| 74 | + if len(rawVariables) == 0 { |
| 75 | + return nil, cmderrors.Usage("must provide at least one variable assignment") |
| 76 | + } |
| 77 | + |
| 78 | + variables := make(map[string]string, len(rawVariables)) |
| 79 | + for _, v := range rawVariables { |
| 80 | + name, rawValue, ok := strings.Cut(v, "=") |
| 81 | + if !ok || name == "" { |
| 82 | + return nil, cmderrors.Usagef("invalid variable assignment %q: must be in <name>=<value> form", v) |
| 83 | + } |
| 84 | + |
| 85 | + value, remove, err := marshalABCVariableValue(rawValue) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + if remove { |
| 90 | + return nil, cmderrors.Usagef("invalid variable assignment %q: use 'src abc variables delete <workflow-instance-id> %s' to remove a variable", rawValue, name) |
| 91 | + } |
| 92 | + |
| 93 | + variables[name] = value |
| 94 | + } |
| 95 | + |
| 96 | + return variables, nil |
| 97 | +} |
| 98 | + |
| 99 | +func runABCVariablesSet(ctx context.Context, client api.Client, instanceID string, variables map[string]string, output io.Writer) error { |
| 100 | + graphqlVariables := make([]map[string]string, 0, len(variables)) |
| 101 | + keys := make([]string, 0, len(variables)) |
| 102 | + for k := range variables { |
| 103 | + keys = append(keys, k) |
| 104 | + } |
| 105 | + sort.Strings(keys) |
| 106 | + |
| 107 | + for _, k := range keys { |
| 108 | + graphqlVariables = append(graphqlVariables, map[string]string{ |
| 109 | + "key": k, |
| 110 | + "value": variables[k], |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + ok, err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables) |
| 115 | + if err != nil || !ok { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + _, err = fmt.Fprintf(output, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID) |
| 120 | + return err |
| 121 | +} |
| 122 | + |
| 123 | +func updateABCWorkflowInstanceVariables(ctx context.Context, client api.Client, instanceID string, variables []map[string]string) (bool, error) { |
| 124 | + var result struct { |
| 125 | + UpdateAgenticWorkflowInstanceVariables struct { |
| 126 | + ID string `json:"id"` |
| 127 | + } `json:"updateAgenticWorkflowInstanceVariables"` |
| 128 | + } |
| 129 | + if ok, err := client.NewRequest(updateABCWorkflowInstanceVariablesMutation, map[string]any{ |
| 130 | + "instanceID": instanceID, |
| 131 | + "variables": variables, |
| 132 | + }).Do(ctx, &result); err != nil || !ok { |
| 133 | + return ok, err |
| 134 | + } |
| 135 | + |
| 136 | + return true, nil |
| 137 | +} |
| 138 | + |
| 139 | +func marshalABCVariableValue(raw string) (value string, remove bool, err error) { |
| 140 | + // Try to compact valid JSON literals first so numbers, arrays, and objects are sent unchanged. |
| 141 | + // A bare null is detected separately so the CLI can require the explicit delete command. |
| 142 | + // If compacting doesn't work for the given value, fall back to string encoding. |
| 143 | + var compact bytes.Buffer |
| 144 | + if err := json.Compact(&compact, []byte(raw)); err == nil { |
| 145 | + value := compact.String() |
| 146 | + return value, value == "null", nil |
| 147 | + } |
| 148 | + |
| 149 | + encoded, err := json.Marshal(raw) |
| 150 | + if err != nil { |
| 151 | + return "", false, err |
| 152 | + } |
| 153 | + |
| 154 | + return string(encoded), false, nil |
| 155 | +} |
0 commit comments