github.com/google/go-github/v57@v57.0.0/github/actions_variables.go (about) 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 // ListOrgVariables lists all variables available in an organization. 63 // 64 // GitHub API docs: https://docs.github.com/rest/actions/variables#list-organization-variables 65 // 66 //meta:operation GET /orgs/{org}/actions/variables 67 func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts *ListOptions) (*ActionsVariables, *Response, error) { 68 url := fmt.Sprintf("orgs/%v/actions/variables", org) 69 return s.listVariables(ctx, url, opts) 70 } 71 72 // ListEnvVariables lists all variables available in an environment. 73 // 74 // GitHub API docs: https://docs.github.com/rest/actions/variables#list-environment-variables 75 // 76 //meta:operation GET /repositories/{repository_id}/environments/{environment_name}/variables 77 func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env string, opts *ListOptions) (*ActionsVariables, *Response, error) { 78 url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) 79 return s.listVariables(ctx, url, opts) 80 } 81 82 func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionsVariable, *Response, error) { 83 req, err := s.client.NewRequest("GET", url, nil) 84 if err != nil { 85 return nil, nil, err 86 } 87 88 variable := new(ActionsVariable) 89 resp, err := s.client.Do(ctx, req, variable) 90 if err != nil { 91 return nil, resp, err 92 } 93 94 return variable, resp, nil 95 } 96 97 // GetRepoVariable gets a single repository variable. 98 // 99 // GitHub API docs: https://docs.github.com/rest/actions/variables#get-a-repository-variable 100 // 101 //meta:operation GET /repos/{owner}/{repo}/actions/variables/{name} 102 func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*ActionsVariable, *Response, error) { 103 url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) 104 return s.getVariable(ctx, url) 105 } 106 107 // GetOrgVariable gets a single organization variable. 108 // 109 // GitHub API docs: https://docs.github.com/rest/actions/variables#get-an-organization-variable 110 // 111 //meta:operation GET /orgs/{org}/actions/variables/{name} 112 func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*ActionsVariable, *Response, error) { 113 url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) 114 return s.getVariable(ctx, url) 115 } 116 117 // GetEnvVariable gets a single environment variable. 118 // 119 // GitHub API docs: https://docs.github.com/rest/actions/variables#get-an-environment-variable 120 // 121 //meta:operation GET /repositories/{repository_id}/environments/{environment_name}/variables/{name} 122 func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*ActionsVariable, *Response, error) { 123 url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) 124 return s.getVariable(ctx, url) 125 } 126 127 func (s *ActionsService) postVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { 128 req, err := s.client.NewRequest("POST", url, variable) 129 if err != nil { 130 return nil, err 131 } 132 return s.client.Do(ctx, req, nil) 133 } 134 135 // CreateRepoVariable creates a repository variable. 136 // 137 // GitHub API docs: https://docs.github.com/rest/actions/variables#create-a-repository-variable 138 // 139 //meta:operation POST /repos/{owner}/{repo}/actions/variables 140 func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { 141 url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) 142 return s.postVariable(ctx, url, variable) 143 } 144 145 // CreateOrgVariable creates an organization variable. 146 // 147 // GitHub API docs: https://docs.github.com/rest/actions/variables#create-an-organization-variable 148 // 149 //meta:operation POST /orgs/{org}/actions/variables 150 func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { 151 url := fmt.Sprintf("orgs/%v/actions/variables", org) 152 return s.postVariable(ctx, url, variable) 153 } 154 155 // CreateEnvVariable creates an environment variable. 156 // 157 // GitHub API docs: https://docs.github.com/rest/actions/variables#create-an-environment-variable 158 // 159 //meta:operation POST /repositories/{repository_id}/environments/{environment_name}/variables 160 func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { 161 url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) 162 return s.postVariable(ctx, url, variable) 163 } 164 165 func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { 166 req, err := s.client.NewRequest("PATCH", url, variable) 167 if err != nil { 168 return nil, err 169 } 170 return s.client.Do(ctx, req, nil) 171 } 172 173 // UpdateRepoVariable updates a repository variable. 174 // 175 // GitHub API docs: https://docs.github.com/rest/actions/variables#update-a-repository-variable 176 // 177 //meta:operation PATCH /repos/{owner}/{repo}/actions/variables/{name} 178 func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { 179 url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name) 180 return s.patchVariable(ctx, url, variable) 181 } 182 183 // UpdateOrgVariable updates an organization variable. 184 // 185 // GitHub API docs: https://docs.github.com/rest/actions/variables#update-an-organization-variable 186 // 187 //meta:operation PATCH /orgs/{org}/actions/variables/{name} 188 func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { 189 url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name) 190 return s.patchVariable(ctx, url, variable) 191 } 192 193 // UpdateEnvVariable updates an environment variable. 194 // 195 // GitHub API docs: https://docs.github.com/rest/actions/variables#update-an-environment-variable 196 // 197 //meta:operation PATCH /repositories/{repository_id}/environments/{environment_name}/variables/{name} 198 func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { 199 url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variable.Name) 200 return s.patchVariable(ctx, url, variable) 201 } 202 203 func (s *ActionsService) deleteVariable(ctx context.Context, url string) (*Response, error) { 204 req, err := s.client.NewRequest("DELETE", url, nil) 205 if err != nil { 206 return nil, err 207 } 208 209 return s.client.Do(ctx, req, nil) 210 } 211 212 // DeleteRepoVariable deletes a variable in a repository. 213 // 214 // GitHub API docs: https://docs.github.com/rest/actions/variables#delete-a-repository-variable 215 // 216 //meta:operation DELETE /repos/{owner}/{repo}/actions/variables/{name} 217 func (s *ActionsService) DeleteRepoVariable(ctx context.Context, owner, repo, name string) (*Response, error) { 218 url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) 219 return s.deleteVariable(ctx, url) 220 } 221 222 // DeleteOrgVariable deletes a variable in an organization. 223 // 224 // GitHub API docs: https://docs.github.com/rest/actions/variables#delete-an-organization-variable 225 // 226 //meta:operation DELETE /orgs/{org}/actions/variables/{name} 227 func (s *ActionsService) DeleteOrgVariable(ctx context.Context, org, name string) (*Response, error) { 228 url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) 229 return s.deleteVariable(ctx, url) 230 } 231 232 // DeleteEnvVariable deletes a variable in an environment. 233 // 234 // GitHub API docs: https://docs.github.com/rest/actions/variables#delete-an-environment-variable 235 // 236 //meta:operation DELETE /repositories/{repository_id}/environments/{environment_name}/variables/{name} 237 func (s *ActionsService) DeleteEnvVariable(ctx context.Context, repoID int, env, variableName string) (*Response, error) { 238 url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) 239 return s.deleteVariable(ctx, url) 240 } 241 242 func (s *ActionsService) listSelectedReposForVariable(ctx context.Context, url string, opts *ListOptions) (*SelectedReposList, *Response, error) { 243 u, err := addOptions(url, opts) 244 if err != nil { 245 return nil, nil, err 246 } 247 248 req, err := s.client.NewRequest("GET", u, nil) 249 if err != nil { 250 return nil, nil, err 251 } 252 253 result := new(SelectedReposList) 254 resp, err := s.client.Do(ctx, req, result) 255 if err != nil { 256 return nil, resp, err 257 } 258 259 return result, resp, nil 260 } 261 262 // ListSelectedReposForOrgVariable lists all repositories that have access to a variable. 263 // 264 // GitHub API docs: https://docs.github.com/rest/actions/variables#list-selected-repositories-for-an-organization-variable 265 // 266 //meta:operation GET /orgs/{org}/actions/variables/{name}/repositories 267 func (s *ActionsService) ListSelectedReposForOrgVariable(ctx context.Context, org, name string, opts *ListOptions) (*SelectedReposList, *Response, error) { 268 url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) 269 return s.listSelectedReposForVariable(ctx, url, opts) 270 } 271 272 func (s *ActionsService) setSelectedReposForVariable(ctx context.Context, url string, ids SelectedRepoIDs) (*Response, error) { 273 type repoIDs struct { 274 SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"` 275 } 276 277 req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids}) 278 if err != nil { 279 return nil, err 280 } 281 282 return s.client.Do(ctx, req, nil) 283 } 284 285 // SetSelectedReposForOrgVariable sets the repositories that have access to a variable. 286 // 287 // GitHub API docs: https://docs.github.com/rest/actions/variables#set-selected-repositories-for-an-organization-variable 288 // 289 //meta:operation PUT /orgs/{org}/actions/variables/{name}/repositories 290 func (s *ActionsService) SetSelectedReposForOrgVariable(ctx context.Context, org, name string, ids SelectedRepoIDs) (*Response, error) { 291 url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) 292 return s.setSelectedReposForVariable(ctx, url, ids) 293 } 294 295 func (s *ActionsService) addSelectedRepoToVariable(ctx context.Context, url string) (*Response, error) { 296 req, err := s.client.NewRequest("PUT", url, nil) 297 if err != nil { 298 return nil, err 299 } 300 301 return s.client.Do(ctx, req, nil) 302 } 303 304 // AddSelectedRepoToOrgVariable adds a repository to an organization variable. 305 // 306 // GitHub API docs: https://docs.github.com/rest/actions/variables#add-selected-repository-to-an-organization-variable 307 // 308 //meta:operation PUT /orgs/{org}/actions/variables/{name}/repositories/{repository_id} 309 func (s *ActionsService) AddSelectedRepoToOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { 310 url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) 311 return s.addSelectedRepoToVariable(ctx, url) 312 } 313 314 func (s *ActionsService) removeSelectedRepoFromVariable(ctx context.Context, url string) (*Response, error) { 315 req, err := s.client.NewRequest("DELETE", url, nil) 316 if err != nil { 317 return nil, err 318 } 319 320 return s.client.Do(ctx, req, nil) 321 } 322 323 // RemoveSelectedRepoFromOrgVariable removes a repository from an organization variable. 324 // 325 // GitHub API docs: https://docs.github.com/rest/actions/variables#remove-selected-repository-from-an-organization-variable 326 // 327 //meta:operation DELETE /orgs/{org}/actions/variables/{name}/repositories/{repository_id} 328 func (s *ActionsService) RemoveSelectedRepoFromOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { 329 url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) 330 return s.removeSelectedRepoFromVariable(ctx, url) 331 }