collect_project.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "go-common/app/admin/ep/saga/conf"
  7. "go-common/app/admin/ep/saga/model"
  8. "go-common/library/log"
  9. "github.com/xanzy/go-gitlab"
  10. )
  11. // collectprojectproc cron func
  12. func (s *Service) collectprojectproc() {
  13. /*defer func() {
  14. if x := recover(); x != nil {
  15. log.Error("collectprojectproc panic(%v)", errors.WithStack(fmt.Errorf("%v", x)))
  16. go s.collectprojectproc()
  17. log.Info("collectprojectproc recover")
  18. }
  19. }()*/
  20. var err error
  21. if err = s.CollectProject(context.TODO()); err != nil {
  22. log.Error("s.CollectProject err (%+v)", err)
  23. }
  24. }
  25. // CollectProject collect project information
  26. func (s *Service) CollectProject(c context.Context) (err error) {
  27. var (
  28. projects []*gitlab.Project
  29. total = 0
  30. page = 1
  31. )
  32. log.Info("Collect Project start")
  33. for page <= 1000 {
  34. if projects, err = s.gitlab.ListProjects(page); err != nil {
  35. return
  36. }
  37. num := len(projects)
  38. if num <= 0 {
  39. break
  40. }
  41. total = total + num
  42. for _, p := range projects {
  43. if err = s.insertDB(p); err != nil {
  44. return
  45. }
  46. }
  47. page = page + 1
  48. }
  49. log.Info("Collect Project end, find %d projects", total)
  50. return
  51. }
  52. // insertDB
  53. func (s *Service) insertDB(project *gitlab.Project) (err error) {
  54. var (
  55. b bool
  56. parseFail bool
  57. projectInfo = &model.ProjectInfo{
  58. ProjectID: project.ID,
  59. Name: project.Name,
  60. Description: project.Description,
  61. WebURL: project.WebURL,
  62. Repo: project.SSHURLToRepo,
  63. DefaultBranch: project.DefaultBranch,
  64. //Owner: project.Owner.Name,
  65. SpaceName: project.Namespace.Name,
  66. SpaceKind: project.Namespace.Kind,
  67. Saga: false,
  68. Runner: false,
  69. Department: "",
  70. Business: "",
  71. Language: "",
  72. }
  73. )
  74. if project.Namespace.Kind == "user" {
  75. return
  76. }
  77. if b, err = s.dao.HasProjectInfo(project.ID); err != nil {
  78. return
  79. }
  80. if len(project.Description) > 6 {
  81. projectInfo.Department, projectInfo.Business, projectInfo.Language, parseFail = parseDes(project.Description)
  82. }
  83. if parseFail {
  84. projectInfo.Department, projectInfo.Business = parseGroup(project.Namespace.Name)
  85. }
  86. if b {
  87. /*if err = s.update(project.ID, projectInfo); err != nil {
  88. return
  89. }*/
  90. if err = s.dao.UpdateProjectInfo(project.ID, projectInfo); err != nil {
  91. log.Warn("UpdateProjectInfo ProjectID(%d), Description: (%s)", projectInfo.ProjectID, projectInfo.Description)
  92. if strings.Contains(err.Error(), "Incorrect string value") {
  93. projectInfo.Description = strconv.QuoteToASCII(projectInfo.Description)
  94. }
  95. if err = s.dao.UpdateProjectInfo(project.ID, projectInfo); err != nil {
  96. return
  97. }
  98. }
  99. } else {
  100. if err = s.dao.AddProjectInfo(projectInfo); err != nil {
  101. log.Warn("AddProjectInfo ProjectID(%d), Description: (%s)", projectInfo.ProjectID, projectInfo.Description)
  102. if strings.Contains(err.Error(), "Incorrect string value") {
  103. projectInfo.Description = strconv.QuoteToASCII(projectInfo.Description)
  104. }
  105. if err = s.dao.AddProjectInfo(projectInfo); err != nil {
  106. return
  107. }
  108. }
  109. }
  110. return
  111. }
  112. // update database
  113. /*func (s *Service) update(projectID int, projectSrc *model.ProjectInfo) (err error) {
  114. var (
  115. projectDes *model.ProjectInfo
  116. )
  117. if projectDes, err = s.dao.ProjectInfoByID(projectID); err != nil {
  118. return
  119. }
  120. if *projectSrc == *projectDes {
  121. return
  122. }
  123. s.dao.UpdateProjectInfo(projectID, projectSrc)
  124. return
  125. }*/
  126. // parseDes get info from project description
  127. func parseDes(s string) (department, business, language string, parseFail bool) {
  128. //[主站 android java]
  129. ids := strings.Index(s, "[")
  130. idx := strings.LastIndex(s, "]")
  131. if ids == -1 || idx == -1 {
  132. parseFail = true
  133. return
  134. }
  135. str := s[ids+1 : idx]
  136. fields := strings.Fields(str)
  137. if len(fields) < 3 {
  138. parseFail = true
  139. return
  140. }
  141. department = fields[0]
  142. business = fields[1]
  143. language = fields[2]
  144. for _, de := range conf.Conf.Property.DeInfo {
  145. if department == de.Label {
  146. department = de.Value
  147. }
  148. }
  149. for _, bu := range conf.Conf.Property.BuInfo {
  150. if business == bu.Label {
  151. business = bu.Value
  152. }
  153. }
  154. return
  155. }
  156. func parseGroup(s string) (department, business string) {
  157. group := strings.Fields(conf.Conf.Property.Group.Name)
  158. de := strings.Fields(conf.Conf.Property.Group.Department)
  159. bu := strings.Fields(conf.Conf.Property.Group.Business)
  160. for i := 0; i < len(group); i++ {
  161. if s == group[i] {
  162. department, business = de[i], bu[i]
  163. }
  164. }
  165. return
  166. }