From 14a50df132f69c0684fc6bc32c78913481bc258a Mon Sep 17 00:00:00 2001 From: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> Date: Tue, 17 Jan 2023 09:56:48 +1100 Subject: [PATCH 1/4] Add RequireLastPushApproval field Signed-off-by: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> --- github/repos.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/github/repos.go b/github/repos.go index bef81b3d89..247a55b6ea 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"` } // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review From ee62df7bc29690b63a7023af033b619bb855f4a6 Mon Sep 17 00:00:00 2001 From: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:05:06 +1100 Subject: [PATCH 2/4] Add new test for UpdateBranchProtection Signed-off-by: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> --- github/repos_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/github/repos_test.go b/github/repos_test.go index bcaa7d3d61..9088920862 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -1702,6 +1702,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: 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() From 66ca34f21e4a44c29062346b6b949fa4fc22eb1f Mon Sep 17 00:00:00 2001 From: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:21:38 +1100 Subject: [PATCH 3/4] Update test for PullRequestReviewsEnforcementRequest Signed-off-by: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> --- github/repos_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/github/repos_test.go b/github/repos_test.go index 9088920862..5e1644c80f 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"}], @@ -2578,7 +2581,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) } - want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` + want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":false}` if want != string(got) { t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) } @@ -2592,7 +2595,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) } - want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` + want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":false}` if want != string(got) { t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) } @@ -2603,6 +2606,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio Teams: &[]string{}, Apps: &[]string{}, }, + RequireLastPushApproval: true, } got, err = json.Marshal(req) @@ -2610,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) } From b1cf2278c8dfb10fff9deabb0677d5afd92b7088 Mon Sep 17 00:00:00 2001 From: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:48:59 +1100 Subject: [PATCH 4/4] Make RequireLastPushApproval field optional Signed-off-by: Daniel Liao <10663736+liaodaniel@users.noreply.github.com> --- github/github-accessors.go | 8 ++++++++ github/github-accessors_test.go | 10 ++++++++++ github/repos.go | 2 +- github/repos_test.go | 8 ++++---- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 299e686a27..9017783e18 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/github-accessors_test.go b/github/github-accessors_test.go index 12cbdfa1fb..da69e2632e 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.go b/github/repos.go index 247a55b6ea..f27444b727 100644 --- a/github/repos.go +++ b/github/repos.go @@ -1065,7 +1065,7 @@ type PullRequestReviewsEnforcementRequest struct { // 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"` + RequireLastPushApproval *bool `json:"require_last_push_approval,omitempty"` } // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review diff --git a/github/repos_test.go b/github/repos_test.go index 5e1644c80f..4a3f4f5986 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -1711,7 +1711,7 @@ func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *t input := &ProtectionRequest{ RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{ - RequireLastPushApproval: true, + RequireLastPushApproval: Bool(true), }, } @@ -2581,7 +2581,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) } - want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":false}` + want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` if want != string(got) { t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) } @@ -2595,7 +2595,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) } - want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":false}` + want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` if want != string(got) { t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) } @@ -2606,7 +2606,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio Teams: &[]string{}, Apps: &[]string{}, }, - RequireLastPushApproval: true, + RequireLastPushApproval: Bool(true), } got, err = json.Marshal(req)