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