@@ -18,6 +18,7 @@ import (
1818// checkPullRequestMergeableAndUpdateStatus checks whether a pull request is mergeable and updates its status accordingly.
1919// It uses 'git merge-tree' if supported by the Git version, otherwise it falls back to using a temporary repository.
2020// This function updates the pr.Status, pr.MergeBase and pr.ConflictedFiles fields as necessary.
21+ // The pull request parameter may not be created yet in the database, so do not assume it has an ID.
2122func checkPullRequestMergeableAndUpdateStatus (ctx context.Context , pr * issues_model.PullRequest ) error {
2223 if git .DefaultFeatures ().SupportGitMergeTree {
2324 return checkPullRequestMergeableAndUpdateStatusMergeTree (ctx , pr )
@@ -72,15 +73,6 @@ func checkPullRequestMergeableAndUpdateStatusMergeTree(ctx context.Context, pr *
7273 }
7374 defer headGitRepo .Close ()
7475
75- if pr .Flow == issues_model .PullRequestFlowGithub {
76- pr .HeadCommitID , err = headGitRepo .GetRefCommitID (git .BranchPrefix + pr .HeadBranch )
77- if err != nil {
78- return fmt .Errorf ("GetBranchCommitID: can't find commit ID for head: %w" , err )
79- }
80- } else if pr .HeadCommitID == "" {
81- return errors .New ("head commit ID is empty for pull request Agit flow" )
82- }
83-
8476 // 2. Get base commit id
8577 var baseGitRepo * git.Repository
8678 if pr .IsSameRepo () {
@@ -97,26 +89,44 @@ func checkPullRequestMergeableAndUpdateStatusMergeTree(ctx context.Context, pr *
9789 return fmt .Errorf ("FetchRemoteCommit: %w" , err )
9890 }
9991 }
92+
93+ // 3. Get head commit id
94+ if pr .Flow == issues_model .PullRequestFlowGithub {
95+ pr .HeadCommitID , err = headGitRepo .GetRefCommitID (git .BranchPrefix + pr .HeadBranch )
96+ if err != nil {
97+ return fmt .Errorf ("GetBranchCommitID: can't find commit ID for head: %w" , err )
98+ }
99+ } else {
100+ if pr .ID > 0 {
101+ pr .HeadCommitID , err = baseGitRepo .GetRefCommitID (pr .GetGitHeadRefName ())
102+ if err != nil {
103+ return fmt .Errorf ("GetRefCommitID: can't find commit ID for head: %w" , err )
104+ }
105+ } else if pr .HeadCommitID == "" { // for new pull request with agit, the head commit id must be provided
106+ return errors .New ("head commit ID is empty for pull request Agit flow" )
107+ }
108+ }
109+
110+ // 4. update merge base
100111 baseCommitID , err := baseGitRepo .GetRefCommitID (git .BranchPrefix + pr .BaseBranch )
101112 if err != nil {
102113 return fmt .Errorf ("GetBranchCommitID: can't find commit ID for base: %w" , err )
103114 }
104115
105- // 3. update merge base
106116 pr .MergeBase , err = gitrepo .MergeBase (ctx , pr .BaseRepo , baseCommitID , pr .HeadCommitID )
107117 if err != nil {
108118 log .Error ("GetMergeBase: %v and can't find commit ID for base: %v" , err , baseCommitID )
109119 pr .Status = issues_model .PullRequestStatusEmpty // if there is no merge base, then it's empty but we still need to allow the pull request created
110120 return nil
111121 }
112122
113- // 4 . if base == head, then it's an ancestor
123+ // 5 . if base == head, then it's an ancestor
114124 if pr .HeadCommitID == pr .MergeBase {
115125 pr .Status = issues_model .PullRequestStatusAncestor
116126 return nil
117127 }
118128
119- // 5 . Check for conflicts
129+ // 6 . Check for conflicts
120130 conflicted , err := checkConflictsMergeTree (ctx , pr , baseCommitID )
121131 if err != nil {
122132 log .Error ("checkConflictsMergeTree: %v" , err )
@@ -126,7 +136,7 @@ func checkPullRequestMergeableAndUpdateStatusMergeTree(ctx context.Context, pr *
126136 return nil
127137 }
128138
129- // 6 . Check for protected files changes
139+ // 7 . Check for protected files changes
130140 if err = checkPullFilesProtection (ctx , pr , pr .BaseRepo .RepoPath ()); err != nil {
131141 return fmt .Errorf ("pr.CheckPullFilesProtection(): %v" , err )
132142 }
0 commit comments