Repository: google/go-github
Pull request #2629 (2023-01-16 23:26:34)

Add RequireLastPushApproval field to UpdateBranchProtection
Fixes https://github.com/google/go-github/issues/2628

Signed-off-by: Daniel Liao <10663736+liaodaniel@users.noreply.github.com>

codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2629?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2629](https://codecov.io/gh/google/go-github/pull/2629?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (66ca34f) into [master](https://codecov.io/gh/google/go-github/commit/ea25803a8490a2b065395a36b9fca59ea3ac12f2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (ea25803) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master    #2629   +/-   ##
=======================================
  Coverage   98.03%   98.03%           
=======================================
  Files         130      130           
  Lines       11144    11144           
=======================================
  Hits        10925    10925           
  Misses        150      150           
  Partials       69       69           
```


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

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)


Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 299e686a..9017783e 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -13854,6 +13854,14 @@ func (p *PullRequestReviewsEnforcementRequest) GetDismissalRestrictionsRequest()
 	return p.DismissalRestrictionsRequest
 }
 
+// GetRequireLastPushApproval returns the RequireLastPushApproval field if it's non-nil, zero value otherwise.
+func (p *PullRequestReviewsEnforcementRequest) GetRequireLastPushApproval() bool {
+	if p == nil || p.RequireLastPushApproval == nil {
+		return false
+	}
+	return *p.RequireLastPushApproval
+}
+
 // GetBypassPullRequestAllowancesRequest returns the BypassPullRequestAllowancesRequest field.
 func (p *PullRequestReviewsEnforcementUpdate) GetBypassPullRequestAllowancesRequest() *BypassPullRequestAllowancesRequest {
 	if p == nil {
diff --git a/github/repos.go b/github/repos.go
index bef81b3d..f27444b7 100644
--- a/github/repos.go
+++ b/github/repos.go
@@ -1064,6 +1064,8 @@ type PullRequestReviewsEnforcementRequest struct {
 	// RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
 	// Valid values are 1-6.
 	RequiredApprovingReviewCount int `json:"required_approving_review_count"`
+	// RequireLastPushApproval specifies whether the last pusher to a pull request branch can approve it.
+	RequireLastPushApproval *bool `json:"require_last_push_approval,omitempty"`
 }
 
 // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 12cbdfa1..da69e263 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -16100,6 +16100,16 @@ func TestPullRequestReviewsEnforcementRequest_GetDismissalRestrictionsRequest(tt
 	p.GetDismissalRestrictionsRequest()
 }
 
+func TestPullRequestReviewsEnforcementRequest_GetRequireLastPushApproval(tt *testing.T) {
+	var zeroValue bool
+	p := &PullRequestReviewsEnforcementRequest{RequireLastPushApproval: &zeroValue}
+	p.GetRequireLastPushApproval()
+	p = &PullRequestReviewsEnforcementRequest{}
+	p.GetRequireLastPushApproval()
+	p = nil
+	p.GetRequireLastPushApproval()
+}
+
 func TestPullRequestReviewsEnforcementUpdate_GetBypassPullRequestAllowancesRequest(tt *testing.T) {
 	p := &PullRequestReviewsEnforcementUpdate{}
 	p.GetBypassPullRequestAllowancesRequest()
diff --git a/github/repos_test.go b/github/repos_test.go
index bcaa7d3d..4a3f4f59 100644
--- a/github/repos_test.go
+++ b/github/repos_test.go
@@ -1082,6 +1082,7 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
 					},
 					"dismiss_stale_reviews":true,
 					"require_code_owner_reviews":true,
+					"require_last_push_approval":false,
 					"required_approving_review_count":1
 					},
 					"enforce_admins":{
@@ -1130,6 +1131,7 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
 			},
 			RequireCodeOwnerReviews:      true,
 			RequiredApprovingReviewCount: 1,
+			RequireLastPushApproval:      false,
 		},
 		EnforceAdmins: &AdminEnforcement{
 			URL:     String("/repos/o/r/branches/b/protection/enforce_admins"),
@@ -1633,6 +1635,7 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
 				},
 				"dismiss_stale_reviews":true,
 				"require_code_owner_reviews":true,
+				"require_last_push_approval":false,
 				"bypass_pull_request_allowances": {
 					"users":[{"id":10,"login":"uuu"}],
 					"teams":[{"id":20,"slug":"ttt"}],
@@ -1702,6 +1705,48 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
 	}
 }
 
+func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	input := &ProtectionRequest{
+		RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
+			RequireLastPushApproval: Bool(true),
+		},
+	}
+
+	mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
+		v := new(ProtectionRequest)
+		json.NewDecoder(r.Body).Decode(v)
+
+		testMethod(t, r, "PUT")
+		if !cmp.Equal(v, input) {
+			t.Errorf("Request body = %+v, want %+v", v, input)
+		}
+
+		fmt.Fprintf(w, `{
+			"required_pull_request_reviews":{
+				"require_last_push_approval":true
+			}
+		}`)
+	})
+
+	ctx := context.Background()
+	protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
+	if err != nil {
+		t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
+	}
+
+	want := &Protection{
+		RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
+			RequireLastPushApproval: true,
+		},
+	}
+	if !cmp.Equal(protection, want) {
+		t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
+	}
+}
+
 func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
 	client, mux, _, teardown := setup()
 	defer teardown()
@@ -2561,6 +2606,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio
 			Teams: &[]string{},
 			Apps:  &[]string{},
 		},
+		RequireLastPushApproval: Bool(true),
 	}
 
 	got, err = json.Marshal(req)
@@ -2568,7 +2614,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio
 		t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
 	}
 
-	want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
+	want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":true}`
 	if want != string(got) {
 		t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
 	}