From 69f54dbda2bbd5590781ad1eba10cfca16a7e585 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 Mar 2024 16:03:19 +0800 Subject: [PATCH 1/3] Fix the duplicated repositories load on dashboard --- routers/web/repo/repo.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 7a626a70659b8..74c3a09dc4af2 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -622,7 +622,20 @@ func SearchRepo(ctx *context.Context) { } } - var err error + // To improve performance when only the count is requested + if ctx.FormBool("count_only") { + count, err := repo_model.CountRepository(ctx, opts) + if err != nil { + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: err.Error(), + }) + return + } + ctx.SetTotalCountHeader(count) + return + } + repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { ctx.JSON(http.StatusInternalServerError, api.SearchError{ @@ -634,11 +647,6 @@ func SearchRepo(ctx *context.Context) { ctx.SetTotalCountHeader(count) - // To improve performance when only the count is requested - if ctx.FormBool("count_only") { - return - } - latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos) if err != nil { log.Error("FindReposLastestCommitStatuses: %v", err) From 413b6c0ebbc5796657ccca2f254c95b335286ca7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 Mar 2024 16:42:20 +0800 Subject: [PATCH 2/3] Don't leak real error on search repositories --- routers/web/repo/repo.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 74c3a09dc4af2..0770423461a97 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -626,9 +626,14 @@ func SearchRepo(ctx *context.Context) { if ctx.FormBool("count_only") { count, err := repo_model.CountRepository(ctx, opts) if err != nil { + log.Error("CountRepository: %v", err) + errStr := "CountRepository internal error" + if ctx.Doer != nil && ctx.Doer.IsAdmin { + errStr += ": " + err.Error() + } ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: err.Error(), + Error: errStr, }) return } @@ -638,9 +643,14 @@ func SearchRepo(ctx *context.Context) { repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { + log.Error("SearchRepository: %v", err) + errStr := "SearchRepository internal error" + if ctx.Doer != nil && ctx.Doer.IsAdmin { + errStr += ": " + err.Error() + } ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: err.Error(), + Error: errStr, }) return } @@ -650,6 +660,14 @@ func SearchRepo(ctx *context.Context) { latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos) if err != nil { log.Error("FindReposLastestCommitStatuses: %v", err) + errStr := "FindReposLastestCommitStatuses internal error" + if ctx.Doer != nil && ctx.Doer.IsAdmin { + errStr += ": " + err.Error() + } + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: errStr, + }) return } From 06d5bac8dd395d14b23cb1bc73aed71e676138fb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 Mar 2024 17:25:23 +0800 Subject: [PATCH 3/3] Simple the return error --- routers/web/repo/repo.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 0770423461a97..bc21de3b83da7 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -622,18 +622,21 @@ func SearchRepo(ctx *context.Context) { } } + getErrorStr := func(ctx *context.Context, err error, fallbackStr string) string { + if ctx.Doer == nil || !ctx.Doer.IsAdmin { + return fallbackStr + } + return fmt.Sprintf("%s: %v", fallbackStr, err) + } + // To improve performance when only the count is requested if ctx.FormBool("count_only") { count, err := repo_model.CountRepository(ctx, opts) if err != nil { log.Error("CountRepository: %v", err) - errStr := "CountRepository internal error" - if ctx.Doer != nil && ctx.Doer.IsAdmin { - errStr += ": " + err.Error() - } ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: errStr, + Error: getErrorStr(ctx, err, "CountRepository internal error"), }) return } @@ -644,13 +647,9 @@ func SearchRepo(ctx *context.Context) { repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { log.Error("SearchRepository: %v", err) - errStr := "SearchRepository internal error" - if ctx.Doer != nil && ctx.Doer.IsAdmin { - errStr += ": " + err.Error() - } ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: errStr, + Error: getErrorStr(ctx, err, "SearchRepository internal error"), }) return } @@ -660,13 +659,9 @@ func SearchRepo(ctx *context.Context) { latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos) if err != nil { log.Error("FindReposLastestCommitStatuses: %v", err) - errStr := "FindReposLastestCommitStatuses internal error" - if ctx.Doer != nil && ctx.Doer.IsAdmin { - errStr += ": " + err.Error() - } ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: errStr, + Error: getErrorStr(ctx, err, "FindReposLastestCommitStatuses internal error"), }) return }