Skip to content
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

Add GetWorkflowRunAttempt #2290

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"`
reeves122 marked this conversation as resolved.
Show resolved Hide resolved
}

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: 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