-
-
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
Sign protected branches #8993
Merged
Merged
Sign protected branches #8993
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8d68f15
Move SignMerge to PullRequest
zeripath ca50579
Add approved signing mode
zeripath b96d605
Merge branch 'master' into sign-protected
zeripath 4508b7e
Merge branch 'master' into sign-protected
lunny 6caa925
Merge branch 'master' into sign-protected
zeripath 70ab01c
As per @guillep2k comment
zeripath b705175
Merge branch 'master' into sign-protected
lafriks b2ab76b
Merge branch 'master' into sign-protected
lunny 7c9b15b
Merge branch 'master' into sign-protected
lunny 801bcfd
Merge branch 'master' into sign-protected
lunny bd0081b
Merge branch 'master' into sign-protected
lafriks 9a63f61
Merge branch 'master' into sign-protected
lunny 00ec9ed
Merge branch 'master' into sign-protected
lunny dbce143
Merge branch 'master' into sign-protected
zeripath 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,121 @@ | ||
// 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 models | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// SignMerge determines if we should sign a PR merge commit to the base repository | ||
func (pr *PullRequest) SignMerge(u *User, tmpBasePath, baseCommit, headCommit string) (bool, string) { | ||
if err := pr.GetBaseRepo(); err != nil { | ||
log.Error("Unable to get Base Repo for pull request") | ||
return false, "" | ||
} | ||
repo := pr.BaseRepo | ||
|
||
signingKey := signingKey(repo.RepoPath()) | ||
if signingKey == "" { | ||
lafriks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false, "" | ||
} | ||
rules := signingModeFromStrings(setting.Repository.Signing.Merges) | ||
|
||
var gitRepo *git.Repository | ||
var err error | ||
|
||
for _, rule := range rules { | ||
switch rule { | ||
case never: | ||
return false, "" | ||
case always: | ||
break | ||
case pubkey: | ||
keys, err := ListGPGKeys(u.ID) | ||
if err != nil || len(keys) == 0 { | ||
return false, "" | ||
} | ||
case twofa: | ||
twofa, err := GetTwoFactorByUID(u.ID) | ||
if err != nil || twofa == nil { | ||
return false, "" | ||
} | ||
case approved: | ||
protectedBranch, err := GetProtectedBranchBy(repo.ID, pr.BaseBranch) | ||
if err != nil || protectedBranch == nil { | ||
return false, "" | ||
} | ||
if protectedBranch.GetGrantedApprovalsCount(pr) < 1 { | ||
lafriks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false, "" | ||
} | ||
case baseSigned: | ||
if gitRepo == nil { | ||
gitRepo, err = git.OpenRepository(tmpBasePath) | ||
if err != nil { | ||
return false, "" | ||
} | ||
defer gitRepo.Close() | ||
} | ||
commit, err := gitRepo.GetCommit(baseCommit) | ||
if err != nil { | ||
return false, "" | ||
} | ||
verification := ParseCommitWithSignature(commit) | ||
if !verification.Verified { | ||
return false, "" | ||
} | ||
case headSigned: | ||
if gitRepo == nil { | ||
gitRepo, err = git.OpenRepository(tmpBasePath) | ||
if err != nil { | ||
return false, "" | ||
} | ||
defer gitRepo.Close() | ||
} | ||
commit, err := gitRepo.GetCommit(headCommit) | ||
if err != nil { | ||
return false, "" | ||
} | ||
verification := ParseCommitWithSignature(commit) | ||
if !verification.Verified { | ||
return false, "" | ||
} | ||
case commitsSigned: | ||
if gitRepo == nil { | ||
gitRepo, err = git.OpenRepository(tmpBasePath) | ||
if err != nil { | ||
return false, "" | ||
} | ||
defer gitRepo.Close() | ||
} | ||
commit, err := gitRepo.GetCommit(headCommit) | ||
if err != nil { | ||
return false, "" | ||
} | ||
verification := ParseCommitWithSignature(commit) | ||
if !verification.Verified { | ||
return false, "" | ||
} | ||
// need to work out merge-base | ||
mergeBaseCommit, _, err := gitRepo.GetMergeBase("", baseCommit, headCommit) | ||
if err != nil { | ||
return false, "" | ||
} | ||
commitList, err := commit.CommitsBeforeUntil(mergeBaseCommit) | ||
if err != nil { | ||
return false, "" | ||
} | ||
for e := commitList.Front(); e != nil; e = e.Next() { | ||
commit = e.Value.(*git.Commit) | ||
verification := ParseCommitWithSignature(commit) | ||
if !verification.Verified { | ||
return false, "" | ||
} | ||
} | ||
} | ||
} | ||
return true, signingKey | ||
} |
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
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.
How about move these new codes to
services/pull
?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.
So SignInitialCommit has to currently be in models and given the rest of this code is in models - it probably makes more sense to keep it there until we can get SignInitialCommit out.
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.
I think we can move them step by step so that it will be easy to review. There is some intermediate state that some parts in models, some parts in pull but I think that's OK.