2019-12-15 11:06:31 +00:00
|
|
|
// 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
|
2020-09-19 16:44:55 +00:00
|
|
|
func (pr *PullRequest) SignMerge(u *User, tmpBasePath, baseCommit, headCommit string) (bool, string, *git.Signature, error) {
|
2020-03-02 22:31:55 +00:00
|
|
|
if err := pr.LoadBaseRepo(); err != nil {
|
2019-12-15 11:06:31 +00:00
|
|
|
log.Error("Unable to get Base Repo for pull request")
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
repo := pr.BaseRepo
|
|
|
|
|
2020-09-19 16:44:55 +00:00
|
|
|
signingKey, signer := SigningKey(repo.RepoPath())
|
2019-12-15 11:06:31 +00:00
|
|
|
if signingKey == "" {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{noKey}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
rules := signingModeFromStrings(setting.Repository.Signing.Merges)
|
|
|
|
|
|
|
|
var gitRepo *git.Repository
|
|
|
|
var err error
|
|
|
|
|
2020-07-31 13:26:47 +00:00
|
|
|
Loop:
|
2019-12-15 11:06:31 +00:00
|
|
|
for _, rule := range rules {
|
|
|
|
switch rule {
|
|
|
|
case never:
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{never}
|
2019-12-15 11:06:31 +00:00
|
|
|
case always:
|
2020-07-31 13:26:47 +00:00
|
|
|
break Loop
|
2019-12-15 11:06:31 +00:00
|
|
|
case pubkey:
|
2020-01-24 19:00:29 +00:00
|
|
|
keys, err := ListGPGKeys(u.ID, ListOptions{})
|
2020-01-15 08:32:57 +00:00
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2020-01-15 08:32:57 +00:00
|
|
|
}
|
|
|
|
if len(keys) == 0 {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{pubkey}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
case twofa:
|
2020-01-15 08:32:57 +00:00
|
|
|
twofaModel, err := GetTwoFactorByUID(u.ID)
|
2020-01-26 23:44:12 +00:00
|
|
|
if err != nil && !IsErrTwoFactorNotEnrolled(err) {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2020-01-15 08:32:57 +00:00
|
|
|
}
|
|
|
|
if twofaModel == nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{twofa}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
case approved:
|
|
|
|
protectedBranch, err := GetProtectedBranchBy(repo.ID, pr.BaseBranch)
|
2020-01-15 08:32:57 +00:00
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2020-01-15 08:32:57 +00:00
|
|
|
}
|
|
|
|
if protectedBranch == nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{approved}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
if protectedBranch.GetGrantedApprovalsCount(pr) < 1 {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{approved}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
case baseSigned:
|
|
|
|
if gitRepo == nil {
|
|
|
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
}
|
|
|
|
commit, err := gitRepo.GetCommit(baseCommit)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
verification := ParseCommitWithSignature(commit)
|
|
|
|
if !verification.Verified {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{baseSigned}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
case headSigned:
|
|
|
|
if gitRepo == nil {
|
|
|
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
}
|
|
|
|
commit, err := gitRepo.GetCommit(headCommit)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
verification := ParseCommitWithSignature(commit)
|
|
|
|
if !verification.Verified {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{headSigned}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
case commitsSigned:
|
|
|
|
if gitRepo == nil {
|
|
|
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
}
|
|
|
|
commit, err := gitRepo.GetCommit(headCommit)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
verification := ParseCommitWithSignature(commit)
|
|
|
|
if !verification.Verified {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{commitsSigned}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
// need to work out merge-base
|
|
|
|
mergeBaseCommit, _, err := gitRepo.GetMergeBase("", baseCommit, headCommit)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
commitList, err := commit.CommitsBeforeUntil(mergeBaseCommit)
|
|
|
|
if err != nil {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, err
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
for e := commitList.Front(); e != nil; e = e.Next() {
|
|
|
|
commit = e.Value.(*git.Commit)
|
|
|
|
verification := ParseCommitWithSignature(commit)
|
|
|
|
if !verification.Verified {
|
2020-09-19 16:44:55 +00:00
|
|
|
return false, "", nil, &ErrWontSign{commitsSigned}
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 16:44:55 +00:00
|
|
|
return true, signingKey, signer, nil
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|