本站源代码
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.

175 lines
4.9KB

  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/timeutil"
  8. "github.com/unknwon/com"
  9. "xorm.io/core"
  10. "xorm.io/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 timeutil.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. AllowRebaseMerge bool
  77. AllowSquash bool
  78. }
  79. // FromDB fills up a PullRequestsConfig from serialized format.
  80. func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
  81. return json.Unmarshal(bs, &cfg)
  82. }
  83. // ToDB exports a PullRequestsConfig to a serialized format.
  84. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
  85. return json.Marshal(cfg)
  86. }
  87. // IsMergeStyleAllowed returns if merge style is allowed
  88. func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
  89. return mergeStyle == MergeStyleMerge && cfg.AllowMerge ||
  90. mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
  91. mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
  92. mergeStyle == MergeStyleSquash && cfg.AllowSquash
  93. }
  94. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  95. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  96. switch colName {
  97. case "type":
  98. switch UnitType(Cell2Int64(val)) {
  99. case UnitTypeCode, UnitTypeReleases, UnitTypeWiki:
  100. r.Config = new(UnitConfig)
  101. case UnitTypeExternalWiki:
  102. r.Config = new(ExternalWikiConfig)
  103. case UnitTypeExternalTracker:
  104. r.Config = new(ExternalTrackerConfig)
  105. case UnitTypePullRequests:
  106. r.Config = new(PullRequestsConfig)
  107. case UnitTypeIssues:
  108. r.Config = new(IssuesConfig)
  109. default:
  110. panic("unrecognized repo unit type: " + com.ToStr(*val))
  111. }
  112. }
  113. }
  114. // Unit returns Unit
  115. func (r *RepoUnit) Unit() Unit {
  116. return Units[r.Type]
  117. }
  118. // CodeConfig returns config for UnitTypeCode
  119. func (r *RepoUnit) CodeConfig() *UnitConfig {
  120. return r.Config.(*UnitConfig)
  121. }
  122. // PullRequestsConfig returns config for UnitTypePullRequests
  123. func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
  124. return r.Config.(*PullRequestsConfig)
  125. }
  126. // ReleasesConfig returns config for UnitTypeReleases
  127. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  128. return r.Config.(*UnitConfig)
  129. }
  130. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  131. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  132. return r.Config.(*ExternalWikiConfig)
  133. }
  134. // IssuesConfig returns config for UnitTypeIssues
  135. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  136. return r.Config.(*IssuesConfig)
  137. }
  138. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  139. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  140. return r.Config.(*ExternalTrackerConfig)
  141. }
  142. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  143. return units, e.Where("repo_id = ?", repoID).Find(&units)
  144. }
上海开阖软件有限公司 沪ICP备12045867号-1