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

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