Repository: google/go-github
Pull request #2621 (2023-01-09 16:45:15)

Add new DeploymentBranchPolicies endpoints
This PR adds all the endpoints from deployment-branch-policies: https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#about-deployment-branch-policies

Fixes: #2610.

google-cla[bot]: Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this [failed invocation](https://github.com/google/go-github/pull/2621/checks?check_run_id=10530459439) of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.
gmlewis: Also, please make sure all unit tests pass locally before pushing the next commit.
Ismael-Moreno: This last one pass the three checks shown in the Contributing guide:
go generate github.com/google/go-github/...
go test github.com/google/go-github/...
go vet github.com/google/go-github/...
Please tell me if there is any other tests I'm missing, thank you for your patience.
codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2621?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2621](https://codecov.io/gh/google/go-github/pull/2621?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (12b0a53) into [master](https://codecov.io/gh/google/go-github/commit/e4ff2a27f7699954c87d81d723012c1c3f0de0f1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (e4ff2a2) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master    #2621      +/-   ##
==========================================
+ Coverage   98.02%   98.03%   +0.01%     
==========================================
  Files         129      130       +1     
  Lines       11084    11144      +60     
==========================================
+ Hits        10865    10925      +60     
  Misses        150      150              
  Partials       69       69              
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2621?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/repos\_deployments\_branch\_policies.go](https://codecov.io/gh/google/go-github/pull/2621?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL3JlcG9zX2RlcGxveW1lbnRzX2JyYW5jaF9wb2xpY2llcy5nbw==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

gmlewis: Thank you, @Parker77 !
Merging.

Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 1813988b..299e686a 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -4238,6 +4238,46 @@ func (d *Deployment) GetURL() string {
 	return *d.URL
 }
 
+// GetID returns the ID field if it's non-nil, zero value otherwise.
+func (d *DeploymentBranchPolicy) GetID() int64 {
+	if d == nil || d.ID == nil {
+		return 0
+	}
+	return *d.ID
+}
+
+// GetName returns the Name field if it's non-nil, zero value otherwise.
+func (d *DeploymentBranchPolicy) GetName() string {
+	if d == nil || d.Name == nil {
+		return ""
+	}
+	return *d.Name
+}
+
+// GetNodeID returns the NodeID field if it's non-nil, zero value otherwise.
+func (d *DeploymentBranchPolicy) GetNodeID() string {
+	if d == nil || d.NodeID == nil {
+		return ""
+	}
+	return *d.NodeID
+}
+
+// GetName returns the Name field if it's non-nil, zero value otherwise.
+func (d *DeploymentBranchPolicyRequest) GetName() string {
+	if d == nil || d.Name == nil {
+		return ""
+	}
+	return *d.Name
+}
+
+// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise.
+func (d *DeploymentBranchPolicyResponse) GetTotalCount() int {
+	if d == nil || d.TotalCount == nil {
+		return 0
+	}
+	return *d.TotalCount
+}
+
 // GetDeployment returns the Deployment field.
 func (d *DeploymentEvent) GetDeployment() *Deployment {
 	if d == nil {
diff --git a/github/repos_deployment_branch_policies.go b/github/repos_deployment_branch_policies.go
new file mode 100644
index 00000000..8c4628b3
--- /dev/null
+++ b/github/repos_deployment_branch_policies.go
@@ -0,0 +1,123 @@
+// Copyright 2023 The go-github AUTHORS. All rights reserved.
+//
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package github
+
+import (
+	"context"
+	"fmt"
+)
+
+// DeploymentBranchPolicy represents a single deployment branch policy for an environment.
+type DeploymentBranchPolicy struct {
+	Name   *string `json:"name,omitempty"`
+	ID     *int64  `json:"id,omitempty"`
+	NodeID *string `json:"node_id,omitempty"`
+}
+
+// DeploymentBranchPolicyResponse represents the slightly different format of response that comes back when you list deployment branch policies.
+type DeploymentBranchPolicyResponse struct {
+	TotalCount     *int                      `json:"total_count,omitempty"`
+	BranchPolicies []*DeploymentBranchPolicy `json:"branch_policies,omitempty"`
+}
+
+// DeploymentBranchPolicyRequest represents a deployment branch policy request.
+type DeploymentBranchPolicyRequest struct {
+	Name *string `json:"name,omitempty"`
+}
+
+// ListDeploymentBranchPolicies lists the deployment branch policies for an environment.
+//
+// GitHub API docs: https://docs.github.com/en/rest/deployments/branch-policies#list-deployment-branch-policies
+func (s *RepositoriesService) ListDeploymentBranchPolicies(ctx context.Context, owner, repo, environment string) (*DeploymentBranchPolicyResponse, *Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies", owner, repo, environment)
+
+	req, err := s.client.NewRequest("GET", u, nil)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var list *DeploymentBranchPolicyResponse
+	resp, err := s.client.Do(ctx, req, &list)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return list, resp, nil
+}
+
+// GetDeploymentBranchPolicy gets a deployment branch policy for an environment.
+//
+// GitHub API docs: https://docs.github.com/en/rest/deployments/branch-policies#get-a-deployment-branch-policy
+func (s *RepositoriesService) GetDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64) (*DeploymentBranchPolicy, *Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID)
+
+	req, err := s.client.NewRequest("GET", u, nil)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var policy *DeploymentBranchPolicy
+	resp, err := s.client.Do(ctx, req, &policy)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return policy, resp, nil
+}
+
+// CreateDeploymentBranchPolicy creates a deployment branch policy for an environment.
+//
+// GitHub API docs: https://docs.github.com/en/rest/deployments/branch-policies#create-a-deployment-branch-policy
+func (s *RepositoriesService) CreateDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, request *DeploymentBranchPolicyRequest) (*DeploymentBranchPolicy, *Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies", owner, repo, environment)
+
+	req, err := s.client.NewRequest("POST", u, request)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var policy *DeploymentBranchPolicy
+	resp, err := s.client.Do(ctx, req, &policy)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return policy, resp, nil
+}
+
+// UpdateDeploymentBranchPolicy updates a deployment branch policy for an environment.
+//
+// GitHub API docs: https://docs.github.com/en/rest/deployments/branch-policies#update-a-deployment-branch-policy
+func (s *RepositoriesService) UpdateDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64, request *DeploymentBranchPolicyRequest) (*DeploymentBranchPolicy, *Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID)
+
+	req, err := s.client.NewRequest("PUT", u, request)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var policy *DeploymentBranchPolicy
+	resp, err := s.client.Do(ctx, req, &policy)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return policy, resp, nil
+}
+
+// DeleteDeploymentBranchPolicy deletes a deployment branch policy for an environment.
+//
+// GitHub API docs: https://docs.github.com/en/rest/deployments/branch-policies#delete-a-deployment-branch-policy
+func (s *RepositoriesService) DeleteDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64) (*Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID)
+
+	req, err := s.client.NewRequest("DELETE", u, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	return s.client.Do(ctx, req, nil)
+}


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 3fba5154..12cbdfa1 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -4959,6 +4959,56 @@ func TestDeployment_GetURL(tt *testing.T) {
 	d.GetURL()
 }
 
+func TestDeploymentBranchPolicy_GetID(tt *testing.T) {
+	var zeroValue int64
+	d := &DeploymentBranchPolicy{ID: &zeroValue}
+	d.GetID()
+	d = &DeploymentBranchPolicy{}
+	d.GetID()
+	d = nil
+	d.GetID()
+}
+
+func TestDeploymentBranchPolicy_GetName(tt *testing.T) {
+	var zeroValue string
+	d := &DeploymentBranchPolicy{Name: &zeroValue}
+	d.GetName()
+	d = &DeploymentBranchPolicy{}
+	d.GetName()
+	d = nil
+	d.GetName()
+}
+
+func TestDeploymentBranchPolicy_GetNodeID(tt *testing.T) {
+	var zeroValue string
+	d := &DeploymentBranchPolicy{NodeID: &zeroValue}
+	d.GetNodeID()
+	d = &DeploymentBranchPolicy{}
+	d.GetNodeID()
+	d = nil
+	d.GetNodeID()
+}
+
+func TestDeploymentBranchPolicyRequest_GetName(tt *testing.T) {
+	var zeroValue string
+	d := &DeploymentBranchPolicyRequest{Name: &zeroValue}
+	d.GetName()
+	d = &DeploymentBranchPolicyRequest{}
+	d.GetName()
+	d = nil
+	d.GetName()
+}
+
+func TestDeploymentBranchPolicyResponse_GetTotalCount(tt *testing.T) {
+	var zeroValue int
+	d := &DeploymentBranchPolicyResponse{TotalCount: &zeroValue}
+	d.GetTotalCount()
+	d = &DeploymentBranchPolicyResponse{}
+	d.GetTotalCount()
+	d = nil
+	d.GetTotalCount()
+}
+
 func TestDeploymentEvent_GetDeployment(tt *testing.T) {
 	d := &DeploymentEvent{}
 	d.GetDeployment()
diff --git a/github/repos_deployment_branch_policies_test.go b/github/repos_deployment_branch_policies_test.go
new file mode 100644
index 00000000..69bffac8
--- /dev/null
+++ b/github/repos_deployment_branch_policies_test.go
@@ -0,0 +1,158 @@
+// Copyright 2023 The go-github AUTHORS. All rights reserved.
+//
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package github
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"reflect"
+	"testing"
+)
+
+func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprint(w, `{"total_count":2, "branch_policies":[{"id":1}, {"id": 2}]}`)
+	})
+
+	ctx := context.Background()
+	got, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e")
+	if err != nil {
+		t.Errorf("Repositories.ListDeploymentBranchPolicies returned error: %v", err)
+	}
+
+	want := &DeploymentBranchPolicyResponse{
+		BranchPolicies: []*DeploymentBranchPolicy{
+			{ID: Int64(1)},
+			{ID: Int64(2)},
+		},
+		TotalCount: Int(2),
+	}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want)
+	}
+
+	const methodName = "ListDeploymentBranchPolicies"
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		got, resp, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e")
+		if got != nil {
+			t.Errorf("got non-nil Repositories.ListDeploymentBranchPolicies response: %+v", got)
+		}
+		return resp, err
+	})
+}
+
+func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprint(w, `{"id":1}`)
+	})
+
+	ctx := context.Background()
+	got, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
+	if err != nil {
+		t.Errorf("Repositories.GetDeploymentBranchPolicy returned error: %v", err)
+	}
+
+	want := &DeploymentBranchPolicy{ID: Int64(1)}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want)
+	}
+
+	const methodName = "GetDeploymentBranchPolicy"
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		got, resp, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
+		if got != nil {
+			t.Errorf("got non-nil Repositories.GetDeploymentBranchPolicy response: %+v", got)
+		}
+		return resp, err
+	})
+}
+
+func TestRepositoriesService_CreateDeploymentBranchPolicy(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "POST")
+		fmt.Fprint(w, `{"id":1}`)
+	})
+
+	ctx := context.Background()
+	got, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n")})
+	if err != nil {
+		t.Errorf("Repositories.CreateDeploymentBranchPolicy returned error: %v", err)
+	}
+
+	want := &DeploymentBranchPolicy{ID: Int64(1)}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("Repositories.CreateDeploymentBranchPolicy = %+v, want %+v", got, want)
+	}
+
+	const methodName = "CreateDeploymentBranchPolicy"
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		got, resp, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n")})
+		if got != nil {
+			t.Errorf("got non-nil Repositories.CreateDeploymentBranchPolicy response: %+v", got)
+		}
+		return resp, err
+	})
+}
+
+func TestRepositoriesService_UpdateDeploymentBranchPolicy(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "PUT")
+		fmt.Fprint(w, `{"id":1}`)
+	})
+
+	ctx := context.Background()
+	got, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")})
+	if err != nil {
+		t.Errorf("Repositories.UpdateDeploymentBranchPolicy returned error: %v", err)
+	}
+
+	want := &DeploymentBranchPolicy{ID: Int64(1)}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("Repositories.UpdateDeploymentBranchPolicy = %+v, want %+v", got, want)
+	}
+
+	const methodName = "UpdateDeploymentBranchPolicy"
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		got, resp, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")})
+		if got != nil {
+			t.Errorf("got non-nil Repositories.UpdateDeploymentBranchPolicy response: %+v", got)
+		}
+		return resp, err
+	})
+}
+
+func TestRepositoriesService_DeleteDeploymentBranchPolicy(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "DELETE")
+	})
+
+	ctx := context.Background()
+	_, err := client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
+	if err != nil {
+		t.Errorf("Repositories.DeleteDeploymentBranchPolicy returned error: %v", err)
+	}
+
+	const methodName = "DeleteDeploymentBranchPolicy"
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		return client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
+	})
+}