From 1d526d365ed400bc55c5003edadf83208c60067b Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 25 May 2026 15:05:50 +0200 Subject: [PATCH] chore: move code around --- models/repo/repo.go | 34 ++++++++++++++++++++++++---------- services/convert/repository.go | 8 ++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/models/repo/repo.go b/models/repo/repo.go index fe2879950e..31a6b5d8fc 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -293,6 +293,28 @@ func (repo *Repository) AfterLoad() { repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects } +// LoadLanguage loads the primary language of the repository, if one exists. +// If one doesn't exists `nil` is still returned. +func (repo *Repository) LoadLanguage(ctx context.Context) error { + if repo.PrimaryLanguage != nil { + return nil + } + + var stat LanguageStat + has, err := db.GetEngine(ctx). + Where("`repo_id` = ? AND `is_primary` = ? AND `language` != ?", repo.ID, true, "other"). + Get(&stat) + if err != nil { + return fmt.Errorf("unable to find the primary languages: %w", err) + } + if has { + stat.LoadAttributes() + repo.PrimaryLanguage = &stat + } + + return nil +} + // LoadAttributes loads attributes of the repository. func (repo *Repository) LoadAttributes(ctx context.Context) error { // Load owner @@ -301,16 +323,8 @@ func (repo *Repository) LoadAttributes(ctx context.Context) error { } // Load the primary language. - var stat LanguageStat - has, err := db.GetEngine(ctx). - Where("`repo_id` = ? AND `is_primary` = ? AND `language` != ?", repo.ID, true, "other"). - Get(&stat) - if err != nil { - return fmt.Errorf("unable to find the primary languages: %w", repo.ID, err) - } - if has { - stat.LoadAttributes() - repo.PrimaryLanguage = &stat + if err := repo.LoadLanguage(ctx); err != nil { + return fmt.Errorf("load language: %w", err) } return nil diff --git a/services/convert/repository.go b/services/convert/repository.go index 9b8880ecb9..bf3e136e8f 100644 --- a/services/convert/repository.go +++ b/services/convert/repository.go @@ -24,10 +24,6 @@ func ToRepo(ctx stdCtx.Context, repo *repo_model.Repository, permissionInRepo ac } func innerToRepo(ctx stdCtx.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission, isParent bool) *api.Repository { - if err := repo.LoadAttributes(ctx); err != nil { - log.Error("Unable to load attributes for repo[id=%d]: %w", repo.ID, err) - } - var parent *api.Repository if permissionInRepo.Units == nil && permissionInRepo.UnitsMode == nil { @@ -177,6 +173,10 @@ func innerToRepo(ctx stdCtx.Context, repo *repo_model.Repository, permissionInRe } } + if err := repo.LoadLanguage(ctx); err != nil { + log.Warn("Unable to load language for repo[id=%d]: %w", repo.ID, err) + } + var language string if repo.PrimaryLanguage != nil { language = repo.PrimaryLanguage.Language