From 6fbfe441decc018a122b0b66185002db92b404a2 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 21 Feb 2024 12:42:12 +0100 Subject: [PATCH] [BUG] Load `AllUnitsEnabled` when necessary - In order to determine if the "Add more..." tab should be shown, the template has to know if the repository has all units enabled, this is done in the repository header which can be shown for quite a lot of pages (code, issues, projects, actions etc.) - This was previously set in the `RepoRefByType` function, which would be called by pages such as code, issues and releases, but it was not being called for all pages such as actions, packages and wiki. Which would in turn incorrectly show the "Add more..." button when it shouldn't. - Now call it from the template itself, so the value is 'always' loaded when necessary. --- models/repo/repo.go | 25 +++++++++++++++++++++++++ modules/context/repo.go | 26 -------------------------- templates/repo/header.tmpl | 2 +- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/models/repo/repo.go b/models/repo/repo.go index a7bc4b3c7..b24e5c1db 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -439,6 +439,31 @@ func (repo *Repository) GetUnit(ctx context.Context, tp unit.Type) (*RepoUnit, e return nil, ErrUnitTypeNotExist{tp} } +// AllUnitsEnabled returns true if all units are enabled for the repo. +func (repo *Repository) AllUnitsEnabled(ctx context.Context) bool { + hasAnyUnitEnabled := func(unitGroup []unit.Type) bool { + // Loop over the group of units + for _, unit := range unitGroup { + // If *any* of them is enabled, return true. + if repo.UnitEnabled(ctx, unit) { + return true + } + } + + // If none are enabled, return false. + return false + } + + for _, unitGroup := range unit.AllowedRepoUnitGroups { + // If any disabled unit is found, return false immediately. + if !hasAnyUnitEnabled(unitGroup) { + return false + } + } + + return true +} + // LoadOwner loads owner user func (repo *Repository) LoadOwner(ctx context.Context) (err error) { if repo.Owner != nil { diff --git a/modules/context/repo.go b/modules/context/repo.go index 8e8a42b69..9d63f9eec 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -82,31 +82,6 @@ func (r *Repository) CanCreateBranch() bool { return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanCreateBranch() } -// AllUnitsEnabled returns true if all units are enabled for the repo. -func (r *Repository) AllUnitsEnabled(ctx context.Context) bool { - hasAnyUnitEnabled := func(unitGroup []unit_model.Type) bool { - // Loop over the group of units - for _, unit := range unitGroup { - // If *any* of them is enabled, return true. - if r.Repository.UnitEnabled(ctx, unit) { - return true - } - } - - // If none are enabled, return false. - return false - } - - for _, unitGroup := range unit_model.AllowedRepoUnitGroups { - // If any disabled unit is found, return false immediately. - if !hasAnyUnitEnabled(unitGroup) { - return false - } - } - - return true -} - // RepoMustNotBeArchived checks if a repo is archived func RepoMustNotBeArchived() func(ctx *Context) { return func(ctx *Context) { @@ -1079,7 +1054,6 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() - ctx.Data["AllUnitsEnabled"] = ctx.Repo.AllUnitsEnabled(ctx) ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount() if err != nil { diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 086ffd85f..2a3167f98 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -219,7 +219,7 @@ {{end}} {{if .Permission.IsAdmin}} - {{if not .AllUnitsEnabled}} + {{if not (.Repository.AllUnitsEnabled ctx)}} {{svg "octicon-diff-added"}} {{ctx.Locale.Tr "repo.settings.units.add_more"}}