From 3a900917a4904a16350cafa8ebfed4283323c8ac Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 30 Jun 2023 16:27:07 +0800 Subject: [PATCH 1/3] Get latest commit statuses from database instead of git data on dashboard for repositories --- models/git/branch_list.go | 17 +++++++++++++++++ routers/web/repo/repo.go | 13 +++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/models/git/branch_list.go b/models/git/branch_list.go index da78248c0bc01..836d4ffc41777 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -130,3 +130,20 @@ func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, err } return branches, nil } + +func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64]string) (map[int64]string, error) { + cond := builder.NewCond() + for repoID, branchName := range repoBranches { + cond = cond.Or(builder.And(builder.Eq{"repo_id": repoID}, builder.Eq{"name": branchName})) + } + var branches []*Branch + if err := db.GetEngine(ctx). + Where(cond).Find(&branches); err != nil { + return nil, err + } + branchMap := make(map[int64]string, len(branches)) + for _, branch := range branches { + branchMap[branch.RepoID] = branch.CommitID + } + return branchMap, nil +} diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index a1e1346b38c4b..41e6b8c153dce 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -579,13 +579,14 @@ func SearchRepo(ctx *context.Context) { // collect the latest commit of each repo // at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment - repoIDsToLatestCommitSHAs := make(map[int64]string, len(repos)) + repoBranchNamees := make(map[int64]string, len(repos)) for _, repo := range repos { - commitID, err := repo_service.GetBranchCommitID(ctx, repo, repo.DefaultBranch) - if err != nil { - continue - } - repoIDsToLatestCommitSHAs[repo.ID] = commitID + repoBranchNamees[repo.ID] = repo.DefaultBranch + } + + repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNamees) + if err != nil { + return } // call the database O(1) times to get the commit statuses for all repos From ea86ee0c1fe0fe83d8d39cf36271be4be80cc9bb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 30 Jun 2023 17:15:38 +0800 Subject: [PATCH 2/3] Fix lint --- routers/web/repo/repo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 41e6b8c153dce..8c40b94658689 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -586,7 +586,8 @@ func SearchRepo(ctx *context.Context) { repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNamees) if err != nil { - return + log.Error("FindBranchesByRepoAndBranchName: %v", err) + return } // call the database O(1) times to get the commit statuses for all repos From 5f1376363724638055be23f8347e43f8433a3ca6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 2 Jul 2023 10:59:08 +0800 Subject: [PATCH 3/3] Fix typo --- routers/web/repo/repo.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 8c40b94658689..781a12b2d9270 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -579,12 +579,12 @@ func SearchRepo(ctx *context.Context) { // collect the latest commit of each repo // at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment - repoBranchNamees := make(map[int64]string, len(repos)) + repoBranchNames := make(map[int64]string, len(repos)) for _, repo := range repos { - repoBranchNamees[repo.ID] = repo.DefaultBranch + repoBranchNames[repo.ID] = repo.DefaultBranch } - repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNamees) + repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNames) if err != nil { log.Error("FindBranchesByRepoAndBranchName: %v", err) return