fix(pull): refresh commits behind when an AGit pull request is updated (#39613)

Fixes #39598

The AGit update path in `services/agit` moved `refs/pull/N/head` without
recomputing commits_behind.

The AGit update path now calls `syncCommitDivergence` in
`UpdateRefForAgit`, the same as AGit PR creation (`NewPullRequest`)
already does.

---------

Signed-off-by: dziulatex <paweldziurasoftware@yahoo.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
dziulatexandwxiaoguang authored and GitHub committed 2026-10-06 07:21:43 +00:00
1 parent bd2a6c40d7
commit 1264072754
3 files changed
+25 -25

No files matched your search

+1 -1
View File
@@ -252,7 +252,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
oldHeadCommitID := pr.HeadCommitID
pr.HeadCommitID = opts.NewCommitIDs[i]
if err = pull_service.UpdateRef(ctx, pr); err != nil {
if err = pull_service.UpdateRefForAgit(ctx, pr); err != nil {
return nil, fmt.Errorf("failed to update pull ref. Error: %w", err)
}
+19 -24
View File
@@ -98,8 +98,9 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
assigneeCommentMap := make(map[int64]*issues_model.Comment)
assignees := make(map[int64]*user_model.User)
var reviewNotifiers []*issue_service.ReviewRequestNotifier
if err := db.WithTx(ctx, func(ctx context.Context) error {
if err := issues_model.NewPullRequest(ctx, repo, issue, labelIDs, uuids, pr); err != nil {
err := db.WithTx(ctx, func(ctx context.Context) error {
err := issues_model.NewPullRequest(ctx, repo, issue, labelIDs, uuids, pr)
if err != nil {
return err
}
@@ -126,20 +127,17 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
pr.Issue = issue
issue.PullRequest = pr
var err error
if pr.Flow == issues_model.PullRequestFlowGithub {
err = PushToBaseRepo(ctx, pr)
} else {
err = UpdateRef(ctx, pr)
}
if err != nil {
return err
}
// Update Commit Divergence
err = syncCommitDivergence(ctx, pr)
if err != nil {
return err
if err = PushToBaseRepo(ctx, pr); err != nil {
return err
}
if err = syncCommitDivergence(ctx, pr); err != nil {
return err
}
} else { // agit
if err = UpdateRefForAgit(ctx, pr); err != nil {
return err
}
}
// add first push codes comment
@@ -154,7 +152,8 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
}
}
return nil
}); err != nil {
})
if err != nil {
// cleanup: this will only remove the reference, the real commit will be clean up when next GC
if err1 := git.RemoveRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName()); err1 != nil {
log.Error("RemoveRef: %v", err1)
@@ -602,19 +601,15 @@ func UpdatePullsRefs(ctx context.Context, repo *repo_model.Repository, update *r
}
}
// UpdateRef update refs/pull/id/head directly for agit flow pull request
func UpdateRef(ctx context.Context, pr *issues_model.PullRequest) (err error) {
log.Trace("UpdateRef[%d]: upgate pull request ref in base repo '%s'", pr.ID, pr.GetGitHeadRefName())
// UpdateRefForAgit update refs/pull/id/head directly for agit flow pull request
func UpdateRefForAgit(ctx context.Context, pr *issues_model.PullRequest) (err error) {
if err := pr.LoadBaseRepo(ctx); err != nil {
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
return err
}
if err := git.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID); err != nil {
log.Error("Unable to update ref in base repository for PR[%d] Error: %v", pr.ID, err)
return fmt.Errorf("unable to update ref %s for base repo %s, err: %w", pr.GetGitHeadRefName(), pr.BaseRepo.FullName(), err)
}
return err
return syncCommitDivergence(ctx, pr)
}
// retargetBranchPulls change target branch for all pull requests whose base branch is the branch
+5
View File
@@ -109,6 +109,8 @@ func TestAgitPullPush(t *testing.T) {
assert.NoError(t, pr.LoadIssue(t.Context()))
assert.Equal(t, "test-title", pr.Issue.Title)
assert.Equal(t, "test-description", pr.Issue.Content)
assert.Equal(t, 0, pr.CommitsBehind)
assert.Equal(t, 1, pr.CommitsAhead)
// commit 2
_, err = generateCommitWithNewData(t.Context(), testFileSizeSmall, dstPath, "user2@example.com", "User Two", "branch-data-file-2-")
@@ -119,6 +121,9 @@ func TestAgitPullPush(t *testing.T) {
WithDir(dstPath).
Run(t.Context())
assert.NoError(t, err)
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{BaseRepoID: 1, Flow: issues_model.PullRequestFlowAGit, HeadBranch: "user2/test-agit-push"})
assert.Equal(t, 0, pr.CommitsBehind)
assert.Equal(t, 2, pr.CommitsAhead)
// reset to first commit
err = gitcmd.NewCommand("reset", "--hard", "HEAD~1").WithDir(dstPath).Run(t.Context())