github.com/google/go-github/v50@v50.2.0/github/actions_workflow_runs.go (about) 1 // Copyright 2020 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 "net/http" 12 "net/url" 13 ) 14 15 // WorkflowRun represents a repository action workflow run. 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 Status *string `json:"status,omitempty"` 26 Conclusion *string `json:"conclusion,omitempty"` 27 WorkflowID *int64 `json:"workflow_id,omitempty"` 28 CheckSuiteID *int64 `json:"check_suite_id,omitempty"` 29 CheckSuiteNodeID *string `json:"check_suite_node_id,omitempty"` 30 URL *string `json:"url,omitempty"` 31 HTMLURL *string `json:"html_url,omitempty"` 32 PullRequests []*PullRequest `json:"pull_requests,omitempty"` 33 CreatedAt *Timestamp `json:"created_at,omitempty"` 34 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 35 RunStartedAt *Timestamp `json:"run_started_at,omitempty"` 36 JobsURL *string `json:"jobs_url,omitempty"` 37 LogsURL *string `json:"logs_url,omitempty"` 38 CheckSuiteURL *string `json:"check_suite_url,omitempty"` 39 ArtifactsURL *string `json:"artifacts_url,omitempty"` 40 CancelURL *string `json:"cancel_url,omitempty"` 41 RerunURL *string `json:"rerun_url,omitempty"` 42 PreviousAttemptURL *string `json:"previous_attempt_url,omitempty"` 43 HeadCommit *HeadCommit `json:"head_commit,omitempty"` 44 WorkflowURL *string `json:"workflow_url,omitempty"` 45 Repository *Repository `json:"repository,omitempty"` 46 HeadRepository *Repository `json:"head_repository,omitempty"` 47 Actor *User `json:"actor,omitempty"` 48 } 49 50 // WorkflowRuns represents a slice of repository action workflow run. 51 type WorkflowRuns struct { 52 TotalCount *int `json:"total_count,omitempty"` 53 WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"` 54 } 55 56 // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns. 57 type ListWorkflowRunsOptions struct { 58 Actor string `url:"actor,omitempty"` 59 Branch string `url:"branch,omitempty"` 60 Event string `url:"event,omitempty"` 61 Status string `url:"status,omitempty"` 62 Created string `url:"created,omitempty"` 63 HeadSHA string `url:"head_sha,omitempty"` 64 ListOptions 65 } 66 67 // WorkflowRunUsage represents a usage of a specific workflow run. 68 type WorkflowRunUsage struct { 69 Billable *WorkflowRunBillMap `json:"billable,omitempty"` 70 RunDurationMS *int64 `json:"run_duration_ms,omitempty"` 71 } 72 73 // WorkflowRunBillMap represents different runner environments available for a workflow run. 74 // Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc. 75 type WorkflowRunBillMap map[string]*WorkflowRunBill 76 77 // WorkflowRunBill specifies billable time for a specific environment in a workflow run. 78 type WorkflowRunBill struct { 79 TotalMS *int64 `json:"total_ms,omitempty"` 80 Jobs *int `json:"jobs,omitempty"` 81 JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"` 82 } 83 84 // WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run. 85 type WorkflowRunJobRun struct { 86 JobID *int `json:"job_id,omitempty"` 87 DurationMS *int64 `json:"duration_ms,omitempty"` 88 } 89 90 // WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt. 91 type WorkflowRunAttemptOptions struct { 92 ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` 93 } 94 95 // PendingDeploymentsRequest specifies body parameters to PendingDeployments. 96 type PendingDeploymentsRequest struct { 97 EnvironmentIDs []int64 `json:"environment_ids"` 98 // State can be one of: "approved", "rejected". 99 State string `json:"state"` 100 Comment string `json:"comment"` 101 } 102 103 func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 104 u, err := addOptions(endpoint, opts) 105 if err != nil { 106 return nil, nil, err 107 } 108 109 req, err := s.client.NewRequest("GET", u, nil) 110 if err != nil { 111 return nil, nil, err 112 } 113 114 runs := new(WorkflowRuns) 115 resp, err := s.client.Do(ctx, req, &runs) 116 if err != nil { 117 return nil, resp, err 118 } 119 120 return runs, resp, nil 121 } 122 123 // ListWorkflowRunsByID lists all workflow runs by workflow ID. 124 // 125 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs 126 func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 127 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID) 128 return s.listWorkflowRuns(ctx, u, opts) 129 } 130 131 // ListWorkflowRunsByFileName lists all workflow runs by workflow file name. 132 // 133 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs 134 func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 135 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName) 136 return s.listWorkflowRuns(ctx, u, opts) 137 } 138 139 // ListRepositoryWorkflowRuns lists all workflow runs for a repository. 140 // 141 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs-for-a-repository 142 func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 143 u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo) 144 u, err := addOptions(u, opts) 145 if err != nil { 146 return nil, nil, err 147 } 148 149 req, err := s.client.NewRequest("GET", u, nil) 150 if err != nil { 151 return nil, nil, err 152 } 153 154 runs := new(WorkflowRuns) 155 resp, err := s.client.Do(ctx, req, &runs) 156 if err != nil { 157 return nil, resp, err 158 } 159 160 return runs, resp, nil 161 } 162 163 // GetWorkflowRunByID gets a specific workflow run by ID. 164 // 165 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run 166 func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) { 167 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID) 168 169 req, err := s.client.NewRequest("GET", u, nil) 170 if err != nil { 171 return nil, nil, err 172 } 173 174 run := new(WorkflowRun) 175 resp, err := s.client.Do(ctx, req, run) 176 if err != nil { 177 return nil, resp, err 178 } 179 180 return run, resp, nil 181 } 182 183 // GetWorkflowRunAttempt gets a specific workflow run attempt. 184 // 185 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run-attempt 186 func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) { 187 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber) 188 u, err := addOptions(u, opts) 189 if err != nil { 190 return nil, nil, err 191 } 192 193 req, err := s.client.NewRequest("GET", u, nil) 194 if err != nil { 195 return nil, nil, err 196 } 197 198 run := new(WorkflowRun) 199 resp, err := s.client.Do(ctx, req, run) 200 if err != nil { 201 return nil, resp, err 202 } 203 204 return run, resp, nil 205 } 206 207 // RerunWorkflowByID re-runs a workflow by ID. 208 // 209 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-workflow 210 func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 211 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID) 212 213 req, err := s.client.NewRequest("POST", u, nil) 214 if err != nil { 215 return nil, err 216 } 217 218 return s.client.Do(ctx, req, nil) 219 } 220 221 // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID. 222 // 223 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run 224 func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 225 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID) 226 227 req, err := s.client.NewRequest("POST", u, nil) 228 if err != nil { 229 return nil, err 230 } 231 232 return s.client.Do(ctx, req, nil) 233 } 234 235 // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID. 236 // 237 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run 238 func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) { 239 u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID) 240 241 req, err := s.client.NewRequest("POST", u, nil) 242 if err != nil { 243 return nil, err 244 } 245 246 return s.client.Do(ctx, req, nil) 247 } 248 249 // CancelWorkflowRunByID cancels a workflow run by ID. 250 // 251 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#cancel-a-workflow-run 252 func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 253 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID) 254 255 req, err := s.client.NewRequest("POST", u, nil) 256 if err != nil { 257 return nil, err 258 } 259 260 return s.client.Do(ctx, req, nil) 261 } 262 263 // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run. 264 // 265 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-logs 266 func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) { 267 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID) 268 269 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects) 270 if err != nil { 271 return nil, nil, err 272 } 273 defer resp.Body.Close() 274 275 if resp.StatusCode != http.StatusFound { 276 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) 277 } 278 279 parsedURL, err := url.Parse(resp.Header.Get("Location")) 280 return parsedURL, newResponse(resp), err 281 } 282 283 // DeleteWorkflowRun deletes a workflow run by ID. 284 // 285 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-a-workflow-run 286 func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 287 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID) 288 289 req, err := s.client.NewRequest("DELETE", u, nil) 290 if err != nil { 291 return nil, err 292 } 293 294 return s.client.Do(ctx, req, nil) 295 } 296 297 // DeleteWorkflowRunLogs deletes all logs for a workflow run. 298 // 299 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-workflow-run-logs 300 func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 301 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID) 302 303 req, err := s.client.NewRequest("DELETE", u, nil) 304 if err != nil { 305 return nil, err 306 } 307 308 return s.client.Do(ctx, req, nil) 309 } 310 311 // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds. 312 // 313 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-workflow-run-usage 314 func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) { 315 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID) 316 317 req, err := s.client.NewRequest("GET", u, nil) 318 if err != nil { 319 return nil, nil, err 320 } 321 322 workflowRunUsage := new(WorkflowRunUsage) 323 resp, err := s.client.Do(ctx, req, workflowRunUsage) 324 if err != nil { 325 return nil, resp, err 326 } 327 328 return workflowRunUsage, resp, nil 329 } 330 331 // PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer. 332 // 333 // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run 334 func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) { 335 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID) 336 337 req, err := s.client.NewRequest("POST", u, request) 338 if err != nil { 339 return nil, nil, err 340 } 341 342 var deployments []*Deployment 343 resp, err := s.client.Do(ctx, req, &deployments) 344 if err != nil { 345 return nil, resp, err 346 } 347 348 return deployments, resp, nil 349 }