1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "net/url"
13 )
14
15
16 type WorkflowRun struct {
17 ID *int64 `json:"id,omitempty"`
18 Name *string `json:"name,omitempty"`
19 NodeID *string `json:"node_id,omitempty"`
20 HeadBranch *string `json:"head_branch,omitempty"`
21 HeadSHA *string `json:"head_sha,omitempty"`
22 RunNumber *int `json:"run_number,omitempty"`
23 RunAttempt *int `json:"run_attempt,omitempty"`
24 Event *string `json:"event,omitempty"`
25 DisplayTitle *string `json:"display_title,omitempty"`
26 Status *string `json:"status,omitempty"`
27 Conclusion *string `json:"conclusion,omitempty"`
28 WorkflowID *int64 `json:"workflow_id,omitempty"`
29 CheckSuiteID *int64 `json:"check_suite_id,omitempty"`
30 CheckSuiteNodeID *string `json:"check_suite_node_id,omitempty"`
31 URL *string `json:"url,omitempty"`
32 HTMLURL *string `json:"html_url,omitempty"`
33 PullRequests []*PullRequest `json:"pull_requests,omitempty"`
34 CreatedAt *Timestamp `json:"created_at,omitempty"`
35 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
36 RunStartedAt *Timestamp `json:"run_started_at,omitempty"`
37 JobsURL *string `json:"jobs_url,omitempty"`
38 LogsURL *string `json:"logs_url,omitempty"`
39 CheckSuiteURL *string `json:"check_suite_url,omitempty"`
40 ArtifactsURL *string `json:"artifacts_url,omitempty"`
41 CancelURL *string `json:"cancel_url,omitempty"`
42 RerunURL *string `json:"rerun_url,omitempty"`
43 PreviousAttemptURL *string `json:"previous_attempt_url,omitempty"`
44 HeadCommit *HeadCommit `json:"head_commit,omitempty"`
45 WorkflowURL *string `json:"workflow_url,omitempty"`
46 Repository *Repository `json:"repository,omitempty"`
47 HeadRepository *Repository `json:"head_repository,omitempty"`
48 Actor *User `json:"actor,omitempty"`
49 TriggeringActor *User `json:"triggering_actor,omitempty"`
50 }
51
52
53 type WorkflowRuns struct {
54 TotalCount *int `json:"total_count,omitempty"`
55 WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
56 }
57
58
59 type ListWorkflowRunsOptions struct {
60 Actor string `url:"actor,omitempty"`
61 Branch string `url:"branch,omitempty"`
62 Event string `url:"event,omitempty"`
63 Status string `url:"status,omitempty"`
64 Created string `url:"created,omitempty"`
65 HeadSHA string `url:"head_sha,omitempty"`
66 ExcludePullRequests bool `url:"exclude_pull_requests,omitempty"`
67 CheckSuiteID int64 `url:"check_suite_id,omitempty"`
68 ListOptions
69 }
70
71
72 type WorkflowRunUsage struct {
73 Billable *WorkflowRunBillMap `json:"billable,omitempty"`
74 RunDurationMS *int64 `json:"run_duration_ms,omitempty"`
75 }
76
77
78
79 type WorkflowRunBillMap map[string]*WorkflowRunBill
80
81
82 type WorkflowRunBill struct {
83 TotalMS *int64 `json:"total_ms,omitempty"`
84 Jobs *int `json:"jobs,omitempty"`
85 JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"`
86 }
87
88
89 type WorkflowRunJobRun struct {
90 JobID *int `json:"job_id,omitempty"`
91 DurationMS *int64 `json:"duration_ms,omitempty"`
92 }
93
94
95 type WorkflowRunAttemptOptions struct {
96 ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"`
97 }
98
99
100 type PendingDeploymentsRequest struct {
101 EnvironmentIDs []int64 `json:"environment_ids"`
102
103 State string `json:"state"`
104 Comment string `json:"comment"`
105 }
106
107 func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
108 u, err := addOptions(endpoint, opts)
109 if err != nil {
110 return nil, nil, err
111 }
112
113 req, err := s.client.NewRequest("GET", u, nil)
114 if err != nil {
115 return nil, nil, err
116 }
117
118 runs := new(WorkflowRuns)
119 resp, err := s.client.Do(ctx, req, &runs)
120 if err != nil {
121 return nil, resp, err
122 }
123
124 return runs, resp, nil
125 }
126
127
128
129
130 func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
131 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
132 return s.listWorkflowRuns(ctx, u, opts)
133 }
134
135
136
137
138 func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
139 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
140 return s.listWorkflowRuns(ctx, u, opts)
141 }
142
143
144
145
146 func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
147 u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
148 u, err := addOptions(u, opts)
149 if err != nil {
150 return nil, nil, err
151 }
152
153 req, err := s.client.NewRequest("GET", u, nil)
154 if err != nil {
155 return nil, nil, err
156 }
157
158 runs := new(WorkflowRuns)
159 resp, err := s.client.Do(ctx, req, &runs)
160 if err != nil {
161 return nil, resp, err
162 }
163
164 return runs, resp, nil
165 }
166
167
168
169
170 func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
171 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
172
173 req, err := s.client.NewRequest("GET", u, nil)
174 if err != nil {
175 return nil, nil, err
176 }
177
178 run := new(WorkflowRun)
179 resp, err := s.client.Do(ctx, req, run)
180 if err != nil {
181 return nil, resp, err
182 }
183
184 return run, resp, nil
185 }
186
187
188
189
190 func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
191 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
192 u, err := addOptions(u, opts)
193 if err != nil {
194 return nil, nil, err
195 }
196
197 req, err := s.client.NewRequest("GET", u, nil)
198 if err != nil {
199 return nil, nil, err
200 }
201
202 run := new(WorkflowRun)
203 resp, err := s.client.Do(ctx, req, run)
204 if err != nil {
205 return nil, resp, err
206 }
207
208 return run, resp, nil
209 }
210
211
212
213
214 func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, followRedirects bool) (*url.URL, *Response, error) {
215 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber)
216
217 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
218 if err != nil {
219 return nil, nil, err
220 }
221 defer resp.Body.Close()
222
223 if resp.StatusCode != http.StatusFound {
224 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
225 }
226
227 parsedURL, err := url.Parse(resp.Header.Get("Location"))
228 return parsedURL, newResponse(resp), err
229 }
230
231
232
233
234 func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
235 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
236
237 req, err := s.client.NewRequest("POST", u, nil)
238 if err != nil {
239 return nil, err
240 }
241
242 return s.client.Do(ctx, req, nil)
243 }
244
245
246
247
248 func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
249 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID)
250
251 req, err := s.client.NewRequest("POST", u, nil)
252 if err != nil {
253 return nil, err
254 }
255
256 return s.client.Do(ctx, req, nil)
257 }
258
259
260
261
262 func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) {
263 u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID)
264
265 req, err := s.client.NewRequest("POST", u, nil)
266 if err != nil {
267 return nil, err
268 }
269
270 return s.client.Do(ctx, req, nil)
271 }
272
273
274
275
276 func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
277 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
278
279 req, err := s.client.NewRequest("POST", u, 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) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
291 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
292
293 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
294 if err != nil {
295 return nil, nil, err
296 }
297 defer resp.Body.Close()
298
299 if resp.StatusCode != http.StatusFound {
300 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
301 }
302
303 parsedURL, err := url.Parse(resp.Header.Get("Location"))
304 return parsedURL, newResponse(resp), err
305 }
306
307
308
309
310 func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
311 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
312
313 req, err := s.client.NewRequest("DELETE", u, nil)
314 if err != nil {
315 return nil, err
316 }
317
318 return s.client.Do(ctx, req, nil)
319 }
320
321
322
323
324 func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
325 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
326
327 req, err := s.client.NewRequest("DELETE", u, nil)
328 if err != nil {
329 return nil, err
330 }
331
332 return s.client.Do(ctx, req, nil)
333 }
334
335
336
337
338 func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
339 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
340
341 req, err := s.client.NewRequest("GET", u, nil)
342 if err != nil {
343 return nil, nil, err
344 }
345
346 workflowRunUsage := new(WorkflowRunUsage)
347 resp, err := s.client.Do(ctx, req, workflowRunUsage)
348 if err != nil {
349 return nil, resp, err
350 }
351
352 return workflowRunUsage, resp, nil
353 }
354
355
356
357
358 func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) {
359 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID)
360
361 req, err := s.client.NewRequest("POST", u, request)
362 if err != nil {
363 return nil, nil, err
364 }
365
366 var deployments []*Deployment
367 resp, err := s.client.Do(ctx, req, &deployments)
368 if err != nil {
369 return nil, resp, err
370 }
371
372 return deployments, resp, nil
373 }
374
View as plain text