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

Always send 'checks' field when creating branch protection #2468

Merged
merged 3 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion github/repos.go
Expand Up @@ -905,7 +905,7 @@ type RequiredStatusChecks struct {
Contexts []string `json:"contexts,omitempty"`
// The list of status checks to require in order to merge into this
// branch.
Checks []*RequiredStatusCheck `json:"checks,omitempty"`
Checks []*RequiredStatusCheck `json:"checks"`
}

// RequiredStatusChecksRequest represents a request to edit a protected branch's status checks.
Expand Down
124 changes: 124 additions & 0 deletions github/repos_test.go
Expand Up @@ -1543,6 +1543,130 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
}
}

func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &ProtectionRequest{
RequiredStatusChecks: &RequiredStatusChecks{
Strict: true,
Checks: []*RequiredStatusCheck{},
},
RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
DismissStaleReviews: true,
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"uu"},
Teams: &[]string{"tt"},
},
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
Users: []string{"uuu"},
Teams: []string{"ttt"},
Apps: []string{"aaa"},
},
},
Restrictions: &BranchRestrictionsRequest{
Users: []string{"u"},
Teams: []string{"t"},
Apps: []string{"a"},
},
}

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)
}

// TODO: remove custom Accept header when this API fully launches
testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
fmt.Fprintf(w, `{
"required_status_checks":{
"strict":true,
"contexts":[],
"checks": []
},
"required_pull_request_reviews":{
"dismissal_restrictions":{
"users":[{
"id":3,
"login":"uu"
}],
"teams":[{
"id":4,
"slug":"tt"
}]
},
"dismiss_stale_reviews":true,
"require_code_owner_reviews":true,
"bypass_pull_request_allowances": {
"users":[{"id":10,"login":"uuu"}],
"teams":[{"id":20,"slug":"ttt"}],
"apps":[{"id":30,"slug":"aaa"}]
}
},
"restrictions":{
"users":[{"id":1,"login":"u"}],
"teams":[{"id":2,"slug":"t"}],
"apps":[{"id":3,"slug":"a"}]
}
}`)
})

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{
RequiredStatusChecks: &RequiredStatusChecks{
Strict: true,
Contexts: []string{},
Checks: []*RequiredStatusCheck{},
},
RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
DismissStaleReviews: true,
DismissalRestrictions: &DismissalRestrictions{
Users: []*User{
{Login: String("uu"), ID: Int64(3)},
},
Teams: []*Team{
{Slug: String("tt"), ID: Int64(4)},
},
},
RequireCodeOwnerReviews: true,
BypassPullRequestAllowances: &BypassPullRequestAllowances{
Users: []*User{
{Login: String("uuu"), ID: Int64(10)},
},
Teams: []*Team{
{Slug: String("ttt"), ID: Int64(20)},
},
Apps: []*App{
{Slug: String("aaa"), ID: Int64(30)},
},
},
},
Restrictions: &BranchRestrictions{
Users: []*User{
{Login: String("u"), ID: Int64(1)},
},
Teams: []*Team{
{Slug: String("t"), ID: Int64(2)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(3)},
},
},
}
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()
Expand Down