From 6cd585162df3dec9f75363b5f5e9e6df04cccf2f Mon Sep 17 00:00:00 2001 From: Justin Reeves Date: Mon, 14 Feb 2022 09:59:26 -0600 Subject: [PATCH 1/3] Add GetWorkflowRunAttempt --- github/actions_workflow_runs.go | 29 ++++++++++++++++++ github/actions_workflow_runs_test.go | 44 ++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 1e1ff61ae8..193ff5a8ab 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -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 { @@ -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 diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 7ebdbf58b3..607d06b711 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -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() From 1f0e394194c06db3b08517b77a43a4ec305abaed Mon Sep 17 00:00:00 2001 From: Justin Reeves Date: Mon, 14 Feb 2022 13:08:03 -0600 Subject: [PATCH 2/3] Use pointer for bool in ExcludePullRequests for WorkflowRunAttemptOptions --- github/actions_workflow_runs.go | 2 +- github/actions_workflow_runs_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 193ff5a8ab..273d2cc466 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -90,7 +90,7 @@ type WorkflowRunJobRun struct { // WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt. type WorkflowRunAttemptOptions struct { - ExcludePullRequests bool `url:"exclude_pull_requests,omitempty"` + ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` } func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 607d06b711..0b38ae38ce 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -153,7 +153,7 @@ func TestActionsService_GetWorkflowRunAttempt(t *testing.T) { 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} + opts := &WorkflowRunAttemptOptions{ExcludePullRequests: Bool(true)} ctx := context.Background() runs, _, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts) if err != nil { From 40c8110d2b257e4d3167c4c6254a78809f7b5c74 Mon Sep 17 00:00:00 2001 From: Justin Reeves Date: Mon, 14 Feb 2022 13:24:52 -0600 Subject: [PATCH 3/3] Add GetExcludePullRequests from go generate --- github/github-accessors.go | 8 ++++++++ github/github-accessors_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index 90b9233439..d4df8e45a6 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -19500,6 +19500,14 @@ func (w *WorkflowRun) GetWorkflowURL() string { return *w.WorkflowURL } +// GetExcludePullRequests returns the ExcludePullRequests field if it's non-nil, zero value otherwise. +func (w *WorkflowRunAttemptOptions) GetExcludePullRequests() bool { + if w == nil || w.ExcludePullRequests == nil { + return false + } + return *w.ExcludePullRequests +} + // GetJobs returns the Jobs field if it's non-nil, zero value otherwise. func (w *WorkflowRunBill) GetJobs() int { if w == nil || w.Jobs == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index df8754f518..76656ef5e7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -22840,6 +22840,16 @@ func TestWorkflowRun_GetWorkflowURL(tt *testing.T) { w.GetWorkflowURL() } +func TestWorkflowRunAttemptOptions_GetExcludePullRequests(tt *testing.T) { + var zeroValue bool + w := &WorkflowRunAttemptOptions{ExcludePullRequests: &zeroValue} + w.GetExcludePullRequests() + w = &WorkflowRunAttemptOptions{} + w.GetExcludePullRequests() + w = nil + w.GetExcludePullRequests() +} + func TestWorkflowRunBill_GetJobs(tt *testing.T) { var zeroValue int w := &WorkflowRunBill{Jobs: &zeroValue}