| 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 |
// TaskStep represents a single task step from a sequence of tasks of a job. |
| 16 |
type TaskStep struct { |
| 17 |
Name *string `json:"name,omitempty"` |
| 18 |
Status *string `json:"status,omitempty"` |
| 19 |
Conclusion *string `json:"conclusion,omitempty"` |
| 20 |
Number *int64 `json:"number,omitempty"` |
| 21 |
StartedAt *Timestamp `json:"started_at,omitempty"` |
| 22 |
CompletedAt *Timestamp `json:"completed_at,omitempty"` |
| 23 |
} |
| 24 |
|
| 25 |
// WorkflowJob represents a repository action workflow job. |
| 26 |
type WorkflowJob struct { |
| 27 |
ID *int64 `json:"id,omitempty"` |
| 28 |
RunID *int64 `json:"run_id,omitempty"` |
| 29 |
RunURL *string `json:"run_url,omitempty"` |
| 30 |
NodeID *string `json:"node_id,omitempty"` |
| 31 |
HeadBranch *string `json:"head_branch,omitempty"` |
| 32 |
HeadSHA *string `json:"head_sha,omitempty"` |
| 33 |
URL *string `json:"url,omitempty"` |
| 34 |
HTMLURL *string `json:"html_url,omitempty"` |
| 35 |
Status *string `json:"status,omitempty"` |
| 36 |
Conclusion *string `json:"conclusion,omitempty"` |
| 37 |
CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 38 |
StartedAt *Timestamp `json:"started_at,omitempty"` |
| 39 |
CompletedAt *Timestamp `json:"completed_at,omitempty"` |
| 40 |
Name *string `json:"name,omitempty"` |
| 41 |
Steps []*TaskStep `json:"steps,omitempty"` |
| 42 |
CheckRunURL *string `json:"check_run_url,omitempty"` |
| 43 |
// Labels represents runner labels from the `runs-on:` key from a GitHub Actions workflow. |
| 44 |
Labels []string `json:"labels,omitempty"` |
| 45 |
RunnerID *int64 `json:"runner_id,omitempty"` |
| 46 |
RunnerName *string `json:"runner_name,omitempty"` |
| 47 |
RunnerGroupID *int64 `json:"runner_group_id,omitempty"` |
| 48 |
RunnerGroupName *string `json:"runner_group_name,omitempty"` |
| 49 |
RunAttempt *int64 `json:"run_attempt,omitempty"` |
| 50 |
WorkflowName *string `json:"workflow_name,omitempty"` |
| 51 |
} |
| 52 |
|
| 53 |
// Jobs represents a slice of repository action workflow job. |
| 54 |
type Jobs struct { |
| 55 |
TotalCount *int `json:"total_count,omitempty"` |
| 56 |
Jobs []*WorkflowJob `json:"jobs,omitempty"` |
| 57 |
} |
| 58 |
|
| 59 |
// ListWorkflowJobsOptions specifies optional parameters to ListWorkflowJobs. |
| 60 |
type ListWorkflowJobsOptions struct { |
| 61 |
// Filter specifies how jobs should be filtered by their completed_at timestamp. |
| 62 |
// Possible values are: |
| 63 |
// latest - Returns jobs from the most recent execution of the workflow run |
| 64 |
// all - Returns all jobs for a workflow run, including from old executions of the workflow run |
| 65 |
// |
| 66 |
// Default value is "latest". |
| 67 |
Filter string `url:"filter,omitempty"` |
| 68 |
ListOptions |
| 69 |
} |
| 70 |
|
| 71 |
// ListWorkflowJobs lists all jobs for a workflow run. |
| 72 |
// |
| 73 |
// GitHub API docs: https://docs.github.com/rest/actions/workflow-jobs#list-jobs-for-a-workflow-run |
| 74 |
// |
| 75 |
//meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs |
| 76 |
func (s *ActionsService) ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *ListWorkflowJobsOptions) (*Jobs, *Response, error) { |
| 77 |
u := fmt.Sprintf("repos/%s/%s/actions/runs/%v/jobs", owner, repo, runID) |
| 78 |
u, err := addOptions(u, 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 |
jobs := new(Jobs) |
| 89 |
resp, err := s.client.Do(ctx, req, &jobs) |
| 90 |
if err != nil { |
| 91 |
return nil, resp, err |
| 92 |
} |
| 93 |
|
| 94 |
return jobs, resp, nil |
| 95 |
} |
| 96 |
|
| 97 |
// ListWorkflowJobsAttempt lists jobs for a workflow run Attempt. |
| 98 |
// |
| 99 |
// GitHub API docs: https://docs.github.com/rest/actions/workflow-jobs#list-jobs-for-a-workflow-run-attempt |
| 100 |
// |
| 101 |
//meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number}/jobs |
| 102 |
func (s *ActionsService) ListWorkflowJobsAttempt(ctx context.Context, owner, repo string, runID, attemptNumber int64, opts *ListOptions) (*Jobs, *Response, error) { |
| 103 |
u := fmt.Sprintf("repos/%s/%s/actions/runs/%v/attempts/%v/jobs", owner, repo, runID, attemptNumber) |
| 104 |
u, err := addOptions(u, 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 |
jobs := new(Jobs) |
| 115 |
resp, err := s.client.Do(ctx, req, &jobs) |
| 116 |
if err != nil { |
| 117 |
return nil, resp, err |
| 118 |
} |
| 119 |
|
| 120 |
return jobs, resp, nil |
| 121 |
} |
| 122 |
|
| 123 |
// GetWorkflowJobByID gets a specific job in a workflow run by ID. |
| 124 |
// |
| 125 |
// GitHub API docs: https://docs.github.com/rest/actions/workflow-jobs#get-a-job-for-a-workflow-run |
| 126 |
// |
| 127 |
//meta:operation GET /repos/{owner}/{repo}/actions/jobs/{job_id} |
| 128 |
func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo string, jobID int64) (*WorkflowJob, *Response, error) { |
| 129 |
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v", owner, repo, jobID) |
| 130 |
|
| 131 |
req, err := s.client.NewRequest("GET", u, nil) |
| 132 |
if err != nil { |
| 133 |
return nil, nil, err |
| 134 |
} |
| 135 |
|
| 136 |
job := new(WorkflowJob) |
| 137 |
resp, err := s.client.Do(ctx, req, job) |
| 138 |
if err != nil { |
| 139 |
return nil, resp, err |
| 140 |
} |
| 141 |
|
| 142 |
return job, resp, nil |
| 143 |
} |
| 144 |
|
| 145 |
// GetWorkflowJobLogs gets a redirect URL to download a plain text file of logs for a workflow job. |
| 146 |
// |
| 147 |
// GitHub API docs: https://docs.github.com/rest/actions/workflow-jobs#download-job-logs-for-a-workflow-run |
| 148 |
// |
| 149 |
//meta:operation GET /repos/{owner}/{repo}/actions/jobs/{job_id}/logs |
| 150 |
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, maxRedirects int) (*url.URL, *Response, error) { |
| 151 |
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID) |
| 152 |
|
| 153 |
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects) |
| 154 |
if err != nil { |
| 155 |
return nil, nil, err |
| 156 |
} |
| 157 |
defer resp.Body.Close() |
| 158 |
|
| 159 |
if resp.StatusCode != http.StatusFound { |
| 160 |
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) |
| 161 |
} |
| 162 |
|
| 163 |
parsedURL, err := url.Parse(resp.Header.Get("Location")) |
| 164 |
return parsedURL, newResponse(resp), err |
| 165 |
} |