You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

unit.go 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // UnitType is Unit's Type
  11. type UnitType int
  12. // Enumerate all the unit types
  13. const (
  14. UnitTypeCode UnitType = iota + 1 // 1 code
  15. UnitTypeIssues // 2 issues
  16. UnitTypePullRequests // 3 PRs
  17. UnitTypeReleases // 4 Releases
  18. UnitTypeWiki // 5 Wiki
  19. UnitTypeExternalWiki // 6 ExternalWiki
  20. UnitTypeExternalTracker // 7 ExternalTracker
  21. )
  22. func (u UnitType) String() string {
  23. switch u {
  24. case UnitTypeCode:
  25. return "UnitTypeCode"
  26. case UnitTypeIssues:
  27. return "UnitTypeIssues"
  28. case UnitTypePullRequests:
  29. return "UnitTypePullRequests"
  30. case UnitTypeReleases:
  31. return "UnitTypeReleases"
  32. case UnitTypeWiki:
  33. return "UnitTypeWiki"
  34. case UnitTypeExternalWiki:
  35. return "UnitTypeExternalWiki"
  36. case UnitTypeExternalTracker:
  37. return "UnitTypeExternalTracker"
  38. }
  39. return fmt.Sprintf("Unknown UnitType %d", u)
  40. }
  41. // ColorFormat provides a ColorFormatted version of this UnitType
  42. func (u UnitType) ColorFormat(s fmt.State) {
  43. log.ColorFprintf(s, "%d:%s",
  44. log.NewColoredIDValue(u),
  45. u)
  46. }
  47. var (
  48. // allRepUnitTypes contains all the unit types
  49. allRepUnitTypes = []UnitType{
  50. UnitTypeCode,
  51. UnitTypeIssues,
  52. UnitTypePullRequests,
  53. UnitTypeReleases,
  54. UnitTypeWiki,
  55. UnitTypeExternalWiki,
  56. UnitTypeExternalTracker,
  57. }
  58. // defaultRepoUnits contains the default unit types
  59. defaultRepoUnits = []UnitType{
  60. UnitTypeCode,
  61. UnitTypeIssues,
  62. UnitTypePullRequests,
  63. UnitTypeReleases,
  64. UnitTypeWiki,
  65. }
  66. // MustRepoUnits contains the units could not be disabled currently
  67. MustRepoUnits = []UnitType{
  68. UnitTypeCode,
  69. UnitTypeReleases,
  70. }
  71. )
  72. // Unit is a section of one repository
  73. type Unit struct {
  74. Type UnitType
  75. NameKey string
  76. URI string
  77. DescKey string
  78. Idx int
  79. }
  80. // CanDisable returns if this unit could be disabled.
  81. func (u *Unit) CanDisable() bool {
  82. return true
  83. }
  84. // IsLessThan compares order of two units
  85. func (u Unit) IsLessThan(unit Unit) bool {
  86. if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
  87. return false
  88. }
  89. return u.Idx < unit.Idx
  90. }
  91. // Enumerate all the units
  92. var (
  93. UnitCode = Unit{
  94. UnitTypeCode,
  95. "repo.code",
  96. "/",
  97. "repo.code.desc",
  98. 0,
  99. }
  100. UnitIssues = Unit{
  101. UnitTypeIssues,
  102. "repo.issues",
  103. "/issues",
  104. "repo.issues.desc",
  105. 1,
  106. }
  107. UnitExternalTracker = Unit{
  108. UnitTypeExternalTracker,
  109. "repo.ext_issues",
  110. "/issues",
  111. "repo.ext_issues.desc",
  112. 1,
  113. }
  114. UnitPullRequests = Unit{
  115. UnitTypePullRequests,
  116. "repo.pulls",
  117. "/pulls",
  118. "repo.pulls.desc",
  119. 2,
  120. }
  121. UnitReleases = Unit{
  122. UnitTypeReleases,
  123. "repo.releases",
  124. "/releases",
  125. "repo.releases.desc",
  126. 3,
  127. }
  128. UnitWiki = Unit{
  129. UnitTypeWiki,
  130. "repo.wiki",
  131. "/wiki",
  132. "repo.wiki.desc",
  133. 4,
  134. }
  135. UnitExternalWiki = Unit{
  136. UnitTypeExternalWiki,
  137. "repo.ext_wiki",
  138. "/wiki",
  139. "repo.ext_wiki.desc",
  140. 4,
  141. }
  142. // Units contains all the units
  143. Units = map[UnitType]Unit{
  144. UnitTypeCode: UnitCode,
  145. UnitTypeIssues: UnitIssues,
  146. UnitTypeExternalTracker: UnitExternalTracker,
  147. UnitTypePullRequests: UnitPullRequests,
  148. UnitTypeReleases: UnitReleases,
  149. UnitTypeWiki: UnitWiki,
  150. UnitTypeExternalWiki: UnitExternalWiki,
  151. }
  152. )
  153. // FindUnitTypes give the unit key name and return unit
  154. func FindUnitTypes(nameKeys ...string) (res []UnitType) {
  155. for _, key := range nameKeys {
  156. for t, u := range Units {
  157. if strings.EqualFold(key, u.NameKey) {
  158. res = append(res, t)
  159. break
  160. }
  161. }
  162. }
  163. return
  164. }