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