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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "code.gitea.io/gitea/modules/timeutil"
  8. jsoniter "github.com/json-iterator/go"
  9. "xorm.io/xorm"
  10. "xorm.io/xorm/convert"
  11. )
  12. // RepoUnit describes all units of a repository
  13. type RepoUnit struct {
  14. ID int64
  15. RepoID int64 `xorm:"INDEX(s)"`
  16. Type UnitType `xorm:"INDEX(s)"`
  17. Config convert.Conversion `xorm:"TEXT"`
  18. CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
  19. }
  20. // UnitConfig describes common unit config
  21. type UnitConfig struct{}
  22. // FromDB fills up a UnitConfig from serialized format.
  23. func (cfg *UnitConfig) FromDB(bs []byte) error {
  24. json := jsoniter.ConfigCompatibleWithStandardLibrary
  25. return json.Unmarshal(bs, &cfg)
  26. }
  27. // ToDB exports a UnitConfig to a serialized format.
  28. func (cfg *UnitConfig) ToDB() ([]byte, error) {
  29. json := jsoniter.ConfigCompatibleWithStandardLibrary
  30. return json.Marshal(cfg)
  31. }
  32. // ExternalWikiConfig describes external wiki config
  33. type ExternalWikiConfig struct {
  34. ExternalWikiURL string
  35. }
  36. // FromDB fills up a ExternalWikiConfig from serialized format.
  37. func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
  38. json := jsoniter.ConfigCompatibleWithStandardLibrary
  39. return json.Unmarshal(bs, &cfg)
  40. }
  41. // ToDB exports a ExternalWikiConfig to a serialized format.
  42. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
  43. json := jsoniter.ConfigCompatibleWithStandardLibrary
  44. return json.Marshal(cfg)
  45. }
  46. // ExternalTrackerConfig describes external tracker config
  47. type ExternalTrackerConfig struct {
  48. ExternalTrackerURL string
  49. ExternalTrackerFormat string
  50. ExternalTrackerStyle string
  51. }
  52. // FromDB fills up a ExternalTrackerConfig from serialized format.
  53. func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
  54. json := jsoniter.ConfigCompatibleWithStandardLibrary
  55. return json.Unmarshal(bs, &cfg)
  56. }
  57. // ToDB exports a ExternalTrackerConfig to a serialized format.
  58. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
  59. json := jsoniter.ConfigCompatibleWithStandardLibrary
  60. return json.Marshal(cfg)
  61. }
  62. // IssuesConfig describes issues config
  63. type IssuesConfig struct {
  64. EnableTimetracker bool
  65. AllowOnlyContributorsToTrackTime bool
  66. EnableDependencies bool
  67. }
  68. // FromDB fills up a IssuesConfig from serialized format.
  69. func (cfg *IssuesConfig) FromDB(bs []byte) error {
  70. json := jsoniter.ConfigCompatibleWithStandardLibrary
  71. return json.Unmarshal(bs, &cfg)
  72. }
  73. // ToDB exports a IssuesConfig to a serialized format.
  74. func (cfg *IssuesConfig) ToDB() ([]byte, error) {
  75. json := jsoniter.ConfigCompatibleWithStandardLibrary
  76. return json.Marshal(cfg)
  77. }
  78. // PullRequestsConfig describes pull requests config
  79. type PullRequestsConfig struct {
  80. IgnoreWhitespaceConflicts bool
  81. AllowMerge bool
  82. AllowRebase bool
  83. AllowRebaseMerge bool
  84. AllowSquash bool
  85. AllowManualMerge bool
  86. AutodetectManualMerge bool
  87. }
  88. // FromDB fills up a PullRequestsConfig from serialized format.
  89. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  90. json := jsoniter.ConfigCompatibleWithStandardLibrary
  91. return json.Unmarshal(bs, &cfg)
  92. }
  93. // ToDB exports a PullRequestsConfig to a serialized format.
  94. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  95. json := jsoniter.ConfigCompatibleWithStandardLibrary
  96. return json.Marshal(cfg)
  97. }
  98. // IsMergeStyleAllowed returns if merge style is allowed
  99. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  100. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  101. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  102. mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
  103. mergeStyle == MergeStyleSquash && cfg.AllowSquash ||
  104. mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
  105. }
  106. // AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig
  107. func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
  108. count := 0
  109. if cfg.AllowMerge {
  110. count++
  111. }
  112. if cfg.AllowRebase {
  113. count++
  114. }
  115. if cfg.AllowRebaseMerge {
  116. count++
  117. }
  118. if cfg.AllowSquash {
  119. count++
  120. }
  121. return count
  122. }
  123. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  124. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  125. switch colName {
  126. case "type":
  127. switch UnitType(Cell2Int64(val)) {
  128. case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects:
  129. r.Config = new(UnitConfig)
  130. case UnitTypeExternalWiki:
  131. r.Config = new(ExternalWikiConfig)
  132. case UnitTypeExternalTracker:
  133. r.Config = new(ExternalTrackerConfig)
  134. case UnitTypePullRequests:
  135. r.Config = new(PullRequestsConfig)
  136. case UnitTypeIssues:
  137. r.Config = new(IssuesConfig)
  138. default:
  139. panic(fmt.Sprintf("unrecognized repo unit type: %v", *val))
  140. }
  141. }
  142. }
  143. // Unit returns Unit
  144. func (r *RepoUnit) Unit() Unit {
  145. return Units[r.Type]
  146. }
  147. // CodeConfig returns config for UnitTypeCode
  148. func (r *RepoUnit) CodeConfig() *UnitConfig {
  149. return r.Config.(*UnitConfig)
  150. }
  151. // PullRequestsConfig returns config for UnitTypePullRequests
  152. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  153. return r.Config.(*PullRequestsConfig)
  154. }
  155. // ReleasesConfig returns config for UnitTypeReleases
  156. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  157. return r.Config.(*UnitConfig)
  158. }
  159. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  160. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  161. return r.Config.(*ExternalWikiConfig)
  162. }
  163. // IssuesConfig returns config for UnitTypeIssues
  164. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  165. return r.Config.(*IssuesConfig)
  166. }
  167. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  168. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  169. return r.Config.(*ExternalTrackerConfig)
  170. }
  171. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  172. var tmpUnits []*RepoUnit
  173. if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
  174. return nil, err
  175. }
  176. for _, u := range tmpUnits {
  177. if !u.Type.UnitGlobalDisabled() {
  178. units = append(units, u)
  179. }
  180. }
  181. return units, nil
  182. }