diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 18fdd57d6a..9fd01c4a56 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -94,6 +94,14 @@ type WorkflowRunAttemptOptions struct { ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"` } +// PendingDeploymentsRequest specifies body parameters to PendingDeployments. +type PendingDeploymentsRequest struct { + EnvironmentIDs []int64 `json:"environment_ids"` + // State can be one of: "approved", "rejected". + 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 +329,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..2705363ba6 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,47 @@ func TestWorkflowRunUsage_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } + +func TestActionService_PendingDeployments(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + 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) + 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 + }) +}