diff options
author | David Svantesson <davidsvantesson@gmail.com> | 2020-01-17 08:34:37 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2020-01-17 09:34:37 +0200 |
commit | 3c07d03c0388d3b86138572401281b51f2db9282 (patch) | |
tree | 06eecf8b818ee8721a5dbfdd688eb5f45e5bef51 /models/unit.go | |
parent | 36943e56d66a2d711a6b0c27219ce91a3ddc020a (diff) | |
download | gitea-3c07d03c0388d3b86138572401281b51f2db9282.tar.gz gitea-3c07d03c0388d3b86138572401281b51f2db9282.zip |
Add setting to set default and global disabled repository units. (#8788)
* Add possibility to global disable repo units.
* Add Default Repo Unit app.ini setting.
* Hide units
* Hide disabled repo units
* Minor fixes
* Indicate disabled units in team settings.
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/unit.go')
-rw-r--r-- | models/unit.go | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/models/unit.go b/models/unit.go index 9f5c8d3cbb..bd2e6b13a6 100644 --- a/models/unit.go +++ b/models/unit.go @@ -9,6 +9,7 @@ import ( "strings" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" ) // UnitType is Unit's Type @@ -78,13 +79,89 @@ var ( UnitTypeWiki, } + // NotAllowedDefaultRepoUnits contains units that can't be default + NotAllowedDefaultRepoUnits = []UnitType{ + UnitTypeExternalWiki, + UnitTypeExternalTracker, + } + // MustRepoUnits contains the units could not be disabled currently MustRepoUnits = []UnitType{ UnitTypeCode, UnitTypeReleases, } + + // DisabledRepoUnits contains the units that have been globally disabled + DisabledRepoUnits = []UnitType{} ) +func loadUnitConfig() { + setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...) + // Default repo units set if setting is not empty + if len(setDefaultRepoUnits) > 0 { + // MustRepoUnits required as default + DefaultRepoUnits = make([]UnitType, len(MustRepoUnits)) + copy(DefaultRepoUnits, MustRepoUnits) + for _, defaultU := range setDefaultRepoUnits { + if !defaultU.CanBeDefault() { + log.Warn("Not allowed as default unit: %s", defaultU.String()) + continue + } + // MustRepoUnits already added + if defaultU.CanDisable() { + DefaultRepoUnits = append(DefaultRepoUnits, defaultU) + } + } + } + + DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...) + // Check that must units are not disabled + for i, disabledU := range DisabledRepoUnits { + if !disabledU.CanDisable() { + log.Warn("Not allowed to global disable unit %s", disabledU.String()) + DisabledRepoUnits = append(DisabledRepoUnits[:i], DisabledRepoUnits[i+1:]...) + } + } + // Remove disabled units from default units + for _, disabledU := range DisabledRepoUnits { + for i, defaultU := range DefaultRepoUnits { + if defaultU == disabledU { + DefaultRepoUnits = append(DefaultRepoUnits[:i], DefaultRepoUnits[i+1:]...) + } + } + } +} + +// UnitGlobalDisabled checks if unit type is global disabled +func (u UnitType) UnitGlobalDisabled() bool { + for _, ud := range DisabledRepoUnits { + if u == ud { + return true + } + } + return false +} + +// CanDisable checks if this unit type can be disabled. +func (u *UnitType) CanDisable() bool { + for _, mu := range MustRepoUnits { + if *u == mu { + return false + } + } + return true +} + +// CanBeDefault checks if the unit type can be a default repo unit +func (u *UnitType) CanBeDefault() bool { + for _, nadU := range NotAllowedDefaultRepoUnits { + if *u == nadU { + return false + } + } + return true +} + // Unit is a section of one repository type Unit struct { Type UnitType @@ -96,7 +173,7 @@ type Unit struct { // CanDisable returns if this unit could be disabled. func (u *Unit) CanDisable() bool { - return true + return u.Type.CanDisable() } // IsLessThan compares order of two units |