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 NodeID *string `json:"node_id,omitempty"`
19 HeadBranch *string `json:"head_branch,omitempty"`
20 HeadSHA *string `json:"head_sha,omitempty"`
21 RunNumber *int `json:"run_number,omitempty"`
22 Event *string `json:"event,omitempty"`
23 Status *string `json:"status,omitempty"`
24 Conclusion *string `json:"conclusion,omitempty"`
25 WorkflowID *int64 `json:"workflow_id,omitempty"`
26 URL *string `json:"url,omitempty"`
27 HTMLURL *string `json:"html_url,omitempty"`
28 PullRequests []*PullRequest `json:"pull_requests,omitempty"`
29 CreatedAt *Timestamp `json:"created_at,omitempty"`
30 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
31 JobsURL *string `json:"jobs_url,omitempty"`
32 LogsURL *string `json:"logs_url,omitempty"`
33 CheckSuiteURL *string `json:"check_suite_url,omitempty"`
34 ArtifactsURL *string `json:"artifacts_url,omitempty"`
35 CancelURL *string `json:"cancel_url,omitempty"`
36 RerunURL *string `json:"rerun_url,omitempty"`
37 HeadCommit *HeadCommit `json:"head_commit,omitempty"`
38 WorkflowURL *string `json:"workflow_url,omitempty"`
39 Repository *Repository `json:"repository,omitempty"`
40 HeadRepository *Repository `json:"head_repository,omitempty"`
41 }
42
43
44 type WorkflowRuns struct {
45 TotalCount *int `json:"total_count,omitempty"`
46 WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
47 }
48
49
50 type ListWorkflowRunsOptions struct {
51 Actor string `url:"actor,omitempty"`
52 Branch string `url:"branch,omitempty"`
53 Event string `url:"event,omitempty"`
54 Status string `url:"status,omitempty"`
55 ListOptions
56 }
57
58
59 type WorkflowRunUsage struct {
60 Billable *WorkflowRunEnvironment `json:"billable,omitempty"`
61 RunDurationMS *int64 `json:"run_duration_ms,omitempty"`
62 }
63
64
65 type WorkflowRunEnvironment struct {
66 Ubuntu *WorkflowRunBill `json:"UBUNTU,omitempty"`
67 MacOS *WorkflowRunBill `json:"MACOS,omitempty"`
68 Windows *WorkflowRunBill `json:"WINDOWS,omitempty"`
69 }
70
71
72 type WorkflowRunBill struct {
73 TotalMS *int64 `json:"total_ms,omitempty"`
74 Jobs *int `json:"jobs,omitempty"`
75 }
76
77 func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
78 u, err := addOptions(endpoint, opts)
79 if err != nil {
80 return nil, nil, err
81 }
82
83 req, err := s.client.NewRequest("GET", u, nil)
84 if err != nil {
85 return nil, nil, err
86 }
87
88 runs := new(WorkflowRuns)
89 resp, err := s.client.Do(ctx, req, &runs)
90 if err != nil {
91 return nil, resp, err
92 }
93
94 return runs, resp, nil
95 }
96
97
98
99
100 func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
101 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
102 return s.listWorkflowRuns(ctx, u, opts)
103 }
104
105
106
107
108 func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
109 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
110 return s.listWorkflowRuns(ctx, u, opts)
111 }
112
113
114
115
116 func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
117 u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
118 u, err := addOptions(u, opts)
119 if err != nil {
120 return nil, nil, err
121 }
122
123 req, err := s.client.NewRequest("GET", u, nil)
124 if err != nil {
125 return nil, nil, err
126 }
127
128 runs := new(WorkflowRuns)
129 resp, err := s.client.Do(ctx, req, &runs)
130 if err != nil {
131 return nil, resp, err
132 }
133
134 return runs, resp, nil
135 }
136
137
138
139
140 func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
141 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
142
143 req, err := s.client.NewRequest("GET", u, nil)
144 if err != nil {
145 return nil, nil, err
146 }
147
148 run := new(WorkflowRun)
149 resp, err := s.client.Do(ctx, req, run)
150 if err != nil {
151 return nil, resp, err
152 }
153
154 return run, resp, nil
155 }
156
157
158
159
160 func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
161 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
162
163 req, err := s.client.NewRequest("POST", u, nil)
164 if err != nil {
165 return nil, err
166 }
167
168 return s.client.Do(ctx, req, nil)
169 }
170
171
172
173
174 func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
175 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
176
177 req, err := s.client.NewRequest("POST", u, nil)
178 if err != nil {
179 return nil, err
180 }
181
182 return s.client.Do(ctx, req, nil)
183 }
184
185
186
187
188 func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
189 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
190
191 resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects)
192 if err != nil {
193 return nil, nil, err
194 }
195
196 if resp.StatusCode != http.StatusFound {
197 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
198 }
199 parsedURL, err := url.Parse(resp.Header.Get("Location"))
200 return parsedURL, newResponse(resp), err
201 }
202
203
204
205
206 func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
207 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
208
209 req, err := s.client.NewRequest("DELETE", u, nil)
210 if err != nil {
211 return nil, err
212 }
213
214 return s.client.Do(ctx, req, nil)
215 }
216
217
218
219
220 func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
221 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
222
223 req, err := s.client.NewRequest("GET", u, nil)
224 if err != nil {
225 return nil, nil, err
226 }
227
228 workflowRunUsage := new(WorkflowRunUsage)
229 resp, err := s.client.Do(ctx, req, workflowRunUsage)
230 if err != nil {
231 return nil, resp, err
232 }
233
234 return workflowRunUsage, resp, nil
235 }
236
View as plain text