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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // BeforeSet is invoked from XORM before setting the value of a field of this object.
  60. func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
  61. switch colName {
  62. case "type":
  63. switch UnitType(Cell2Int64(val)) {
  64. case UnitTypeCode, UnitTypeIssues, UnitTypePullRequests, UnitTypeCommits, UnitTypeReleases,
  65. UnitTypeWiki, UnitTypeSettings:
  66. r.Config = new(UnitConfig)
  67. case UnitTypeExternalWiki:
  68. r.Config = new(ExternalWikiConfig)
  69. case UnitTypeExternalTracker:
  70. r.Config = new(ExternalTrackerConfig)
  71. default:
  72. panic("unrecognized repo unit type: " + com.ToStr(*val))
  73. }
  74. }
  75. }
  76. // AfterSet is invoked from XORM after setting the value of a field of this object.
  77. func (r *RepoUnit) AfterSet(colName string, _ xorm.Cell) {
  78. switch colName {
  79. case "created_unix":
  80. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  81. }
  82. }
  83. // Unit returns Unit
  84. func (r *RepoUnit) Unit() Unit {
  85. return Units[r.Type]
  86. }
  87. // CodeConfig returns config for UnitTypeCode
  88. func (r *RepoUnit) CodeConfig() *UnitConfig {
  89. return r.Config.(*UnitConfig)
  90. }
  91. // IssuesConfig returns config for UnitTypeIssues
  92. func (r *RepoUnit) IssuesConfig() *UnitConfig {
  93. return r.Config.(*UnitConfig)
  94. }
  95. // PullRequestsConfig returns config for UnitTypePullRequests
  96. func (r *RepoUnit) PullRequestsConfig() *UnitConfig {
  97. return r.Config.(*UnitConfig)
  98. }
  99. // CommitsConfig returns config for UnitTypeCommits
  100. func (r *RepoUnit) CommitsConfig() *UnitConfig {
  101. return r.Config.(*UnitConfig)
  102. }
  103. // ReleasesConfig returns config for UnitTypeReleases
  104. func (r *RepoUnit) ReleasesConfig() *UnitConfig {
  105. return r.Config.(*UnitConfig)
  106. }
  107. // ExternalWikiConfig returns config for UnitTypeExternalWiki
  108. func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
  109. return r.Config.(*ExternalWikiConfig)
  110. }
  111. // ExternalTrackerConfig returns config for UnitTypeExternalTracker
  112. func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
  113. return r.Config.(*ExternalTrackerConfig)
  114. }
  115. func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
  116. return units, e.Where("repo_id = ?", repoID).Find(&units)
  117. }
  118. func getUnitsByRepoIDAndIDs(e Engine, repoID int64, types []UnitType) (units []*RepoUnit, err error) {
  119. return units, e.Where("repo_id = ?", repoID).In("`type`", types).Find(&units)
  120. }