use searchrepo

This commit is contained in:
yp05327 2023-07-21 02:48:42 +00:00
parent 0bfec1a3e8
commit f7295177bd
3 changed files with 28 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
@ -401,20 +402,30 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep
if err != nil {
return nil, err
}
// search all related repos
repoCond := builder.Select("id").From("repository").
Where(builder.Or(
builder.Eq{"id": baseRepo.ID},
builder.Eq{"is_fork": true, "fork_id": baseRepo.ID},
))
repoOpts := repo_model.SearchRepoOptions{
Actor: doer,
Private: true,
AllPublic: false, // Include also all public repositories of users and public organisations
AllLimited: false, // Include also all public repositories of limited organisations
Fork: util.OptionalBoolTrue,
ForkID: baseRepo.ID,
Archived: util.OptionalBoolFalse,
}
repoCond := repo_model.SearchRepositoryCondition(&repoOpts).
And(repo_model.AccessibleRepositoryCondition(doer, unit.TypeCode)).
Or(builder.Eq{"id": baseRepo.ID})
repoIds := builder.Select("id").From("repository").Where(repoCond)
// avoid check branches which have already created PRs
// TODO add head_branch_id in pull_request table then we can get the branch id from pull_request table directly
invalidBranchCond := builder.Select("branch.id").From("branch").
prBranchIds := builder.Select("branch.id").From("branch").
InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id").
InnerJoin("issue", "issue.id = pull_request.issue_id").
Where(builder.Or(
builder.Eq{"pull_request.has_merged": true},
builder.In("pull_request.head_repo_id", repoCond),
builder.In("pull_request.head_repo_id", repoIds),
builder.Eq{"branch.id": baseBranch.ID},
))
@ -422,8 +433,8 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep
commitAfterUnix = time.Now().Add(-time.Hour * 6).Unix()
}
opts := FindBranchOptions{
IDCond: builder.NotIn("id", invalidBranchCond),
RepoCond: builder.In("repo_id", repoCond),
IDCond: builder.NotIn("id", prBranchIds),
RepoCond: builder.In("repo_id", repoIds),
CommitCond: builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them,
PusherID: doer.ID,
IsDeletedBranch: util.OptionalBoolFalse,

View File

@ -218,4 +218,6 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
branches, err = git_model.FindRecentlyPushedNewBranches(db.DefaultContext, repo, user5, 1689838760)
assert.NoError(t, err)
assert.Equal(t, 0, len(branches))
// TODO:test pr branch
}

View File

@ -133,7 +133,8 @@ type SearchRepoOptions struct {
// None -> include forks AND non-forks
// True -> include just forks
// False -> include just non-forks
Fork util.OptionalBool
Fork util.OptionalBool
ForkID int64 // If Fork option is True, you can use this option to limit the forks of a special repo by repo id.
// None -> include templates AND non-templates
// True -> include just templates
// False -> include just non-templates
@ -467,6 +468,10 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
cond = cond.And(builder.Eq{"is_fork": false})
} else {
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
if opts.ForkID > 0 && opts.Fork == util.OptionalBoolTrue {
cond = cond.And(builder.Eq{"fork_id": opts.ForkID})
}
}
}