Browse Source

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.
Unknwon 7 years ago
parent
commit
1038916460
6 changed files with 26 additions and 9 deletions
  1. 1 1
      gogs.go
  2. 2 0
      models/migrations/migrations.go
  3. 14 0
      models/migrations/v17.go
  4. 1 1
      models/org_team.go
  5. 7 6
      models/repo_branch.go
  6. 1 1
      templates/.VERSION

+ 1 - 1
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

+ 2 - 0
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

+ 14 - 0
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
+}

+ 1 - 1
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)
 	}

+ 7 - 6
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)
 	}

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.10.30.0325
+0.10.31.0327