Skip to content

Commit

Permalink
Add GetWorkflowRunAttempt (#2290)
Browse files Browse the repository at this point in the history
* Add GetWorkflowRunAttempt

* Use pointer for bool in ExcludePullRequests for WorkflowRunAttemptOptions

* Add GetExcludePullRequests from go generate
  • Loading branch information
reeves122 committed Feb 14, 2022
1 parent 41cbf81 commit 2981e4f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
29 changes: 29 additions & 0 deletions github/actions_workflow_runs.go
Expand Up @@ -88,6 +88,11 @@ type WorkflowRunJobRun struct {
DurationMS *int64 `json:"duration_ms,omitempty"`
}

// WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt.
type WorkflowRunAttemptOptions struct {
ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"`
}

func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
u, err := addOptions(endpoint, opts)
if err != nil {
Expand Down Expand Up @@ -168,6 +173,30 @@ func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo str
return run, resp, nil
}

// GetWorkflowRunAttempt gets a specific workflow run attempt.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-workflow-run-attempt
func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
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
}

run := new(WorkflowRun)
resp, err := s.client.Do(ctx, req, run)
if err != nil {
return nil, resp, err
}

return run, resp, nil
}

// RerunWorkflowByID re-runs a workflow by ID.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#re-run-a-workflow
Expand Down
44 changes: 44 additions & 0 deletions github/actions_workflow_runs_test.go
Expand Up @@ -143,6 +143,50 @@ func TestActionsService_GetWorkflowRunByID(t *testing.T) {
})
}

func TestActionsService_GetWorkflowRunAttempt(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/3", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"exclude_pull_requests": "true"})
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"}}`)
})

opts := &WorkflowRunAttemptOptions{ExcludePullRequests: Bool(true)}
ctx := context.Background()
runs, _, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
if err != nil {
t.Errorf("Actions.GetWorkflowRunAttempt returned error: %v", err)
}

want := &WorkflowRun{
ID: Int64(399444496),
RunNumber: Int(296),
RunAttempt: Int(3),
CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
}

if !cmp.Equal(runs, want) {
t.Errorf("Actions.GetWorkflowRunAttempt returned %+v, want %+v", runs, want)
}

const methodName = "GetWorkflowRunAttempt"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Actions.GetWorkflowRunAttempt(ctx, "\n", "\n", 29679449, 3, opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActionsService_RerunWorkflowRunByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2981e4f

Please sign in to comment.