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.0KB

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