Skip to content

Fix adding branch as protected to not allow pushing to it #2556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,12 @@ func runHookPreReceive(c *cli.Context) error {
}

if protectBranch != nil {
if !protectBranch.CanPush {
// check and deletion
if newCommitID == git.EmptySHA {
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
} else {
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
}
// check and deletion
if newCommitID == git.EmptySHA {
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
} else if !protectBranch.CanPush {
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion integrations/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
"_csrf": csrf,
"branchName": "master",
"canPush": "true",
"canPush": "false",
})
resp := session.MakeRequest(t, req, http.StatusOK)
// Check if master branch has been locked successfully
Expand Down
8 changes: 4 additions & 4 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const (

// ProtectedBranch struct
type ProtectedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s)"`
BranchName string `xorm:"UNIQUE(s)"`
CanPush bool
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s)"`
BranchName string `xorm:"UNIQUE(s)"`
CanPush bool `xorm:"NOT NULL DEFAULT false"`
Created time.Time `xorm:"-"`
CreatedUnix int64
Updated time.Time `xorm:"-"`
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ var migrations = []Migration{
NewMigration("unescape user full names", unescapeUserFullNames),
// v38 -> v39
NewMigration("remove commits and settings unit types", removeCommitsUnitType),
// v43 -> v44
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
}

// Migrate database to current version
Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v43.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"code.gitea.io/gitea/models"

"github.com/go-xorm/xorm"
)

func fixProtectedBranchCanPushValue(x *xorm.Engine) error {
_, err := x.Cols("can_push").Update(&models.ProtectedBranch{
CanPush: false,
})
return err
}
4 changes: 2 additions & 2 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ function initProtectedBranch() {
var $this = $(this);
$.post($this.data('url'), {
"_csrf": csrf,
"canPush": true,
"canPush": false,
"branchName": $this.val(),
},
function (data) {
Expand All @@ -642,7 +642,7 @@ function initProtectedBranch() {
var $this = $(this);
$.post($this.data('url'), {
"_csrf": csrf,
"canPush": false,
"canPush": true,
"branchName": $this.data('val'),
},
function (data) {
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func ProtectedBranchPost(ctx *context.Context) {

canPush := ctx.QueryBool("canPush")

if canPush {
if !canPush {
if err := ctx.Repo.Repository.AddProtectedBranch(branchName, canPush); err != nil {
ctx.Flash.Error(ctx.Tr("repo.settings.add_protected_branch_failed", branchName))
ctx.JSON(200, map[string]string{
Expand Down