-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Protected branches system #339
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0912cbb
Protected branches system
denji 47f9f54
finished the UI and add/delete protected branch response
lunny 4fc3563
reformat tmpl
denji 92e5ae3
remove unused comment
lunny ab01219
indent all the template files and remove ru translations since we use…
lunny 7385620
fix the push bug
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2016 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 models | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Protected metadata | ||
const ( | ||
// Protected User ID | ||
ProtectedBranchUserID = "GITEA_USER_ID" | ||
// Protected Repo ID | ||
ProtectedBranchRepoID = "GITEA_REPO_ID" | ||
// Protected access mode | ||
ProtectedBranchAccessMode = "GITEA_ACCESS_MODE" | ||
) | ||
|
||
// ProtectedBranch struct | ||
type ProtectedBranch struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"UNIQUE(s)"` | ||
BranchName string `xorm:"UNIQUE(s)"` | ||
CanPush bool | ||
Created time.Time `xorm:"-"` | ||
CreatedUnix int64 | ||
Updated time.Time `xorm:"-"` | ||
UpdatedUnix int64 | ||
} | ||
|
||
// BeforeInsert before protected branch insert create and update time | ||
func (protectBranch *ProtectedBranch) BeforeInsert() { | ||
protectBranch.CreatedUnix = time.Now().Unix() | ||
protectBranch.UpdatedUnix = protectBranch.CreatedUnix | ||
} | ||
|
||
// BeforeUpdate before protected branch update time | ||
func (protectBranch *ProtectedBranch) BeforeUpdate() { | ||
protectBranch.UpdatedUnix = time.Now().Unix() | ||
} | ||
|
||
// GetProtectedBranchByRepoID getting protected branch by repo ID | ||
func GetProtectedBranchByRepoID(RepoID int64) ([]*ProtectedBranch, error) { | ||
protectedBranches := make([]*ProtectedBranch, 0) | ||
return protectedBranches, x.Where("repo_id = ?", RepoID).Desc("updated_unix").Find(&protectedBranches) | ||
} | ||
|
||
// GetProtectedBranchBy getting protected branch by ID/Name | ||
func GetProtectedBranchBy(repoID int64, BranchName string) (*ProtectedBranch, error) { | ||
rel := &ProtectedBranch{RepoID: repoID, BranchName: strings.ToLower(BranchName)} | ||
has, err := x.Get(rel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !has { | ||
return nil, nil | ||
} | ||
return rel, nil | ||
} | ||
|
||
// GetProtectedBranches get all protected btanches | ||
func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) { | ||
protectedBranches := make([]*ProtectedBranch, 0) | ||
return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID}) | ||
} | ||
|
||
// AddProtectedBranch add protection to branch | ||
func (repo *Repository) AddProtectedBranch(branchName string, canPush bool) error { | ||
protectedBranch := &ProtectedBranch{ | ||
RepoID: repo.ID, | ||
BranchName: branchName, | ||
} | ||
|
||
has, err := x.Get(protectedBranch) | ||
if err != nil { | ||
return err | ||
} else if has { | ||
return nil | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sessionRelease(sess) | ||
if err = sess.Begin(); err != nil { | ||
return err | ||
} | ||
protectedBranch.CanPush = canPush | ||
if _, err = sess.InsertOne(protectedBranch); err != nil { | ||
return err | ||
} | ||
|
||
return sess.Commit() | ||
} | ||
|
||
// ChangeProtectedBranch access mode sets new access mode for the ProtectedBranch. | ||
func (repo *Repository) ChangeProtectedBranch(id int64, canPush bool) error { | ||
ProtectedBranch := &ProtectedBranch{ | ||
RepoID: repo.ID, | ||
ID: id, | ||
} | ||
has, err := x.Get(ProtectedBranch) | ||
if err != nil { | ||
return fmt.Errorf("get ProtectedBranch: %v", err) | ||
} else if !has { | ||
return nil | ||
} | ||
|
||
if ProtectedBranch.CanPush == canPush { | ||
return nil | ||
} | ||
ProtectedBranch.CanPush = canPush | ||
|
||
sess := x.NewSession() | ||
defer sessionRelease(sess) | ||
if err = sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = sess.Id(ProtectedBranch.ID).AllCols().Update(ProtectedBranch); err != nil { | ||
return fmt.Errorf("update ProtectedBranch: %v", err) | ||
} | ||
|
||
return sess.Commit() | ||
} | ||
|
||
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository. | ||
func (repo *Repository) DeleteProtectedBranch(id int64) (err error) { | ||
protectedBranch := &ProtectedBranch{ | ||
RepoID: repo.ID, | ||
ID: id, | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sessionRelease(sess) | ||
if err = sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if affected, err := sess.Delete(protectedBranch); err != nil { | ||
return err | ||
} else if affected != 1 { | ||
return fmt.Errorf("delete protected branch ID(%v) failed", id) | ||
} | ||
|
||
return sess.Commit() | ||
} | ||
|
||
// newProtectedBranch insert one queue | ||
func newProtectedBranch(protectedBranch *ProtectedBranch) error { | ||
_, err := x.InsertOne(protectedBranch) | ||
return err | ||
} | ||
|
||
// UpdateProtectedBranch update queue | ||
func UpdateProtectedBranch(protectedBranch *ProtectedBranch) error { | ||
_, err := x.Update(protectedBranch) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2016 Gitea. 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 ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-xorm/xorm" | ||
) | ||
|
||
func setProtectedBranchUpdatedWithCreated(x *xorm.Engine) (err error) { | ||
type ProtectedBranch struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"UNIQUE(s)"` | ||
BranchName string `xorm:"UNIQUE(s)"` | ||
CanPush bool | ||
Created time.Time `xorm:"-"` | ||
CreatedUnix int64 | ||
Updated time.Time `xorm:"-"` | ||
UpdatedUnix int64 | ||
} | ||
if err = x.Sync2(new(ProtectedBranch)); err != nil { | ||
return fmt.Errorf("Sync2: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an entirely new struct, that doesn't need any migration because of the anyway executed sync function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Sync on a migration is better safe. I have found the original method's bug, see #871. When you upgrade from an old version which haven't a column external_tracker_url, then a new version add this column by Sync not migration. but another new version will use this column, then the bug occupied.