Skip to content

Commit

Permalink
Always send 'checks' field when creating branch protection (#2468)
Browse files Browse the repository at this point in the history
Fixes: #2467
  • Loading branch information
luisdavim authored Sep 19, 2022
1 parent 642c343 commit 23a2636
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion github/repos.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 23a2636

Please sign in to comment.