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

268 lines
6.0 KiB

  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. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // UnitType is Unit's Type
  12. type UnitType int
  13. // Enumerate all the unit types
  14. const (
  15. UnitTypeCode UnitType = iota + 1 // 1 code
  16. UnitTypeIssues // 2 issues
  17. UnitTypePullRequests // 3 PRs
  18. UnitTypeReleases // 4 Releases
  19. UnitTypeWiki // 5 Wiki
  20. UnitTypeExternalWiki // 6 ExternalWiki
  21. UnitTypeExternalTracker // 7 ExternalTracker
  22. )
  23. // Value returns integer value for unit type
  24. func (u UnitType) Value() int {
  25. return int(u)
  26. }
  27. func (u UnitType) String() string {
  28. switch u {
  29. case UnitTypeCode:
  30. return "UnitTypeCode"
  31. case UnitTypeIssues:
  32. return "UnitTypeIssues"
  33. case UnitTypePullRequests:
  34. return "UnitTypePullRequests"
  35. case UnitTypeReleases:
  36. return "UnitTypeReleases"
  37. case UnitTypeWiki:
  38. return "UnitTypeWiki"
  39. case UnitTypeExternalWiki:
  40. return "UnitTypeExternalWiki"
  41. case UnitTypeExternalTracker:
  42. return "UnitTypeExternalTracker"
  43. }
  44. return fmt.Sprintf("Unknown UnitType %d", u)
  45. }
  46. // ColorFormat provides a ColorFormatted version of this UnitType
  47. func (u UnitType) ColorFormat(s fmt.State) {
  48. log.ColorFprintf(s, "%d:%s",
  49. log.NewColoredIDValue(u),
  50. u)
  51. }
  52. var (
  53. // AllRepoUnitTypes contains all the unit types
  54. AllRepoUnitTypes = []UnitType{
  55. UnitTypeCode,
  56. UnitTypeIssues,
  57. UnitTypePullRequests,
  58. UnitTypeReleases,
  59. UnitTypeWiki,
  60. UnitTypeExternalWiki,
  61. UnitTypeExternalTracker,
  62. }
  63. // DefaultRepoUnits contains the default unit types
  64. DefaultRepoUnits = []UnitType{
  65. UnitTypeCode,
  66. UnitTypeIssues,
  67. UnitTypePullRequests,
  68. UnitTypeReleases,
  69. UnitTypeWiki,
  70. }
  71. // NotAllowedDefaultRepoUnits contains units that can't be default
  72. NotAllowedDefaultRepoUnits = []UnitType{
  73. UnitTypeExternalWiki,
  74. UnitTypeExternalTracker,
  75. }
  76. // MustRepoUnits contains the units could not be disabled currently
  77. MustRepoUnits = []UnitType{
  78. UnitTypeCode,
  79. UnitTypeReleases,
  80. }
  81. // DisabledRepoUnits contains the units that have been globally disabled
  82. DisabledRepoUnits = []UnitType{}
  83. )
  84. func loadUnitConfig() {
  85. setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
  86. // Default repo units set if setting is not empty
  87. if len(setDefaultRepoUnits) > 0 {
  88. // MustRepoUnits required as default
  89. DefaultRepoUnits = make([]UnitType, len(MustRepoUnits))
  90. copy(DefaultRepoUnits, MustRepoUnits)
  91. for _, defaultU := range setDefaultRepoUnits {
  92. if !defaultU.CanBeDefault() {
  93. log.Warn("Not allowed as default unit: %s", defaultU.String())
  94. continue
  95. }
  96. // MustRepoUnits already added
  97. if defaultU.CanDisable() {
  98. DefaultRepoUnits = append(DefaultRepoUnits, defaultU)
  99. }
  100. }
  101. }
  102. DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...)
  103. // Check that must units are not disabled
  104. for i, disabledU := range DisabledRepoUnits {
  105. if !disabledU.CanDisable() {
  106. log.Warn("Not allowed to global disable unit %s", disabledU.String())
  107. DisabledRepoUnits = append(DisabledRepoUnits[:i], DisabledRepoUnits[i+1:]...)
  108. }
  109. }
  110. // Remove disabled units from default units
  111. for _, disabledU := range DisabledRepoUnits {
  112. for i, defaultU := range DefaultRepoUnits {
  113. if defaultU == disabledU {
  114. DefaultRepoUnits = append(DefaultRepoUnits[:i], DefaultRepoUnits[i+1:]...)
  115. }
  116. }
  117. }
  118. }
  119. // UnitGlobalDisabled checks if unit type is global disabled
  120. func (u UnitType) UnitGlobalDisabled() bool {
  121. for _, ud := range DisabledRepoUnits {
  122. if u == ud {
  123. return true
  124. }
  125. }
  126. return false
  127. }
  128. // CanDisable checks if this unit type can be disabled.
  129. func (u *UnitType) CanDisable() bool {
  130. for _, mu := range MustRepoUnits {
  131. if *u == mu {
  132. return false
  133. }
  134. }
  135. return true
  136. }
  137. // CanBeDefault checks if the unit type can be a default repo unit
  138. func (u *UnitType) CanBeDefault() bool {
  139. for _, nadU := range NotAllowedDefaultRepoUnits {
  140. if *u == nadU {
  141. return false
  142. }
  143. }
  144. return true
  145. }
  146. // Unit is a section of one repository
  147. type Unit struct {
  148. Type UnitType
  149. NameKey string
  150. URI string
  151. DescKey string
  152. Idx int
  153. }
  154. // CanDisable returns if this unit could be disabled.
  155. func (u *Unit) CanDisable() bool {
  156. return u.Type.CanDisable()
  157. }
  158. // IsLessThan compares order of two units
  159. func (u Unit) IsLessThan(unit Unit) bool {
  160. if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
  161. return false
  162. }
  163. return u.Idx < unit.Idx
  164. }
  165. // Enumerate all the units
  166. var (
  167. UnitCode = Unit{
  168. UnitTypeCode,
  169. "repo.code",
  170. "/",
  171. "repo.code.desc",
  172. 0,
  173. }
  174. UnitIssues = Unit{
  175. UnitTypeIssues,
  176. "repo.issues",
  177. "/issues",
  178. "repo.issues.desc",
  179. 1,
  180. }
  181. UnitExternalTracker = Unit{
  182. UnitTypeExternalTracker,
  183. "repo.ext_issues",
  184. "/issues",
  185. "repo.ext_issues.desc",
  186. 1,
  187. }
  188. UnitPullRequests = Unit{
  189. UnitTypePullRequests,
  190. "repo.pulls",
  191. "/pulls",
  192. "repo.pulls.desc",
  193. 2,
  194. }
  195. UnitReleases = Unit{
  196. UnitTypeReleases,
  197. "repo.releases",
  198. "/releases",
  199. "repo.releases.desc",
  200. 3,
  201. }
  202. UnitWiki = Unit{
  203. UnitTypeWiki,
  204. "repo.wiki",
  205. "/wiki",
  206. "repo.wiki.desc",
  207. 4,
  208. }
  209. UnitExternalWiki = Unit{
  210. UnitTypeExternalWiki,
  211. "repo.ext_wiki",
  212. "/wiki",
  213. "repo.ext_wiki.desc",
  214. 4,
  215. }
  216. // Units contains all the units
  217. Units = map[UnitType]Unit{
  218. UnitTypeCode: UnitCode,
  219. UnitTypeIssues: UnitIssues,
  220. UnitTypeExternalTracker: UnitExternalTracker,
  221. UnitTypePullRequests: UnitPullRequests,
  222. UnitTypeReleases: UnitReleases,
  223. UnitTypeWiki: UnitWiki,
  224. UnitTypeExternalWiki: UnitExternalWiki,
  225. }
  226. )
  227. // FindUnitTypes give the unit key name and return unit
  228. func FindUnitTypes(nameKeys ...string) (res []UnitType) {
  229. for _, key := range nameKeys {
  230. for t, u := range Units {
  231. if strings.EqualFold(key, u.NameKey) {
  232. res = append(res, t)
  233. break
  234. }
  235. }
  236. }
  237. return
  238. }