aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-06-18 08:51:13 +0800
committerGitHub <noreply@github.com>2024-06-18 00:51:13 +0000
commit37a4b233a0a4ca516b90e0c8e15d8dafb8d13358 (patch)
tree906fc88c06e46d164157707886833332aebcf5c8 /models
parentd32648b204395fe3590ca2de5f38f0f97da510aa (diff)
downloadgitea-37a4b233a0a4ca516b90e0c8e15d8dafb8d13358.tar.gz
gitea-37a4b233a0a4ca516b90e0c8e15d8dafb8d13358.zip
Refactor repo unit "disabled" check (#31389)
1. There are already global "unit consts", no need to use context data, which is fragile 2. Remove the "String()" method from "unit", it would only cause rendering problems in templates --------- Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'models')
-rw-r--r--models/repo/repo.go2
-rw-r--r--models/repo/repo_unit.go2
-rw-r--r--models/unit/unit.go37
3 files changed, 10 insertions, 31 deletions
diff --git a/models/repo/repo.go b/models/repo/repo.go
index f02c55fc89..189c4aba6c 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -362,7 +362,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
if log.IsTrace() {
unitTypeStrings := make([]string, len(repo.Units))
for i, unit := range repo.Units {
- unitTypeStrings[i] = unit.Type.String()
+ unitTypeStrings[i] = unit.Type.LogString()
}
log.Trace("repo.Units, ID=%d, Types: [%s]", repo.ID, strings.Join(unitTypeStrings, ", "))
}
diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go
index fd5baa9488..cb52c2c9e2 100644
--- a/models/repo/repo_unit.go
+++ b/models/repo/repo_unit.go
@@ -33,7 +33,7 @@ func IsErrUnitTypeNotExist(err error) bool {
}
func (err ErrUnitTypeNotExist) Error() string {
- return fmt.Sprintf("Unit type does not exist: %s", err.UT.String())
+ return fmt.Sprintf("Unit type does not exist: %s", err.UT.LogString())
}
func (err ErrUnitTypeNotExist) Unwrap() error {
diff --git a/models/unit/unit.go b/models/unit/unit.go
index 8eedcbd347..3b62e5f982 100644
--- a/models/unit/unit.go
+++ b/models/unit/unit.go
@@ -33,39 +33,18 @@ const (
TypeActions // 10 Actions
)
-// Value returns integer value for unit type
+// Value returns integer value for unit type (used by template)
func (u Type) Value() int {
return int(u)
}
-func (u Type) String() string {
- switch u {
- case TypeCode:
- return "TypeCode"
- case TypeIssues:
- return "TypeIssues"
- case TypePullRequests:
- return "TypePullRequests"
- case TypeReleases:
- return "TypeReleases"
- case TypeWiki:
- return "TypeWiki"
- case TypeExternalWiki:
- return "TypeExternalWiki"
- case TypeExternalTracker:
- return "TypeExternalTracker"
- case TypeProjects:
- return "TypeProjects"
- case TypePackages:
- return "TypePackages"
- case TypeActions:
- return "TypeActions"
- }
- return fmt.Sprintf("Unknown Type %d", u)
-}
-
func (u Type) LogString() string {
- return fmt.Sprintf("<UnitType:%d:%s>", u, u.String())
+ unit, ok := Units[u]
+ unitName := "unknown"
+ if ok {
+ unitName = unit.NameKey
+ }
+ return fmt.Sprintf("<UnitType:%d:%s>", u, unitName)
}
var (
@@ -133,7 +112,7 @@ func validateDefaultRepoUnits(defaultUnits, settingDefaultUnits []Type) []Type {
units = make([]Type, 0, len(settingDefaultUnits))
for _, settingUnit := range settingDefaultUnits {
if !settingUnit.CanBeDefault() {
- log.Warn("Not allowed as default unit: %s", settingUnit.String())
+ log.Warn("Not allowed as default unit: %s", settingUnit.LogString())
continue
}
units = append(units, settingUnit)