commit.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/ep/saga/model"
  9. "go-common/app/admin/ep/saga/service/utils"
  10. "go-common/library/log"
  11. "github.com/xanzy/go-gitlab"
  12. )
  13. // QueryProjectCommit query project commit info according to project id.
  14. func (s *Service) QueryProjectCommit(c context.Context, req *model.ProjectDataReq) (resp *model.ProjectDataResp, err error) {
  15. if resp, err = s.QueryProject(c, "commit", req); err != nil {
  16. return
  17. }
  18. return
  19. }
  20. // QueryTeamCommit query team commit info according to department and business
  21. func (s *Service) QueryTeamCommit(c context.Context, req *model.TeamDataRequest) (resp *model.TeamDataResp, err error) {
  22. if resp, err = s.QueryTeam(c, "commit", req); err != nil {
  23. return
  24. }
  25. return
  26. }
  27. // QueryCommit query commit info according to department、 business and time.
  28. func (s *Service) QueryCommit(c context.Context, req *model.CommitRequest) (resp *model.CommitResp, err error) {
  29. var (
  30. layout = "2006-01-02"
  31. projectInfo []*model.ProjectInfo
  32. reqProject = &model.ProjectInfoRequest{}
  33. ProjectCommit []*model.ProjectCommit
  34. respCommit *gitlab.Response
  35. since time.Time
  36. until time.Time
  37. commitNum int
  38. )
  39. if len(req.Department) <= 0 && len(req.Business) <= 0 {
  40. log.Warn("query department and business are empty!")
  41. return
  42. }
  43. reqProject.Department = req.Department
  44. reqProject.Business = req.Business
  45. reqProject.Username = req.Username
  46. if _, projectInfo, err = s.dao.QueryProjectInfo(false, reqProject); err != nil {
  47. return
  48. }
  49. if len(projectInfo) <= 0 {
  50. log.Warn("Found no project!")
  51. return
  52. }
  53. //since, err = time.Parse("2006-01-02 15:04:05", "2018-08-13 00:00:00")
  54. if since, err = time.ParseInLocation(layout, req.Since, time.Local); err != nil {
  55. return
  56. }
  57. if until, err = time.ParseInLocation(layout, req.Until, time.Local); err != nil {
  58. return
  59. }
  60. log.Info("query commit start!")
  61. for _, project := range projectInfo {
  62. if _, respCommit, err = s.gitlab.ListProjectCommit(project.ProjectID, 1, &since, &until); err != nil {
  63. return
  64. }
  65. //log.Info("query: %s, result: %+v", project.Name, respCommit)
  66. CommitPer := &model.ProjectCommit{
  67. ProjectID: project.ProjectID,
  68. Name: project.Name,
  69. CommitNum: respCommit.TotalItems,
  70. }
  71. ProjectCommit = append(ProjectCommit, CommitPer)
  72. commitNum = commitNum + respCommit.TotalItems
  73. }
  74. log.Info("query commit end!")
  75. resp = &model.CommitResp{
  76. Total: commitNum,
  77. ProjectCommit: ProjectCommit,
  78. }
  79. return
  80. }
  81. /*-------------------------------------- sync commit ----------------------------------------*/
  82. // SyncProjectCommit ...
  83. func (s *Service) SyncProjectCommit(projectID int) (result *model.SyncResult, err error) {
  84. var (
  85. //syncAllTime = conf.Conf.Property.SyncData.SyncAllTime
  86. syncAllTime = false
  87. commits []*gitlab.Commit
  88. resp *gitlab.Response
  89. since *time.Time
  90. until *time.Time
  91. projectInfo *model.ProjectInfo
  92. )
  93. result = &model.SyncResult{}
  94. if projectInfo, err = s.dao.ProjectInfoByID(projectID); err != nil {
  95. return
  96. }
  97. if !syncAllTime {
  98. since, until = utils.CalSyncTime()
  99. }
  100. log.Info("sync project(%d) commit time since: %v, until: %v", projectID, since, until)
  101. for page := 1; ; page++ {
  102. result.TotalPage++
  103. if commits, resp, err = s.gitlab.ListProjectCommit(projectID, page, since, until); err != nil {
  104. return
  105. }
  106. for _, commit := range commits {
  107. var (
  108. statsAdditions int
  109. statsDeletions int
  110. parentIDs string
  111. commitStatus string
  112. )
  113. if commit.Stats != nil {
  114. statsAdditions = commit.Stats.Additions
  115. statsDeletions = commit.Stats.Deletions
  116. }
  117. if commit.Status != nil {
  118. commitStatusByte, _ := json.Marshal(commit.Status)
  119. commitStatus = string(commitStatusByte)
  120. }
  121. parentIDsByte, _ := json.Marshal(commit.ParentIDs)
  122. parentIDs = string(parentIDsByte)
  123. commitDB := &model.StatisticsCommits{
  124. CommitID: commit.ID,
  125. ProjectID: projectID,
  126. ProjectName: projectInfo.Name,
  127. ShortID: commit.ShortID,
  128. Title: commit.Title,
  129. AuthorName: commit.AuthorName,
  130. AuthoredDate: commit.AuthoredDate,
  131. CommitterName: commit.CommitterName,
  132. CommittedDate: commit.CommittedDate,
  133. CreatedAt: commit.CreatedAt,
  134. Message: commit.Message,
  135. ParentIDs: parentIDs,
  136. StatsAdditions: statsAdditions,
  137. StatsDeletions: statsDeletions,
  138. Status: commitStatus,
  139. }
  140. if len(commitDB.Message) > model.MessageMaxLen {
  141. commitDB.Message = commitDB.Message[0 : model.MessageMaxLen-1]
  142. }
  143. if err = s.SaveDatabaseCommit(commitDB); err != nil {
  144. log.Error("Commit Save Database err: projectID(%d), commitID(%s)", projectID, commit.ID)
  145. err = nil
  146. errData := &model.FailData{
  147. ChildIDStr: commit.ID,
  148. }
  149. result.FailData = append(result.FailData, errData)
  150. continue
  151. }
  152. result.TotalNum++
  153. }
  154. if resp.NextPage == 0 {
  155. break
  156. }
  157. }
  158. return
  159. }
  160. // SaveDatabaseCommit ...
  161. func (s *Service) SaveDatabaseCommit(commitDB *model.StatisticsCommits) (err error) {
  162. var total int
  163. if total, err = s.dao.HasCommit(commitDB.ProjectID, commitDB.CommitID); err != nil {
  164. log.Error("SaveDatabaseCommit HasCommit(%+v)", err)
  165. return
  166. }
  167. // found only one, so update
  168. if total == 1 {
  169. if err = s.dao.UpdateCommit(commitDB.ProjectID, commitDB.CommitID, commitDB); err != nil {
  170. if strings.Contains(err.Error(), model.DatabaseErrorText) {
  171. commitDB.Title = strconv.QuoteToASCII(commitDB.Title)
  172. commitDB.Message = strconv.QuoteToASCII(commitDB.Message)
  173. commitDB.Title = utils.Unicode2Chinese(commitDB.Title)
  174. commitDB.Message = utils.Unicode2Chinese(commitDB.Message)
  175. }
  176. if err = s.dao.UpdateCommit(commitDB.ProjectID, commitDB.CommitID, commitDB); err != nil {
  177. log.Error("SaveDatabaseCommit UpdateCommit err(%+v)", err)
  178. return
  179. }
  180. }
  181. return
  182. } else if total > 1 {
  183. // found repeated row, this situation will not exist under normal
  184. log.Warn("SaveDatabaseCommit commit has more rows(%d)", total)
  185. return
  186. }
  187. // insert row now
  188. if err = s.dao.CreateCommit(commitDB); err != nil {
  189. if strings.Contains(err.Error(), model.DatabaseErrorText) {
  190. commitDB.Title = strconv.QuoteToASCII(commitDB.Title)
  191. commitDB.Message = strconv.QuoteToASCII(commitDB.Message)
  192. commitDB.Title = utils.Unicode2Chinese(commitDB.Title)
  193. commitDB.Message = utils.Unicode2Chinese(commitDB.Message)
  194. }
  195. if err = s.dao.CreateCommit(commitDB); err != nil {
  196. log.Error("SaveDatabaseCommit CreateCommit err(%+v)", err)
  197. return
  198. }
  199. }
  200. return
  201. }