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 (
|
2021-09-24 11:32:56 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-09-25 13:00:12 +00:00
|
|
|
"code.gitea.io/gitea/models/login"
|
2019-12-15 11:06:31 +00:00
|
|
|
"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:
|
2021-09-24 11:32:56 +00:00
|
|
|
keys, err := ListGPGKeys(u.ID, db.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:
|
2021-09-25 13:00:12 +00:00
|
|
|
twofaModel, err := login.GetTwoFactorByUID(u.ID)
|
|
|
|
if err != nil && !login.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
|
|
|
}
|
2021-08-09 18:08:51 +00:00
|
|
|
for _, commit := range commitList {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 16:44:55 +00:00
|
|
|
return true, signingKey, signer, nil
|
2019-12-15 11:06:31 +00:00
|
|
|
}
|