Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

repo_unit.go 4.1KB

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