| 1 |
// Copyright 2023 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// ActionsVariable represents a repository action variable. |
| 14 |
type ActionsVariable struct { |
| 15 |
Name string `json:"name"` |
| 16 |
Value string `json:"value"` |
| 17 |
CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 18 |
UpdatedAt *Timestamp `json:"updated_at,omitempty"` |
| 19 |
Visibility *string `json:"visibility,omitempty"` |
| 20 |
// Used by ListOrgVariables and GetOrgVariables |
| 21 |
SelectedRepositoriesURL *string `json:"selected_repositories_url,omitempty"` |
| 22 |
// Used by UpdateOrgVariable and CreateOrgVariable |
| 23 |
SelectedRepositoryIDs *SelectedRepoIDs `json:"selected_repository_ids,omitempty"` |
| 24 |
} |
| 25 |
|
| 26 |
// ActionsVariables represents one item from the ListVariables response. |
| 27 |
type ActionsVariables struct { |
| 28 |
TotalCount int `json:"total_count"` |
| 29 |
Variables []*ActionsVariable `json:"variables"` |
| 30 |
} |
| 31 |
|
| 32 |
func (s *ActionsService) listVariables(ctx context.Context, url string, opts *ListOptions) (*ActionsVariables, *Response, error) { |
| 33 |
u, err := addOptions(url, opts) |
| 34 |
if err != nil { |
| 35 |
return nil, nil, err |
| 36 |
} |
| 37 |
|
| 38 |
req, err := s.client.NewRequest("GET", u, nil) |
| 39 |
if err != nil { |
| 40 |
return nil, nil, err |
| 41 |
} |
| 42 |
|
| 43 |
variables := new(ActionsVariables) |
| 44 |
resp, err := s.client.Do(ctx, req, &variables) |
| 45 |
if err != nil { |
| 46 |
return nil, resp, err |
| 47 |
} |
| 48 |
|
| 49 |
return variables, resp, nil |
| 50 |
} |
| 51 |
|
| 52 |
// ListRepoVariables lists all variables available in a repository. |
| 53 |
// |
| 54 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#list-repository-variables |
| 55 |
// |
| 56 |
//meta:operation GET /repos/{owner}/{repo}/actions/variables |
| 57 |
func (s *ActionsService) ListRepoVariables(ctx context.Context, owner, repo string, opts *ListOptions) (*ActionsVariables, *Response, error) { |
| 58 |
url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) |
| 59 |
return s.listVariables(ctx, url, opts) |
| 60 |
} |
| 61 |
|
| 62 |
// ListRepoOrgVariables lists all organization variables available in a repository. |
| 63 |
// |
| 64 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#list-repository-organization-variables |
| 65 |
// |
| 66 |
//meta:operation GET /repos/{owner}/{repo}/actions/organization-variables |
| 67 |
func (s *ActionsService) ListRepoOrgVariables(ctx context.Context, owner, repo string, opts *ListOptions) (*ActionsVariables, *Response, error) { |
| 68 |
url := fmt.Sprintf("repos/%v/%v/actions/organization-variables", owner, repo) |
| 69 |
return s.listVariables(ctx, url, opts) |
| 70 |
} |
| 71 |
|
| 72 |
// ListOrgVariables lists all variables available in an organization. |
| 73 |
// |
| 74 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#list-organization-variables |
| 75 |
// |
| 76 |
//meta:operation GET /orgs/{org}/actions/variables |
| 77 |
func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts *ListOptions) (*ActionsVariables, *Response, error) { |
| 78 |
url := fmt.Sprintf("orgs/%v/actions/variables", org) |
| 79 |
return s.listVariables(ctx, url, opts) |
| 80 |
} |
| 81 |
|
| 82 |
// ListEnvVariables lists all variables available in an environment. |
| 83 |
// |
| 84 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#list-environment-variables |
| 85 |
// |
| 86 |
//meta:operation GET /repositories/{repository_id}/environments/{environment_name}/variables |
| 87 |
func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env string, opts *ListOptions) (*ActionsVariables, *Response, error) { |
| 88 |
url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) |
| 89 |
return s.listVariables(ctx, url, opts) |
| 90 |
} |
| 91 |
|
| 92 |
func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionsVariable, *Response, error) { |
| 93 |
req, err := s.client.NewRequest("GET", url, nil) |
| 94 |
if err != nil { |
| 95 |
return nil, nil, err |
| 96 |
} |
| 97 |
|
| 98 |
variable := new(ActionsVariable) |
| 99 |
resp, err := s.client.Do(ctx, req, variable) |
| 100 |
if err != nil { |
| 101 |
return nil, resp, err |
| 102 |
} |
| 103 |
|
| 104 |
return variable, resp, nil |
| 105 |
} |
| 106 |
|
| 107 |
// GetRepoVariable gets a single repository variable. |
| 108 |
// |
| 109 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#get-a-repository-variable |
| 110 |
// |
| 111 |
//meta:operation GET /repos/{owner}/{repo}/actions/variables/{name} |
| 112 |
func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*ActionsVariable, *Response, error) { |
| 113 |
url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) |
| 114 |
return s.getVariable(ctx, url) |
| 115 |
} |
| 116 |
|
| 117 |
// GetOrgVariable gets a single organization variable. |
| 118 |
// |
| 119 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#get-an-organization-variable |
| 120 |
// |
| 121 |
//meta:operation GET /orgs/{org}/actions/variables/{name} |
| 122 |
func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*ActionsVariable, *Response, error) { |
| 123 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) |
| 124 |
return s.getVariable(ctx, url) |
| 125 |
} |
| 126 |
|
| 127 |
// GetEnvVariable gets a single environment variable. |
| 128 |
// |
| 129 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#get-an-environment-variable |
| 130 |
// |
| 131 |
//meta:operation GET /repositories/{repository_id}/environments/{environment_name}/variables/{name} |
| 132 |
func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*ActionsVariable, *Response, error) { |
| 133 |
url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) |
| 134 |
return s.getVariable(ctx, url) |
| 135 |
} |
| 136 |
|
| 137 |
func (s *ActionsService) postVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { |
| 138 |
req, err := s.client.NewRequest("POST", url, variable) |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
return s.client.Do(ctx, req, nil) |
| 143 |
} |
| 144 |
|
| 145 |
// CreateRepoVariable creates a repository variable. |
| 146 |
// |
| 147 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#create-a-repository-variable |
| 148 |
// |
| 149 |
//meta:operation POST /repos/{owner}/{repo}/actions/variables |
| 150 |
func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { |
| 151 |
url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) |
| 152 |
return s.postVariable(ctx, url, variable) |
| 153 |
} |
| 154 |
|
| 155 |
// CreateOrgVariable creates an organization variable. |
| 156 |
// |
| 157 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#create-an-organization-variable |
| 158 |
// |
| 159 |
//meta:operation POST /orgs/{org}/actions/variables |
| 160 |
func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { |
| 161 |
url := fmt.Sprintf("orgs/%v/actions/variables", org) |
| 162 |
return s.postVariable(ctx, url, variable) |
| 163 |
} |
| 164 |
|
| 165 |
// CreateEnvVariable creates an environment variable. |
| 166 |
// |
| 167 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#create-an-environment-variable |
| 168 |
// |
| 169 |
//meta:operation POST /repositories/{repository_id}/environments/{environment_name}/variables |
| 170 |
func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { |
| 171 |
url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) |
| 172 |
return s.postVariable(ctx, url, variable) |
| 173 |
} |
| 174 |
|
| 175 |
func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { |
| 176 |
req, err := s.client.NewRequest("PATCH", url, variable) |
| 177 |
if err != nil { |
| 178 |
return nil, err |
| 179 |
} |
| 180 |
return s.client.Do(ctx, req, nil) |
| 181 |
} |
| 182 |
|
| 183 |
// UpdateRepoVariable updates a repository variable. |
| 184 |
// |
| 185 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#update-a-repository-variable |
| 186 |
// |
| 187 |
//meta:operation PATCH /repos/{owner}/{repo}/actions/variables/{name} |
| 188 |
func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { |
| 189 |
url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name) |
| 190 |
return s.patchVariable(ctx, url, variable) |
| 191 |
} |
| 192 |
|
| 193 |
// UpdateOrgVariable updates an organization variable. |
| 194 |
// |
| 195 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#update-an-organization-variable |
| 196 |
// |
| 197 |
//meta:operation PATCH /orgs/{org}/actions/variables/{name} |
| 198 |
func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { |
| 199 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name) |
| 200 |
return s.patchVariable(ctx, url, variable) |
| 201 |
} |
| 202 |
|
| 203 |
// UpdateEnvVariable updates an environment variable. |
| 204 |
// |
| 205 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#update-an-environment-variable |
| 206 |
// |
| 207 |
//meta:operation PATCH /repositories/{repository_id}/environments/{environment_name}/variables/{name} |
| 208 |
func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { |
| 209 |
url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variable.Name) |
| 210 |
return s.patchVariable(ctx, url, variable) |
| 211 |
} |
| 212 |
|
| 213 |
func (s *ActionsService) deleteVariable(ctx context.Context, url string) (*Response, error) { |
| 214 |
req, err := s.client.NewRequest("DELETE", url, nil) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
|
| 219 |
return s.client.Do(ctx, req, nil) |
| 220 |
} |
| 221 |
|
| 222 |
// DeleteRepoVariable deletes a variable in a repository. |
| 223 |
// |
| 224 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#delete-a-repository-variable |
| 225 |
// |
| 226 |
//meta:operation DELETE /repos/{owner}/{repo}/actions/variables/{name} |
| 227 |
func (s *ActionsService) DeleteRepoVariable(ctx context.Context, owner, repo, name string) (*Response, error) { |
| 228 |
url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) |
| 229 |
return s.deleteVariable(ctx, url) |
| 230 |
} |
| 231 |
|
| 232 |
// DeleteOrgVariable deletes a variable in an organization. |
| 233 |
// |
| 234 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#delete-an-organization-variable |
| 235 |
// |
| 236 |
//meta:operation DELETE /orgs/{org}/actions/variables/{name} |
| 237 |
func (s *ActionsService) DeleteOrgVariable(ctx context.Context, org, name string) (*Response, error) { |
| 238 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) |
| 239 |
return s.deleteVariable(ctx, url) |
| 240 |
} |
| 241 |
|
| 242 |
// DeleteEnvVariable deletes a variable in an environment. |
| 243 |
// |
| 244 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#delete-an-environment-variable |
| 245 |
// |
| 246 |
//meta:operation DELETE /repositories/{repository_id}/environments/{environment_name}/variables/{name} |
| 247 |
func (s *ActionsService) DeleteEnvVariable(ctx context.Context, repoID int, env, variableName string) (*Response, error) { |
| 248 |
url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) |
| 249 |
return s.deleteVariable(ctx, url) |
| 250 |
} |
| 251 |
|
| 252 |
func (s *ActionsService) listSelectedReposForVariable(ctx context.Context, url string, opts *ListOptions) (*SelectedReposList, *Response, error) { |
| 253 |
u, err := addOptions(url, opts) |
| 254 |
if err != nil { |
| 255 |
return nil, nil, err |
| 256 |
} |
| 257 |
|
| 258 |
req, err := s.client.NewRequest("GET", u, nil) |
| 259 |
if err != nil { |
| 260 |
return nil, nil, err |
| 261 |
} |
| 262 |
|
| 263 |
result := new(SelectedReposList) |
| 264 |
resp, err := s.client.Do(ctx, req, result) |
| 265 |
if err != nil { |
| 266 |
return nil, resp, err |
| 267 |
} |
| 268 |
|
| 269 |
return result, resp, nil |
| 270 |
} |
| 271 |
|
| 272 |
// ListSelectedReposForOrgVariable lists all repositories that have access to a variable. |
| 273 |
// |
| 274 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#list-selected-repositories-for-an-organization-variable |
| 275 |
// |
| 276 |
//meta:operation GET /orgs/{org}/actions/variables/{name}/repositories |
| 277 |
func (s *ActionsService) ListSelectedReposForOrgVariable(ctx context.Context, org, name string, opts *ListOptions) (*SelectedReposList, *Response, error) { |
| 278 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) |
| 279 |
return s.listSelectedReposForVariable(ctx, url, opts) |
| 280 |
} |
| 281 |
|
| 282 |
func (s *ActionsService) setSelectedReposForVariable(ctx context.Context, url string, ids SelectedRepoIDs) (*Response, error) { |
| 283 |
type repoIDs struct { |
| 284 |
SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"` |
| 285 |
} |
| 286 |
|
| 287 |
req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids}) |
| 288 |
if err != nil { |
| 289 |
return nil, err |
| 290 |
} |
| 291 |
|
| 292 |
return s.client.Do(ctx, req, nil) |
| 293 |
} |
| 294 |
|
| 295 |
// SetSelectedReposForOrgVariable sets the repositories that have access to a variable. |
| 296 |
// |
| 297 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#set-selected-repositories-for-an-organization-variable |
| 298 |
// |
| 299 |
//meta:operation PUT /orgs/{org}/actions/variables/{name}/repositories |
| 300 |
func (s *ActionsService) SetSelectedReposForOrgVariable(ctx context.Context, org, name string, ids SelectedRepoIDs) (*Response, error) { |
| 301 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) |
| 302 |
return s.setSelectedReposForVariable(ctx, url, ids) |
| 303 |
} |
| 304 |
|
| 305 |
func (s *ActionsService) addSelectedRepoToVariable(ctx context.Context, url string) (*Response, error) { |
| 306 |
req, err := s.client.NewRequest("PUT", url, nil) |
| 307 |
if err != nil { |
| 308 |
return nil, err |
| 309 |
} |
| 310 |
|
| 311 |
return s.client.Do(ctx, req, nil) |
| 312 |
} |
| 313 |
|
| 314 |
// AddSelectedRepoToOrgVariable adds a repository to an organization variable. |
| 315 |
// |
| 316 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#add-selected-repository-to-an-organization-variable |
| 317 |
// |
| 318 |
//meta:operation PUT /orgs/{org}/actions/variables/{name}/repositories/{repository_id} |
| 319 |
func (s *ActionsService) AddSelectedRepoToOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { |
| 320 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) |
| 321 |
return s.addSelectedRepoToVariable(ctx, url) |
| 322 |
} |
| 323 |
|
| 324 |
func (s *ActionsService) removeSelectedRepoFromVariable(ctx context.Context, url string) (*Response, error) { |
| 325 |
req, err := s.client.NewRequest("DELETE", url, nil) |
| 326 |
if err != nil { |
| 327 |
return nil, err |
| 328 |
} |
| 329 |
|
| 330 |
return s.client.Do(ctx, req, nil) |
| 331 |
} |
| 332 |
|
| 333 |
// RemoveSelectedRepoFromOrgVariable removes a repository from an organization variable. |
| 334 |
// |
| 335 |
// GitHub API docs: https://docs.github.com/rest/actions/variables#remove-selected-repository-from-an-organization-variable |
| 336 |
// |
| 337 |
//meta:operation DELETE /orgs/{org}/actions/variables/{name}/repositories/{repository_id} |
| 338 |
func (s *ActionsService) RemoveSelectedRepoFromOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { |
| 339 |
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) |
| 340 |
return s.removeSelectedRepoFromVariable(ctx, url) |
| 341 |
} |