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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 repo
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/unit"
  9. "code.gitea.io/gitea/modules/json"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "xorm.io/xorm"
  12. "xorm.io/xorm/convert"
  13. )
  14. // ErrUnitTypeNotExist represents a "UnitTypeNotExist" kind of error.
  15. type ErrUnitTypeNotExist struct {
  16. UT unit.Type
  17. }
  18. // IsErrUnitTypeNotExist checks if an error is a ErrUnitNotExist.
  19. func IsErrUnitTypeNotExist(err error) bool {
  20. _, ok := err.(ErrUnitTypeNotExist)
  21. return ok
  22. }
  23. func (err ErrUnitTypeNotExist) Error() string {
  24. return fmt.Sprintf("Unit type does not exist: %s", err.UT.String())
  25. }
  26. // RepoUnit describes all units of a repository
  27. type RepoUnit struct { //revive:disable-line:exported
  28. ID int64
  29. RepoID int64 `xorm:"INDEX(s)"`
  30. Type unit.Type `xorm:"INDEX(s)"`
  31. Config convert.Conversion `xorm:"TEXT"`
  32. CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
  33. }
  34. func init() {
  35. db.RegisterModel(new(RepoUnit))
  36. }
  37. // UnitConfig describes common unit config
  38. type UnitConfig struct{}
  39. // FromDB fills up a UnitConfig from serialized format.
  40. func (cfg *UnitConfig) FromDB(bs []byte) error {
  41. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  42. }
  43. // ToDB exports a UnitConfig to a serialized format.
  44. func (cfg *UnitConfig) ToDB() ([]byte, error) {
  45. return json.Marshal(cfg)
  46. }
  47. // ExternalWikiConfig describes external wiki config
  48. type ExternalWikiConfig struct {
  49. ExternalWikiURL string
  50. }
  51. // FromDB fills up a ExternalWikiConfig from serialized format.
  52. func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
  53. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  54. }
  55. // ToDB exports a ExternalWikiConfig to a serialized format.
  56. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
  57. return json.Marshal(cfg)
  58. }
  59. // ExternalTrackerConfig describes external tracker config
  60. type ExternalTrackerConfig struct {
  61. ExternalTrackerURL string
  62. ExternalTrackerFormat string
  63. ExternalTrackerStyle string
  64. }
  65. // FromDB fills up a ExternalTrackerConfig from serialized format.
  66. func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
  67. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  68. }
  69. // ToDB exports a ExternalTrackerConfig to a serialized format.
  70. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
  71. return json.Marshal(cfg)
  72. }
  73. // IssuesConfig describes issues config
  74. type IssuesConfig struct {
  75. EnableTimetracker bool
  76. AllowOnlyContributorsToTrackTime bool
  77. EnableDependencies bool
  78. }
  79. // FromDB fills up a IssuesConfig from serialized format.
  80. func (cfg *IssuesConfig) FromDB(bs []byte) error {
  81. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  82. }
  83. // ToDB exports a IssuesConfig to a serialized format.
  84. func (cfg *IssuesConfig) ToDB() ([]byte, error) {
  85. return json.Marshal(cfg)
  86. }
  87. // PullRequestsConfig describes pull requests config
  88. type PullRequestsConfig struct {
  89. IgnoreWhitespaceConflicts bool
  90. AllowMerge bool
  91. AllowRebase bool
  92. AllowRebaseMerge bool
  93. AllowSquash bool
  94. AllowManualMerge bool
  95. AutodetectManualMerge bool
  96. DefaultDeleteBranchAfterMerge bool
  97. DefaultMergeStyle MergeStyle
  98. }
  99. // FromDB fills up a PullRequestsConfig from serialized format.
  100. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  101. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  102. }
  103. // ToDB exports a PullRequestsConfig to a serialized format.
  104. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  105. return json.Marshal(cfg)
  106. }
  107. // IsMergeStyleAllowed returns if merge style is allowed
  108. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  109. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  110. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  111. mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
  112. mergeStyle == MergeStyleSquash && cfg.AllowSquash ||
  113. mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
  114. }
  115. // GetDefaultMergeStyle returns the default merge style for this pull request
  116. func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
  117. if len(cfg.DefaultMergeStyle) != 0 {
  118. return cfg.DefaultMergeStyle
  119. }
  120. return MergeStyleMerge
  121. }
  122. // AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig
  123. func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
  124. count := 0
  125. if cfg.AllowMerge {
  126. count++
  127. }
  128. if cfg.AllowRebase {
  129. count++
  130. }
  131. if cfg.AllowRebaseMerge {
  132. count++
  133. }
  134. if cfg.AllowSquash {
  135. count++
  136. }
  137. return count
  138. }
  139. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  140. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  141. switch colName {
  142. case "type":
  143. switch unit.Type(db.Cell2Int64(val)) {
  144. case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects:
  145. r.Config = new(UnitConfig)
  146. case unit.TypeExternalWiki:
  147. r.Config = new(ExternalWikiConfig)
  148. case unit.TypeExternalTracker:
  149. r.Config = new(ExternalTrackerConfig)
  150. case unit.TypePullRequests:
  151. r.Config = new(PullRequestsConfig)
  152. case unit.TypeIssues:
  153. r.Config = new(IssuesConfig)
  154. default:
  155. panic(fmt.Sprintf("unrecognized repo unit type: %v", *val))
  156. }
  157. }
  158. }
  159. // Unit returns Unit
  160. func (r *RepoUnit) Unit() unit.Unit {
  161. return unit.Units[r.Type]
  162. }
  163. // CodeConfig returns config for unit.TypeCode
  164. func (r *RepoUnit) CodeConfig() *UnitConfig {
  165. return r.Config.(*UnitConfig)
  166. }
  167. // PullRequestsConfig returns config for unit.TypePullRequests
  168. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  169. return r.Config.(*PullRequestsConfig)
  170. }
  171. // ReleasesConfig returns config for unit.TypeReleases
  172. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  173. return r.Config.(*UnitConfig)
  174. }
  175. // ExternalWikiConfig returns config for unit.TypeExternalWiki
  176. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  177. return r.Config.(*ExternalWikiConfig)
  178. }
  179. // IssuesConfig returns config for unit.TypeIssues
  180. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  181. return r.Config.(*IssuesConfig)
  182. }
  183. // ExternalTrackerConfig returns config for unit.TypeExternalTracker
  184. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  185. return r.Config.(*ExternalTrackerConfig)
  186. }
  187. func getUnitsByRepoID(e db.Engine, repoID int64) (units []*RepoUnit, err error) {
  188. var tmpUnits []*RepoUnit
  189. if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
  190. return nil, err
  191. }
  192. for _, u := range tmpUnits {
  193. if !u.Type.UnitGlobalDisabled() {
  194. units = append(units, u)
  195. }
  196. }
  197. return units, nil
  198. }
  199. // UpdateRepoUnit updates the provided repo unit
  200. func UpdateRepoUnit(unit *RepoUnit) error {
  201. _, err := db.GetEngine(db.DefaultContext).ID(unit.ID).Update(unit)
  202. return err
  203. }
  204. // UpdateRepositoryUnits updates a repository's units
  205. func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) {
  206. ctx, committer, err := db.TxContext()
  207. if err != nil {
  208. return err
  209. }
  210. defer committer.Close()
  211. // Delete existing settings of units before adding again
  212. for _, u := range units {
  213. deleteUnitTypes = append(deleteUnitTypes, u.Type)
  214. }
  215. if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
  216. return err
  217. }
  218. if len(units) > 0 {
  219. if err = db.Insert(ctx, units); err != nil {
  220. return err
  221. }
  222. }
  223. return committer.Commit()
  224. }