model.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package model
  2. import (
  3. "reflect"
  4. "regexp"
  5. )
  6. // reids锁
  7. const (
  8. SagaTask = "_SagaTask_%d"
  9. SagaRepoLockKey = "_SagaRepoLockKey_%d"
  10. SagaLockValue = "_SagaLockValue"
  11. )
  12. // gitlab指令
  13. const (
  14. SagaCommandPlusOne = "+ok"
  15. SagaCommandMerge = "+mr"
  16. SagaCommandPlusOne1 = "+1"
  17. SagaCommandMerge1 = "+merge"
  18. )
  19. // 任务状态
  20. const (
  21. TaskStatusFailed = 1 // 任务失败
  22. TaskStatusSuccess = 2 // 任务成功
  23. TaskStatusRunning = 3 // 任务运行中
  24. TaskStatusWaiting = 4 // 任务等待
  25. )
  26. // CONTRIBUTORS define
  27. const (
  28. SagaContributorsName = "CONTRIBUTORS.md"
  29. )
  30. // RepoConfig def
  31. type RepoConfig struct {
  32. URL string
  33. Group string
  34. Name string
  35. GName string // gitlab仓库别名
  36. Language string
  37. AuthBranches []string // 鉴权分支
  38. TargetBranches []string // 分支白名单
  39. TargetBranchRegexes []*regexp.Regexp
  40. LockTimeout int32
  41. MinReviewer int
  42. RelatePipeline bool
  43. DelayMerge bool
  44. LimitAuth bool
  45. AllowLabel string
  46. SuperAuthUsers []string
  47. }
  48. // RequireReviewFolder ...
  49. type RequireReviewFolder struct {
  50. Folder string
  51. Owners []string
  52. Reviewers []string
  53. }
  54. // AuthUsers ...
  55. type AuthUsers struct {
  56. Owners []string
  57. Reviewers []string
  58. }
  59. // ContactInfo def
  60. type ContactInfo struct {
  61. ID string `json:"id,omitempty" gorm:"column:id"`
  62. UserName string `json:"english_name" gorm:"column:user_name"`
  63. UserID string `json:"userid" gorm:"column:user_id"`
  64. NickName string `json:"name" gorm:"column:nick_name"`
  65. VisibleSaga bool `json:"visible_saga" gorm:"column:visible_saga"`
  66. }
  67. // RequireVisibleUser def
  68. type RequireVisibleUser struct {
  69. UserName string
  70. NickName string
  71. }
  72. // AlmostEqual return the compare result with fields
  73. func (contact *ContactInfo) AlmostEqual(other *ContactInfo) bool {
  74. if contact.UserID == other.UserID &&
  75. contact.UserName == other.UserName &&
  76. contact.NickName == other.NickName {
  77. return true
  78. }
  79. return false
  80. }
  81. // TaskInfo ...
  82. type TaskInfo struct {
  83. NoteID int
  84. Event *HookComment
  85. Repo *Repo
  86. }
  87. // MergeInfo ...
  88. type MergeInfo struct {
  89. PipelineID int
  90. NoteID int
  91. AuthorID int
  92. UserName string
  93. MRIID int
  94. ProjID int
  95. URL string
  96. AuthBranches []string
  97. SourceBranch string
  98. TargetBranch string
  99. MinReviewer int
  100. LockTimeout int32
  101. Title string
  102. Description string
  103. }
  104. // Repo structure
  105. type Repo struct {
  106. Config *RepoConfig
  107. }
  108. // Update if config is changed
  109. func (r *Repo) Update(conf *RepoConfig) bool {
  110. if r.confEqual(conf) {
  111. return false
  112. }
  113. r.Config = conf
  114. return true
  115. }
  116. func (r *Repo) confEqual(conf *RepoConfig) bool {
  117. if r.Config.URL == conf.URL &&
  118. r.Config.Group == conf.Group &&
  119. r.Config.Name == conf.Name &&
  120. r.Config.GName == conf.GName &&
  121. r.Config.Language == conf.Language &&
  122. reflect.DeepEqual(r.Config.AuthBranches, conf.AuthBranches) &&
  123. reflect.DeepEqual(r.Config.TargetBranches, conf.TargetBranches) &&
  124. r.Config.LockTimeout == conf.LockTimeout &&
  125. r.Config.MinReviewer == conf.MinReviewer &&
  126. r.Config.RelatePipeline == conf.RelatePipeline &&
  127. r.Config.DelayMerge == conf.DelayMerge &&
  128. r.Config.LimitAuth == conf.LimitAuth &&
  129. r.Config.AllowLabel == conf.AllowLabel &&
  130. reflect.DeepEqual(r.Config.SuperAuthUsers, conf.SuperAuthUsers) {
  131. return true
  132. }
  133. return false
  134. }
  135. // AuthUpdate ...
  136. func (r *Repo) AuthUpdate(conf *RepoConfig) bool {
  137. if r.Config.Group == conf.Group &&
  138. r.Config.Name == conf.Name &&
  139. reflect.DeepEqual(r.Config.AuthBranches, conf.AuthBranches) {
  140. return false
  141. }
  142. return true
  143. }
  144. // WebHookUpdate ...
  145. func (r *Repo) WebHookUpdate(conf *RepoConfig) bool {
  146. return r.Config.URL != conf.URL
  147. }