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.

repo_unit.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. DefaultMergeStyle MergeStyle
  88. }
  89. // FromDB fills up a PullRequestsConfig from serialized format.
  90. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  91. json := jsoniter.ConfigCompatibleWithStandardLibrary
  92. return json.Unmarshal(bs, &cfg)
  93. }
  94. // ToDB exports a PullRequestsConfig to a serialized format.
  95. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  96. json := jsoniter.ConfigCompatibleWithStandardLibrary
  97. return json.Marshal(cfg)
  98. }
  99. // IsMergeStyleAllowed returns if merge style is allowed
  100. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  101. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  102. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  103. mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
  104. mergeStyle == MergeStyleSquash && cfg.AllowSquash ||
  105. mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
  106. }
  107. // GetDefaultMergeStyle returns the default merge style for this pull request
  108. func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
  109. if len(cfg.DefaultMergeStyle) != 0 {
  110. return cfg.DefaultMergeStyle
  111. }
  112. return MergeStyleMerge
  113. }
  114. // AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig
  115. func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
  116. count := 0
  117. if cfg.AllowMerge {
  118. count++
  119. }
  120. if cfg.AllowRebase {
  121. count++
  122. }
  123. if cfg.AllowRebaseMerge {
  124. count++
  125. }
  126. if cfg.AllowSquash {
  127. count++
  128. }
  129. return count
  130. }
  131. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  132. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  133. switch colName {
  134. case "type":
  135. switch UnitType(Cell2Int64(val)) {
  136. case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects:
  137. r.Config = new(UnitConfig)
  138. case UnitTypeExternalWiki:
  139. r.Config = new(ExternalWikiConfig)
  140. case UnitTypeExternalTracker:
  141. r.Config = new(ExternalTrackerConfig)
  142. case UnitTypePullRequests:
  143. r.Config = new(PullRequestsConfig)
  144. case UnitTypeIssues:
  145. r.Config = new(IssuesConfig)
  146. default:
  147. panic(fmt.Sprintf("unrecognized repo unit type: %v", *val))
  148. }
  149. }
  150. }
  151. // Unit returns Unit
  152. func (r *RepoUnit) Unit() Unit {
  153. return Units[r.Type]
  154. }
  155. // CodeConfig returns config for UnitTypeCode
  156. func (r *RepoUnit) CodeConfig() *UnitConfig {
  157. return r.Config.(*UnitConfig)
  158. }
  159. // PullRequestsConfig returns config for UnitTypePullRequests
  160. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  161. return r.Config.(*PullRequestsConfig)
  162. }
  163. // ReleasesConfig returns config for UnitTypeReleases
  164. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  165. return r.Config.(*UnitConfig)
  166. }
  167. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  168. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  169. return r.Config.(*ExternalWikiConfig)
  170. }
  171. // IssuesConfig returns config for UnitTypeIssues
  172. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  173. return r.Config.(*IssuesConfig)
  174. }
  175. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  176. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  177. return r.Config.(*ExternalTrackerConfig)
  178. }
  179. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  180. var tmpUnits []*RepoUnit
  181. if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
  182. return nil, err
  183. }
  184. for _, u := range tmpUnits {
  185. if !u.Type.UnitGlobalDisabled() {
  186. units = append(units, u)
  187. }
  188. }
  189. return units, nil
  190. }