Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_unit.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "time"
  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. Index int
  18. Config core.Conversion `xorm:"TEXT"`
  19. CreatedUnix int64 `xorm:"INDEX CREATED"`
  20. Created time.Time `xorm:"-"`
  21. }
  22. // UnitConfig describes common unit config
  23. type UnitConfig struct {
  24. }
  25. // FromDB fills up a UnitConfig from serialized format.
  26. func (cfg *UnitConfig) FromDB(bs []byte) error {
  27. return json.Unmarshal(bs, &cfg)
  28. }
  29. // ToDB exports a UnitConfig to a serialized format.
  30. func (cfg *UnitConfig) ToDB() ([]byte, error) {
  31. return json.Marshal(cfg)
  32. }
  33. // ExternalWikiConfig describes external wiki config
  34. type ExternalWikiConfig struct {
  35. ExternalWikiURL string
  36. }
  37. // FromDB fills up a ExternalWikiConfig from serialized format.
  38. func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
  39. return json.Unmarshal(bs, &cfg)
  40. }
  41. // ToDB exports a ExternalWikiConfig to a serialized format.
  42. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
  43. return json.Marshal(cfg)
  44. }
  45. // ExternalTrackerConfig describes external tracker config
  46. type ExternalTrackerConfig struct {
  47. ExternalTrackerURL string
  48. ExternalTrackerFormat string
  49. ExternalTrackerStyle string
  50. }
  51. // FromDB fills up a ExternalTrackerConfig from serialized format.
  52. func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
  53. return json.Unmarshal(bs, &cfg)
  54. }
  55. // ToDB exports a ExternalTrackerConfig to a serialized format.
  56. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
  57. return json.Marshal(cfg)
  58. }
  59. // IssuesConfig describes issues config
  60. type IssuesConfig struct {
  61. EnableTimetracker bool
  62. AllowOnlyContributorsToTrackTime bool
  63. }
  64. // FromDB fills up a IssuesConfig from serialized format.
  65. func (cfg *IssuesConfig) FromDB(bs []byte) error {
  66. return json.Unmarshal(bs, &cfg)
  67. }
  68. // ToDB exports a IssuesConfig to a serialized format.
  69. func (cfg *IssuesConfig) ToDB() ([]byte, error) {
  70. return json.Marshal(cfg)
  71. }
  72. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  73. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  74. switch colName {
  75. case "type":
  76. switch UnitType(Cell2Int64(val)) {
  77. case UnitTypeCode, UnitTypePullRequests, UnitTypeReleases,
  78. UnitTypeWiki:
  79. r.Config = new(UnitConfig)
  80. case UnitTypeExternalWiki:
  81. r.Config = new(ExternalWikiConfig)
  82. case UnitTypeExternalTracker:
  83. r.Config = new(ExternalTrackerConfig)
  84. case UnitTypeIssues:
  85. r.Config = new(IssuesConfig)
  86. default:
  87. panic("unrecognized repo unit type: " + com.ToStr(*val))
  88. }
  89. }
  90. }
  91. // AfterSet is invoked from XORM after setting the value of a field of this object.
  92. func (r *RepoUnit) AfterSet(colName string, _ xorm.Cell) {
  93. switch colName {
  94. case "created_unix":
  95. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  96. }
  97. }
  98. // Unit returns Unit
  99. func (r *RepoUnit) Unit() Unit {
  100. return Units[r.Type]
  101. }
  102. // CodeConfig returns config for UnitTypeCode
  103. func (r *RepoUnit) CodeConfig() *UnitConfig {
  104. return r.Config.(*UnitConfig)
  105. }
  106. // PullRequestsConfig returns config for UnitTypePullRequests
  107. func (r *RepoUnit) PullRequestsConfig() *UnitConfig {
  108. return r.Config.(*UnitConfig)
  109. }
  110. // ReleasesConfig returns config for UnitTypeReleases
  111. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  112. return r.Config.(*UnitConfig)
  113. }
  114. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  115. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  116. return r.Config.(*ExternalWikiConfig)
  117. }
  118. // IssuesConfig returns config for UnitTypeIssues
  119. func (r *RepoUnit) IssuesConfig() *IssuesConfig {
  120. return r.Config.(*IssuesConfig)
  121. }
  122. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  123. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  124. return r.Config.(*ExternalTrackerConfig)
  125. }
  126. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  127. return units, e.Where("repo_id = ?", repoID).Find(&units)
  128. }
  129. func getUnitsByRepoIDAndIDs(e Engine, repoID int64, types []UnitType) (units []*RepoUnit, err error) {
  130. return units, e.Where("repo_id = ?", repoID).In("`type`", types).Find(&units)
  131. }