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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. AllowRebaseUpdate bool
  97. DefaultDeleteBranchAfterMerge bool
  98. DefaultMergeStyle MergeStyle
  99. }
  100. // FromDB fills up a PullRequestsConfig from serialized format.
  101. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  102. // AllowRebaseUpdate = true as default for existing PullRequestConfig in DB
  103. cfg.AllowRebaseUpdate = true
  104. return json.UnmarshalHandleDoubleEncode(bs, &cfg)
  105. }
  106. // ToDB exports a PullRequestsConfig to a serialized format.
  107. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  108. return json.Marshal(cfg)
  109. }
  110. // IsMergeStyleAllowed returns if merge style is allowed
  111. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  112. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  113. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  114. mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
  115. mergeStyle == MergeStyleSquash && cfg.AllowSquash ||
  116. mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
  117. }
  118. // GetDefaultMergeStyle returns the default merge style for this pull request
  119. func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
  120. if len(cfg.DefaultMergeStyle) != 0 {
  121. return cfg.DefaultMergeStyle
  122. }
  123. return MergeStyleMerge
  124. }
  125. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  126. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  127. switch colName {
  128. case "type":
  129. switch unit.Type(db.Cell2Int64(val)) {
  130. case unit.TypeExternalWiki:
  131. r.Config = new(ExternalWikiConfig)
  132. case unit.TypeExternalTracker:
  133. r.Config = new(ExternalTrackerConfig)
  134. case unit.TypePullRequests:
  135. r.Config = new(PullRequestsConfig)
  136. case unit.TypeIssues:
  137. r.Config = new(IssuesConfig)
  138. case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects, unit.TypePackages:
  139. fallthrough
  140. default:
  141. r.Config = new(UnitConfig)
  142. }
  143. }
  144. }
  145. // Unit returns Unit
  146. func (r *RepoUnit) Unit() unit.Unit {
  147. return unit.Units[r.Type]
  148. }
  149. // CodeConfig returns config for unit.TypeCode
  150. func (r *RepoUnit) CodeConfig() *UnitConfig {
  151. return r.Config.(*UnitConfig)
  152. }
  153. // PullRequestsConfig returns config for unit.TypePullRequests
  154. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  155. return r.Config.(*PullRequestsConfig)
  156. }
  157. // ReleasesConfig returns config for unit.TypeReleases
  158. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  159. return r.Config.(*UnitConfig)
  160. }
  161. // ExternalWikiConfig returns config for unit.TypeExternalWiki
  162. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  163. return r.Config.(*ExternalWikiConfig)
  164. }
  165. // IssuesConfig returns config for unit.TypeIssues
  166. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  167. return r.Config.(*IssuesConfig)
  168. }
  169. // ExternalTrackerConfig returns config for unit.TypeExternalTracker
  170. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  171. return r.Config.(*ExternalTrackerConfig)
  172. }
  173. func getUnitsByRepoID(e db.Engine, repoID int64) (units []*RepoUnit, err error) {
  174. var tmpUnits []*RepoUnit
  175. if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
  176. return nil, err
  177. }
  178. for _, u := range tmpUnits {
  179. if !u.Type.UnitGlobalDisabled() {
  180. units = append(units, u)
  181. }
  182. }
  183. return units, nil
  184. }
  185. // UpdateRepoUnit updates the provided repo unit
  186. func UpdateRepoUnit(unit *RepoUnit) error {
  187. _, err := db.GetEngine(db.DefaultContext).ID(unit.ID).Update(unit)
  188. return err
  189. }
  190. // UpdateRepositoryUnits updates a repository's units
  191. func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) {
  192. ctx, committer, err := db.TxContext()
  193. if err != nil {
  194. return err
  195. }
  196. defer committer.Close()
  197. // Delete existing settings of units before adding again
  198. for _, u := range units {
  199. deleteUnitTypes = append(deleteUnitTypes, u.Type)
  200. }
  201. if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
  202. return err
  203. }
  204. if len(units) > 0 {
  205. if err = db.Insert(ctx, units); err != nil {
  206. return err
  207. }
  208. }
  209. return committer.Commit()
  210. }