service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/tool/saga/conf"
  7. "go-common/app/tool/saga/dao"
  8. "go-common/app/tool/saga/model"
  9. "go-common/app/tool/saga/service/command"
  10. "go-common/app/tool/saga/service/gitlab"
  11. "go-common/library/log"
  12. "github.com/pkg/errors"
  13. "github.com/robfig/cron"
  14. )
  15. // Service biz service def.
  16. type Service struct {
  17. missch chan func()
  18. d *dao.Dao
  19. gitlab *gitlab.Gitlab
  20. gitRepoMap map[string]*model.Repo // map[repoName]*repo
  21. cmd *command.Command
  22. cron *cron.Cron
  23. }
  24. // New a DirService and return.
  25. func New() (s *Service) {
  26. s = &Service{
  27. d: dao.New(),
  28. missch: make(chan func(), 10240),
  29. }
  30. // init gitlab client
  31. s.gitlab = gitlab.New(conf.Conf.Property.Gitlab.API, conf.Conf.Property.Gitlab.Token)
  32. s.cmd = command.New(s.d, s.gitlab)
  33. s.cmd.Registers()
  34. go s.cmd.ListenTask()
  35. s.loadRepos(false)
  36. go s.updateproc()
  37. // start cron
  38. s.cron = cron.New()
  39. if err := s.cron.AddFunc(conf.Conf.Property.SyncContact.CheckCron, s.synccontactsproc); err != nil {
  40. panic(err)
  41. }
  42. s.cron.Start()
  43. //
  44. return
  45. }
  46. func (s *Service) validTargetBranch(targetBranch string, gitRepo *model.Repo) bool {
  47. for _, r := range gitRepo.Config.TargetBranchRegexes {
  48. if r.MatchString(targetBranch) {
  49. return true
  50. }
  51. }
  52. /*for _, r := range gitRepo.Config.TargetBranches {
  53. if r == targetBranch {
  54. return true
  55. }
  56. }*/
  57. return false
  58. }
  59. func (s *Service) loadRepos(reload bool) {
  60. var (
  61. repo *model.Repo
  62. ok bool
  63. )
  64. // init code repo
  65. if s.gitRepoMap == nil {
  66. s.gitRepoMap = make(map[string]*model.Repo)
  67. }
  68. webHookRepos := make([]*model.Repo, 0)
  69. authRepos := make([]*model.Repo, 0)
  70. for _, r := range conf.Conf.Property.Repos {
  71. if repo, ok = s.gitRepoMap[r.GName]; !ok {
  72. repo = &model.Repo{
  73. Config: r,
  74. }
  75. s.gitRepoMap[r.GName] = repo
  76. webHookRepos = append(webHookRepos, repo)
  77. authRepos = append(authRepos, repo)
  78. } else {
  79. if repo.AuthUpdate(r) {
  80. authRepos = append(authRepos, repo)
  81. }
  82. if repo.WebHookUpdate(r) {
  83. webHookRepos = append(webHookRepos, repo)
  84. }
  85. if repo.Update(r) {
  86. s.gitRepoMap[r.GName] = repo
  87. }
  88. }
  89. }
  90. if reload {
  91. s.BuildContributors(authRepos)
  92. }
  93. if err := s.gitlab.AuditProjects(webHookRepos, conf.Conf.Property.WebHooks); err != nil {
  94. log.Error("loadRepos err (%+v)", err)
  95. }
  96. }
  97. func (s *Service) findRepo(gitURL string) (ok bool, repo *model.Repo) {
  98. for _, r := range conf.Conf.Property.Repos {
  99. if strings.EqualFold(r.URL, gitURL) {
  100. repo = &model.Repo{
  101. Config: r,
  102. }
  103. ok = true
  104. return
  105. }
  106. }
  107. return
  108. }
  109. // Ping check dao health.
  110. func (s *Service) Ping(c context.Context) (err error) {
  111. return s.d.Ping(c)
  112. }
  113. // Wait wait all closed.
  114. func (s *Service) Wait() {
  115. }
  116. // Close close all dao.
  117. func (s *Service) Close() {
  118. s.d.Close()
  119. }
  120. func (s *Service) updateproc() {
  121. defer func() {
  122. if x := recover(); x != nil {
  123. log.Error("updateproc panic(%v)", errors.WithStack(fmt.Errorf("%v", x)))
  124. go s.updateproc()
  125. log.Info("updateproc recover")
  126. }
  127. }()
  128. for range conf.ReloadEvents() {
  129. log.Info("DirService reload")
  130. s.loadRepos(true)
  131. }
  132. }