Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'pending_deployments' endpoint support (#2421) #2422

Merged
merged 5 commits into from Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions github/actions_workflow_runs.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
45 changes: 45 additions & 0 deletions github/actions_workflow_runs_test.go
Expand Up @@ -7,6 +7,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
})
}