Skip to content

Commit 25e71ad

Browse files
authored
Fix adding branch as protected to not allow pushing to it (#2556)
* Fix adding branch as protected to not allow pushing to it * Fix can_push value to false in protected_branch (#2560) * Fix integration test
1 parent f014e42 commit 25e71ad

File tree

7 files changed

+34
-16
lines changed

7 files changed

+34
-16
lines changed

cmd/hook.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ func runHookPreReceive(c *cli.Context) error {
127127
}
128128

129129
if protectBranch != nil {
130-
if !protectBranch.CanPush {
131-
// check and deletion
132-
if newCommitID == git.EmptySHA {
133-
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
134-
} else {
135-
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
136-
//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
137-
}
130+
// check and deletion
131+
if newCommitID == git.EmptySHA {
132+
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
133+
} else if !protectBranch.CanPush {
134+
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
135+
//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
138136
}
139137
}
140138
}

integrations/editor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
4646
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
4747
"_csrf": csrf,
4848
"branchName": "master",
49-
"canPush": "true",
49+
"canPush": "false",
5050
})
5151
resp := session.MakeRequest(t, req, http.StatusOK)
5252
// Check if master branch has been locked successfully

models/branches.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const (
1717

1818
// ProtectedBranch struct
1919
type ProtectedBranch struct {
20-
ID int64 `xorm:"pk autoincr"`
21-
RepoID int64 `xorm:"UNIQUE(s)"`
22-
BranchName string `xorm:"UNIQUE(s)"`
23-
CanPush bool
20+
ID int64 `xorm:"pk autoincr"`
21+
RepoID int64 `xorm:"UNIQUE(s)"`
22+
BranchName string `xorm:"UNIQUE(s)"`
23+
CanPush bool `xorm:"NOT NULL DEFAULT false"`
2424
Created time.Time `xorm:"-"`
2525
CreatedUnix int64
2626
Updated time.Time `xorm:"-"`

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ var migrations = []Migration{
126126
NewMigration("unescape user full names", unescapeUserFullNames),
127127
// v38 -> v39
128128
NewMigration("remove commits and settings unit types", removeCommitsUnitType),
129+
// v43 -> v44
130+
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
129131
}
130132

131133
// Migrate database to current version

models/migrations/v43.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func fixProtectedBranchCanPushValue(x *xorm.Engine) error {
14+
_, err := x.Cols("can_push").Update(&models.ProtectedBranch{
15+
CanPush: false,
16+
})
17+
return err
18+
}

public/js/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ function initProtectedBranch() {
625625
var $this = $(this);
626626
$.post($this.data('url'), {
627627
"_csrf": csrf,
628-
"canPush": true,
628+
"canPush": false,
629629
"branchName": $this.val(),
630630
},
631631
function (data) {
@@ -642,7 +642,7 @@ function initProtectedBranch() {
642642
var $this = $(this);
643643
$.post($this.data('url'), {
644644
"_csrf": csrf,
645-
"canPush": false,
645+
"canPush": true,
646646
"branchName": $this.data('val'),
647647
},
648648
function (data) {

routers/repo/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func ProtectedBranchPost(ctx *context.Context) {
520520

521521
canPush := ctx.QueryBool("canPush")
522522

523-
if canPush {
523+
if !canPush {
524524
if err := ctx.Repo.Repository.AddProtectedBranch(branchName, canPush); err != nil {
525525
ctx.Flash.Error(ctx.Tr("repo.settings.add_protected_branch_failed", branchName))
526526
ctx.JSON(200, map[string]string{

0 commit comments

Comments
 (0)