Skip to content

Commit 23a2636

Browse files
author
Luis Davim
authored
Always send 'checks' field when creating branch protection (#2468)
Fixes: #2467
1 parent 642c343 commit 23a2636

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

github/repos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ type RequiredStatusChecks struct {
905905
Contexts []string `json:"contexts,omitempty"`
906906
// The list of status checks to require in order to merge into this
907907
// branch.
908-
Checks []*RequiredStatusCheck `json:"checks,omitempty"`
908+
Checks []*RequiredStatusCheck `json:"checks"`
909909
}
910910

911911
// RequiredStatusChecksRequest represents a request to edit a protected branch's status checks.

github/repos_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,130 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
15431543
}
15441544
}
15451545

1546+
func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) {
1547+
client, mux, _, teardown := setup()
1548+
defer teardown()
1549+
1550+
input := &ProtectionRequest{
1551+
RequiredStatusChecks: &RequiredStatusChecks{
1552+
Strict: true,
1553+
Checks: []*RequiredStatusCheck{},
1554+
},
1555+
RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1556+
DismissStaleReviews: true,
1557+
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1558+
Users: &[]string{"uu"},
1559+
Teams: &[]string{"tt"},
1560+
},
1561+
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1562+
Users: []string{"uuu"},
1563+
Teams: []string{"ttt"},
1564+
Apps: []string{"aaa"},
1565+
},
1566+
},
1567+
Restrictions: &BranchRestrictionsRequest{
1568+
Users: []string{"u"},
1569+
Teams: []string{"t"},
1570+
Apps: []string{"a"},
1571+
},
1572+
}
1573+
1574+
mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1575+
v := new(ProtectionRequest)
1576+
json.NewDecoder(r.Body).Decode(v)
1577+
1578+
testMethod(t, r, "PUT")
1579+
if !cmp.Equal(v, input) {
1580+
t.Errorf("Request body = %+v, want %+v", v, input)
1581+
}
1582+
1583+
// TODO: remove custom Accept header when this API fully launches
1584+
testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1585+
fmt.Fprintf(w, `{
1586+
"required_status_checks":{
1587+
"strict":true,
1588+
"contexts":[],
1589+
"checks": []
1590+
},
1591+
"required_pull_request_reviews":{
1592+
"dismissal_restrictions":{
1593+
"users":[{
1594+
"id":3,
1595+
"login":"uu"
1596+
}],
1597+
"teams":[{
1598+
"id":4,
1599+
"slug":"tt"
1600+
}]
1601+
},
1602+
"dismiss_stale_reviews":true,
1603+
"require_code_owner_reviews":true,
1604+
"bypass_pull_request_allowances": {
1605+
"users":[{"id":10,"login":"uuu"}],
1606+
"teams":[{"id":20,"slug":"ttt"}],
1607+
"apps":[{"id":30,"slug":"aaa"}]
1608+
}
1609+
},
1610+
"restrictions":{
1611+
"users":[{"id":1,"login":"u"}],
1612+
"teams":[{"id":2,"slug":"t"}],
1613+
"apps":[{"id":3,"slug":"a"}]
1614+
}
1615+
}`)
1616+
})
1617+
1618+
ctx := context.Background()
1619+
protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1620+
if err != nil {
1621+
t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1622+
}
1623+
1624+
want := &Protection{
1625+
RequiredStatusChecks: &RequiredStatusChecks{
1626+
Strict: true,
1627+
Contexts: []string{},
1628+
Checks: []*RequiredStatusCheck{},
1629+
},
1630+
RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1631+
DismissStaleReviews: true,
1632+
DismissalRestrictions: &DismissalRestrictions{
1633+
Users: []*User{
1634+
{Login: String("uu"), ID: Int64(3)},
1635+
},
1636+
Teams: []*Team{
1637+
{Slug: String("tt"), ID: Int64(4)},
1638+
},
1639+
},
1640+
RequireCodeOwnerReviews: true,
1641+
BypassPullRequestAllowances: &BypassPullRequestAllowances{
1642+
Users: []*User{
1643+
{Login: String("uuu"), ID: Int64(10)},
1644+
},
1645+
Teams: []*Team{
1646+
{Slug: String("ttt"), ID: Int64(20)},
1647+
},
1648+
Apps: []*App{
1649+
{Slug: String("aaa"), ID: Int64(30)},
1650+
},
1651+
},
1652+
},
1653+
Restrictions: &BranchRestrictions{
1654+
Users: []*User{
1655+
{Login: String("u"), ID: Int64(1)},
1656+
},
1657+
Teams: []*Team{
1658+
{Slug: String("t"), ID: Int64(2)},
1659+
},
1660+
Apps: []*App{
1661+
{Slug: String("a"), ID: Int64(3)},
1662+
},
1663+
},
1664+
}
1665+
if !cmp.Equal(protection, want) {
1666+
t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1667+
}
1668+
}
1669+
15461670
func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
15471671
client, mux, _, teardown := setup()
15481672
defer teardown()

0 commit comments

Comments
 (0)