Skip to content

Commit 9708994

Browse files
authored
Add RequireLastPushApproval field to UpdateBranchProtection (#2629)
Fixes: #2628.
1 parent ea25803 commit 9708994

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,8 @@ type PullRequestReviewsEnforcementRequest struct {
10641064
// RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
10651065
// Valid values are 1-6.
10661066
RequiredApprovingReviewCount int `json:"required_approving_review_count"`
1067+
// RequireLastPushApproval specifies whether the last pusher to a pull request branch can approve it.
1068+
RequireLastPushApproval *bool `json:"require_last_push_approval,omitempty"`
10671069
}
10681070

10691071
// PullRequestReviewsEnforcementUpdate represents request to patch the pull request review

github/repos_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
10821082
},
10831083
"dismiss_stale_reviews":true,
10841084
"require_code_owner_reviews":true,
1085+
"require_last_push_approval":false,
10851086
"required_approving_review_count":1
10861087
},
10871088
"enforce_admins":{
@@ -1130,6 +1131,7 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
11301131
},
11311132
RequireCodeOwnerReviews: true,
11321133
RequiredApprovingReviewCount: 1,
1134+
RequireLastPushApproval: false,
11331135
},
11341136
EnforceAdmins: &AdminEnforcement{
11351137
URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
@@ -1633,6 +1635,7 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
16331635
},
16341636
"dismiss_stale_reviews":true,
16351637
"require_code_owner_reviews":true,
1638+
"require_last_push_approval":false,
16361639
"bypass_pull_request_allowances": {
16371640
"users":[{"id":10,"login":"uuu"}],
16381641
"teams":[{"id":20,"slug":"ttt"}],
@@ -1702,6 +1705,48 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
17021705
}
17031706
}
17041707

1708+
func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *testing.T) {
1709+
client, mux, _, teardown := setup()
1710+
defer teardown()
1711+
1712+
input := &ProtectionRequest{
1713+
RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1714+
RequireLastPushApproval: Bool(true),
1715+
},
1716+
}
1717+
1718+
mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1719+
v := new(ProtectionRequest)
1720+
json.NewDecoder(r.Body).Decode(v)
1721+
1722+
testMethod(t, r, "PUT")
1723+
if !cmp.Equal(v, input) {
1724+
t.Errorf("Request body = %+v, want %+v", v, input)
1725+
}
1726+
1727+
fmt.Fprintf(w, `{
1728+
"required_pull_request_reviews":{
1729+
"require_last_push_approval":true
1730+
}
1731+
}`)
1732+
})
1733+
1734+
ctx := context.Background()
1735+
protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1736+
if err != nil {
1737+
t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1738+
}
1739+
1740+
want := &Protection{
1741+
RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1742+
RequireLastPushApproval: true,
1743+
},
1744+
}
1745+
if !cmp.Equal(protection, want) {
1746+
t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1747+
}
1748+
}
1749+
17051750
func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
17061751
client, mux, _, teardown := setup()
17071752
defer teardown()
@@ -2561,14 +2606,15 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio
25612606
Teams: &[]string{},
25622607
Apps: &[]string{},
25632608
},
2609+
RequireLastPushApproval: Bool(true),
25642610
}
25652611

25662612
got, err = json.Marshal(req)
25672613
if err != nil {
25682614
t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
25692615
}
25702616

2571-
want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2617+
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}`
25722618
if want != string(got) {
25732619
t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
25742620
}

0 commit comments

Comments
 (0)