use branch.id instead of branch.name

This commit is contained in:
yp05327 2023-07-11 04:18:36 +00:00
parent 71dd740416
commit 2b0abf9331
2 changed files with 13 additions and 21 deletions

View File

@ -393,26 +393,31 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
}
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, latestCommitID string) (BranchList, error) {
func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, userID int64) (BranchList, error) {
branches := make(BranchList, 0, 2)
baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch)
if err != nil {
return nil, err
}
// search all related repos
repoCond := builder.Select("id").From("repository").
Where(builder.Or(
builder.Eq{"id": repoID, "is_fork": false},
builder.Eq{"is_fork": true, "fork_id": repoID},
builder.Eq{"id": baseRepo.ID, "is_fork": false},
builder.Eq{"is_fork": true, "fork_id": baseRepo.ID},
))
// avoid check branches which have already created PRs
usedBranchIDs := builder.Select("branch.id").From("branch").
// TODO add head_branch_id in pull_request table
invalidBranchCond := 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.And(
builder.Eq{"issue.is_closed": false},
builder.In("pull_request.head_repo_id", repoCond),
))
err := db.GetEngine(ctx).
Where("commit_id != ? AND pusher_id = ? AND is_deleted = ?", latestCommitID, userID, false).
err = db.GetEngine(ctx).
Where("id != ? AND commit_id != ? AND pusher_id = ? AND is_deleted = ?", baseBranch.ID, baseBranch.CommitID, userID, false).
And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()).
NotIn("id", usedBranchIDs).
NotIn("id", invalidBranchCond).
In("repo_id", repoCond).
OrderBy("branch.updated_unix DESC").
Limit(2).

View File

@ -983,24 +983,11 @@ func renderCode(ctx *context.Context) {
return
}
var latestCommit *git.Commit
baseRepo := ctx.Repo.Repository
gitRepo := ctx.Repo.GitRepo
if ctx.Repo.Repository.IsFork {
baseRepo = ctx.Repo.Repository.BaseRepo
repoPath := repo_model.RepoPath(baseRepo.OwnerName, baseRepo.Name)
gitRepo, err = git.OpenRepository(ctx, repoPath)
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
}
latestCommit, err = gitRepo.GetBranchCommit(baseRepo.DefaultBranch)
if err != nil {
ctx.ServerError("GetBranchCommit", err)
return
}
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo.ID, ctx.Doer.ID, latestCommit.ID.String())
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo, ctx.Doer.ID)
if err != nil {
ctx.ServerError("FindRecentlyPushedNewBranches", err)
return