Skip to content

Commit

Permalink
Add a direct link from repo header to unit settings
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
algernon committed Feb 13, 2024
1 parent fa73375 commit e07b0e7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
43 changes: 43 additions & 0 deletions models/unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ var (

// DisabledRepoUnits contains the units that have been globally disabled
DisabledRepoUnits = []Type{}

// AllowedRepoUnitGroups contains the units that have been globally enabled,
// with mutually exclusive units grouped together.
AllowedRepoUnitGroups = [][]Type{}
)

// Get valid set of default repository units from settings
Expand Down Expand Up @@ -162,6 +166,45 @@ func LoadUnitConfig() error {
if len(DefaultForkRepoUnits) == 0 {
return errors.New("no default fork repository units found")
}

// Collect the allowed repo unit groups. Mutually exclusive units are
// grouped together.
AllowedRepoUnitGroups = [][]Type{}
for _, unit := range []Type{
TypeCode,
TypePullRequests,
TypeProjects,
TypePackages,
TypeActions,
} {
// If unit is globally disabled, ignore it.
if unit.UnitGlobalDisabled() {
continue
}

// If it is allowed, add it to the group list.
AllowedRepoUnitGroups = append(AllowedRepoUnitGroups, []Type{unit})
}

addMutuallyExclusiveGroup := func(unit1, unit2 Type) {
var list []Type

if !unit1.UnitGlobalDisabled() {
list = append(list, unit1)
}

if !unit2.UnitGlobalDisabled() {
list = append(list, unit2)
}

if len(list) > 0 {
AllowedRepoUnitGroups = append(AllowedRepoUnitGroups, list)
}
}

addMutuallyExclusiveGroup(TypeIssues, TypeExternalTracker)
addMutuallyExclusiveGroup(TypeWiki, TypeExternalWiki)

return nil
}

Expand Down
26 changes: 26 additions & 0 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,7 @@ settings.mirror_settings.push_mirror.edit_sync_time = Edit mirror sync interval

settings.units.units = Repository Units
settings.units.overview = Overview
settings.units.add_more = Add more...

settings.sync_mirror = Synchronize Now
settings.pull_mirror_sync_in_progress = Pulling changes from the remote %s at the moment.
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@
{{end}}

{{if .Permission.IsAdmin}}
{{if not .AllUnitsEnabled}}
<a class="{{if .PageIsRepoSettingsUnits}}active {{end}}item" href="{{.RepoLink}}/settings/units">
{{svg "octicon-diff-added"}} {{ctx.Locale.Tr "repo.settings.units.add_more"}}
</a>
{{end}}
<a class="{{if .PageIsRepoSettings}}active {{end}} item" href="{{.RepoLink}}/settings">
{{svg "octicon-tools"}} {{ctx.Locale.Tr "repo.settings"}}
</a>
Expand Down

0 comments on commit e07b0e7

Please sign in to comment.