From 579b10953725a4fc041c97e8c2a7021c72089f60 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Wed, 27 Jul 2022 12:16:30 -0600 Subject: [PATCH 1/5] Add 'pending_deployments' endpoint support (#2421) --- github/actions_workflow_runs.go | 27 ++++++++++++++++ github/actions_workflow_runs_test.go | 46 ++++++++++++++++++++++++++++ github/github-accessors.go | 24 +++++++++++++++ github/github-accessors_test.go | 30 ++++++++++++++++++ 4 files changed, 127 insertions(+) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 18fdd57d6a2..b7419983038 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -94,6 +94,13 @@ type WorkflowRunAttemptOptions struct { ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` } +// PendingDeploymentsRequest specificies body parameters to PendingDeployments +type PendingDeploymentsRequest struct { + EnvironmentIDs *[]int `json:"environment_ids"` + State *string `json:"state"` + Comment *string `json:"comment"` +} + func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { u, err := addOptions(endpoint, opts) if err != nil { @@ -321,3 +328,23 @@ func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, rep return workflowRunUsage, resp, nil } + +// PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run +func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID) + + req, err := s.client.NewRequest("POST", u, request) + if err != nil { + return nil, nil, err + } + + var deployments []*Deployment + resp, err := s.client.Do(ctx, req, &deployments) + if err != nil { + return nil, resp, err + } + + return deployments, resp, nil +} diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index d22b8a52e84..7103e28455c 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -7,6 +7,7 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" "net/url" @@ -1075,3 +1076,48 @@ func TestWorkflowRunUsage_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } + +func TestActionService_PendingDeployments(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + envIDs := []int{3, 4} + input := &PendingDeploymentsRequest{EnvironmentIDs: &envIDs, State: String("approved"), Comment: String("")} + + mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) { + v := new(PendingDeploymentsRequest) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "POST") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `[{"id":1}, {"id":2}]`) + }) + + ctx := context.Background() + deployments, _, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input) + if err != nil { + t.Errorf("Actions.PendingDeployments returned error: %v", err) + } + + want := []*Deployment{{ID: Int64(1)}, {ID: Int64(2)}} + if !cmp.Equal(deployments, want) { + t.Errorf("Actions.PendingDeployments returned %+v, want %+v", deployments, want) + } + + const methodName = "PendingDeployments" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.PendingDeployments(ctx, "\n", "\n", 399444496, input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/github-accessors.go b/github/github-accessors.go index 334ff395100..17676efaf6f 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -10814,6 +10814,30 @@ func (p *PagesUpdate) GetSource() *PagesSource { return p.Source } +// GetComment returns the Comment field if it's non-nil, zero value otherwise. +func (p *PendingDeploymentsRequest) GetComment() string { + if p == nil || p.Comment == nil { + return "" + } + return *p.Comment +} + +// GetEnvironmentIDs returns the EnvironmentIDs field if it's non-nil, zero value otherwise. +func (p *PendingDeploymentsRequest) GetEnvironmentIDs() []int { + if p == nil || p.EnvironmentIDs == nil { + return nil + } + return *p.EnvironmentIDs +} + +// GetState returns the State field if it's non-nil, zero value otherwise. +func (p *PendingDeploymentsRequest) GetState() string { + if p == nil || p.State == nil { + return "" + } + return *p.State +} + // GetHook returns the Hook field. func (p *PingEvent) GetHook() *Hook { if p == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 4339a879f65..51ab7cd0cd6 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -12669,6 +12669,36 @@ func TestPagesUpdate_GetSource(tt *testing.T) { p.GetSource() } +func TestPendingDeploymentsRequest_GetComment(tt *testing.T) { + var zeroValue string + p := &PendingDeploymentsRequest{Comment: &zeroValue} + p.GetComment() + p = &PendingDeploymentsRequest{} + p.GetComment() + p = nil + p.GetComment() +} + +func TestPendingDeploymentsRequest_GetEnvironmentIDs(tt *testing.T) { + var zeroValue []int + p := &PendingDeploymentsRequest{EnvironmentIDs: &zeroValue} + p.GetEnvironmentIDs() + p = &PendingDeploymentsRequest{} + p.GetEnvironmentIDs() + p = nil + p.GetEnvironmentIDs() +} + +func TestPendingDeploymentsRequest_GetState(tt *testing.T) { + var zeroValue string + p := &PendingDeploymentsRequest{State: &zeroValue} + p.GetState() + p = &PendingDeploymentsRequest{} + p.GetState() + p = nil + p.GetState() +} + func TestPingEvent_GetHook(tt *testing.T) { p := &PingEvent{} p.GetHook() From 3934f3916a3982b511fbd359896cc184634c53a5 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Wed, 27 Jul 2022 13:52:24 -0600 Subject: [PATCH 2/5] Fix Environment ID type in PendingDeploymentsRequest (#2421) --- github/actions_workflow_runs.go | 2 +- github/actions_workflow_runs_test.go | 3 +-- github/github-accessors.go | 8 -------- github/github-accessors_test.go | 10 ---------- 4 files changed, 2 insertions(+), 21 deletions(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index b7419983038..c31ab2ef035 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -96,7 +96,7 @@ type WorkflowRunAttemptOptions struct { // PendingDeploymentsRequest specificies body parameters to PendingDeployments type PendingDeploymentsRequest struct { - EnvironmentIDs *[]int `json:"environment_ids"` + EnvironmentIDs []int64 `json:"environment_ids"` State *string `json:"state"` Comment *string `json:"comment"` } diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 7103e28455c..5da6063d787 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -1081,8 +1081,7 @@ func TestActionService_PendingDeployments(t *testing.T) { client, mux, _, teardown := setup() defer teardown() - envIDs := []int{3, 4} - input := &PendingDeploymentsRequest{EnvironmentIDs: &envIDs, State: String("approved"), Comment: String("")} + input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: String("approved"), Comment: String("")} mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) { v := new(PendingDeploymentsRequest) diff --git a/github/github-accessors.go b/github/github-accessors.go index 17676efaf6f..c4d7f51b8ba 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -10822,14 +10822,6 @@ func (p *PendingDeploymentsRequest) GetComment() string { return *p.Comment } -// GetEnvironmentIDs returns the EnvironmentIDs field if it's non-nil, zero value otherwise. -func (p *PendingDeploymentsRequest) GetEnvironmentIDs() []int { - if p == nil || p.EnvironmentIDs == nil { - return nil - } - return *p.EnvironmentIDs -} - // GetState returns the State field if it's non-nil, zero value otherwise. func (p *PendingDeploymentsRequest) GetState() string { if p == nil || p.State == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 51ab7cd0cd6..ff14e522122 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -12679,16 +12679,6 @@ func TestPendingDeploymentsRequest_GetComment(tt *testing.T) { p.GetComment() } -func TestPendingDeploymentsRequest_GetEnvironmentIDs(tt *testing.T) { - var zeroValue []int - p := &PendingDeploymentsRequest{EnvironmentIDs: &zeroValue} - p.GetEnvironmentIDs() - p = &PendingDeploymentsRequest{} - p.GetEnvironmentIDs() - p = nil - p.GetEnvironmentIDs() -} - func TestPendingDeploymentsRequest_GetState(tt *testing.T) { var zeroValue string p := &PendingDeploymentsRequest{State: &zeroValue} From 70feb57f33d36aae1f22d077ce84f9690392d0e8 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Wed, 27 Jul 2022 14:55:40 -0600 Subject: [PATCH 3/5] Typo Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/actions_workflow_runs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index c31ab2ef035..a0ac279ecdf 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -94,7 +94,7 @@ type WorkflowRunAttemptOptions struct { ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` } -// PendingDeploymentsRequest specificies body parameters to PendingDeployments +// PendingDeploymentsRequest specifies body parameters to PendingDeployments. type PendingDeploymentsRequest struct { EnvironmentIDs []int64 `json:"environment_ids"` State *string `json:"state"` From 5874537a1763a7c1fb9363827de2df30bbfad497 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Wed, 27 Jul 2022 14:56:27 -0600 Subject: [PATCH 4/5] Indicate valid values for State attribute in PendingDeploymentsRequest Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/actions_workflow_runs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index a0ac279ecdf..fc353abbd7f 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -97,7 +97,8 @@ type WorkflowRunAttemptOptions struct { // PendingDeploymentsRequest specifies body parameters to PendingDeployments. type PendingDeploymentsRequest struct { EnvironmentIDs []int64 `json:"environment_ids"` - State *string `json:"state"` + // State can be one of: "approved", "rejected". + State string `json:"state"` Comment *string `json:"comment"` } From bfb419c0b3d9fdbdad04130f7dcb40a2469a64b5 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Wed, 27 Jul 2022 15:01:07 -0600 Subject: [PATCH 5/5] Remove pointer to required attributes for PendingDeploymentsRequest --- github/actions_workflow_runs.go | 4 ++-- github/actions_workflow_runs_test.go | 2 +- github/github-accessors.go | 16 ---------------- github/github-accessors_test.go | 20 -------------------- 4 files changed, 3 insertions(+), 39 deletions(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index fc353abbd7f..9fd01c4a563 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -98,8 +98,8 @@ type WorkflowRunAttemptOptions struct { type PendingDeploymentsRequest struct { EnvironmentIDs []int64 `json:"environment_ids"` // State can be one of: "approved", "rejected". - State string `json:"state"` - Comment *string `json:"comment"` + State string `json:"state"` + Comment string `json:"comment"` } 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 5da6063d787..2705363ba64 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -1081,7 +1081,7 @@ func TestActionService_PendingDeployments(t *testing.T) { client, mux, _, teardown := setup() defer teardown() - input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: String("approved"), Comment: String("")} + input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: "approved", Comment: ""} mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) { v := new(PendingDeploymentsRequest) diff --git a/github/github-accessors.go b/github/github-accessors.go index c4d7f51b8ba..334ff395100 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -10814,22 +10814,6 @@ func (p *PagesUpdate) GetSource() *PagesSource { return p.Source } -// GetComment returns the Comment field if it's non-nil, zero value otherwise. -func (p *PendingDeploymentsRequest) GetComment() string { - if p == nil || p.Comment == nil { - return "" - } - return *p.Comment -} - -// GetState returns the State field if it's non-nil, zero value otherwise. -func (p *PendingDeploymentsRequest) GetState() string { - if p == nil || p.State == nil { - return "" - } - return *p.State -} - // GetHook returns the Hook field. func (p *PingEvent) GetHook() *Hook { if p == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index ff14e522122..4339a879f65 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -12669,26 +12669,6 @@ func TestPagesUpdate_GetSource(tt *testing.T) { p.GetSource() } -func TestPendingDeploymentsRequest_GetComment(tt *testing.T) { - var zeroValue string - p := &PendingDeploymentsRequest{Comment: &zeroValue} - p.GetComment() - p = &PendingDeploymentsRequest{} - p.GetComment() - p = nil - p.GetComment() -} - -func TestPendingDeploymentsRequest_GetState(tt *testing.T) { - var zeroValue string - p := &PendingDeploymentsRequest{State: &zeroValue} - p.GetState() - p = &PendingDeploymentsRequest{} - p.GetState() - p = nil - p.GetState() -} - func TestPingEvent_GetHook(tt *testing.T) { p := &PingEvent{} p.GetHook()