Skip to content

Commit 8e2f501

Browse files
authored
Merge branch 'main' into improve-repo-view
2 parents c1fb56d + 9c673d0 commit 8e2f501

File tree

14 files changed

+202
-84
lines changed

14 files changed

+202
-84
lines changed

modules/gitrepo/gitrepo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Reposito
6969
ctx.SetContextValue(ck, gitRepo)
7070
return gitRepo, nil
7171
}
72+
73+
// IsRepositoryExist returns true if the repository directory exists in the disk
74+
func IsRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
75+
return util.IsExist(repoPath(repo))
76+
}

modules/repository/init.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
issues_model "code.gitea.io/gitea/models/issues"
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
"code.gitea.io/gitea/modules/git"
16+
"code.gitea.io/gitea/modules/gitrepo"
1617
"code.gitea.io/gitea/modules/label"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/options"
@@ -120,25 +121,24 @@ func LoadRepoConfig() error {
120121
return nil
121122
}
122123

123-
func CheckInitRepository(ctx context.Context, owner, name, objectFormatName string) (err error) {
124+
func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err error) {
124125
// Somehow the directory could exist.
125-
repoPath := repo_model.RepoPath(owner, name)
126-
isExist, err := util.IsExist(repoPath)
126+
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
127127
if err != nil {
128-
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
128+
log.Error("Unable to check if %s exists. Error: %v", repo.FullName(), err)
129129
return err
130130
}
131131
if isExist {
132132
return repo_model.ErrRepoFilesAlreadyExist{
133-
Uname: owner,
134-
Name: name,
133+
Uname: repo.OwnerName,
134+
Name: repo.Name,
135135
}
136136
}
137137

138138
// Init git bare new repository.
139-
if err = git.InitRepository(ctx, repoPath, true, objectFormatName); err != nil {
139+
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
140140
return fmt.Errorf("git.InitRepository: %w", err)
141-
} else if err = CreateDelegateHooks(repoPath); err != nil {
141+
} else if err = CreateDelegateHooks(repo.RepoPath()); err != nil {
142142
return fmt.Errorf("createDelegateHooks: %w", err)
143143
}
144144
return nil

routers/web/repo/view_file.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"image"
1010
"io"
1111
"path"
12-
"slices"
1312
"strings"
1413

1514
git_model "code.gitea.io/gitea/models/git"
@@ -79,7 +78,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
7978
if workFlowErr != nil {
8079
ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error())
8180
}
82-
} else if slices.Contains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) {
81+
} else if issue_service.IsCodeOwnerFile(ctx.Repo.TreePath) {
8382
if data, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize); err == nil {
8483
_, warnings := issue_model.GetCodeOwnersFromContent(ctx, data)
8584
if len(warnings) > 0 {

services/issue/issue.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_mode
9292

9393
var reviewNotifiers []*ReviewRequestNotifier
9494
if issue.IsPull && issues_model.HasWorkInProgressPrefix(oldTitle) && !issues_model.HasWorkInProgressPrefix(title) {
95+
if err := issue.LoadPullRequest(ctx); err != nil {
96+
return err
97+
}
98+
9599
var err error
96-
reviewNotifiers, err = PullRequestCodeOwnersReview(ctx, issue, issue.PullRequest)
100+
reviewNotifiers, err = PullRequestCodeOwnersReview(ctx, issue.PullRequest)
97101
if err != nil {
98102
log.Error("PullRequestCodeOwnersReview: %v", err)
99103
}

services/issue/pull.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issue
66
import (
77
"context"
88
"fmt"
9+
"slices"
910
"time"
1011

1112
issues_model "code.gitea.io/gitea/models/issues"
@@ -40,20 +41,31 @@ type ReviewRequestNotifier struct {
4041
ReviewTeam *org_model.Team
4142
}
4243

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

46+
func IsCodeOwnerFile(f string) bool {
47+
return slices.Contains(codeOwnerFiles, f)
48+
}
49+
50+
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) {
55+
if err := pr.LoadIssue(ctx); err != nil {
56+
return nil, err
57+
}
58+
issue := pr.Issue
4659
if pr.IsWorkInProgress(ctx) {
4760
return nil, nil
4861
}
49-
5062
if err := pr.LoadHeadRepo(ctx); err != nil {
5163
return nil, err
5264
}
53-
5465
if err := pr.LoadBaseRepo(ctx); err != nil {
5566
return nil, err
5667
}
68+
pr.Issue.Repo = pr.BaseRepo
5769

5870
if pr.BaseRepo.IsFork {
5971
return nil, nil
@@ -71,26 +83,36 @@ func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue,
7183
}
7284

7385
var data string
74-
for _, file := range files {
86+
for _, file := range codeOwnerFiles {
7587
if blob, err := commit.GetBlobByPath(file); err == nil {
7688
data, err = blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
7789
if err == nil {
7890
break
7991
}
8092
}
8193
}
94+
if data == "" {
95+
return nil, nil
96+
}
8297

8398
rules, _ := issues_model.GetCodeOwnersFromContent(ctx, data)
99+
if len(rules) == 0 {
100+
return nil, nil
101+
}
84102

85-
// get the mergebase
86-
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
87-
if err != nil {
88-
return nil, err
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()
89111
}
90112

91113
// https://github.com/go-gitea/gitea/issues/29763, we need to get the files changed
92114
// between the merge base and the head commit but not the base branch and the head commit
93-
changedFiles, err := repo.GetFilesChangedBetween(mergeBase, pr.GetGitRefName())
115+
changedFiles, err := repo.GetFilesChangedBetween(startCommitID, endCommitID)
94116
if err != nil {
95117
return nil, err
96118
}

services/pull/merge.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ 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(TestPullRequestOptions{
215+
RepoID: pr.BaseRepo.ID,
216+
Doer: doer,
217+
Branch: pr.BaseBranch,
218+
IsSync: false,
219+
IsForcePush: false,
220+
OldCommitID: "",
221+
NewCommitID: "",
222+
})
215223
}()
216224

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

services/pull/pull.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
176176
}
177177

178178
if !pr.IsWorkInProgress(ctx) {
179-
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, issue, pr)
179+
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
180180
if err != nil {
181181
return err
182182
}
@@ -372,19 +372,29 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest
372372
return nil
373373
}
374374

375+
type TestPullRequestOptions struct {
376+
RepoID int64
377+
Doer *user_model.User
378+
Branch string
379+
IsSync bool // True means it's a pull request synchronization, false means it's triggered for pull request merging or updating
380+
IsForcePush bool
381+
OldCommitID string
382+
NewCommitID string
383+
}
384+
375385
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
376386
// and generate new patch for testing as needed.
377-
func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, isSync bool, oldCommitID, newCommitID string) {
378-
log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
387+
func AddTestPullRequestTask(opts TestPullRequestOptions) {
388+
log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", opts.RepoID, opts.Branch)
379389
graceful.GetManager().RunWithShutdownContext(func(ctx context.Context) {
380390
// There is no sensible way to shut this down ":-("
381391
// If you don't let it run all the way then you will lose data
382392
// TODO: graceful: AddTestPullRequestTask needs to become a queue!
383393

384394
// GetUnmergedPullRequestsByHeadInfo() only return open and unmerged PR.
385-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repoID, branch)
395+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, opts.RepoID, opts.Branch)
386396
if err != nil {
387-
log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
397+
log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", opts.RepoID, opts.Branch, err)
388398
return
389399
}
390400

@@ -400,24 +410,24 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
400410
}
401411

402412
AddToTaskQueue(ctx, pr)
403-
comment, err := CreatePushPullComment(ctx, doer, pr, oldCommitID, newCommitID)
413+
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID)
404414
if err == nil && comment != nil {
405-
notify_service.PullRequestPushCommits(ctx, doer, pr, comment)
415+
notify_service.PullRequestPushCommits(ctx, opts.Doer, pr, comment)
406416
}
407417
}
408418

409-
if isSync {
419+
if opts.IsSync {
410420
if err = prs.LoadAttributes(ctx); err != nil {
411421
log.Error("PullRequestList.LoadAttributes: %v", err)
412422
}
413-
if invalidationErr := checkForInvalidation(ctx, prs, repoID, doer, branch); invalidationErr != nil {
423+
if invalidationErr := checkForInvalidation(ctx, prs, opts.RepoID, opts.Doer, opts.Branch); invalidationErr != nil {
414424
log.Error("checkForInvalidation: %v", invalidationErr)
415425
}
416426
if err == nil {
417427
for _, pr := range prs {
418428
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
419-
if newCommitID != "" && newCommitID != objectFormat.EmptyObjectID().String() {
420-
changed, err := checkIfPRContentChanged(ctx, pr, oldCommitID, newCommitID)
429+
if opts.NewCommitID != "" && opts.NewCommitID != objectFormat.EmptyObjectID().String() {
430+
changed, err := checkIfPRContentChanged(ctx, pr, opts.OldCommitID, opts.NewCommitID)
421431
if err != nil {
422432
log.Error("checkIfPRContentChanged: %v", err)
423433
}
@@ -433,12 +443,12 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
433443
log.Error("GetFirstMatchProtectedBranchRule: %v", err)
434444
}
435445
if pb != nil && pb.DismissStaleApprovals {
436-
if err := DismissApprovalReviews(ctx, doer, pr); err != nil {
446+
if err := DismissApprovalReviews(ctx, opts.Doer, pr); err != nil {
437447
log.Error("DismissApprovalReviews: %v", err)
438448
}
439449
}
440450
}
441-
if err := issues_model.MarkReviewsAsNotStale(ctx, pr.IssueID, newCommitID); err != nil {
451+
if err := issues_model.MarkReviewsAsNotStale(ctx, pr.IssueID, opts.NewCommitID); err != nil {
442452
log.Error("MarkReviewsAsNotStale: %v", err)
443453
}
444454
divergence, err := GetDiverging(ctx, pr)
@@ -452,15 +462,30 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
452462
}
453463
}
454464

455-
notify_service.PullRequestSynchronized(ctx, doer, pr)
465+
if !pr.IsWorkInProgress(ctx) {
466+
var reviewNotifiers []*issue_service.ReviewRequestNotifier
467+
if opts.IsForcePush {
468+
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReview(ctx, pr)
469+
} else {
470+
reviewNotifiers, err = issue_service.PullRequestCodeOwnersReviewSpecialCommits(ctx, pr, opts.OldCommitID, opts.NewCommitID)
471+
}
472+
if err != nil {
473+
log.Error("PullRequestCodeOwnersReview: %v", err)
474+
}
475+
if len(reviewNotifiers) > 0 {
476+
issue_service.ReviewRequestNotify(ctx, pr.Issue, opts.Doer, reviewNotifiers)
477+
}
478+
}
479+
480+
notify_service.PullRequestSynchronized(ctx, opts.Doer, pr)
456481
}
457482
}
458483
}
459484

460-
log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
461-
prs, err = issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
485+
log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", opts.RepoID, opts.Branch)
486+
prs, err = issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, opts.RepoID, opts.Branch)
462487
if err != nil {
463-
log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
488+
log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", opts.RepoID, opts.Branch, err)
464489
return
465490
}
466491
for _, pr := range prs {

services/pull/update.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ 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(TestPullRequestOptions{
46+
RepoID: pr.BaseRepo.ID,
47+
Doer: doer,
48+
Branch: pr.BaseBranch,
49+
IsSync: false,
50+
IsForcePush: false,
51+
OldCommitID: "",
52+
NewCommitID: "",
53+
})
4654
}()
4755

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

8593
defer func() {
86-
go AddTestPullRequestTask(doer, reversePR.HeadRepo.ID, reversePR.HeadBranch, false, "", "")
94+
go AddTestPullRequestTask(TestPullRequestOptions{
95+
RepoID: reversePR.HeadRepo.ID,
96+
Doer: doer,
97+
Branch: reversePR.HeadBranch,
98+
IsSync: false,
99+
IsForcePush: false,
100+
OldCommitID: "",
101+
NewCommitID: "",
102+
})
87103
}()
88104

89105
return err

services/repository/adopt.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
5252
IsEmpty: !opts.AutoInit,
5353
}
5454

55-
repoPath := repo_model.RepoPath(u.Name, repo.Name)
56-
5755
if err := db.WithTx(ctx, func(ctx context.Context) error {
58-
isExist, err := util.IsExist(repoPath)
56+
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
5957
if err != nil {
60-
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
58+
log.Error("Unable to check if %s exists. Error: %v", repo.FullName(), err)
6159
return err
6260
}
6361
if !isExist {
@@ -82,7 +80,7 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
8280
}
8381

8482
if err := func() error {
85-
if err := adoptRepository(ctx, repoPath, repo, opts.DefaultBranch); err != nil {
83+
if err := adoptRepository(ctx, repo, opts.DefaultBranch); err != nil {
8684
return fmt.Errorf("adoptRepository: %w", err)
8785
}
8886

@@ -91,7 +89,7 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
9189
}
9290

9391
if stdout, _, err := git.NewCommand("update-server-info").
94-
RunStdString(ctx, &git.RunOpts{Dir: repoPath}); err != nil {
92+
RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath()}); err != nil {
9593
log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err)
9694
return fmt.Errorf("CreateRepository(git update-server-info): %w", err)
9795
}
@@ -107,17 +105,17 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
107105
return repo, nil
108106
}
109107

110-
func adoptRepository(ctx context.Context, repoPath string, repo *repo_model.Repository, defaultBranch string) (err error) {
111-
isExist, err := util.IsExist(repoPath)
108+
func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBranch string) (err error) {
109+
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
112110
if err != nil {
113-
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
111+
log.Error("Unable to check if %s exists. Error: %v", repo.FullName(), err)
114112
return err
115113
}
116114
if !isExist {
117-
return fmt.Errorf("adoptRepository: path does not already exist: %s", repoPath)
115+
return fmt.Errorf("adoptRepository: path does not already exist: %s", repo.FullName())
118116
}
119117

120-
if err := repo_module.CreateDelegateHooks(repoPath); err != nil {
118+
if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil {
121119
return fmt.Errorf("createDelegateHooks: %w", err)
122120
}
123121

0 commit comments

Comments
 (0)