-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement support for actions workflow jobs #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
c5b7b72
cae90e1
c59a6f8
22cf2d2
ada4acf
b695b0a
db076ae
518cd9d
1bae46c
016438d
00b8d43
aefa9a0
ac8c3cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2020 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// Step represents a single task from a sequence of tasks of a job. | ||
type Step struct { | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
Conclusion string `json:"conclusion"` | ||
Number int64 `json:"number"` | ||
StartedAt Timestamp `json:"started_at"` | ||
CompletedAt Timestamp `json:"completed_at"` | ||
} | ||
|
||
// Job represents a repository action workflow job. | ||
type Job struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The more I think about this, the more I'm thinking that all fields in I'd like to hear what @gauntface thinks about this, specifically. Technically, I don't think they are needed... but it might be an odd experience for a user of this repo to find this pair of structs not follow the pattern set by the rest of the repo. |
||
ID int64 `json:"id"` | ||
RunID int64 `json:"run_id"` | ||
RunURL string `json:"run_url"` | ||
NodeID string `json:"node_id"` | ||
HeadSHA string `json:"head_sha"` | ||
URL string `json:"url"` | ||
HTMLURL string `json:"html_url"` | ||
Status string `json:"status"` | ||
Conclusion string `json:"conclusion"` | ||
StartedAt Timestamp `json:"started_at"` | ||
CompletedAt Timestamp `json:"completed_at"` | ||
Name string `json:"name"` | ||
Steps []*Step `json:"steps"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
CheckRunURL string `json:"check_run_url"` | ||
} | ||
|
||
// Jobs represents a slice of repository action workflow job. | ||
type Jobs struct { | ||
TotalCount int `json:"total_count"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here:
|
||
Jobs []*Job `json:"jobs"` | ||
} | ||
|
||
// ListWorkflowJobs lists all jobs for a workflow run. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/actions/workflow_jobs/#list-jobs-for-a-workflow-run | ||
func (s *ActionsService) ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *ListOptions) (*Jobs, *Response, error) { | ||
u := fmt.Sprintf("repos/%s/%s/actions/runs/%v/jobs", owner, repo, runID) | ||
u, err := addOptions(u, opts) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
jobs := new(Jobs) | ||
resp, err := s.client.Do(ctx, req, &jobs) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return jobs, resp, nil | ||
} | ||
|
||
// GetWorkflowJobByID gets a specific job in a workflow run by ID. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/actions/workflow_jobs/#list-jobs-for-a-workflow-run | ||
func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo string, jobID int64) (*Job, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v", owner, repo, jobID) | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
job := new(Job) | ||
resp, err := s.client.Do(ctx, req, job) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return job, resp, nil | ||
} | ||
|
||
// GetWorkflowJobLogs gets a redirect URL to download a plain text file of logs for a workflow job. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/actions/workflow_jobs/#list-workflow-job-logs | ||
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, followRedirects bool) (*url.URL, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID) | ||
|
||
resp, err := s.getWorkflowJobLogsFromURL(ctx, u, followRedirects) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusFound { | ||
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) | ||
} | ||
parsedURL, err := url.Parse(resp.Header.Get("Location")) | ||
return parsedURL, newResponse(resp), err | ||
} | ||
|
||
func (s *ActionsService) getWorkflowJobLogsFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) { | ||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var resp *http.Response | ||
// Use http.DefaultTransport if no custom Transport is configured | ||
req = withContext(ctx, req) | ||
if s.client.client.Transport == nil { | ||
resp, err = http.DefaultTransport.RoundTrip(req) | ||
} else { | ||
resp, err = s.client.client.Transport.RoundTrip(req) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp.Body.Close() | ||
|
||
// If redirect response is returned, follow it | ||
if followRedirects && resp.StatusCode == http.StatusMovedPermanently { | ||
u = resp.Header.Get("Location") | ||
resp, err = s.getWorkflowJobLogsFromURL(ctx, u, false) | ||
} | ||
return resp, err | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2020 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestActionsService_ListWorkflowJobs(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testFormValues(t, r, values{"per_page": "2", "page": "2"}) | ||
fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`) | ||
}) | ||
|
||
opts := &ListOptions{Page: 2, PerPage: 2} | ||
jobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), "o", "r", 29679449, opts) | ||
if err != nil { | ||
t.Errorf("Actions.ListWorkflowJobs returned error: %v", err) | ||
} | ||
|
||
want := &Jobs{ | ||
TotalCount: 4, | ||
Jobs: []*Job{ | ||
{ID: 399444496, RunID: 29679449, StartedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, | ||
{ID: 399444497, RunID: 29679449, StartedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, | ||
}, | ||
} | ||
if !reflect.DeepEqual(jobs, want) { | ||
t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want) | ||
} | ||
} | ||
|
||
func TestActionsService_GetWorkflowJobByID(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/actions/jobs/399444496", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
fmt.Fprint(w, `{"id":399444496,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}`) | ||
}) | ||
|
||
job, _, err := client.Actions.GetWorkflowJobByID(context.Background(), "o", "r", 399444496) | ||
if err != nil { | ||
t.Errorf("Actions.GetWorkflowJobByID returned error: %v", err) | ||
} | ||
|
||
want := &Job{ | ||
ID: 399444496, | ||
StartedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, | ||
CompletedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if !reflect.DeepEqual(job, want) { | ||
t.Errorf("Actions.GetWorkflowJobByID returned %+v, want %+v", job, want) | ||
} | ||
} | ||
|
||
func TestActionsService_GetWorkflowJobLogs(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
http.Redirect(w, r, "http://github.com/a", http.StatusFound) | ||
}) | ||
|
||
url, resp, err := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, true) | ||
if err != nil { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) | ||
} | ||
if resp.StatusCode != http.StatusFound { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) | ||
} | ||
want := "http://github.com/a" | ||
if url.String() != want { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) | ||
} | ||
} | ||
|
||
func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently) | ||
}) | ||
|
||
_, resp, _ := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, false) | ||
if resp.StatusCode != http.StatusMovedPermanently { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently) | ||
} | ||
} | ||
|
||
func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_followRedirects(t *testing.T) { | ||
client, mux, serverURL, teardown := setup() | ||
defer teardown() | ||
|
||
// Mock a redirect link, which leads to an archive link | ||
mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect") | ||
http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) | ||
}) | ||
|
||
mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
http.Redirect(w, r, "http://github.com/a", http.StatusFound) | ||
}) | ||
|
||
url, resp, err := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, true) | ||
if err != nil { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusFound { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) | ||
} | ||
|
||
want := "http://github.com/a" | ||
if url.String() != want { | ||
t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In light of #1429, #79, #45, and #19, I'm going to make the executive decision and say that these two structs (
Job
andStep
) should all use pointers.In addition, please rename
Step
to beTaskStep
to make it more clear when seeing it by itself.And let's please change
Job
toWorkflowJob
to make it more descriptive as well.Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Friendly ping, @joshuabezaleel to switch these fields to pointers, please.