Add a direct link from repo header to unit settings

If a repository administrator is viewing a repository, and there are
units that can be enabled, display an "Add more..." link that leads to
the repository unit settings page.

The goal here is to allow instances to configure a small set of repo
units to be enabled by default, but also highlight for repo admins that
they can add more.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2024-01-23 10:57:49 +01:00
parent fa73375e13
commit e07b0e75ff
No known key found for this signature in database
4 changed files with 75 additions and 0 deletions

View file

@ -81,6 +81,31 @@ 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) {
@ -1053,6 +1078,7 @@ 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 {