github.com/google/go-github/v68@v68.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 Path *string `json:"path,omitempty"` 23 RunNumber *int `json:"run_number,omitempty"` 24 RunAttempt *int `json:"run_attempt,omitempty"` 25 Event *string `json:"event,omitempty"` 26 DisplayTitle *string `json:"display_title,omitempty"` 27 Status *string `json:"status,omitempty"` 28 Conclusion *string `json:"conclusion,omitempty"` 29 WorkflowID *int64 `json:"workflow_id,omitempty"` 30 CheckSuiteID *int64 `json:"check_suite_id,omitempty"` 31 CheckSuiteNodeID *string `json:"check_suite_node_id,omitempty"` 32 URL *string `json:"url,omitempty"` 33 HTMLURL *string `json:"html_url,omitempty"` 34 PullRequests []*PullRequest `json:"pull_requests,omitempty"` 35 CreatedAt *Timestamp `json:"created_at,omitempty"` 36 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 37 RunStartedAt *Timestamp `json:"run_started_at,omitempty"` 38 JobsURL *string `json:"jobs_url,omitempty"` 39 LogsURL *string `json:"logs_url,omitempty"` 40 CheckSuiteURL *string `json:"check_suite_url,omitempty"` 41 ArtifactsURL *string `json:"artifacts_url,omitempty"` 42 CancelURL *string `json:"cancel_url,omitempty"` 43 RerunURL *string `json:"rerun_url,omitempty"` 44 PreviousAttemptURL *string `json:"previous_attempt_url,omitempty"` 45 HeadCommit *HeadCommit `json:"head_commit,omitempty"` 46 WorkflowURL *string `json:"workflow_url,omitempty"` 47 Repository *Repository `json:"repository,omitempty"` 48 HeadRepository *Repository `json:"head_repository,omitempty"` 49 Actor *User `json:"actor,omitempty"` 50 TriggeringActor *User `json:"triggering_actor,omitempty"` 51 ReferencedWorkflows []*ReferencedWorkflow `json:"referenced_workflows,omitempty"` 52 } 53 54 // WorkflowRuns represents a slice of repository action workflow run. 55 type WorkflowRuns struct { 56 TotalCount *int `json:"total_count,omitempty"` 57 WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"` 58 } 59 60 // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns. 61 type ListWorkflowRunsOptions struct { 62 Actor string `url:"actor,omitempty"` 63 Branch string `url:"branch,omitempty"` 64 Event string `url:"event,omitempty"` 65 Status string `url:"status,omitempty"` 66 Created string `url:"created,omitempty"` 67 HeadSHA string `url:"head_sha,omitempty"` 68 ExcludePullRequests bool `url:"exclude_pull_requests,omitempty"` 69 CheckSuiteID int64 `url:"check_suite_id,omitempty"` 70 ListOptions 71 } 72 73 // WorkflowRunUsage represents a usage of a specific workflow run. 74 type WorkflowRunUsage struct { 75 Billable *WorkflowRunBillMap `json:"billable,omitempty"` 76 RunDurationMS *int64 `json:"run_duration_ms,omitempty"` 77 } 78 79 // WorkflowRunBillMap represents different runner environments available for a workflow run. 80 // Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc. 81 type WorkflowRunBillMap map[string]*WorkflowRunBill 82 83 // WorkflowRunBill specifies billable time for a specific environment in a workflow run. 84 type WorkflowRunBill struct { 85 TotalMS *int64 `json:"total_ms,omitempty"` 86 Jobs *int `json:"jobs,omitempty"` 87 JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"` 88 } 89 90 // WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run. 91 type WorkflowRunJobRun struct { 92 JobID *int `json:"job_id,omitempty"` 93 DurationMS *int64 `json:"duration_ms,omitempty"` 94 } 95 96 // WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt. 97 type WorkflowRunAttemptOptions struct { 98 ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` 99 } 100 101 // PendingDeploymentsRequest specifies body parameters to PendingDeployments. 102 type PendingDeploymentsRequest struct { 103 EnvironmentIDs []int64 `json:"environment_ids"` 104 // State can be one of: "approved", "rejected". 105 State string `json:"state"` 106 Comment string `json:"comment"` 107 } 108 109 type ReferencedWorkflow struct { 110 Path *string `json:"path,omitempty"` 111 SHA *string `json:"sha,omitempty"` 112 Ref *string `json:"ref,omitempty"` 113 } 114 115 // PendingDeployment represents the pending_deployments response. 116 type PendingDeployment struct { 117 Environment *PendingDeploymentEnvironment `json:"environment,omitempty"` 118 WaitTimer *int64 `json:"wait_timer,omitempty"` 119 WaitTimerStartedAt *Timestamp `json:"wait_timer_started_at,omitempty"` 120 CurrentUserCanApprove *bool `json:"current_user_can_approve,omitempty"` 121 Reviewers []*RequiredReviewer `json:"reviewers,omitempty"` 122 } 123 124 // PendingDeploymentEnvironment represents pending deployment environment properties. 125 type PendingDeploymentEnvironment struct { 126 ID *int64 `json:"id,omitempty"` 127 NodeID *string `json:"node_id,omitempty"` 128 Name *string `json:"name,omitempty"` 129 URL *string `json:"url,omitempty"` 130 HTMLURL *string `json:"html_url,omitempty"` 131 } 132 133 // ReviewCustomDeploymentProtectionRuleRequest specifies the parameters to ReviewCustomDeploymentProtectionRule. 134 type ReviewCustomDeploymentProtectionRuleRequest struct { 135 EnvironmentName string `json:"environment_name"` 136 State string `json:"state"` 137 Comment string `json:"comment"` 138 } 139 140 func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 141 u, err := addOptions(endpoint, opts) 142 if err != nil { 143 return nil, nil, err 144 } 145 146 req, err := s.client.NewRequest("GET", u, nil) 147 if err != nil { 148 return nil, nil, err 149 } 150 151 runs := new(WorkflowRuns) 152 resp, err := s.client.Do(ctx, req, &runs) 153 if err != nil { 154 return nil, resp, err 155 } 156 157 return runs, resp, nil 158 } 159 160 // ListWorkflowRunsByID lists all workflow runs by workflow ID. 161 // 162 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-workflow 163 // 164 //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs 165 func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 166 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID) 167 return s.listWorkflowRuns(ctx, u, opts) 168 } 169 170 // ListWorkflowRunsByFileName lists all workflow runs by workflow file name. 171 // 172 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-workflow 173 // 174 //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs 175 func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 176 u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName) 177 return s.listWorkflowRuns(ctx, u, opts) 178 } 179 180 // ListRepositoryWorkflowRuns lists all workflow runs for a repository. 181 // 182 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-repository 183 // 184 //meta:operation GET /repos/{owner}/{repo}/actions/runs 185 func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { 186 u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo) 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 runs := new(WorkflowRuns) 198 resp, err := s.client.Do(ctx, req, &runs) 199 if err != nil { 200 return nil, resp, err 201 } 202 203 return runs, resp, nil 204 } 205 206 // GetWorkflowRunByID gets a specific workflow run by ID. 207 // 208 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run 209 // 210 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id} 211 func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) { 212 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID) 213 214 req, err := s.client.NewRequest("GET", u, nil) 215 if err != nil { 216 return nil, nil, err 217 } 218 219 run := new(WorkflowRun) 220 resp, err := s.client.Do(ctx, req, run) 221 if err != nil { 222 return nil, resp, err 223 } 224 225 return run, resp, nil 226 } 227 228 // GetWorkflowRunAttempt gets a specific workflow run attempt. 229 // 230 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run-attempt 231 // 232 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number} 233 func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) { 234 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber) 235 u, err := addOptions(u, opts) 236 if err != nil { 237 return nil, nil, err 238 } 239 240 req, err := s.client.NewRequest("GET", u, nil) 241 if err != nil { 242 return nil, nil, err 243 } 244 245 run := new(WorkflowRun) 246 resp, err := s.client.Do(ctx, req, run) 247 if err != nil { 248 return nil, resp, err 249 } 250 251 return run, resp, nil 252 } 253 254 // GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number. 255 // 256 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-attempt-logs 257 // 258 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number}/logs 259 func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, maxRedirects int) (*url.URL, *Response, error) { 260 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber) 261 262 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects) 263 if err != nil { 264 return nil, nil, err 265 } 266 defer resp.Body.Close() 267 268 if resp.StatusCode != http.StatusFound { 269 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) 270 } 271 272 parsedURL, err := url.Parse(resp.Header.Get("Location")) 273 return parsedURL, newResponse(resp), err 274 } 275 276 // RerunWorkflowByID re-runs a workflow by ID. 277 // 278 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-workflow 279 // 280 //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun 281 func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 282 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID) 283 284 req, err := s.client.NewRequest("POST", u, nil) 285 if err != nil { 286 return nil, err 287 } 288 289 return s.client.Do(ctx, req, nil) 290 } 291 292 // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID. 293 // 294 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run 295 // 296 //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun-failed-jobs 297 func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 298 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID) 299 300 req, err := s.client.NewRequest("POST", u, nil) 301 if err != nil { 302 return nil, err 303 } 304 305 return s.client.Do(ctx, req, nil) 306 } 307 308 // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID. 309 // 310 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run 311 // 312 //meta:operation POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun 313 func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) { 314 u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID) 315 316 req, err := s.client.NewRequest("POST", u, nil) 317 if err != nil { 318 return nil, err 319 } 320 321 return s.client.Do(ctx, req, nil) 322 } 323 324 // CancelWorkflowRunByID cancels a workflow run by ID. 325 // 326 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#cancel-a-workflow-run 327 // 328 //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel 329 func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 330 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID) 331 332 req, err := s.client.NewRequest("POST", u, nil) 333 if err != nil { 334 return nil, err 335 } 336 337 return s.client.Do(ctx, req, nil) 338 } 339 340 // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run. 341 // 342 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-logs 343 // 344 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/logs 345 func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, maxRedirects int) (*url.URL, *Response, error) { 346 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID) 347 348 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects) 349 if err != nil { 350 return nil, nil, err 351 } 352 defer resp.Body.Close() 353 354 if resp.StatusCode != http.StatusFound { 355 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) 356 } 357 358 parsedURL, err := url.Parse(resp.Header.Get("Location")) 359 return parsedURL, newResponse(resp), err 360 } 361 362 // DeleteWorkflowRun deletes a workflow run by ID. 363 // 364 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-a-workflow-run 365 // 366 //meta:operation DELETE /repos/{owner}/{repo}/actions/runs/{run_id} 367 func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 368 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID) 369 370 req, err := s.client.NewRequest("DELETE", u, nil) 371 if err != nil { 372 return nil, err 373 } 374 375 return s.client.Do(ctx, req, nil) 376 } 377 378 // DeleteWorkflowRunLogs deletes all logs for a workflow run. 379 // 380 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-workflow-run-logs 381 // 382 //meta:operation DELETE /repos/{owner}/{repo}/actions/runs/{run_id}/logs 383 func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) { 384 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID) 385 386 req, err := s.client.NewRequest("DELETE", u, nil) 387 if err != nil { 388 return nil, err 389 } 390 391 return s.client.Do(ctx, req, nil) 392 } 393 394 // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds. 395 // 396 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-workflow-run-usage 397 // 398 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/timing 399 func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) { 400 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID) 401 402 req, err := s.client.NewRequest("GET", u, nil) 403 if err != nil { 404 return nil, nil, err 405 } 406 407 workflowRunUsage := new(WorkflowRunUsage) 408 resp, err := s.client.Do(ctx, req, workflowRunUsage) 409 if err != nil { 410 return nil, resp, err 411 } 412 413 return workflowRunUsage, resp, nil 414 } 415 416 // GetPendingDeployments get all deployment environments for a workflow run that are waiting for protection rules to pass. 417 // 418 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-pending-deployments-for-a-workflow-run 419 // 420 //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/pending_deployments 421 func (s *ActionsService) GetPendingDeployments(ctx context.Context, owner, repo string, runID int64) ([]*PendingDeployment, *Response, error) { 422 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID) 423 424 req, err := s.client.NewRequest("GET", u, nil) 425 if err != nil { 426 return nil, nil, err 427 } 428 429 var deployments []*PendingDeployment 430 resp, err := s.client.Do(ctx, req, &deployments) 431 if err != nil { 432 return nil, resp, err 433 } 434 435 return deployments, resp, nil 436 } 437 438 // PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer. 439 // 440 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run 441 // 442 //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/pending_deployments 443 func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) { 444 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID) 445 446 req, err := s.client.NewRequest("POST", u, request) 447 if err != nil { 448 return nil, nil, err 449 } 450 451 var deployments []*Deployment 452 resp, err := s.client.Do(ctx, req, &deployments) 453 if err != nil { 454 return nil, resp, err 455 } 456 457 return deployments, resp, nil 458 } 459 460 // ReviewCustomDeploymentProtectionRule approves or rejects custom deployment protection rules provided by a GitHub App for a workflow run. 461 // 462 // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run 463 // 464 //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/deployment_protection_rule 465 func (s *ActionsService) ReviewCustomDeploymentProtectionRule(ctx context.Context, owner, repo string, runID int64, request *ReviewCustomDeploymentProtectionRuleRequest) (*Response, error) { 466 u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/deployment_protection_rule", owner, repo, runID) 467 468 req, err := s.client.NewRequest("POST", u, request) 469 if err != nil { 470 return nil, err 471 } 472 473 resp, err := s.client.Do(ctx, req, nil) 474 return resp, err 475 }