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.

191 lines
3.9 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. )
  10. // UnitType is Unit's Type
  11. type UnitType int
  12. // Enumerate all the unit types
  13. const (
  14. UnitTypeCode UnitType = iota + 1 // 1 code
  15. UnitTypeIssues // 2 issues
  16. UnitTypePullRequests // 3 PRs
  17. UnitTypeReleases // 4 Releases
  18. UnitTypeWiki // 5 Wiki
  19. UnitTypeExternalWiki // 6 ExternalWiki
  20. UnitTypeExternalTracker // 7 ExternalTracker
  21. )
  22. // Value returns integer value for unit type
  23. func (u UnitType) Value() int {
  24. return int(u)
  25. }
  26. func (u UnitType) String() string {
  27. switch u {
  28. case UnitTypeCode:
  29. return "UnitTypeCode"
  30. case UnitTypeIssues:
  31. return "UnitTypeIssues"
  32. case UnitTypePullRequests:
  33. return "UnitTypePullRequests"
  34. case UnitTypeReleases:
  35. return "UnitTypeReleases"
  36. case UnitTypeWiki:
  37. return "UnitTypeWiki"
  38. case UnitTypeExternalWiki:
  39. return "UnitTypeExternalWiki"
  40. case UnitTypeExternalTracker:
  41. return "UnitTypeExternalTracker"
  42. }
  43. return fmt.Sprintf("Unknown UnitType %d", u)
  44. }
  45. // ColorFormat provides a ColorFormatted version of this UnitType
  46. func (u UnitType) ColorFormat(s fmt.State) {
  47. log.ColorFprintf(s, "%d:%s",
  48. log.NewColoredIDValue(u),
  49. u)
  50. }
  51. var (
  52. // AllRepoUnitTypes contains all the unit types
  53. AllRepoUnitTypes = []UnitType{
  54. UnitTypeCode,
  55. UnitTypeIssues,
  56. UnitTypePullRequests,
  57. UnitTypeReleases,
  58. UnitTypeWiki,
  59. UnitTypeExternalWiki,
  60. UnitTypeExternalTracker,
  61. }
  62. // DefaultRepoUnits contains the default unit types
  63. DefaultRepoUnits = []UnitType{
  64. UnitTypeCode,
  65. UnitTypeIssues,
  66. UnitTypePullRequests,
  67. UnitTypeReleases,
  68. UnitTypeWiki,
  69. }
  70. // MustRepoUnits contains the units could not be disabled currently
  71. MustRepoUnits = []UnitType{
  72. UnitTypeCode,
  73. UnitTypeReleases,
  74. }
  75. )
  76. // Unit is a section of one repository
  77. type Unit struct {
  78. Type UnitType
  79. NameKey string
  80. URI string
  81. DescKey string
  82. Idx int
  83. }
  84. // CanDisable returns if this unit could be disabled.
  85. func (u *Unit) CanDisable() bool {
  86. return true
  87. }
  88. // IsLessThan compares order of two units
  89. func (u Unit) IsLessThan(unit Unit) bool {
  90. if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
  91. return false
  92. }
  93. return u.Idx < unit.Idx
  94. }
  95. // Enumerate all the units
  96. var (
  97. UnitCode = Unit{
  98. UnitTypeCode,
  99. "repo.code",
  100. "/",
  101. "repo.code.desc",
  102. 0,
  103. }
  104. UnitIssues = Unit{
  105. UnitTypeIssues,
  106. "repo.issues",
  107. "/issues",
  108. "repo.issues.desc",
  109. 1,
  110. }
  111. UnitExternalTracker = Unit{
  112. UnitTypeExternalTracker,
  113. "repo.ext_issues",
  114. "/issues",
  115. "repo.ext_issues.desc",
  116. 1,
  117. }
  118. UnitPullRequests = Unit{
  119. UnitTypePullRequests,
  120. "repo.pulls",
  121. "/pulls",
  122. "repo.pulls.desc",
  123. 2,
  124. }
  125. UnitReleases = Unit{
  126. UnitTypeReleases,
  127. "repo.releases",
  128. "/releases",
  129. "repo.releases.desc",
  130. 3,
  131. }
  132. UnitWiki = Unit{
  133. UnitTypeWiki,
  134. "repo.wiki",
  135. "/wiki",
  136. "repo.wiki.desc",
  137. 4,
  138. }
  139. UnitExternalWiki = Unit{
  140. UnitTypeExternalWiki,
  141. "repo.ext_wiki",
  142. "/wiki",
  143. "repo.ext_wiki.desc",
  144. 4,
  145. }
  146. // Units contains all the units
  147. Units = map[UnitType]Unit{
  148. UnitTypeCode: UnitCode,
  149. UnitTypeIssues: UnitIssues,
  150. UnitTypeExternalTracker: UnitExternalTracker,
  151. UnitTypePullRequests: UnitPullRequests,
  152. UnitTypeReleases: UnitReleases,
  153. UnitTypeWiki: UnitWiki,
  154. UnitTypeExternalWiki: UnitExternalWiki,
  155. }
  156. )
  157. // FindUnitTypes give the unit key name and return unit
  158. func FindUnitTypes(nameKeys ...string) (res []UnitType) {
  159. for _, key := range nameKeys {
  160. for t, u := range Units {
  161. if strings.EqualFold(key, u.NameKey) {
  162. res = append(res, t)
  163. break
  164. }
  165. }
  166. }
  167. return
  168. }