Skip to content

Commit

Permalink
Allow updating branch protection context list even when status checks…
Browse files Browse the repository at this point in the history
… are disabled

Fixes: #26333
  • Loading branch information
Infinoid committed Aug 5, 2023
1 parent c1c83db commit cc76add
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ func EditBranchProtection(ctx *context.APIContext) {
if form.EnableStatusCheck != nil {
protectBranch.EnableStatusCheck = *form.EnableStatusCheck
}
if protectBranch.EnableStatusCheck {

if form.StatusCheckContexts != nil {
protectBranch.StatusCheckContexts = form.StatusCheckContexts
}

Expand Down
22 changes: 21 additions & 1 deletion tests/integration/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
assert.True(t, branch.UserCanMerge)
}

func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) *api.BranchProtection {
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository)
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
resp := MakeRequest(t, req, expectedHTTPStatus)
Expand All @@ -40,7 +40,9 @@ func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPSta
var branchProtection api.BranchProtection
DecodeJSON(t, resp, &branchProtection)
assert.EqualValues(t, branchName, branchProtection.RuleName)
return &branchProtection
}
return nil
}

func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
Expand Down Expand Up @@ -186,6 +188,24 @@ func TestAPIBranchProtection(t *testing.T) {
EnablePush: true,
}, http.StatusOK)

// enable status checks, require the "test1" check to pass
testAPIEditBranchProtection(t, "master", &api.BranchProtection{
EnableStatusCheck: true,
StatusCheckContexts: []string{"test1"},
}, http.StatusOK)
bp := testAPIGetBranchProtection(t, "master", http.StatusOK)
assert.Equal(t, true, bp.EnableStatusCheck)
assert.Equal(t, []string{"test1"}, bp.StatusCheckContexts)

// disable status checks, clear the list of required checks
testAPIEditBranchProtection(t, "master", &api.BranchProtection{
EnableStatusCheck: false,
StatusCheckContexts: []string{},
}, http.StatusOK)
bp = testAPIGetBranchProtection(t, "master", http.StatusOK)
assert.Equal(t, false, bp.EnableStatusCheck)
assert.Equal(t, []string{}, bp.StatusCheckContexts)

testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)

// Test branch deletion
Expand Down

0 comments on commit cc76add

Please sign in to comment.