Skip to content

Commit 05eaf1c

Browse files
author
Earl Warren
committed
Merge pull request 'Repository settings refactor' (go-gitea#2221) from algernon/forgejo:repo-units/ui-refactor into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2221 Reviewed-by: oliverpool <[email protected]>
2 parents 6c38956 + 44424bf commit 05eaf1c

File tree

15 files changed

+791
-510
lines changed

15 files changed

+791
-510
lines changed

models/unit/unit.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ var (
108108

109109
// DisabledRepoUnits contains the units that have been globally disabled
110110
DisabledRepoUnits = []Type{}
111+
112+
// AllowedRepoUnitGroups contains the units that have been globally enabled,
113+
// with mutually exclusive units grouped together.
114+
AllowedRepoUnitGroups = [][]Type{}
111115
)
112116

113117
// Get valid set of default repository units from settings
@@ -162,6 +166,45 @@ func LoadUnitConfig() error {
162166
if len(DefaultForkRepoUnits) == 0 {
163167
return errors.New("no default fork repository units found")
164168
}
169+
170+
// Collect the allowed repo unit groups. Mutually exclusive units are
171+
// grouped together.
172+
AllowedRepoUnitGroups = [][]Type{}
173+
for _, unit := range []Type{
174+
TypeCode,
175+
TypePullRequests,
176+
TypeProjects,
177+
TypePackages,
178+
TypeActions,
179+
} {
180+
// If unit is globally disabled, ignore it.
181+
if unit.UnitGlobalDisabled() {
182+
continue
183+
}
184+
185+
// If it is allowed, add it to the group list.
186+
AllowedRepoUnitGroups = append(AllowedRepoUnitGroups, []Type{unit})
187+
}
188+
189+
addMutuallyExclusiveGroup := func(unit1, unit2 Type) {
190+
var list []Type
191+
192+
if !unit1.UnitGlobalDisabled() {
193+
list = append(list, unit1)
194+
}
195+
196+
if !unit2.UnitGlobalDisabled() {
197+
list = append(list, unit2)
198+
}
199+
200+
if len(list) > 0 {
201+
AllowedRepoUnitGroups = append(AllowedRepoUnitGroups, list)
202+
}
203+
}
204+
205+
addMutuallyExclusiveGroup(TypeIssues, TypeExternalTracker)
206+
addMutuallyExclusiveGroup(TypeWiki, TypeExternalWiki)
207+
165208
return nil
166209
}
167210

modules/context/repo.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ func (r *Repository) CanCreateBranch() bool {
8181
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanCreateBranch()
8282
}
8383

84+
// AllUnitsEnabled returns true if all units are enabled for the repo.
85+
func (r *Repository) AllUnitsEnabled(ctx context.Context) bool {
86+
hasAnyUnitEnabled := func(unitGroup []unit_model.Type) bool {
87+
// Loop over the group of units
88+
for _, unit := range unitGroup {
89+
// If *any* of them is enabled, return true.
90+
if r.Repository.UnitEnabled(ctx, unit) {
91+
return true
92+
}
93+
}
94+
95+
// If none are enabled, return false.
96+
return false
97+
}
98+
99+
for _, unitGroup := range unit_model.AllowedRepoUnitGroups {
100+
// If any disabled unit is found, return false immediately.
101+
if !hasAnyUnitEnabled(unitGroup) {
102+
return false
103+
}
104+
}
105+
106+
return true
107+
}
108+
84109
// RepoMustNotBeArchived checks if a repo is archived
85110
func RepoMustNotBeArchived() func(ctx *Context) {
86111
return func(ctx *Context) {
@@ -1053,6 +1078,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
10531078
ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag
10541079
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
10551080
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
1081+
ctx.Data["AllUnitsEnabled"] = ctx.Repo.AllUnitsEnabled(ctx)
10561082

10571083
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
10581084
if err != nil {

options/locale/locale_en-US.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,10 @@ settings.mirror_settings.push_mirror.remote_url = Git Remote Repository URL
20662066
settings.mirror_settings.push_mirror.add = Add Push Mirror
20672067
settings.mirror_settings.push_mirror.edit_sync_time = Edit mirror sync interval
20682068

2069+
settings.units.units = Repository Units
2070+
settings.units.overview = Overview
2071+
settings.units.add_more = Add more...
2072+
20692073
settings.sync_mirror = Synchronize Now
20702074
settings.pull_mirror_sync_in_progress = Pulling changes from the remote %s at the moment.
20712075
settings.push_mirror_sync_in_progress = Pushing changes to the remote %s at the moment.

0 commit comments

Comments
 (0)