Skip to content

Commit b31d067

Browse files
committed
Only request code owner when syncing commits files ownered by the code owners.
1 parent fb1fb03 commit b31d067

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

services/issue/pull.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func IsCodeOwnerFile(f string) bool {
4848
}
4949

5050
func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
51+
return PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, "", "")
52+
}
53+
54+
func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_model.PullRequest, startCommitID, endCommitID string) ([]*ReviewRequestNotifier, error) {
5155
if err := pr.LoadIssue(ctx); err != nil {
5256
return nil, err
5357
}
@@ -78,7 +82,6 @@ func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullReque
7882
}
7983

8084
var data string
81-
8285
for _, file := range codeOwnerFiles {
8386
if blob, err := commit.GetBlobByPath(file); err == nil {
8487
data, err = blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
@@ -87,18 +90,28 @@ func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullReque
8790
}
8891
}
8992
}
93+
if data == "" {
94+
return nil, nil
95+
}
9096

9197
rules, _ := issues_model.GetCodeOwnersFromContent(ctx, data)
98+
if len(rules) == 0 {
99+
return nil, nil
100+
}
92101

93-
// get the mergebase
94-
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
95-
if err != nil {
96-
return nil, err
102+
if startCommitID == "" && endCommitID == "" {
103+
// get the mergebase
104+
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
105+
if err != nil {
106+
return nil, err
107+
}
108+
startCommitID = mergeBase
109+
endCommitID = pr.GetGitRefName()
97110
}
98111

99112
// https://github.com/go-gitea/gitea/issues/29763, we need to get the files changed
100113
// between the merge base and the head commit but not the base branch and the head commit
101-
changedFiles, err := repo.GetFilesChangedBetween(mergeBase, pr.GetGitRefName())
114+
changedFiles, err := repo.GetFilesChangedBetween(startCommitID, endCommitID)
102115
if err != nil {
103116
return nil, err
104117
}

services/pull/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
211211
}
212212
defer releaser()
213213
defer func() {
214-
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
214+
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, false, "", "")
215215
}()
216216

217217
_, err = doMergeAndPush(ctx, pr, doer, mergeStyle, expectedHeadCommitID, message, repo_module.PushTriggerPRMergeToBase)

services/pull/pull.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest
374374

375375
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
376376
// and generate new patch for testing as needed.
377-
func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, isSync bool, oldCommitID, newCommitID string) {
377+
func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, isSync, isForPush bool, oldCommitID, newCommitID string) {
378378
log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
379379
graceful.GetManager().RunWithShutdownContext(func(ctx context.Context) {
380380
// There is no sensible way to shut this down ":-("
@@ -454,10 +454,16 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
454454
}
455455

456456
if !pr.IsWorkInProgress(ctx) {
457-
reviewNotifiers, err := issue_service.PullRequestCodeOwnersReview(ctx, pr)
457+
var reviewNotifiers []*issue_service.ReviewRequestNotifier
458+
if isForPush {
459+
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
460+
} else {
461+
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, oldCommitID, newCommitID)
462+
}
458463
if err != nil {
459464
log.Error("PullRequestCodeOwnersReview: %v", err)
460-
} else {
465+
}
466+
if len(reviewNotifiers) > 0 {
461467
issue_service.ReviewRequestNotify(ctx, pr.Issue, doer, reviewNotifiers)
462468
}
463469
}

services/pull/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.
4242

4343
if rebase {
4444
defer func() {
45-
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
45+
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, false, "", "")
4646
}()
4747

4848
return updateHeadByRebaseOnToBase(ctx, pr, doer)
@@ -83,7 +83,7 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.
8383
_, err = doMergeAndPush(ctx, reversePR, doer, repo_model.MergeStyleMerge, "", message, repository.PushTriggerPRUpdateWithBase)
8484

8585
defer func() {
86-
go AddTestPullRequestTask(doer, reversePR.HeadRepo.ID, reversePR.HeadBranch, false, "", "")
86+
go AddTestPullRequestTask(doer, reversePR.HeadRepo.ID, reversePR.HeadBranch, false, false, "", "")
8787
}()
8888

8989
return err

services/repository/push.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
166166
branch := opts.RefFullName.BranchName()
167167
if !opts.IsDelRef() {
168168
log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
169-
go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
170169

171170
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
172171
if err != nil {
@@ -208,6 +207,9 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
208207
log.Error("IsForcePush %s:%s failed: %v", repo.FullName(), branch, err)
209208
}
210209

210+
// only update branch can trigger pull request task because the pull request hasn't been created yet when creaing a branch
211+
go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, isForcePush, opts.OldCommitID, opts.NewCommitID)
212+
211213
if isForcePush {
212214
log.Trace("Push %s is a force push", opts.NewCommitID)
213215

0 commit comments

Comments
 (0)