Skip to content

Commit 807c971

Browse files
authored
Get latest commit statuses from database instead of git data on dashboard for repositories (#25605)
related #24638
1 parent 640a88f commit 807c971

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

models/git/branch_list.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,20 @@ func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, err
130130
}
131131
return branches, nil
132132
}
133+
134+
func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64]string) (map[int64]string, error) {
135+
cond := builder.NewCond()
136+
for repoID, branchName := range repoBranches {
137+
cond = cond.Or(builder.And(builder.Eq{"repo_id": repoID}, builder.Eq{"name": branchName}))
138+
}
139+
var branches []*Branch
140+
if err := db.GetEngine(ctx).
141+
Where(cond).Find(&branches); err != nil {
142+
return nil, err
143+
}
144+
branchMap := make(map[int64]string, len(branches))
145+
for _, branch := range branches {
146+
branchMap[branch.RepoID] = branch.CommitID
147+
}
148+
return branchMap, nil
149+
}

routers/web/repo/repo.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,15 @@ func SearchRepo(ctx *context.Context) {
579579

580580
// collect the latest commit of each repo
581581
// at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
582-
repoIDsToLatestCommitSHAs := make(map[int64]string, len(repos))
582+
repoBranchNames := make(map[int64]string, len(repos))
583583
for _, repo := range repos {
584-
commitID, err := repo_service.GetBranchCommitID(ctx, repo, repo.DefaultBranch)
585-
if err != nil {
586-
continue
587-
}
588-
repoIDsToLatestCommitSHAs[repo.ID] = commitID
584+
repoBranchNames[repo.ID] = repo.DefaultBranch
585+
}
586+
587+
repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNames)
588+
if err != nil {
589+
log.Error("FindBranchesByRepoAndBranchName: %v", err)
590+
return
589591
}
590592

591593
// call the database O(1) times to get the commit statuses for all repos

0 commit comments

Comments
 (0)