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 7.1 kB

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