From 1038916460a7ed2fa559bcf2f14b2eecb6ddaad6 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 27 Mar 2017 13:13:04 -0400 Subject: [PATCH] models/protect_branch: fix whitelist with invalid 'protect_branch_id' (#4333) If user creates a protect branch for the first time (which has ID=0), it generates invalid whitelist records with 'protect_branch_id=0'. This prevents future updates of protect branch whitelist. Migration: remove existing invalid protect branch whitelist records. --- gogs.go | 2 +- models/migrations/migrations.go | 2 ++ models/migrations/v17.go | 14 ++++++++++++++ models/org_team.go | 2 +- models/repo_branch.go | 13 +++++++------ templates/.VERSION | 2 +- 6 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 models/migrations/v17.go diff --git a/gogs.go b/gogs.go index 99d365baa..060b150b5 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.10.30.0325" +const APP_VER = "0.10.31.0327" func init() { setting.AppVer = APP_VER diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 99ba8a43d..7acd7259d 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -62,6 +62,8 @@ var migrations = []Migration{ NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks), // v15 -> v16:v0.10.16 NewMigration("update repository sizes", updateRepositorySizes), + // v16 -> v17:v0.10.31 + NewMigration("remove invalid protect branch whitelist", removeInvalidProtectBranchWhitelist), } // Migrate database to current version diff --git a/models/migrations/v17.go b/models/migrations/v17.go new file mode 100644 index 000000000..6769b7540 --- /dev/null +++ b/models/migrations/v17.go @@ -0,0 +1,14 @@ +// Copyright 2017 The Gogs 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 ( + "github.com/go-xorm/xorm" +) + +func removeInvalidProtectBranchWhitelist(x *xorm.Engine) error { + _, err := x.Exec("DELETE FROM protect_branch_whitelist WHERE protect_branch_id = 0") + return err +} diff --git a/models/org_team.go b/models/org_team.go index 816f23705..a7fe3a338 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -424,7 +424,7 @@ func IsTeamMember(orgID, teamID, uid int64) bool { func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) { teamUsers := make([]*TeamUser, 0, 10) - if err = e.Sql("SELECT `id`, `org_id`, `team_id`, `uid` FROM `team_user` WHERE team_id=?", teamID). + if err = e.Sql("SELECT `id`, `org_id`, `team_id`, `uid` FROM `team_user` WHERE team_id = ?", teamID). Find(&teamUsers); err != nil { return nil, fmt.Errorf("get team-users: %v", err) } diff --git a/models/repo_branch.go b/models/repo_branch.go index 6c5b7ec8d..de0ee26dd 100644 --- a/models/repo_branch.go +++ b/models/repo_branch.go @@ -187,6 +187,13 @@ func UpdateOrgProtectBranch(repo *Repository, protectBranch *ProtectBranch, whit protectBranch.WhitelistTeamIDs = strings.Join(base.Int64sToStrings(validTeamIDs), ",") } + // Make sure protectBranch.ID is not 0 for whitelists + if protectBranch.ID == 0 { + if _, err = x.Insert(protectBranch); err != nil { + return fmt.Errorf("Insert: %v", err) + } + } + // Merge users and members of teams var whitelists []*ProtectBranchWhitelist if hasUsersChanged || hasTeamsChanged { @@ -226,12 +233,6 @@ func UpdateOrgProtectBranch(repo *Repository, protectBranch *ProtectBranch, whit return err } - if protectBranch.ID == 0 { - if _, err = sess.Insert(protectBranch); err != nil { - return fmt.Errorf("Insert: %v", err) - } - } - if _, err = sess.Id(protectBranch.ID).AllCols().Update(protectBranch); err != nil { return fmt.Errorf("Update: %v", err) } diff --git a/templates/.VERSION b/templates/.VERSION index 0e5097c0f..f14cdf1c0 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.10.30.0325 \ No newline at end of file +0.10.31.0327 \ No newline at end of file