Skip to content

Added checks for protected branches in pull requests #3544

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 10 commits into from
Mar 13, 2018
Merged

Added checks for protected branches in pull requests #3544

merged 10 commits into from
Mar 13, 2018

Conversation

Chri-s
Copy link
Contributor

@Chri-s Chri-s commented Feb 19, 2018

Fixes #2875. This pull request adds checks whether the user is allowed to push to the base branch of a pull request.

If a user may not push to the base branch, the green merge button is hidden on the pull request view. If the user tries merge via the web api, an error (http code 500) with
{"message":"branch is protected [name: %s]","url":"https://godoc.org/github.com/go-gitea/go-sdk/gitea"}
is returned.

There shouldn't be a way to circumvent the branch protection via a pull request.

@codecov-io
Copy link

codecov-io commented Feb 19, 2018

Codecov Report

Merging #3544 into master will decrease coverage by 0.02%.
The diff coverage is 12.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3544      +/-   ##
==========================================
- Coverage   35.88%   35.86%   -0.03%     
==========================================
  Files         287      287              
  Lines       41331    41362      +31     
==========================================
+ Hits        14833    14835       +2     
- Misses      24316    24339      +23     
- Partials     2182     2188       +6
Impacted Files Coverage Δ
models/error.go 32.65% <0%> (-0.48%) ⬇️
routers/repo/issue.go 33.09% <14.28%> (-0.18%) ⬇️
models/pull.go 51.2% <14.28%> (-0.97%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c0d41b1...fbf9bf2. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 19, 2018
models/error.go Outdated
@@ -785,6 +785,21 @@ func (err ErrBranchNameConflict) Error() string {
return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName)
}

// ErrBranchProtected represents an error that a branch is protected and the current user is not allowed to modify it
type ErrBranchProtected struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrNotAllowedToMerge would be better name for this error

models/pull.go Outdated
@@ -287,6 +287,14 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
}
prConfig := prUnit.PullRequestsConfig()

if protected, err := pr.BaseRepo.IsProtectedBranch(pr.BaseBranch, doer); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to create new function pr.CheckUserAllowedToMerge that would return error (nil for allowed to merge)

@@ -734,6 +734,20 @@ func ViewIssue(ctx *context.Context) {
}
prConfig := prUnit.PullRequestsConfig()

if ctx.IsSigned {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse function pull.CheckUserAllowedToMerge here

@@ -734,6 +734,20 @@ func ViewIssue(ctx *context.Context) {
}
prConfig := prUnit.PullRequestsConfig()

if ctx.IsSigned {
if err := pull.GetBaseRepo(); err != nil {
log.Error(4, "GetBaseRepo: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in such cases ctx.ServerError(... should be used

log.Error(4, "IsProtectedBranch: %v", err)
ctx.Data["BaseBranchNotProtected"] = false
} else {
ctx.Data["BaseBranchNotProtected"] = !protected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse IsRepositoryWriter && CheckUserAllowedToMerge to assign to something like ctx.Data["AllowMerge"] and check that in pull.tmpl with {{if .AllowMerge}}

@lafriks
Copy link
Member

lafriks commented Feb 19, 2018

With moving that check to function would allow to reuse that function to add additional conditions for merge limitations

Christian Wulff and others added 2 commits February 20, 2018 00:49
@Chri-s
Copy link
Contributor Author

Chri-s commented Feb 20, 2018

Sorry, had a problem with the first commit after your change request :( I hope the last commit meets your requirements.

models/pull.go Outdated
"Not signed in",
}
}
if err = pr.GetBaseRepo(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to load repository if it is already loaded (need check if pr.BaseRepo == nil {)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it, but GetBaseRepo() already checks for this:

func (pr *PullRequest) GetBaseRepo() (err error) {
	if pr.BaseRepo != nil {
		return nil
	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I overlooked that function than... I was checking code but somehow missed that check. If it does than yes there is no need to do additional check

@@ -734,6 +734,12 @@ func ViewIssue(ctx *context.Context) {
}
prConfig := prUnit.PullRequestsConfig()

if err := pull.CheckUserAllowedToMerge(ctx.User); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be rewritten as:

ctx.Data["AllowMerge"] = ctx.Data["IsRepositoryWriter"]
if err := pull.CheckUserAllowedToMerge(ctx.User); err != nil {
    if !models.IsErrNotAllowedToMerge(err) {
        ctx.ServerError(err)
        return
    }
    ctx.Data["AllowMerge"] = false
}

@lunny lunny added this to the 1.x.x milestone Feb 20, 2018
@Chri-s
Copy link
Contributor Author

Chri-s commented Feb 20, 2018

The CI build failed because of a git error (https://drone.gitea.io/go-gitea/gitea/3754/2):

+ git init
Initialized empty Git repository in /srv/app/src/code.gitea.io/gitea/.git/
+ git remote add origin https://github.com/go-gitea/gitea.git
+ git fetch --tags --depth=50 origin +refs/pull/3544/merge:
fatal: The remote end hung up unexpectedly
exit status 128

I don't think that this has something to do with my latest commit, is there some way to tell drone to retry?

@lunny
Copy link
Member

lunny commented Feb 22, 2018

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 22, 2018
@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 22, 2018
@appleboy appleboy modified the milestones: 1.x.x, 1.5.0 Feb 22, 2018
@lunny
Copy link
Member

lunny commented Mar 3, 2018

need @lafriks 's review.

@lunny lunny merged commit a2a49c9 into go-gitea:master Mar 13, 2018
@lunny lunny mentioned this pull request Mar 15, 2018
7 tasks
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User that is not in the white list of the protected branch can update the protected branch by pull request
6 participants