Skip to content

Commit 72dd8ad

Browse files
authored
Add support for actions variables (#2652)
Fixes: #2624.
1 parent 8eac70f commit 72dd8ad

File tree

4 files changed

+1039
-0
lines changed

4 files changed

+1039
-0
lines changed

github/actions_variables.go

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

0 commit comments

Comments
 (0)