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.4 kB

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