Skip to content

Commit 2981e4f

Browse files
authored
Add GetWorkflowRunAttempt (google#2290)
* Add GetWorkflowRunAttempt * Use pointer for bool in ExcludePullRequests for WorkflowRunAttemptOptions * Add GetExcludePullRequests from go generate
1 parent 41cbf81 commit 2981e4f

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

github/actions_workflow_runs.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ type WorkflowRunJobRun struct {
8888
DurationMS *int64 `json:"duration_ms,omitempty"`
8989
}
9090

91+
// WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt.
92+
type WorkflowRunAttemptOptions struct {
93+
ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"`
94+
}
95+
9196
func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
9297
u, err := addOptions(endpoint, opts)
9398
if err != nil {
@@ -168,6 +173,30 @@ func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo str
168173
return run, resp, nil
169174
}
170175

176+
// GetWorkflowRunAttempt gets a specific workflow run attempt.
177+
//
178+
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-workflow-run-attempt
179+
func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
180+
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
181+
u, err := addOptions(u, opts)
182+
if err != nil {
183+
return nil, nil, err
184+
}
185+
186+
req, err := s.client.NewRequest("GET", u, nil)
187+
if err != nil {
188+
return nil, nil, err
189+
}
190+
191+
run := new(WorkflowRun)
192+
resp, err := s.client.Do(ctx, req, run)
193+
if err != nil {
194+
return nil, resp, err
195+
}
196+
197+
return run, resp, nil
198+
}
199+
171200
// RerunWorkflowByID re-runs a workflow by ID.
172201
//
173202
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#re-run-a-workflow

github/actions_workflow_runs_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,50 @@ func TestActionsService_GetWorkflowRunByID(t *testing.T) {
143143
})
144144
}
145145

146+
func TestActionsService_GetWorkflowRunAttempt(t *testing.T) {
147+
client, mux, _, teardown := setup()
148+
defer teardown()
149+
150+
mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/3", func(w http.ResponseWriter, r *http.Request) {
151+
testMethod(t, r, "GET")
152+
testFormValues(t, r, values{"exclude_pull_requests": "true"})
153+
fmt.Fprint(w, `{"id":399444496,"run_number":296,"run_attempt":3,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`)
154+
})
155+
156+
opts := &WorkflowRunAttemptOptions{ExcludePullRequests: Bool(true)}
157+
ctx := context.Background()
158+
runs, _, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
159+
if err != nil {
160+
t.Errorf("Actions.GetWorkflowRunAttempt returned error: %v", err)
161+
}
162+
163+
want := &WorkflowRun{
164+
ID: Int64(399444496),
165+
RunNumber: Int(296),
166+
RunAttempt: Int(3),
167+
CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
168+
UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
169+
}
170+
171+
if !cmp.Equal(runs, want) {
172+
t.Errorf("Actions.GetWorkflowRunAttempt returned %+v, want %+v", runs, want)
173+
}
174+
175+
const methodName = "GetWorkflowRunAttempt"
176+
testBadOptions(t, methodName, func() (err error) {
177+
_, _, err = client.Actions.GetWorkflowRunAttempt(ctx, "\n", "\n", 29679449, 3, opts)
178+
return err
179+
})
180+
181+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
182+
got, resp, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
183+
if got != nil {
184+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
185+
}
186+
return resp, err
187+
})
188+
}
189+
146190
func TestActionsService_RerunWorkflowRunByID(t *testing.T) {
147191
client, mux, _, teardown := setup()
148192
defer teardown()

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)