Skip to content

Commit e38e502

Browse files
daviianlafriks
authored andcommitted
Fix deletion of unprotected branches (#2630)
* fix deletion of unprotected branches * fmt fix * changed internal protected branch api * fix lint error Signed-off-by: David Schneiderbauer <[email protected]>
1 parent 3cc5b11 commit e38e502

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

cmd/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func runHookPreReceive(c *cli.Context) error {
126126
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
127127
}
128128

129-
if protectBranch != nil {
129+
if protectBranch != nil && protectBranch.IsProtected() {
130130
// check and deletion
131131
if newCommitID == git.EmptySHA {
132132
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")

integrations/internal_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/stretchr/testify/assert"
1818
)
1919

20-
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
20+
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, isProtected bool) {
2121
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, url.QueryEscape(branchName))
2222
req := NewRequest(t, "GET", reqURL)
2323
t.Log(reqURL)
@@ -31,14 +31,14 @@ func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr,
3131
var branch models.ProtectedBranch
3232
t.Log(string(resp.Body))
3333
assert.NoError(t, json.Unmarshal(resp.Body, &branch))
34-
assert.Equal(t, canPush, branch.CanPush)
34+
assert.Equal(t, isProtected, branch.IsProtected())
3535
}
3636
}
3737

3838
func TestInternal_GetProtectedBranch(t *testing.T) {
3939
prepareTestEnv(t)
4040

41-
assertProtectedBranch(t, 1, "master", false, true)
42-
assertProtectedBranch(t, 1, "dev", false, true)
43-
assertProtectedBranch(t, 1, "lunny/dev", false, true)
41+
assertProtectedBranch(t, 1, "master", false, false)
42+
assertProtectedBranch(t, 1, "dev", false, false)
43+
assertProtectedBranch(t, 1, "lunny/dev", false, false)
4444
}

models/branches.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func (protectBranch *ProtectedBranch) BeforeUpdate() {
3838
protectBranch.UpdatedUnix = time.Now().Unix()
3939
}
4040

41+
// IsProtected returns if the branch is protected
42+
func (protectBranch *ProtectedBranch) IsProtected() bool {
43+
return protectBranch.ID > 0
44+
}
45+
4146
// GetProtectedBranchByRepoID getting protected branch by repo ID
4247
func GetProtectedBranchByRepoID(RepoID int64) ([]*ProtectedBranch, error) {
4348
protectedBranches := make([]*ProtectedBranch, 0)

routers/private/branch.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ func GetProtectedBranchBy(ctx *macaron.Context) {
2020
"err": err.Error(),
2121
})
2222
return
23-
} else if protectBranch != nil {
24-
ctx.JSON(200, protectBranch)
25-
} else {
26-
ctx.JSON(200, &models.ProtectedBranch{
27-
CanPush: true,
28-
})
2923
}
24+
ctx.JSON(200, protectBranch)
3025
}

0 commit comments

Comments
 (0)