Skip to content

Commit 86ee5b4

Browse files
authored
PATCH branch-protection updates check list even when checks are disabled (#26351)
Fixes: #26333. Previously, this endpoint only updates the `StatusCheckContexts` field when `EnableStatusCheck==true`, which makes it impossible to clear the array otherwise. This patch uses slice `nil`-ness to decide whether to update the list of checks. The field is ignored when either the client explicitly passes in a null, or just omits the field from the json ([which causes `json.Unmarshal` to leave the struct field unchanged](https://go.dev/play/p/Z2XHOILuB1Q)). I think this is a better measure of intent than whether the `EnableStatusCheck` flag was set, because it matches the semantics of other field types. Also adds a test case. I noticed that [`testAPIEditBranchProtection` only checks the branch name](https://github.com/go-gitea/gitea/blob/c1c83dbaec840871c1247f4bc3f875309b0de6bb/tests/integration/api_branch_test.go#L68) and no other fields, so I added some extra `GET` calls and specific checks to make sure the fields are changing properly. I added those checks the existing integration test; is that the right place for it?
1 parent d2e4039 commit 86ee5b4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

routers/api/v1/repo/branch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ func EditBranchProtection(ctx *context.APIContext) {
768768
if form.EnableStatusCheck != nil {
769769
protectBranch.EnableStatusCheck = *form.EnableStatusCheck
770770
}
771-
if protectBranch.EnableStatusCheck {
771+
772+
if form.StatusCheckContexts != nil {
772773
protectBranch.StatusCheckContexts = form.StatusCheckContexts
773774
}
774775

tests/integration/api_branch_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
3131
assert.True(t, branch.UserCanMerge)
3232
}
3333

34-
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
34+
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) *api.BranchProtection {
3535
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository)
3636
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
3737
resp := MakeRequest(t, req, expectedHTTPStatus)
@@ -40,7 +40,9 @@ func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPSta
4040
var branchProtection api.BranchProtection
4141
DecodeJSON(t, resp, &branchProtection)
4242
assert.EqualValues(t, branchName, branchProtection.RuleName)
43+
return &branchProtection
4344
}
45+
return nil
4446
}
4547

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

191+
// enable status checks, require the "test1" check to pass
192+
testAPIEditBranchProtection(t, "master", &api.BranchProtection{
193+
EnableStatusCheck: true,
194+
StatusCheckContexts: []string{"test1"},
195+
}, http.StatusOK)
196+
bp := testAPIGetBranchProtection(t, "master", http.StatusOK)
197+
assert.Equal(t, true, bp.EnableStatusCheck)
198+
assert.Equal(t, []string{"test1"}, bp.StatusCheckContexts)
199+
200+
// disable status checks, clear the list of required checks
201+
testAPIEditBranchProtection(t, "master", &api.BranchProtection{
202+
EnableStatusCheck: false,
203+
StatusCheckContexts: []string{},
204+
}, http.StatusOK)
205+
bp = testAPIGetBranchProtection(t, "master", http.StatusOK)
206+
assert.Equal(t, false, bp.EnableStatusCheck)
207+
assert.Equal(t, []string{}, bp.StatusCheckContexts)
208+
189209
testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
190210

191211
// Test branch deletion

0 commit comments

Comments
 (0)