diff options
author | Chri-s <Chri-s@users.noreply.github.com> | 2018-03-13 04:46:14 +0100 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2018-03-13 11:46:14 +0800 |
commit | a2a49c93c78a81d1eaa6b0eaf84a0c3f4bcd2487 (patch) | |
tree | c59d836cbbc221d79c8d90c2f62775da680b6fef /models | |
parent | c0d41b1b77169553006bd9211d025de4da8bafd8 (diff) | |
download | gitea-a2a49c93c78a81d1eaa6b0eaf84a0c3f4bcd2487.tar.gz gitea-a2a49c93c78a81d1eaa6b0eaf84a0c3f4bcd2487.zip |
Added checks for protected branches in pull requests (#3544)
* Added checks for protected branches in pull requests
Signed-off-by: Christian Wulff <NChris@posteo.net>
* Moved check for protected branch into new function CheckUserAllowedToMerge
Signed-off-by: Christian Wulff <NChris@posteo.net>
* Removed merge conflict lines from last commit
Signed-off-by: Christian Wulff <NChris@posteo.net>
* Explicit check for error type in ViewIssue
Signed-off-by: Christian Wulff <NChris@posteo.net>
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 15 | ||||
-rw-r--r-- | models/pull.go | 29 |
2 files changed, 44 insertions, 0 deletions
diff --git a/models/error.go b/models/error.go index a1c24f4ec0..d24da2642e 100644 --- a/models/error.go +++ b/models/error.go @@ -785,6 +785,21 @@ func (err ErrBranchNameConflict) Error() string { return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName) } +// ErrNotAllowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it +type ErrNotAllowedToMerge struct { + Reason string +} + +// IsErrNotAllowedToMerge checks if an error is an ErrNotAllowedToMerge. +func IsErrNotAllowedToMerge(err error) bool { + _, ok := err.(ErrNotAllowedToMerge) + return ok +} + +func (err ErrNotAllowedToMerge) Error() string { + return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason) +} + // ErrTagAlreadyExists represents an error that tag with such name already exists type ErrTagAlreadyExists struct { TagName string diff --git a/models/pull.go b/models/pull.go index 15b3a4fe18..137900beed 100644 --- a/models/pull.go +++ b/models/pull.go @@ -272,6 +272,31 @@ const ( MergeStyleSquash MergeStyle = "squash" ) +// CheckUserAllowedToMerge checks whether the user is allowed to merge +func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) { + if doer == nil { + return ErrNotAllowedToMerge{ + "Not signed in", + } + } + + if pr.BaseRepo == nil { + if err = pr.GetBaseRepo(); err != nil { + return fmt.Errorf("GetBaseRepo: %v", err) + } + } + + if protected, err := pr.BaseRepo.IsProtectedBranch(pr.BaseBranch, doer); err != nil { + return fmt.Errorf("IsProtectedBranch: %v", err) + } else if protected { + return ErrNotAllowedToMerge{ + "The branch is protected", + } + } + + return nil +} + // Merge merges pull request to base repository. // FIXME: add repoWorkingPull make sure two merges does not happen at same time. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle MergeStyle, message string) (err error) { @@ -287,6 +312,10 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle } prConfig := prUnit.PullRequestsConfig() + if err := pr.CheckUserAllowedToMerge(doer); err != nil { + return fmt.Errorf("CheckUserAllowedToMerge: %v", err) + } + // Check if merge style is correct and allowed if !prConfig.IsMergeStyleAllowed(mergeStyle) { return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle} |