Skip to content

Commit 78fbcf3

Browse files
authored
Fix push commits comments when changing the pull request target branch (#35386) (#35443)
Backport #35386 When changing the pull request target branch, the pushed commits comments will not be changed resulted the number are inconsistent between commits tab number and the pushed commits comments number. This PR will remove all the previous pushed commits comments and calculate new comments when changing the target branch. Before: <img width="928" height="585" alt="image" src="https://github.com/user-attachments/assets/35e4d31f-31a1-4d14-83b0-1786721ab0d9" /> After: <img width="816" height="623" alt="image" src="https://github.com/user-attachments/assets/24b6dafe-9238-4e7e-833d-68472457afab" />
1 parent 8f5b1d2 commit 78fbcf3

File tree

3 files changed

+57
-80
lines changed

3 files changed

+57
-80
lines changed

services/agit/agit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
250250
if err != nil {
251251
return nil, fmt.Errorf("failed to load pull issue. Error: %w", err)
252252
}
253-
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
253+
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i], forcePush.Value())
254254
if err == nil && comment != nil {
255255
notify_service.PullRequestPushCommits(ctx, pusher, pr, comment)
256256
}

services/pull/comment.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,69 @@ import (
1414
)
1515

1616
// getCommitIDsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
17-
// isForcePush will be true if oldCommit isn't on the branch
1817
// Commit on baseBranch will skip
19-
func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
18+
func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, err error) {
2019
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
2120
if err != nil {
22-
return nil, false, err
21+
return nil, err
2322
}
2423
defer closer.Close()
2524

2625
oldCommit, err := gitRepo.GetCommit(oldCommitID)
2726
if err != nil {
28-
return nil, false, err
27+
return nil, err
2928
}
3029

3130
newCommit, err := gitRepo.GetCommit(newCommitID)
3231
if err != nil {
33-
return nil, false, err
34-
}
35-
36-
isForcePush, err = newCommit.IsForcePush(oldCommitID)
37-
if err != nil {
38-
return nil, false, err
39-
}
40-
41-
if isForcePush {
42-
commitIDs = make([]string, 2)
43-
commitIDs[0] = oldCommitID
44-
commitIDs[1] = newCommitID
45-
46-
return commitIDs, isForcePush, err
32+
return nil, err
4733
}
4834

4935
// Find commits between new and old commit excluding base branch commits
5036
commits, err := gitRepo.CommitsBetweenNotBase(newCommit, oldCommit, baseBranch)
5137
if err != nil {
52-
return nil, false, err
38+
return nil, err
5339
}
5440

5541
commitIDs = make([]string, 0, len(commits))
5642
for i := len(commits) - 1; i >= 0; i-- {
5743
commitIDs = append(commitIDs, commits[i].ID.String())
5844
}
5945

60-
return commitIDs, isForcePush, err
46+
return commitIDs, err
6147
}
6248

6349
// CreatePushPullComment create push code to pull base comment
64-
func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *issues_model.PullRequest, oldCommitID, newCommitID string) (comment *issues_model.Comment, err error) {
50+
func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *issues_model.PullRequest, oldCommitID, newCommitID string, isForcePush bool) (comment *issues_model.Comment, err error) {
6551
if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
6652
return nil, nil
6753
}
6854

69-
ops := &issues_model.CreateCommentOptions{
70-
Type: issues_model.CommentTypePullRequestPush,
71-
Doer: pusher,
72-
Repo: pr.BaseRepo,
55+
opts := &issues_model.CreateCommentOptions{
56+
Type: issues_model.CommentTypePullRequestPush,
57+
Doer: pusher,
58+
Repo: pr.BaseRepo,
59+
IsForcePush: isForcePush,
60+
Issue: pr.Issue,
7361
}
7462

7563
var data issues_model.PushActionContent
76-
77-
data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
78-
if err != nil {
79-
return nil, err
64+
if opts.IsForcePush {
65+
data.CommitIDs = []string{oldCommitID, newCommitID}
66+
} else {
67+
data.CommitIDs, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
68+
if err != nil {
69+
return nil, err
70+
}
8071
}
8172

82-
ops.Issue = pr.Issue
83-
8473
dataJSON, err := json.Marshal(data)
8574
if err != nil {
8675
return nil, err
8776
}
8877

89-
ops.Content = string(dataJSON)
90-
91-
comment, err = issues_model.CreateComment(ctx, ops)
78+
opts.Content = string(dataJSON)
79+
comment, err = issues_model.CreateComment(ctx, opts)
9280

9381
return comment, err
9482
}

services/pull/pull.go

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"code.gitea.io/gitea/modules/gitrepo"
2929
"code.gitea.io/gitea/modules/globallock"
3030
"code.gitea.io/gitea/modules/graceful"
31-
"code.gitea.io/gitea/modules/json"
3231
"code.gitea.io/gitea/modules/log"
3332
repo_module "code.gitea.io/gitea/modules/repository"
3433
"code.gitea.io/gitea/modules/setting"
@@ -142,37 +141,9 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
142141
return err
143142
}
144143

145-
compareInfo, err := baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(),
146-
git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName(), false, false)
147-
if err != nil {
144+
if _, err := CreatePushPullComment(ctx, issue.Poster, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName(), false); err != nil {
148145
return err
149146
}
150-
// It maybe an empty pull request. Only non-empty pull request need to create push comment
151-
if len(compareInfo.Commits) > 0 {
152-
data := issues_model.PushActionContent{IsForcePush: false}
153-
data.CommitIDs = make([]string, 0, len(compareInfo.Commits))
154-
for i := len(compareInfo.Commits) - 1; i >= 0; i-- {
155-
data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String())
156-
}
157-
158-
dataJSON, err := json.Marshal(data)
159-
if err != nil {
160-
return err
161-
}
162-
163-
ops := &issues_model.CreateCommentOptions{
164-
Type: issues_model.CommentTypePullRequestPush,
165-
Doer: issue.Poster,
166-
Repo: repo,
167-
Issue: pr.Issue,
168-
IsForcePush: false,
169-
Content: string(dataJSON),
170-
}
171-
172-
if _, err = issues_model.CreateComment(ctx, ops); err != nil {
173-
return err
174-
}
175-
}
176147

177148
if !pr.IsWorkInProgress(ctx) {
178149
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
@@ -335,24 +306,42 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
335306
pr.CommitsAhead = divergence.Ahead
336307
pr.CommitsBehind = divergence.Behind
337308

338-
if err := pr.UpdateColsIfNotMerged(ctx, "merge_base", "status", "conflicted_files", "changed_protected_files", "base_branch", "commits_ahead", "commits_behind"); err != nil {
309+
// add first push codes comment
310+
baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
311+
if err != nil {
339312
return err
340313
}
314+
defer baseGitRepo.Close()
341315

342-
// Create comment
343-
options := &issues_model.CreateCommentOptions{
344-
Type: issues_model.CommentTypeChangeTargetBranch,
345-
Doer: doer,
346-
Repo: pr.Issue.Repo,
347-
Issue: pr.Issue,
348-
OldRef: oldBranch,
349-
NewRef: targetBranch,
350-
}
351-
if _, err = issues_model.CreateComment(ctx, options); err != nil {
352-
return fmt.Errorf("CreateChangeTargetBranchComment: %w", err)
353-
}
316+
return db.WithTx(ctx, func(ctx context.Context) error {
317+
if err := pr.UpdateColsIfNotMerged(ctx, "merge_base", "status", "conflicted_files", "changed_protected_files", "base_branch", "commits_ahead", "commits_behind"); err != nil {
318+
return err
319+
}
354320

355-
return nil
321+
// Create comment
322+
options := &issues_model.CreateCommentOptions{
323+
Type: issues_model.CommentTypeChangeTargetBranch,
324+
Doer: doer,
325+
Repo: pr.Issue.Repo,
326+
Issue: pr.Issue,
327+
OldRef: oldBranch,
328+
NewRef: targetBranch,
329+
}
330+
if _, err = issues_model.CreateComment(ctx, options); err != nil {
331+
return fmt.Errorf("CreateChangeTargetBranchComment: %w", err)
332+
}
333+
334+
// Delete all old push comments and insert new push comments
335+
if _, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
336+
And("type = ?", issues_model.CommentTypePullRequestPush).
337+
NoAutoCondition().
338+
Delete(new(issues_model.Comment)); err != nil {
339+
return err
340+
}
341+
342+
_, err = CreatePushPullComment(ctx, doer, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName(), false)
343+
return err
344+
})
356345
}
357346

358347
func checkForInvalidation(ctx context.Context, requests issues_model.PullRequestList, repoID int64, doer *user_model.User, branch string) error {
@@ -413,7 +402,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
413402
}
414403

415404
StartPullRequestCheckImmediately(ctx, pr)
416-
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID)
405+
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID, opts.IsForcePush)
417406
if err == nil && comment != nil {
418407
notify_service.PullRequestPushCommits(ctx, opts.Doer, pr, comment)
419408
}

0 commit comments

Comments
 (0)