Skip to content

Commit 295af50

Browse files
committed
Fix push commits comments when changing the pull request target branch
1 parent 7a2273a commit 295af50

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
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 & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,68 @@ import (
1616
// getCommitIDsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
1717
// isForcePush will be true if oldCommit isn't on the branch
1818
// 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) {
19+
func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, err error) {
2020
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
2121
if err != nil {
22-
return nil, false, err
22+
return nil, err
2323
}
2424
defer closer.Close()
2525

2626
oldCommit, err := gitRepo.GetCommit(oldCommitID)
2727
if err != nil {
28-
return nil, false, err
28+
return nil, err
2929
}
3030

3131
newCommit, err := gitRepo.GetCommit(newCommitID)
3232
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
33+
return nil, err
4734
}
4835

4936
// Find commits between new and old commit excluding base branch commits
5037
commits, err := gitRepo.CommitsBetweenNotBase(newCommit, oldCommit, baseBranch)
5138
if err != nil {
52-
return nil, false, err
39+
return nil, err
5340
}
5441

5542
commitIDs = make([]string, 0, len(commits))
5643
for i := len(commits) - 1; i >= 0; i-- {
5744
commitIDs = append(commitIDs, commits[i].ID.String())
5845
}
5946

60-
return commitIDs, isForcePush, err
47+
return commitIDs, err
6148
}
6249

6350
// 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) {
51+
func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *issues_model.PullRequest, oldCommitID, newCommitID string, isForcePush bool) (comment *issues_model.Comment, err error) {
6552
if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
6653
return nil, nil
6754
}
6855

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

7564
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
65+
if opts.IsForcePush {
66+
data.CommitIDs = []string{oldCommitID, newCommitID}
67+
} else {
68+
data.CommitIDs, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
69+
if err != nil {
70+
return nil, err
71+
}
8072
}
8173

82-
ops.Issue = pr.Issue
83-
8474
dataJSON, err := json.Marshal(data)
8575
if err != nil {
8676
return nil, err
8777
}
8878

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

9382
return comment, err
9483
}

services/pull/pull.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
140140
return err
141141
}
142142

143-
if _, err := CreatePushPullComment(ctx, issue.Poster, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName()); err != nil {
143+
if _, err := CreatePushPullComment(ctx, issue.Poster, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName(), false); err != nil {
144144
return err
145145
}
146146

@@ -334,7 +334,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
334334
return err
335335
}
336336

337-
_, err = CreatePushPullComment(ctx, doer, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName())
337+
_, err = CreatePushPullComment(ctx, doer, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName(), false)
338338
return err
339339
})
340340
}
@@ -397,7 +397,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
397397
}
398398

399399
StartPullRequestCheckImmediately(ctx, pr)
400-
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID)
400+
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID, opts.IsForcePush)
401401
if err == nil && comment != nil {
402402
notify_service.PullRequestPushCommits(ctx, opts.Doer, pr, comment)
403403
}

0 commit comments

Comments
 (0)