github.com/google/go-github/v42@v42.0.0/github/actions_workflow_jobs.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 // 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 HeadSHA *string `json:"head_sha,omitempty"` 32 URL *string `json:"url,omitempty"` 33 HTMLURL *string `json:"html_url,omitempty"` 34 Status *string `json:"status,omitempty"` 35 Conclusion *string `json:"conclusion,omitempty"` 36 StartedAt *Timestamp `json:"started_at,omitempty"` 37 CompletedAt *Timestamp `json:"completed_at,omitempty"` 38 Name *string `json:"name,omitempty"` 39 Steps []*TaskStep `json:"steps,omitempty"` 40 CheckRunURL *string `json:"check_run_url,omitempty"` 41 // Labels represents runner labels from the `runs-on:` key from a GitHub Actions workflow. 42 Labels []string `json:"labels,omitempty"` 43 RunnerID *int64 `json:"runner_id,omitempty"` 44 RunnerName *string `json:"runner_name,omitempty"` 45 RunnerGroupID *int64 `json:"runner_group_id,omitempty"` 46 RunnerGroupName *string `json:"runner_group_name,omitempty"` 47 } 48 49 // Jobs represents a slice of repository action workflow job. 50 type Jobs struct { 51 TotalCount *int `json:"total_count,omitempty"` 52 Jobs []*WorkflowJob `json:"jobs,omitempty"` 53 } 54 55 // ListWorkflowJobsOptions specifies optional parameters to ListWorkflowJobs. 56 type ListWorkflowJobsOptions struct { 57 // Filter specifies how jobs should be filtered by their completed_at timestamp. 58 // Possible values are: 59 // latest - Returns jobs from the most recent execution of the workflow run 60 // all - Returns all jobs for a workflow run, including from old executions of the workflow run 61 // 62 // Default value is "latest". 63 Filter string `url:"filter,omitempty"` 64 ListOptions 65 } 66 67 // ListWorkflowJobs lists all jobs for a workflow run. 68 // 69 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-jobs-for-a-workflow-run 70 func (s *ActionsService) ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *ListWorkflowJobsOptions) (*Jobs, *Response, error) { 71 u := fmt.Sprintf("repos/%s/%s/actions/runs/%v/jobs", owner, repo, runID) 72 u, err := addOptions(u, opts) 73 if err != nil { 74 return nil, nil, err 75 } 76 77 req, err := s.client.NewRequest("GET", u, nil) 78 if err != nil { 79 return nil, nil, err 80 } 81 82 jobs := new(Jobs) 83 resp, err := s.client.Do(ctx, req, &jobs) 84 if err != nil { 85 return nil, resp, err 86 } 87 88 return jobs, resp, nil 89 } 90 91 // GetWorkflowJobByID gets a specific job in a workflow run by ID. 92 // 93 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-job-for-a-workflow-run 94 func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo string, jobID int64) (*WorkflowJob, *Response, error) { 95 u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v", owner, repo, jobID) 96 97 req, err := s.client.NewRequest("GET", u, nil) 98 if err != nil { 99 return nil, nil, err 100 } 101 102 job := new(WorkflowJob) 103 resp, err := s.client.Do(ctx, req, job) 104 if err != nil { 105 return nil, resp, err 106 } 107 108 return job, resp, nil 109 } 110 111 // GetWorkflowJobLogs gets a redirect URL to download a plain text file of logs for a workflow job. 112 // 113 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#download-job-logs-for-a-workflow-run 114 func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, followRedirects bool) (*url.URL, *Response, error) { 115 u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID) 116 117 resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects) 118 if err != nil { 119 return nil, nil, err 120 } 121 122 if resp.StatusCode != http.StatusFound { 123 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) 124 } 125 parsedURL, err := url.Parse(resp.Header.Get("Location")) 126 return parsedURL, newResponse(resp), err 127 } 128 129 func (s *ActionsService) getWorkflowLogsFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) { 130 req, err := s.client.NewRequest("GET", u, nil) 131 if err != nil { 132 return nil, err 133 } 134 135 var resp *http.Response 136 // Use http.DefaultTransport if no custom Transport is configured 137 req = withContext(ctx, req) 138 if s.client.client.Transport == nil { 139 resp, err = http.DefaultTransport.RoundTrip(req) 140 } else { 141 resp, err = s.client.client.Transport.RoundTrip(req) 142 } 143 if err != nil { 144 return nil, err 145 } 146 resp.Body.Close() 147 148 // If redirect response is returned, follow it 149 if followRedirects && resp.StatusCode == http.StatusMovedPermanently { 150 u = resp.Header.Get("Location") 151 resp, err = s.getWorkflowLogsFromURL(ctx, u, false) 152 } 153 return resp, err 154 }