diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 18fdd57d6a..b741998303 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 d22b8a52e8..7103e28455 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 334ff39510..17676efaf6 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 4339a879f6..51ab7cd0cd 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()