Skip to content

Commit e3e7052

Browse files
GiteaBotlunny
andauthored
Fix wrong review requests when updating the pull request (#34286) (#34304)
Backport #34286 by @lunny Fix #34224 The previous implementation in #33744 will get the pushed commits changed files. But it's not always right when push a merged commit. This PR reverted the logic in #33744 and will always get the PR's changed files and get code owners. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent a9d5ab8 commit e3e7052

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

services/issue/pull.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ func IsCodeOwnerFile(f string) bool {
4848
}
4949

5050
func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
51-
return PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, "", "") // no commit is provided, then it uses PR's base&head branch
52-
}
53-
54-
func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_model.PullRequest, startCommitID, endCommitID string) ([]*ReviewRequestNotifier, error) {
5551
if err := pr.LoadIssue(ctx); err != nil {
5652
return nil, err
5753
}
@@ -100,19 +96,15 @@ func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_m
10096
return nil, nil
10197
}
10298

103-
if startCommitID == "" && endCommitID == "" {
104-
// get the mergebase
105-
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
106-
if err != nil {
107-
return nil, err
108-
}
109-
startCommitID = mergeBase
110-
endCommitID = pr.GetGitRefName()
99+
// get the mergebase
100+
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
101+
if err != nil {
102+
return nil, err
111103
}
112104

113105
// https://github.com/go-gitea/gitea/issues/29763, we need to get the files changed
114106
// between the merge base and the head commit but not the base branch and the head commit
115-
changedFiles, err := repo.GetFilesChangedBetween(startCommitID, endCommitID)
107+
changedFiles, err := repo.GetFilesChangedBetween(mergeBase, pr.GetGitRefName())
116108
if err != nil {
117109
return nil, err
118110
}
@@ -138,8 +130,23 @@ func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_m
138130
return nil, err
139131
}
140132

133+
// load all reviews from database
134+
latestReivews, _, err := issues_model.GetReviewsByIssueID(ctx, pr.IssueID)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
contain := func(list issues_model.ReviewList, u *user_model.User) bool {
140+
for _, review := range list {
141+
if review.ReviewerTeamID == 0 && review.ReviewerID == u.ID {
142+
return true
143+
}
144+
}
145+
return false
146+
}
147+
141148
for _, u := range uniqUsers {
142-
if u.ID != issue.Poster.ID {
149+
if u.ID != issue.Poster.ID && !contain(latestReivews, u) {
143150
comment, err := issues_model.AddReviewRequest(ctx, issue, u, issue.Poster)
144151
if err != nil {
145152
log.Warn("Failed add assignee user: %s to PR review: %s#%d, error: %s", u.Name, pr.BaseRepo.Name, pr.ID, err)
@@ -155,6 +162,7 @@ func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_m
155162
})
156163
}
157164
}
165+
158166
for _, t := range uniqTeams {
159167
comment, err := issues_model.AddTeamReviewRequest(ctx, issue, t, issue.Poster)
160168
if err != nil {

services/pull/pull.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
440440
}
441441

442442
if !pr.IsWorkInProgress(ctx) {
443-
var reviewNotifiers []*issue_service.ReviewRequestNotifier
444-
if opts.IsForcePush {
445-
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
446-
} else {
447-
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, opts.OldCommitID, opts.NewCommitID)
448-
}
443+
reviewNotifiers, err := issue_service.PullRequestCodeOwnersReview(ctx, pr)
449444
if err != nil {
450445
log.Error("PullRequestCodeOwnersReview: %v", err)
451446
}

0 commit comments

Comments
 (0)