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

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