(client *api.Client, host string, opts setOptions)
| 41 | } |
| 42 | |
| 43 | func 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 | |
| 85 | func postVariable(client *api.Client, host, path string, payload interface{}) error { |
| 86 | payloadBytes, err := json.Marshal(payload) |
no test coverage detected