MCPcopy Create free account
hub / github.com/cli/cli / setVariable

Function setVariable

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

Source from the content-addressed store, hash-verified

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