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.

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