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

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