mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-28 11:14:54 -04:00
chore: move code around
This commit is contained in:
parent
dedf0790ee
commit
1d526d365e
2 changed files with 28 additions and 14 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue