Skip to content

Pull request updates will also trigger code owners review requests #33744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f0dd070
pull request updates will also trigger code owners review requests
lunny Feb 27, 2025
172178c
merge the code owner file list
lunny Feb 28, 2025
fb1fb03
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
lunny Feb 28, 2025
b31d067
Only request code owner when syncing commits files ownered by the cod…
lunny Mar 2, 2025
dd1eb52
Use a structure as parameters for TestPullrequestTask
lunny Mar 7, 2025
5b0fb1e
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
lunny Mar 8, 2025
449ed5b
Add test for PullRequestCodeOwnersReview and PullRequestCodeOwnersRev…
lunny Mar 11, 2025
64db717
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
lunny Mar 11, 2025
6f79267
Update services/issue/pull.go
wxiaoguang Mar 12, 2025
f8b02f7
Add comment for IsSync
lunny Mar 13, 2025
480de54
Merge branch 'lunny/add_reviewer_notifier_pr_sync' of github.com:lunn…
lunny Mar 13, 2025
2aee564
Fix test
lunny Mar 13, 2025
afb2be2
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
lunny Mar 13, 2025
4961418
improve the comment for IsSync
lunny Mar 13, 2025
47d3234
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
lunny Mar 13, 2025
aa9dd18
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
GiteaBot Mar 14, 2025
aaf7915
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
GiteaBot Mar 14, 2025
10ce667
Merge branch 'main' into lunny/add_reviewer_notifier_pr_sync
GiteaBot Mar 14, 2025
a950281
Fix test
lunny Mar 14, 2025
f03bc81
Merge branch 'lunny/add_reviewer_notifier_pr_sync' of github.com:lunn…
lunny Mar 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions routers/web/repo/view_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"image"
"io"
"path"
"slices"
"strings"

git_model "code.gitea.io/gitea/models/git"
Expand Down Expand Up @@ -79,7 +78,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
if workFlowErr != nil {
ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error())
}
} else if slices.Contains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) {
} else if issue_service.IsCodeOwnerFile(ctx.Repo.TreePath) {
if data, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize); err == nil {
_, warnings := issue_model.GetCodeOwnersFromContent(ctx, data)
if len(warnings) > 0 {
Expand Down
6 changes: 5 additions & 1 deletion services/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_mode

var reviewNotifiers []*ReviewRequestNotifier
if issue.IsPull && issues_model.HasWorkInProgressPrefix(oldTitle) && !issues_model.HasWorkInProgressPrefix(title) {
if err := issue.LoadPullRequest(ctx); err != nil {
return err
}

var err error
reviewNotifiers, err = PullRequestCodeOwnersReview(ctx, issue, issue.PullRequest)
reviewNotifiers, err = PullRequestCodeOwnersReview(ctx, issue.PullRequest)
if err != nil {
log.Error("PullRequestCodeOwnersReview: %v", err)
}
Expand Down
41 changes: 31 additions & 10 deletions services/issue/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package issue
import (
"context"
"fmt"
"slices"
"time"

issues_model "code.gitea.io/gitea/models/issues"
Expand Down Expand Up @@ -40,17 +41,27 @@ type ReviewRequestNotifier struct {
ReviewTeam *org_model.Team
}

func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
files := []string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}
var codeOwnerFiles = []string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}

func IsCodeOwnerFile(f string) bool {
return slices.Contains(codeOwnerFiles, f)
}

func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
return PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, "", "")
}

func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_model.PullRequest, startCommitID, endCommitID string) ([]*ReviewRequestNotifier, error) {
if err := pr.LoadIssue(ctx); err != nil {
return nil, err
}
issue := pr.Issue
if pr.IsWorkInProgress(ctx) {
return nil, nil
}

if err := pr.LoadHeadRepo(ctx); err != nil {
return nil, err
}

if err := pr.LoadBaseRepo(ctx); err != nil {
return nil, err
}
Expand All @@ -71,26 +82,36 @@ func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue,
}

var data string
for _, file := range files {
for _, file := range codeOwnerFiles {
if blob, err := commit.GetBlobByPath(file); err == nil {
data, err = blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
if err == nil {
break
}
}
}
if data == "" {
return nil, nil
}

rules, _ := issues_model.GetCodeOwnersFromContent(ctx, data)
if len(rules) == 0 {
return nil, nil
}

// get the mergebase
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
if err != nil {
return nil, err
if startCommitID == "" && endCommitID == "" {
// get the mergebase
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
if err != nil {
return nil, err
}
startCommitID = mergeBase
endCommitID = pr.GetGitRefName()
}

// https://github.com/go-gitea/gitea/issues/29763, we need to get the files changed
// between the merge base and the head commit but not the base branch and the head commit
changedFiles, err := repo.GetFilesChangedBetween(mergeBase, pr.GetGitRefName())
changedFiles, err := repo.GetFilesChangedBetween(startCommitID, endCommitID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
}
defer releaser()
defer func() {
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, false, "", "")
}()

_, err = doMergeAndPush(ctx, pr, doer, mergeStyle, expectedHeadCommitID, message, repo_module.PushTriggerPRMergeToBase)
Expand Down
19 changes: 17 additions & 2 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
}

if !pr.IsWorkInProgress(ctx) {
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, issue, pr)
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
if err != nil {
return err
}
Expand Down Expand Up @@ -374,7 +374,7 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest

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

if !pr.IsWorkInProgress(ctx) {
var reviewNotifiers []*issue_service.ReviewRequestNotifier
if isForPush {
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
} else {
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, oldCommitID, newCommitID)
}
if err != nil {
log.Error("PullRequestCodeOwnersReview: %v", err)
}
if len(reviewNotifiers) > 0 {
issue_service.ReviewRequestNotify(ctx, pr.Issue, doer, reviewNotifiers)
}
}

notify_service.PullRequestSynchronized(ctx, doer, pr)
}
}
Expand Down
4 changes: 2 additions & 2 deletions services/pull/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.

if rebase {
defer func() {
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, false, "", "")
}()

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

defer func() {
go AddTestPullRequestTask(doer, reversePR.HeadRepo.ID, reversePR.HeadBranch, false, "", "")
go AddTestPullRequestTask(doer, reversePR.HeadRepo.ID, reversePR.HeadBranch, false, false, "", "")
}()

return err
Expand Down
4 changes: 3 additions & 1 deletion services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
branch := opts.RefFullName.BranchName()
if !opts.IsDelRef() {
log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)

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

// only update branch can trigger pull request task because the pull request hasn't been created yet when creaing a branch
go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, isForcePush, opts.OldCommitID, opts.NewCommitID)

if isForcePush {
log.Trace("Push %s is a force push", opts.NewCommitID)

Expand Down
Loading