2019-06-22 17:35:34 +00:00
// Copyright 2019 The Gitea Authors.
// All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2019-06-22 17:35:34 +00:00
package pull
import (
2022-01-19 23:26:57 +00:00
"context"
2019-06-22 17:35:34 +00:00
"fmt"
"os"
"path/filepath"
2019-11-01 00:30:02 +00:00
"regexp"
2022-05-08 12:32:45 +00:00
"strconv"
2019-06-22 17:35:34 +00:00
"strings"
"code.gitea.io/gitea/models"
2022-05-03 19:46:28 +00:00
"code.gitea.io/gitea/models/db"
2022-06-12 15:51:54 +00:00
git_model "code.gitea.io/gitea/models/git"
2022-06-13 09:37:59 +00:00
issues_model "code.gitea.io/gitea/models/issues"
2022-05-11 10:09:36 +00:00
access_model "code.gitea.io/gitea/models/perm/access"
2021-12-10 01:27:50 +00:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-09 19:57:58 +00:00
"code.gitea.io/gitea/models/unit"
2021-11-24 09:49:20 +00:00
user_model "code.gitea.io/gitea/models/user"
2019-06-22 17:35:34 +00:00
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
2024-09-06 10:12:41 +00:00
"code.gitea.io/gitea/modules/globallock"
2024-06-15 03:43:57 +00:00
"code.gitea.io/gitea/modules/httplib"
2019-06-22 17:35:34 +00:00
"code.gitea.io/gitea/modules/log"
2019-11-18 13:13:07 +00:00
"code.gitea.io/gitea/modules/references"
2022-05-08 16:46:32 +00:00
repo_module "code.gitea.io/gitea/modules/repository"
2019-06-22 17:35:34 +00:00
"code.gitea.io/gitea/modules/setting"
2019-08-15 14:46:21 +00:00
"code.gitea.io/gitea/modules/timeutil"
2019-11-18 13:13:07 +00:00
issue_service "code.gitea.io/gitea/services/issue"
2023-09-05 18:37:47 +00:00
notify_service "code.gitea.io/gitea/services/notify"
2019-06-22 17:35:34 +00:00
)
2023-05-22 01:01:46 +00:00
// getMergeMessage composes the message used when merging a pull request.
func getMergeMessage ( ctx context . Context , baseGitRepo * git . Repository , pr * issues_model . PullRequest , mergeStyle repo_model . MergeStyle , extraVars map [ string ] string ) ( message , body string , err error ) {
2022-11-19 08:12:33 +00:00
if err := pr . LoadBaseRepo ( ctx ) ; err != nil {
2022-12-29 12:40:20 +00:00
return "" , "" , err
2022-05-08 12:32:45 +00:00
}
2023-03-07 20:07:35 +00:00
if err := pr . LoadHeadRepo ( ctx ) ; err != nil {
return "" , "" , err
2022-05-08 12:32:45 +00:00
}
2022-11-19 08:12:33 +00:00
if err := pr . LoadIssue ( ctx ) ; err != nil {
2022-12-29 12:40:20 +00:00
return "" , "" , err
2022-05-08 12:32:45 +00:00
}
2023-10-17 15:07:23 +00:00
if err := pr . Issue . LoadPoster ( ctx ) ; err != nil {
return "" , "" , err
}
2024-06-06 08:35:04 +00:00
if err := pr . Issue . LoadRepo ( ctx ) ; err != nil {
return "" , "" , err
}
2022-05-08 12:32:45 +00:00
2022-12-10 02:46:31 +00:00
isExternalTracker := pr . BaseRepo . UnitEnabled ( ctx , unit . TypeExternalTracker )
2022-05-08 12:32:45 +00:00
issueReference := "#"
if isExternalTracker {
issueReference = "!"
}
2024-06-15 03:43:57 +00:00
reviewedOn := fmt . Sprintf ( "Reviewed-on: %s" , httplib . MakeAbsoluteURL ( ctx , pr . Issue . Link ( ) ) )
2024-06-06 08:35:04 +00:00
reviewedBy := pr . GetApprovers ( ctx )
2022-05-08 12:32:45 +00:00
if mergeStyle != "" {
templateFilepath := fmt . Sprintf ( ".gitea/default_merge_message/%s_TEMPLATE.md" , strings . ToUpper ( string ( mergeStyle ) ) )
commit , err := baseGitRepo . GetBranchCommit ( pr . BaseRepo . DefaultBranch )
if err != nil {
2022-12-29 12:40:20 +00:00
return "" , "" , err
2022-05-08 12:32:45 +00:00
}
templateContent , err := commit . GetFileContent ( templateFilepath , setting . Repository . PullRequest . DefaultMergeMessageSize )
if err != nil {
if ! git . IsErrNotExist ( err ) {
2022-12-29 12:40:20 +00:00
return "" , "" , err
2022-05-08 12:32:45 +00:00
}
} else {
vars := map [ string ] string {
"BaseRepoOwnerName" : pr . BaseRepo . OwnerName ,
"BaseRepoName" : pr . BaseRepo . Name ,
"BaseBranch" : pr . BaseBranch ,
"HeadRepoOwnerName" : "" ,
"HeadRepoName" : "" ,
"HeadBranch" : pr . HeadBranch ,
"PullRequestTitle" : pr . Issue . Title ,
"PullRequestDescription" : pr . Issue . Content ,
"PullRequestPosterName" : pr . Issue . Poster . Name ,
"PullRequestIndex" : strconv . FormatInt ( pr . Index , 10 ) ,
"PullRequestReference" : fmt . Sprintf ( "%s%d" , issueReference , pr . Index ) ,
2024-06-06 08:35:04 +00:00
"ReviewedOn" : reviewedOn ,
"ReviewedBy" : reviewedBy ,
2022-05-08 12:32:45 +00:00
}
if pr . HeadRepo != nil {
vars [ "HeadRepoOwnerName" ] = pr . HeadRepo . OwnerName
vars [ "HeadRepoName" ] = pr . HeadRepo . Name
}
2023-05-22 01:01:46 +00:00
for extraKey , extraValue := range extraVars {
vars [ extraKey ] = extraValue
}
2022-11-19 08:12:33 +00:00
refs , err := pr . ResolveCrossReferences ( ctx )
2022-05-08 12:32:45 +00:00
if err == nil {
closeIssueIndexes := make ( [ ] string , 0 , len ( refs ) )
closeWord := "close"
if len ( setting . Repository . PullRequest . CloseKeywords ) > 0 {
closeWord = setting . Repository . PullRequest . CloseKeywords [ 0 ]
}
for _ , ref := range refs {
if ref . RefAction == references . XRefActionCloses {
2023-02-09 02:47:52 +00:00
if err := ref . LoadIssue ( ctx ) ; err != nil {
return "" , "" , err
}
2022-05-08 12:32:45 +00:00
closeIssueIndexes = append ( closeIssueIndexes , fmt . Sprintf ( "%s %s%d" , closeWord , issueReference , ref . Issue . Index ) )
}
}
if len ( closeIssueIndexes ) > 0 {
vars [ "ClosingIssues" ] = strings . Join ( closeIssueIndexes , ", " )
} else {
vars [ "ClosingIssues" ] = ""
}
}
2022-12-29 12:40:20 +00:00
message , body = expandDefaultMergeMessage ( templateContent , vars )
return message , body , nil
2022-05-08 12:32:45 +00:00
}
}
2023-07-10 08:12:50 +00:00
if mergeStyle == repo_model . MergeStyleRebase {
// for fast-forward rebase, do not amend the last commit if there is no template
return "" , "" , nil
}
2024-06-06 08:35:04 +00:00
body = fmt . Sprintf ( "%s\n%s" , reviewedOn , reviewedBy )
2022-05-08 12:32:45 +00:00
// Squash merge has a different from other styles.
if mergeStyle == repo_model . MergeStyleSquash {
2024-06-06 08:35:04 +00:00
return fmt . Sprintf ( "%s (%s%d)" , pr . Issue . Title , issueReference , pr . Issue . Index ) , body , nil
2022-05-08 12:32:45 +00:00
}
if pr . BaseRepoID == pr . HeadRepoID {
2024-06-06 08:35:04 +00:00
return fmt . Sprintf ( "Merge pull request '%s' (%s%d) from %s into %s" , pr . Issue . Title , issueReference , pr . Issue . Index , pr . HeadBranch , pr . BaseBranch ) , body , nil
2022-05-08 12:32:45 +00:00
}
if pr . HeadRepo == nil {
2024-06-06 08:35:04 +00:00
return fmt . Sprintf ( "Merge pull request '%s' (%s%d) from <deleted>:%s into %s" , pr . Issue . Title , issueReference , pr . Issue . Index , pr . HeadBranch , pr . BaseBranch ) , body , nil
2022-05-08 12:32:45 +00:00
}
2024-06-06 08:35:04 +00:00
return fmt . Sprintf ( "Merge pull request '%s' (%s%d) from %s:%s into %s" , pr . Issue . Title , issueReference , pr . Issue . Index , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseBranch ) , body , nil
2022-12-29 12:40:20 +00:00
}
func expandDefaultMergeMessage ( template string , vars map [ string ] string ) ( message , body string ) {
message = strings . TrimSpace ( template )
if splits := strings . SplitN ( message , "\n" , 2 ) ; len ( splits ) == 2 {
message = splits [ 0 ]
body = strings . TrimSpace ( splits [ 1 ] )
}
mapping := func ( s string ) string { return vars [ s ] }
return os . Expand ( message , mapping ) , os . Expand ( body , mapping )
2022-05-08 12:32:45 +00:00
}
2023-05-22 01:01:46 +00:00
// GetDefaultMergeMessage returns default message used when merging pull request
func GetDefaultMergeMessage ( ctx context . Context , baseGitRepo * git . Repository , pr * issues_model . PullRequest , mergeStyle repo_model . MergeStyle ) ( message , body string , err error ) {
return getMergeMessage ( ctx , baseGitRepo , pr , mergeStyle , nil )
}
2019-06-22 17:35:34 +00:00
// Merge merges pull request to base repository.
2020-01-11 07:29:34 +00:00
// Caller should check PR is ready to be merged (review and status checks)
2022-11-03 15:49:00 +00:00
func Merge ( ctx context . Context , pr * issues_model . PullRequest , doer * user_model . User , baseGitRepo * git . Repository , mergeStyle repo_model . MergeStyle , expectedHeadCommitID , message string , wasAutoMerged bool ) error {
2023-03-07 20:07:35 +00:00
if err := pr . LoadBaseRepo ( ctx ) ; err != nil {
log . Error ( "Unable to load base repo: %v" , err )
return fmt . Errorf ( "unable to load base repo: %w" , err )
} else if err := pr . LoadHeadRepo ( ctx ) ; err != nil {
log . Error ( "Unable to load head repo: %v" , err )
return fmt . Errorf ( "unable to load head repo: %w" , err )
2019-06-22 17:35:34 +00:00
}
2022-12-10 02:46:31 +00:00
prUnit , err := pr . BaseRepo . GetUnit ( ctx , unit . TypePullRequests )
2019-06-22 17:35:34 +00:00
if err != nil {
2021-11-09 19:57:58 +00:00
log . Error ( "pr.BaseRepo.GetUnit(unit.TypePullRequests): %v" , err )
2019-06-22 17:35:34 +00:00
return err
}
prConfig := prUnit . PullRequestsConfig ( )
// Check if merge style is correct and allowed
if ! prConfig . IsMergeStyleAllowed ( mergeStyle ) {
return models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : mergeStyle }
}
2024-09-06 10:12:41 +00:00
releaser , err := globallock . Lock ( ctx , getPullWorkingLockKey ( pr . ID ) )
if err != nil {
log . Error ( "lock.Lock(): %v" , err )
return fmt . Errorf ( "lock.Lock: %w" , err )
}
defer releaser ( )
2019-06-22 17:35:34 +00:00
defer func ( ) {
2020-01-09 01:47:45 +00:00
go AddTestPullRequestTask ( doer , pr . BaseRepo . ID , pr . BaseBranch , false , "" , "" )
2019-06-22 17:35:34 +00:00
} ( )
2024-05-07 07:36:48 +00:00
_ , err = doMergeAndPush ( ctx , pr , doer , mergeStyle , expectedHeadCommitID , message , repo_module . PushTriggerPRMergeToBase )
2024-09-06 10:12:41 +00:00
releaser ( )
2020-01-17 06:03:40 +00:00
if err != nil {
2020-02-09 23:09:31 +00:00
return err
2020-01-17 06:03:40 +00:00
}
2024-05-07 07:36:48 +00:00
// reload pull request because it has been updated by post receive hook
pr , err = issues_model . GetPullRequestByID ( ctx , pr . ID )
if err != nil {
return err
2020-01-17 06:03:40 +00:00
}
2023-07-22 14:14:27 +00:00
if err := pr . LoadIssue ( ctx ) ; err != nil {
2023-03-07 20:07:35 +00:00
log . Error ( "LoadIssue %-v: %v" , pr , err )
2020-02-09 23:09:31 +00:00
}
2023-07-22 14:14:27 +00:00
if err := pr . Issue . LoadRepo ( ctx ) ; err != nil {
2023-03-07 20:07:35 +00:00
log . Error ( "pr.Issue.LoadRepo %-v: %v" , pr , err )
2020-02-09 23:09:31 +00:00
}
2023-07-22 14:14:27 +00:00
if err := pr . Issue . Repo . LoadOwner ( ctx ) ; err != nil {
2023-03-07 20:07:35 +00:00
log . Error ( "LoadOwner for %-v: %v" , pr , err )
2020-02-09 23:09:31 +00:00
}
2022-11-03 15:49:00 +00:00
if wasAutoMerged {
2023-09-05 18:37:47 +00:00
notify_service . AutoMergePullRequest ( ctx , doer , pr )
2022-11-03 15:49:00 +00:00
} else {
2023-09-05 18:37:47 +00:00
notify_service . MergePullRequest ( ctx , doer , pr )
2022-11-03 15:49:00 +00:00
}
2020-01-17 06:03:40 +00:00
// Reset cached commit count
cache . Remove ( pr . Issue . Repo . GetCommitsCountCacheKey ( pr . BaseBranch , true ) )
2024-08-25 17:18:19 +00:00
return handleCloseCrossReferences ( ctx , pr , doer )
}
func handleCloseCrossReferences ( ctx context . Context , pr * issues_model . PullRequest , doer * user_model . User ) error {
2020-01-17 06:03:40 +00:00
// Resolve cross references
2023-07-22 14:14:27 +00:00
refs , err := pr . ResolveCrossReferences ( ctx )
2020-01-17 06:03:40 +00:00
if err != nil {
log . Error ( "ResolveCrossReferences: %v" , err )
return nil
}
for _ , ref := range refs {
2023-07-22 14:14:27 +00:00
if err = ref . LoadIssue ( ctx ) ; err != nil {
2020-01-17 06:03:40 +00:00
return err
}
2023-07-22 14:14:27 +00:00
if err = ref . Issue . LoadRepo ( ctx ) ; err != nil {
2020-01-17 06:03:40 +00:00
return err
}
2024-04-22 11:48:42 +00:00
isClosed := ref . RefAction == references . XRefActionCloses
if isClosed != ref . Issue . IsClosed {
if err = issue_service . ChangeStatus ( ctx , ref . Issue , doer , pr . MergedCommitID , isClosed ) ; err != nil {
2022-01-18 23:26:42 +00:00
// Allow ErrDependenciesLeft
2022-06-13 09:37:59 +00:00
if ! issues_model . IsErrDependenciesLeft ( err ) {
2022-01-18 23:26:42 +00:00
return err
}
2020-01-17 06:03:40 +00:00
}
}
}
return nil
}
2023-03-07 20:07:35 +00:00
// doMergeAndPush performs the merge operation without changing any pull information in database and pushes it up to the base repository
2024-06-11 18:47:45 +00:00
func doMergeAndPush ( ctx context . Context , pr * issues_model . PullRequest , doer * user_model . User , mergeStyle repo_model . MergeStyle , expectedHeadCommitID , message string , pushTrigger repo_module . PushTrigger ) ( string , error ) { //nolint:unparam
2019-06-22 17:35:34 +00:00
// Clone base repo.
2023-03-07 20:07:35 +00:00
mergeCtx , cancel , err := createTemporaryRepoForMerge ( ctx , pr , doer , expectedHeadCommitID )
2019-06-22 17:35:34 +00:00
if err != nil {
2020-02-09 23:09:31 +00:00
return "" , err
2019-06-22 17:35:34 +00:00
}
2023-03-07 20:07:35 +00:00
defer cancel ( )
2019-10-16 13:42:42 +00:00
2019-06-22 17:35:34 +00:00
// Merge commits.
switch mergeStyle {
2021-12-10 01:27:50 +00:00
case repo_model . MergeStyleMerge :
2023-03-07 20:07:35 +00:00
if err := doMergeStyleMerge ( mergeCtx , message ) ; err != nil {
2020-02-09 23:09:31 +00:00
return "" , err
2019-06-22 17:35:34 +00:00
}
2023-03-07 20:07:35 +00:00
case repo_model . MergeStyleRebase , repo_model . MergeStyleRebaseMerge :
if err := doMergeStyleRebase ( mergeCtx , mergeStyle , message ) ; err != nil {
2020-02-09 23:09:31 +00:00
return "" , err
2019-06-22 17:35:34 +00:00
}
2021-12-10 01:27:50 +00:00
case repo_model . MergeStyleSquash :
2023-03-07 20:07:35 +00:00
if err := doMergeStyleSquash ( mergeCtx , message ) ; err != nil {
2020-02-09 23:09:31 +00:00
return "" , err
2019-06-22 17:35:34 +00:00
}
2024-02-12 22:37:23 +00:00
case repo_model . MergeStyleFastForwardOnly :
if err := doMergeStyleFastForwardOnly ( mergeCtx ) ; err != nil {
return "" , err
}
2019-06-22 17:35:34 +00:00
default :
2020-02-09 23:09:31 +00:00
return "" , models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : mergeStyle }
2019-06-22 17:35:34 +00:00
}
// OK we should cache our current head and origin/headbranch
2023-03-07 20:07:35 +00:00
mergeHeadSHA , err := git . GetFullCommitID ( ctx , mergeCtx . tmpBasePath , "HEAD" )
2019-06-22 17:35:34 +00:00
if err != nil {
2022-10-24 19:29:17 +00:00
return "" , fmt . Errorf ( "Failed to get full commit id for HEAD: %w" , err )
2019-06-22 17:35:34 +00:00
}
2023-03-07 20:07:35 +00:00
mergeBaseSHA , err := git . GetFullCommitID ( ctx , mergeCtx . tmpBasePath , "original_" + baseBranch )
2019-06-22 17:35:34 +00:00
if err != nil {
2022-10-24 19:29:17 +00:00
return "" , fmt . Errorf ( "Failed to get full commit id for origin/%s: %w" , pr . BaseBranch , err )
2020-02-09 23:09:31 +00:00
}
2023-03-07 20:07:35 +00:00
mergeCommitID , err := git . GetFullCommitID ( ctx , mergeCtx . tmpBasePath , baseBranch )
2020-02-09 23:09:31 +00:00
if err != nil {
2022-10-24 19:29:17 +00:00
return "" , fmt . Errorf ( "Failed to get full commit id for the new merge: %w" , err )
2019-06-22 17:35:34 +00:00
}
// Now it's questionable about where this should go - either after or before the push
// I think in the interests of data safety - failures to push to the lfs should prevent
// the merge as you can always remerge.
if setting . LFS . StartServer {
2023-03-07 20:07:35 +00:00
if err := LFSPush ( ctx , mergeCtx . tmpBasePath , mergeHeadSHA , mergeBaseSHA , pr ) ; err != nil {
2020-02-09 23:09:31 +00:00
return "" , err
2019-06-22 17:35:34 +00:00
}
}
2021-11-24 09:49:20 +00:00
var headUser * user_model . User
2023-02-18 12:11:03 +00:00
err = pr . HeadRepo . LoadOwner ( ctx )
2019-07-01 01:18:13 +00:00
if err != nil {
2021-11-24 09:49:20 +00:00
if ! user_model . IsErrUserNotExist ( err ) {
2023-03-07 20:07:35 +00:00
log . Error ( "Can't find user: %d for head repository in %-v: %v" , pr . HeadRepo . OwnerID , pr , err )
2020-02-09 23:09:31 +00:00
return "" , err
2019-07-01 01:18:13 +00:00
}
2023-03-07 20:07:35 +00:00
log . Warn ( "Can't find user: %d for head repository in %-v - defaulting to doer: %s - %v" , pr . HeadRepo . OwnerID , pr , doer . Name , err )
2019-07-01 01:18:13 +00:00
headUser = doer
2019-10-18 11:13:31 +00:00
} else {
headUser = pr . HeadRepo . Owner
2019-07-01 01:18:13 +00:00
}
2023-03-07 20:07:35 +00:00
mergeCtx . env = repo_module . FullPushingEnvironment (
headUser ,
doer ,
pr . BaseRepo ,
pr . BaseRepo . Name ,
pr . ID ,
)
2024-05-07 07:36:48 +00:00
mergeCtx . env = append ( mergeCtx . env , repo_module . EnvPushTrigger + "=" + string ( pushTrigger ) )
2023-03-07 20:07:35 +00:00
pushCmd := git . NewCommand ( ctx , "push" , "origin" ) . AddDynamicArguments ( baseBranch + ":" + git . BranchPrefix + pr . BaseBranch )
2021-08-31 14:03:45 +00:00
2019-06-22 17:35:34 +00:00
// Push back to upstream.
2024-05-07 07:36:48 +00:00
// This cause an api call to "/api/internal/hook/post-receive/...",
// If it's merge, all db transaction and operations should be there but not here to prevent deadlock.
2023-03-07 20:07:35 +00:00
if err := pushCmd . Run ( mergeCtx . RunOpts ( ) ) ; err != nil {
if strings . Contains ( mergeCtx . errbuf . String ( ) , "non-fast-forward" ) {
2020-03-28 04:13:18 +00:00
return "" , & git . ErrPushOutOfDate {
2023-03-07 20:07:35 +00:00
StdOut : mergeCtx . outbuf . String ( ) ,
StdErr : mergeCtx . errbuf . String ( ) ,
2019-11-10 08:42:51 +00:00
Err : err ,
}
2023-03-07 20:07:35 +00:00
} else if strings . Contains ( mergeCtx . errbuf . String ( ) , "! [remote rejected]" ) {
2020-03-28 04:13:18 +00:00
err := & git . ErrPushRejected {
2023-03-07 20:07:35 +00:00
StdOut : mergeCtx . outbuf . String ( ) ,
StdErr : mergeCtx . errbuf . String ( ) ,
2020-02-22 13:08:48 +00:00
Err : err ,
}
err . GenerateMessage ( )
return "" , err
2019-11-10 08:42:51 +00:00
}
2023-03-07 20:07:35 +00:00
return "" , fmt . Errorf ( "git push: %s" , mergeCtx . errbuf . String ( ) )
2019-06-22 17:35:34 +00:00
}
2023-03-07 20:07:35 +00:00
mergeCtx . outbuf . Reset ( )
mergeCtx . errbuf . Reset ( )
2019-06-22 17:35:34 +00:00
2020-02-09 23:09:31 +00:00
return mergeCommitID , nil
2019-06-22 17:35:34 +00:00
}
2023-03-07 20:07:35 +00:00
func commitAndSignNoAuthor ( ctx * mergeContext , message string ) error {
2023-03-09 15:48:52 +00:00
cmdCommit := git . NewCommand ( ctx , "commit" ) . AddOptionFormat ( "--message=%s" , message )
if ctx . signKeyID == "" {
cmdCommit . AddArguments ( "--no-gpg-sign" )
} else {
cmdCommit . AddOptionFormat ( "-S%s" , ctx . signKeyID )
}
if err := cmdCommit . Run ( ctx . RunOpts ( ) ) ; err != nil {
2023-03-07 20:07:35 +00:00
log . Error ( "git commit %-v: %v\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
return fmt . Errorf ( "git commit %v: %w\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
2019-11-10 08:42:51 +00:00
}
return nil
}
2023-03-07 20:07:35 +00:00
func runMergeCommand ( ctx * mergeContext , mergeStyle repo_model . MergeStyle , cmd * git . Command ) error {
if err := cmd . Run ( ctx . RunOpts ( ) ) ; err != nil {
2019-11-10 08:42:51 +00:00
// Merge will leave a MERGE_HEAD file in the .git folder if there is a conflict
2023-03-07 20:07:35 +00:00
if _ , statErr := os . Stat ( filepath . Join ( ctx . tmpBasePath , ".git" , "MERGE_HEAD" ) ) ; statErr == nil {
2019-11-10 08:42:51 +00:00
// We have a merge conflict error
2023-03-07 20:07:35 +00:00
log . Debug ( "MergeConflict %-v: %v\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
2019-11-10 08:42:51 +00:00
return models . ErrMergeConflicts {
Style : mergeStyle ,
2023-03-07 20:07:35 +00:00
StdOut : ctx . outbuf . String ( ) ,
StdErr : ctx . errbuf . String ( ) ,
2019-11-10 08:42:51 +00:00
Err : err ,
}
2023-03-07 20:07:35 +00:00
} else if strings . Contains ( ctx . errbuf . String ( ) , "refusing to merge unrelated histories" ) {
log . Debug ( "MergeUnrelatedHistories %-v: %v\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
2019-11-10 08:42:51 +00:00
return models . ErrMergeUnrelatedHistories {
Style : mergeStyle ,
2023-03-07 20:07:35 +00:00
StdOut : ctx . outbuf . String ( ) ,
StdErr : ctx . errbuf . String ( ) ,
2019-11-10 08:42:51 +00:00
Err : err ,
}
2024-02-12 22:37:23 +00:00
} else if mergeStyle == repo_model . MergeStyleFastForwardOnly && strings . Contains ( ctx . errbuf . String ( ) , "Not possible to fast-forward, aborting" ) {
log . Debug ( "MergeDivergingFastForwardOnly %-v: %v\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
return models . ErrMergeDivergingFastForwardOnly {
StdOut : ctx . outbuf . String ( ) ,
StdErr : ctx . errbuf . String ( ) ,
Err : err ,
}
2019-11-10 08:42:51 +00:00
}
2023-03-07 20:07:35 +00:00
log . Error ( "git merge %-v: %v\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
return fmt . Errorf ( "git merge %v: %w\n%s\n%s" , ctx . pr , err , ctx . outbuf . String ( ) , ctx . errbuf . String ( ) )
2019-11-10 08:42:51 +00:00
}
2023-03-07 20:07:35 +00:00
ctx . outbuf . Reset ( )
ctx . errbuf . Reset ( )
2019-11-10 08:42:51 +00:00
return nil
}
2019-11-01 00:30:02 +00:00
var escapedSymbols = regexp . MustCompile ( ` ([*[?! \\]) ` )
2020-01-11 07:29:34 +00:00
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
2022-06-13 09:37:59 +00:00
func IsUserAllowedToMerge ( ctx context . Context , pr * issues_model . PullRequest , p access_model . Permission , user * user_model . User ) ( bool , error ) {
2020-04-14 16:29:31 +00:00
if user == nil {
return false , nil
}
2020-01-11 07:29:34 +00:00
2023-01-16 08:00:22 +00:00
pb , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , pr . BaseRepoID , pr . BaseBranch )
2020-01-11 07:29:34 +00:00
if err != nil {
return false , err
}
2023-01-16 08:00:22 +00:00
if ( p . CanWrite ( unit . TypeCode ) && pb == nil ) || ( pb != nil && git_model . IsUserMergeWhitelisted ( ctx , pb , user . ID , p ) ) {
2020-01-11 07:29:34 +00:00
return true , nil
}
return false , nil
}
2022-05-01 23:54:44 +00:00
// CheckPullBranchProtections checks whether the PR is ready to be merged (reviews and status checks)
2022-06-13 09:37:59 +00:00
func CheckPullBranchProtections ( ctx context . Context , pr * issues_model . PullRequest , skipProtectedFilesCheck bool ) ( err error ) {
2022-11-19 08:12:33 +00:00
if err = pr . LoadBaseRepo ( ctx ) ; err != nil {
2022-10-24 19:29:17 +00:00
return fmt . Errorf ( "LoadBaseRepo: %w" , err )
2020-03-02 22:31:55 +00:00
}
2023-01-16 08:00:22 +00:00
pb , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , pr . BaseRepoID , pr . BaseBranch )
if err != nil {
return fmt . Errorf ( "LoadProtectedBranch: %v" , err )
2020-01-11 07:29:34 +00:00
}
2023-01-16 08:00:22 +00:00
if pb == nil {
2020-03-02 22:31:55 +00:00
return nil
2020-01-11 07:29:34 +00:00
}
2022-01-19 23:26:57 +00:00
isPass , err := IsPullCommitStatusPass ( ctx , pr )
2020-01-11 07:29:34 +00:00
if err != nil {
return err
}
if ! isPass {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-01-11 07:29:34 +00:00
Reason : "Not all required status checks successful" ,
}
}
2023-01-16 08:00:22 +00:00
if ! issues_model . HasEnoughApprovals ( ctx , pb , pr ) {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-01-11 07:29:34 +00:00
Reason : "Does not have enough approvals" ,
}
}
2023-01-16 08:00:22 +00:00
if issues_model . MergeBlockedByRejectedReview ( ctx , pb , pr ) {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-01-11 07:29:34 +00:00
Reason : "There are requested changes" ,
}
}
2023-01-16 08:00:22 +00:00
if issues_model . MergeBlockedByOfficialReviewRequests ( ctx , pb , pr ) {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-11-28 19:30:46 +00:00
Reason : "There are official review requests" ,
}
}
2020-01-11 07:29:34 +00:00
2023-01-16 08:00:22 +00:00
if issues_model . MergeBlockedByOutdatedBranch ( pb , pr ) {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-04-17 01:00:36 +00:00
Reason : "The head branch is behind the base branch" ,
}
}
2020-10-13 18:50:57 +00:00
if skipProtectedFilesCheck {
return nil
}
2023-01-16 08:00:22 +00:00
if pb . MergeBlockedByProtectedFiles ( pr . ChangedProtectedFiles ) {
2022-03-31 14:53:08 +00:00
return models . ErrDisallowedToMerge {
2020-10-13 18:50:57 +00:00
Reason : "Changed protected files" ,
}
}
2020-01-11 07:29:34 +00:00
return nil
}
2021-03-04 03:41:23 +00:00
// MergedManually mark pr as merged manually
2023-10-14 08:37:24 +00:00
func MergedManually ( ctx context . Context , pr * issues_model . PullRequest , doer * user_model . User , baseGitRepo * git . Repository , commitID string ) error {
2024-09-06 10:12:41 +00:00
releaser , err := globallock . Lock ( ctx , getPullWorkingLockKey ( pr . ID ) )
if err != nil {
log . Error ( "lock.Lock(): %v" , err )
return fmt . Errorf ( "lock.Lock: %w" , err )
}
defer releaser ( )
2022-05-04 16:06:23 +00:00
2024-09-06 10:12:41 +00:00
err = db . WithTx ( ctx , func ( ctx context . Context ) error {
2023-01-16 08:00:22 +00:00
if err := pr . LoadBaseRepo ( ctx ) ; err != nil {
return err
}
2022-12-10 02:46:31 +00:00
prUnit , err := pr . BaseRepo . GetUnit ( ctx , unit . TypePullRequests )
2022-05-03 19:46:28 +00:00
if err != nil {
return err
}
prConfig := prUnit . PullRequestsConfig ( )
2021-03-04 03:41:23 +00:00
2022-05-03 19:46:28 +00:00
// Check if merge style is correct and allowed
if ! prConfig . IsMergeStyleAllowed ( repo_model . MergeStyleManuallyMerged ) {
return models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : repo_model . MergeStyleManuallyMerged }
}
2021-03-04 03:41:23 +00:00
2024-02-24 06:55:19 +00:00
objectFormat := git . ObjectFormatFromName ( pr . BaseRepo . ObjectFormatName )
2023-12-13 21:02:00 +00:00
if len ( commitID ) != objectFormat . FullLength ( ) {
2021-03-04 03:41:23 +00:00
return fmt . Errorf ( "Wrong commit ID" )
}
2022-05-03 19:46:28 +00:00
commit , err := baseGitRepo . GetCommit ( commitID )
if err != nil {
if git . IsErrNotExist ( err ) {
return fmt . Errorf ( "Wrong commit ID" )
}
return err
}
commitID = commit . ID . String ( )
2021-03-04 03:41:23 +00:00
2022-05-03 19:46:28 +00:00
ok , err := baseGitRepo . IsCommitInBranch ( commitID , pr . BaseBranch )
if err != nil {
return err
}
if ! ok {
return fmt . Errorf ( "Wrong commit ID" )
}
2021-03-04 03:41:23 +00:00
2022-05-03 19:46:28 +00:00
pr . MergedCommitID = commitID
pr . MergedUnix = timeutil . TimeStamp ( commit . Author . When . Unix ( ) )
2022-06-13 09:37:59 +00:00
pr . Status = issues_model . PullRequestStatusManuallyMerged
2022-05-03 19:46:28 +00:00
pr . Merger = doer
pr . MergerID = doer . ID
2022-06-20 10:02:49 +00:00
var merged bool
2022-05-03 19:46:28 +00:00
if merged , err = pr . SetMerged ( ctx ) ; err != nil {
return err
} else if ! merged {
return fmt . Errorf ( "SetMerged failed" )
}
return nil
2024-09-06 10:12:41 +00:00
} )
releaser ( )
if err != nil {
2022-05-03 19:46:28 +00:00
return err
2021-03-04 03:41:23 +00:00
}
2023-09-05 18:37:47 +00:00
notify_service . MergePullRequest ( baseGitRepo . Ctx , doer , pr )
2022-05-03 19:46:28 +00:00
log . Info ( "manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s" , pr . ID , pr . BaseRepo . Name , pr . BaseBranch , commitID )
2024-08-25 17:18:19 +00:00
return handleCloseCrossReferences ( ctx , pr , doer )
2021-03-04 03:41:23 +00:00
}