Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

unit.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. }