Skip to content

Commit 9390a86

Browse files
authored
Add ListWorkflowJobsAttempt method to ActionsService (#3060)
Fixes: #3059.
1 parent 975b2e5 commit 9390a86

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

github/actions_workflow_jobs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ func (s *ActionsService) ListWorkflowJobs(ctx context.Context, owner, repo strin
9494
return jobs, resp, nil
9595
}
9696

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+
97123
// GetWorkflowJobByID gets a specific job in a workflow run by ID.
98124
//
99125
// GitHub API docs: https://docs.github.com/rest/actions/workflow-jobs#get-a-job-for-a-workflow-run

github/actions_workflow_jobs_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,60 @@ func TestActionsService_ListWorkflowJobs_Filter(t *testing.T) {
8989
}
9090
}
9191

92+
func TestActionsService_ListWorkflowJobsAttempt(t *testing.T) {
93+
client, mux, _, teardown := setup()
94+
defer teardown()
95+
96+
mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/1/jobs", func(w http.ResponseWriter, r *http.Request) {
97+
testMethod(t, r, "GET")
98+
testFormValues(t, r, values{"per_page": "2", "page": "2"})
99+
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","run_attempt":2},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z","run_attempt":2}]}`)
100+
})
101+
opts := &ListOptions{Page: 2, PerPage: 2}
102+
ctx := context.Background()
103+
jobs, _, err := client.Actions.ListWorkflowJobsAttempt(ctx, "o", "r", 29679449, 1, opts)
104+
if err != nil {
105+
t.Errorf("Actions.ListWorkflowJobsAttempt returned error: %v", err)
106+
}
107+
108+
want := &Jobs{
109+
TotalCount: Int(4),
110+
Jobs: []*WorkflowJob{
111+
{
112+
ID: Int64(399444496),
113+
RunID: Int64(29679449),
114+
StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
115+
CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
116+
RunAttempt: Int64(2),
117+
},
118+
{
119+
ID: Int64(399444497),
120+
RunID: Int64(29679449),
121+
StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
122+
CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
123+
RunAttempt: Int64(2),
124+
},
125+
},
126+
}
127+
if !cmp.Equal(jobs, want) {
128+
t.Errorf("Actions.ListWorkflowJobsAttempt returned %+v, want %+v", jobs, want)
129+
}
130+
131+
const methodName = "ListWorkflowJobsAttempt"
132+
testBadOptions(t, methodName, func() (err error) {
133+
_, _, err = client.Actions.ListWorkflowJobsAttempt(ctx, "\n", "\n", 29679449, 1, opts)
134+
return err
135+
})
136+
137+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
138+
got, resp, err := client.Actions.ListWorkflowJobsAttempt(ctx, "o", "r", 29679449, 1, opts)
139+
if got != nil {
140+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
141+
}
142+
return resp, err
143+
})
144+
}
145+
92146
func TestActionsService_GetWorkflowJobByID(t *testing.T) {
93147
client, mux, _, teardown := setup()
94148
defer teardown()

0 commit comments

Comments
 (0)