1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13
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
21 SelectedRepositoriesURL *string `json:"selected_repositories_url,omitempty"`
22
23 SelectedRepositoryIDs *SelectedRepoIDs `json:"selected_repository_ids,omitempty"`
24 }
25
26
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
53
54
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
61
62
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
69
70
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
92
93
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
100
101
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
108
109
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
124
125
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
132
133
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
140
141
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
156
157
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
164
165
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
172
173
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
189
190
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
197
198
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
205
206
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
233
234
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
254
255
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
271
272
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
288
289
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 }
294
View as plain text