diff options
author | Jason Song <i@wolfogre.com> | 2023-04-03 16:42:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-03 16:42:38 +0800 |
commit | d67e40684f43b0eb744cad26e0265002f033dbc3 (patch) | |
tree | 5a930a4691124ae0c4114e8895966ac380d4f3a0 /models/unit/unit.go | |
parent | ca905b82df7f1d2a823d8df4448d485e5902876d (diff) | |
download | gitea-d67e40684f43b0eb744cad26e0265002f033dbc3.tar.gz gitea-d67e40684f43b0eb744cad26e0265002f033dbc3.zip |
Improve LoadUnitConfig to handle invalid or duplicate units (#23736)
The old code just parses an invalid key to `TypeInvalid` and uses it as
normal, and duplicate keys will be kept.
So this PR will ignore invalid key and log warning and also deduplicate
valid units.
Diffstat (limited to 'models/unit/unit.go')
-rw-r--r-- | models/unit/unit.go | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/models/unit/unit.go b/models/unit/unit.go index 883f443cbe..3d5a8842cd 100644 --- a/models/unit/unit.go +++ b/models/unit/unit.go @@ -151,7 +151,11 @@ func validateDefaultRepoUnits(defaultUnits, settingDefaultUnits []Type) []Type { // LoadUnitConfig load units from settings func LoadUnitConfig() { - DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...) + var invalidKeys []string + DisabledRepoUnits, invalidKeys = FindUnitTypes(setting.Repository.DisabledRepoUnits...) + if len(invalidKeys) > 0 { + log.Warn("Invalid keys in disabled repo units: %s", strings.Join(invalidKeys, ", ")) + } // Check that must units are not disabled for i, disabledU := range DisabledRepoUnits { if !disabledU.CanDisable() { @@ -160,9 +164,15 @@ func LoadUnitConfig() { } } - setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...) + setDefaultRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultRepoUnits...) + if len(invalidKeys) > 0 { + log.Warn("Invalid keys in default repo units: %s", strings.Join(invalidKeys, ", ")) + } DefaultRepoUnits = validateDefaultRepoUnits(DefaultRepoUnits, setDefaultRepoUnits) - setDefaultForkRepoUnits := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...) + setDefaultForkRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...) + if len(invalidKeys) > 0 { + log.Warn("Invalid keys in default fork repo units: %s", strings.Join(invalidKeys, ", ")) + } DefaultForkRepoUnits = validateDefaultRepoUnits(DefaultForkRepoUnits, setDefaultForkRepoUnits) } @@ -334,22 +344,19 @@ var ( } ) -// FindUnitTypes give the unit key names and return unit -func FindUnitTypes(nameKeys ...string) (res []Type) { +// FindUnitTypes give the unit key names and return valid unique units and invalid keys +func FindUnitTypes(nameKeys ...string) (res []Type, invalidKeys []string) { + m := map[Type]struct{}{} for _, key := range nameKeys { - var found bool - for t, u := range Units { - if strings.EqualFold(key, u.NameKey) { - res = append(res, t) - found = true - break - } - } - if !found { - res = append(res, TypeInvalid) + t := TypeFromKey(key) + if t == TypeInvalid { + invalidKeys = append(invalidKeys, key) + } else if _, ok := m[t]; !ok { + res = append(res, t) + m[t] = struct{}{} } } - return res + return res, invalidKeys } // TypeFromKey give the unit key name and return unit |