mr.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/tool/saga/model"
  6. "go-common/library/log"
  7. )
  8. // MergeRequest ...
  9. func (s *Service) MergeRequest(c context.Context, event *model.HookMR) (err error) {
  10. var (
  11. ok bool
  12. repo *model.Repo
  13. url = event.Project.GitSSHURL
  14. projID = event.Project.ID
  15. mrIID = int(event.ObjectAttributes.IID)
  16. sourceBranch = event.ObjectAttributes.SourceBranch
  17. targetBranch = event.ObjectAttributes.TargetBranch
  18. wip = event.ObjectAttributes.WorkInProgress
  19. )
  20. log.Info("Hook MergeRequest state: %s, projID: %d, mrid: %d", event.ObjectAttributes.State, projID, mrIID)
  21. if ok, repo = s.findRepo(url); !ok {
  22. return
  23. }
  24. if event.ObjectAttributes.State == model.MRStateOpened {
  25. if wip {
  26. log.Info("MergeRequest mr is wip, project ID: [%d], branch: [%s]", event.Project.ID, sourceBranch)
  27. return
  28. }
  29. if !s.validTargetBranch(targetBranch, repo) {
  30. s.gitlab.CreateMRNote(projID, mrIID, fmt.Sprintf("<pre>警告:目标分支 %s 不在SAGA白名单中,此MR不会触发SAGA行为!</pre>", targetBranch))
  31. return
  32. }
  33. authBranch := s.cmd.GetAuthBranch(targetBranch, repo.Config.AuthBranches)
  34. if err = s.ShowMRRoleInfo(c, authBranch, repo, event); err != nil {
  35. return
  36. }
  37. } else {
  38. if event.ObjectAttributes.State == model.MRStateMerged {
  39. log.Info("MergeRequest.UpdateContributor:%d,%s,%s,%+v", projID, sourceBranch, targetBranch, repo.Config.AuthBranches)
  40. if err = s.cmd.UpdateContributor(projID, mrIID, sourceBranch, targetBranch, repo.Config.AuthBranches); err != nil {
  41. return
  42. }
  43. }
  44. if err = s.d.DeleteReportStatus(c, projID, mrIID); err != nil {
  45. return
  46. }
  47. }
  48. return
  49. }
  50. // ShowMRRoleInfo ...
  51. func (s *Service) ShowMRRoleInfo(c context.Context, authBranch string, repo *model.Repo, event *model.HookMR) (err error) {
  52. var (
  53. projID = event.Project.ID
  54. mrIID = int(event.ObjectAttributes.IID)
  55. result bool
  56. )
  57. if result, err = s.d.ReportStatus(c, projID, mrIID); err != nil {
  58. return
  59. }
  60. log.Info("MergeRequest whether create note auth info: %v", result)
  61. if !result {
  62. if len(repo.Config.SuperAuthUsers) > 0 {
  63. if err = s.ReportSuperRoleInfo(c, repo.Config.SuperAuthUsers, event); err != nil {
  64. return
  65. }
  66. } else {
  67. if err = s.ReportMRRoleInfo(c, authBranch, event); err != nil {
  68. return
  69. }
  70. }
  71. if err = s.d.SetReportStatus(c, projID, mrIID, true); err != nil {
  72. return
  73. }
  74. }
  75. return
  76. }
  77. // ReportMRRoleInfo ...
  78. func (s *Service) ReportMRRoleInfo(c context.Context, authBranch string, event *model.HookMR) (err error) {
  79. var (
  80. projID = event.Project.ID
  81. mrIID = int(event.ObjectAttributes.IID)
  82. pathOwners []model.RequireReviewFolder
  83. )
  84. if pathOwners, err = s.cmd.GetAllPathAuth(projID, mrIID, authBranch); err != nil {
  85. return
  86. }
  87. authInfo := ""
  88. authInfo = fmt.Sprintf("<pre>SAGA权限信息提示,请review: %s</pre>", event.ObjectAttributes.Title)
  89. authInfo += "\n"
  90. for _, os := range pathOwners {
  91. if len(os.Owners) > 0 {
  92. //authInfo += "----- PATH: " + os.Folder + ";OWNER: "
  93. authInfo += fmt.Sprintf("+ PATH: %s;OWNER: ", os.Folder)
  94. for _, o := range os.Owners {
  95. if o == "all" {
  96. authInfo += "所有人" + " 或 "
  97. } else {
  98. authInfo += "@" + o + " 或 "
  99. }
  100. }
  101. authInfo = authInfo[:len(authInfo)-len(" 或 ")]
  102. }
  103. if len(os.Reviewers) > 0 {
  104. authInfo += ";REVIEWER: "
  105. for _, o := range os.Reviewers {
  106. if o == "all" {
  107. authInfo += "所有人" + " 与 "
  108. } else {
  109. authInfo += "@" + o + " 与 "
  110. }
  111. }
  112. authInfo = authInfo[:len(authInfo)-len(" 与 ")]
  113. }
  114. authInfo += "\n\n"
  115. }
  116. if _, err = s.gitlab.CreateMRNote(projID, mrIID, authInfo); err != nil {
  117. return
  118. }
  119. return
  120. }
  121. // ReportSuperRoleInfo ...
  122. func (s *Service) ReportSuperRoleInfo(c context.Context, superUsers []string, event *model.HookMR) (err error) {
  123. var (
  124. projID = event.Project.ID
  125. mrIID = int(event.ObjectAttributes.IID)
  126. )
  127. authInfo := fmt.Sprintf("<pre>SAGA权限信息提示,已配置超级权限用户,请review: %s</pre>", event.ObjectAttributes.Title)
  128. authInfo += "\n"
  129. authInfo += fmt.Sprintf("+ SUPERMAN: ")
  130. for _, user := range superUsers {
  131. authInfo += "@" + user + " 或 "
  132. }
  133. authInfo = authInfo[:len(authInfo)-len(" 或 ")]
  134. if _, err = s.gitlab.CreateMRNote(projID, mrIID, authInfo); err != nil {
  135. return
  136. }
  137. return
  138. }