-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refuse merge until all required status checks success #7481
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
lafriks
merged 19 commits into
go-gitea:master
from
lunny:lunny/refuse_merge_until_ci_success
Sep 18, 2019
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
298c1e5
refuse merge until ci successfully
lunny afad473
deny merge request when required status checkes not succeed on merge …
lunny a9bdf39
add database migration for added columns on protected_branch
lunny 41673cc
fix migration
lunny 64de970
fix protected branch check bug
lunny 7b38636
fix protected branch settings
lunny 5dacbff
remove duplicated code on check pull request's required commit status…
lunny 9841034
remove unused codes
lunny e4d001a
fix migration
lunny f94f1ff
add newline for template file
lunny 9d940fd
fix go mod
lunny 9d27260
rename function name and some other fixes
lunny e7d604a
fix template
lunny d033aba
fix bug pull view
lunny 11804b3
remove go1.12 wrong dependencies
lunny 91ccdd4
add administrator bypass when protected branch status check enabled
lunny 3a1889e
fix bug
lunny eb61923
improve the codes
lunny e06a523
Merge branch 'master' into lunny/refuse_merge_until_ci_success
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
// Copyright 2019 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 migrations | ||
|
||
import "github.com/go-xorm/xorm" | ||
|
||
func addStatusCheckColumnsForProtectedBranches(x *xorm.Engine) error { | ||
type ProtectedBranch struct { | ||
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"` | ||
StatusCheckContexts []string `xorm:"JSON TEXT"` | ||
} | ||
|
||
if err := x.Sync2(new(ProtectedBranch)); err != nil { | ||
return err | ||
} | ||
|
||
_, err := x.Cols("enable_status_check", "status_check_contexts").Update(&ProtectedBranch{ | ||
EnableStatusCheck: false, | ||
StatusCheckContexts: []string{}, | ||
}) | ||
return err | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
// Copyright 2019 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 pull | ||
|
||
import ( | ||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/git" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// IsCommitStatusContextSuccess returns true if all required status check contexts succeed. | ||
func IsCommitStatusContextSuccess(commitStatuses []*models.CommitStatus, requiredContexts []string) bool { | ||
for _, ctx := range requiredContexts { | ||
var found bool | ||
for _, commitStatus := range commitStatuses { | ||
if commitStatus.Context == ctx { | ||
if commitStatus.State != models.CommitStatusSuccess { | ||
return false | ||
} | ||
|
||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// IsPullCommitStatusPass returns if all required status checks PASS | ||
func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) { | ||
if err := pr.LoadProtectedBranch(); err != nil { | ||
return false, errors.Wrap(err, "GetLatestCommitStatus") | ||
} | ||
if pr.ProtectedBranch == nil || !pr.ProtectedBranch.EnableStatusCheck { | ||
return true, nil | ||
} | ||
|
||
// check if all required status checks are successful | ||
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) | ||
if err != nil { | ||
return false, errors.Wrap(err, "OpenRepository") | ||
} | ||
|
||
if !headGitRepo.IsBranchExist(pr.HeadBranch) { | ||
return false, errors.New("Head branch does not exist, can not merge") | ||
} | ||
|
||
sha, err := headGitRepo.GetBranchCommitID(pr.HeadBranch) | ||
if err != nil { | ||
return false, errors.Wrap(err, "GetBranchCommitID") | ||
} | ||
|
||
if err := pr.LoadBaseRepo(); err != nil { | ||
return false, errors.Wrap(err, "LoadBaseRepo") | ||
} | ||
|
||
commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo, sha, 0) | ||
if err != nil { | ||
return false, errors.Wrap(err, "GetLatestCommitStatus") | ||
} | ||
|
||
return IsCommitStatusContextSuccess(commitStatuses, pr.ProtectedBranch.StatusCheckContexts), nil | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.