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 5.9KB

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