Skip to content

Commit

Permalink
Add 'pending_deployments' endpoint support (google#2421)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jul 27, 2022
1 parent 3a432d6 commit 579b109
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
27 changes: 27 additions & 0 deletions github/actions_workflow_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
46 changes: 46 additions & 0 deletions github/actions_workflow_runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
})
}
24 changes: 24 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 579b109

Please sign in to comment.