MCPcopy
hub / github.com/cli/cli / setVariable

Function setVariable

pkg/cmd/variable/set/http.go:43–83  ·  view source on GitHub ↗
(client *api.Client, host string, opts setOptions)

Source from the content-addressed store, hash-verified

41}
42
43func setVariable(client *api.Client, host string, opts setOptions) setResult {
44 var err error
45 var postErr api.HTTPError
46 result := setResult{Operation: createdOperation, Key: opts.Key}
47 switch opts.Entity {
48 case shared.Organization:
49 if err = postOrgVariable(client, host, opts.Organization, opts.Visibility, opts.Key, opts.Value, opts.RepositoryIDs); err == nil {
50 return result
51 } else if errors.As(err, &postErr) && postErr.StatusCode == 409 {
52 // Server will return a 409 if variable already exists
53 result.Operation = updatedOperation
54 err = patchOrgVariable(client, host, opts.Organization, opts.Visibility, opts.Key, opts.Value, opts.RepositoryIDs)
55 }
56 case shared.Environment:
57 var ids []int64
58 ids, err = api.GetRepoIDs(client, opts.Repository.RepoHost(), []ghrepo.Interface{opts.Repository})
59 if err != nil || len(ids) != 1 {
60 err = fmt.Errorf("failed to look up repository %s: %w", ghrepo.FullName(opts.Repository), err)
61 break
62 }
63 if err = postEnvVariable(client, opts.Repository.RepoHost(), ids[0], opts.Environment, opts.Key, opts.Value); err == nil {
64 return result
65 } else if errors.As(err, &postErr) && postErr.StatusCode == 409 {
66 // Server will return a 409 if variable already exists
67 result.Operation = updatedOperation
68 err = patchEnvVariable(client, opts.Repository.RepoHost(), ids[0], opts.Environment, opts.Key, opts.Value)
69 }
70 default:
71 if err = postRepoVariable(client, opts.Repository, opts.Key, opts.Value); err == nil {
72 return result
73 } else if errors.As(err, &postErr) && postErr.StatusCode == 409 {
74 // Server will return a 409 if variable already exists
75 result.Operation = updatedOperation
76 err = patchRepoVariable(client, opts.Repository, opts.Key, opts.Value)
77 }
78 }
79 if err != nil {
80 result.Err = fmt.Errorf("failed to set variable %q: %w", opts.Key, err)
81 }
82 return result
83}
84
85func postVariable(client *api.Client, host, path string, payload interface{}) error {
86 payloadBytes, err := json.Marshal(payload)

Callers 1

setRunFunction · 0.85

Calls 10

GetRepoIDsFunction · 0.92
FullNameFunction · 0.92
postOrgVariableFunction · 0.85
patchOrgVariableFunction · 0.85
postEnvVariableFunction · 0.85
patchEnvVariableFunction · 0.85
postRepoVariableFunction · 0.85
patchRepoVariableFunction · 0.85
RepoHostMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected