Skip to content

Commit a2e8a28

Browse files
authored
Improve pull request list api (#34052)
The pull request list API is slow, for every pull request, it needs to open a git repository. Assume it has 30 records, there will be 30 sub processes back because every repository will open a git cat-file --batch sub process. This PR use base git repository to get the head commit id rather than read it from head repository to avoid open any head git repository.
1 parent 342432e commit a2e8a28

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

models/git/branch.go

+12
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
173173
return &branch, nil
174174
}
175175

176+
// IsBranchExist returns true if the branch exists in the repository.
177+
func IsBranchExist(ctx context.Context, repoID int64, branchName string) (bool, error) {
178+
var branch Branch
179+
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)
180+
if err != nil {
181+
return false, err
182+
} else if !has {
183+
return false, nil
184+
}
185+
return !branch.IsDeleted, nil
186+
}
187+
176188
func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) {
177189
branches := make([]*Branch, 0, len(branchNames))
178190

services/convert/pull.go

+27-56
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
389389
},
390390
Head: &api.PRBranchInfo{
391391
Name: pr.HeadBranch,
392-
Ref: fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index),
392+
Ref: pr.GetGitRefName(),
393393
RepoID: -1,
394394
},
395395
}
@@ -422,72 +422,43 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
422422
return nil, err
423423
}
424424
}
425-
426425
if baseBranch != nil {
427426
apiPullRequest.Base.Sha = baseBranch.CommitID
428427
}
429428

430-
if pr.Flow == issues_model.PullRequestFlowAGit {
431-
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
429+
// pull request head branch, both repository and branch could not exist
430+
if pr.HeadRepo != nil {
431+
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
432+
exist, err := git_model.IsBranchExist(ctx, pr.HeadRepo.ID, pr.HeadBranch)
432433
if err != nil {
433-
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
434+
log.Error("IsBranchExist[%d]: %v", pr.HeadRepo.ID, err)
434435
return nil, err
435436
}
436-
apiPullRequest.Head.RepoID = pr.BaseRepoID
437-
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
438-
apiPullRequest.Head.Name = ""
439-
}
440-
441-
var headGitRepo *git.Repository
442-
if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub {
443-
if pr.HeadRepoID == pr.BaseRepoID {
444-
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
445-
apiPullRequest.Head.Repository = apiRepo
446-
headGitRepo = gitRepo
447-
} else {
448-
p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
449-
if err != nil {
450-
log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err)
451-
p.AccessMode = perm.AccessModeNone
452-
}
453-
454-
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
455-
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
456-
457-
headGitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
458-
if err != nil {
459-
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
460-
return nil, err
461-
}
462-
defer headGitRepo.Close()
437+
if exist {
438+
apiPullRequest.Head.Ref = pr.HeadBranch
463439
}
440+
}
441+
if apiPullRequest.Head.Ref == "" {
442+
apiPullRequest.Head.Ref = pr.GetGitRefName()
443+
}
464444

465-
headBranch, err := headGitRepo.GetBranch(pr.HeadBranch)
466-
if err != nil && !git.IsErrBranchNotExist(err) {
467-
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
468-
return nil, err
445+
if pr.HeadRepoID == pr.BaseRepoID {
446+
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
447+
} else {
448+
p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
449+
if err != nil {
450+
log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err)
451+
p.AccessMode = perm.AccessModeNone
469452
}
453+
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
454+
}
470455

471-
if git.IsErrBranchNotExist(err) {
472-
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
473-
if err != nil && !git.IsErrNotExist(err) {
474-
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
475-
return nil, err
476-
}
477-
if err == nil {
478-
apiPullRequest.Head.Sha = headCommitID
479-
}
480-
} else {
481-
commit, err := headBranch.GetCommit()
482-
if err != nil && !git.IsErrNotExist(err) {
483-
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
484-
return nil, err
485-
}
486-
if err == nil {
487-
apiPullRequest.Head.Ref = pr.HeadBranch
488-
apiPullRequest.Head.Sha = commit.ID.String()
489-
}
490-
}
456+
if pr.Flow == issues_model.PullRequestFlowAGit {
457+
apiPullRequest.Head.Name = ""
458+
}
459+
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
460+
if err != nil {
461+
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
491462
}
492463

493464
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {

0 commit comments

Comments
 (0)