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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "encoding/json"
  7. "code.gitea.io/gitea/modules/util"
  8. "github.com/Unknwon/com"
  9. "github.com/go-xorm/core"
  10. "github.com/go-xorm/xorm"
  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 core.Conversion `xorm:"TEXT"`
  18. CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
  19. }
  20. // UnitConfig describes common unit config
  21. type UnitConfig struct {
  22. }
  23. // FromDB fills up a UnitConfig from serialized format.
  24. func (cfg *UnitConfig) FromDB(bs []byte) error {
  25. return json.Unmarshal(bs, &cfg)
  26. }
  27. // ToDB exports a UnitConfig to a serialized format.
  28. func (cfg *UnitConfig) ToDB() ([]byte, error) {
  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 json.Unmarshal(bs, &cfg)
  38. }
  39. // ToDB exports a ExternalWikiConfig to a serialized format.
  40. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
  41. return json.Marshal(cfg)
  42. }
  43. // ExternalTrackerConfig describes external tracker config
  44. type ExternalTrackerConfig struct {
  45. ExternalTrackerURL string
  46. ExternalTrackerFormat string
  47. ExternalTrackerStyle string
  48. }
  49. // FromDB fills up a ExternalTrackerConfig from serialized format.
  50. func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
  51. return json.Unmarshal(bs, &cfg)
  52. }
  53. // ToDB exports a ExternalTrackerConfig to a serialized format.
  54. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
  55. return json.Marshal(cfg)
  56. }
  57. // IssuesConfig describes issues config
  58. type IssuesConfig struct {
  59. EnableTimetracker bool
  60. AllowOnlyContributorsToTrackTime bool
  61. EnableDependencies bool
  62. }
  63. // FromDB fills up a IssuesConfig from serialized format.
  64. func (cfg *IssuesConfig) FromDB(bs []byte) error {
  65. return json.Unmarshal(bs, &cfg)
  66. }
  67. // ToDB exports a IssuesConfig to a serialized format.
  68. func (cfg *IssuesConfig) ToDB() ([]byte, error) {
  69. return json.Marshal(cfg)
  70. }
  71. // PullRequestsConfig describes pull requests config
  72. type PullRequestsConfig struct {
  73. IgnoreWhitespaceConflicts bool
  74. AllowMerge bool
  75. AllowRebase bool
  76. AllowSquash bool
  77. }
  78. // FromDB fills up a PullRequestsConfig from serialized format.
  79. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  80. return json.Unmarshal(bs, &cfg)
  81. }
  82. // ToDB exports a PullRequestsConfig to a serialized format.
  83. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  84. return json.Marshal(cfg)
  85. }
  86. // IsMergeStyleAllowed returns if merge style is allowed
  87. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  88. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  89. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  90. mergeStyle == MergeStyleSquash && cfg.AllowSquash
  91. }
  92. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  93. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  94. switch colName {
  95. case "type":
  96. switch UnitType(Cell2Int64(val)) {
  97. case UnitTypeCode, UnitTypeReleases, UnitTypeWiki:
  98. r.Config = new(UnitConfig)
  99. case UnitTypeExternalWiki:
  100. r.Config = new(ExternalWikiConfig)
  101. case UnitTypeExternalTracker:
  102. r.Config = new(ExternalTrackerConfig)
  103. case UnitTypePullRequests:
  104. r.Config = new(PullRequestsConfig)
  105. case UnitTypeIssues:
  106. r.Config = new(IssuesConfig)
  107. default:
  108. panic("unrecognized repo unit type: " + com.ToStr(*val))
  109. }
  110. }
  111. }
  112. // Unit returns Unit
  113. func (r *RepoUnit) Unit() Unit {
  114. return Units[r.Type]
  115. }
  116. // CodeConfig returns config for UnitTypeCode
  117. func (r *RepoUnit) CodeConfig() *UnitConfig {
  118. return r.Config.(*UnitConfig)
  119. }
  120. // PullRequestsConfig returns config for UnitTypePullRequests
  121. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  122. return r.Config.(*PullRequestsConfig)
  123. }
  124. // ReleasesConfig returns config for UnitTypeReleases
  125. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  126. return r.Config.(*UnitConfig)
  127. }
  128. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  129. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  130. return r.Config.(*ExternalWikiConfig)
  131. }
  132. // IssuesConfig returns config for UnitTypeIssues
  133. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  134. return r.Config.(*IssuesConfig)
  135. }
  136. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  137. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  138. return r.Config.(*ExternalTrackerConfig)
  139. }
  140. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  141. return units, e.Where("repo_id = ?", repoID).Find(&units)
  142. }
  143. func getUnitsByRepoIDAndIDs(e Engine, repoID int64, types []UnitType) (units []*RepoUnit, err error) {
  144. return units, e.Where("repo_id = ?", repoID).In("`type`", types).Find(&units)
  145. }