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