review.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/tool/saga/model"
  7. "go-common/app/tool/saga/service/notification"
  8. "go-common/library/log"
  9. )
  10. func (c *Command) runPlusOne(ctx context.Context, event *model.HookComment, repo *model.Repo) (err error) {
  11. var (
  12. author string
  13. url = event.ObjectAttributes.URL
  14. commit = event.MergeRequest.LastCommit.ID
  15. reviewer = event.User.UserName
  16. authorID = int(event.MergeRequest.AuthorID)
  17. sourceBranch = event.MergeRequest.SourceBranch
  18. targetBranch = event.MergeRequest.TargetBranch
  19. wip = event.MergeRequest.WorkInProgress
  20. )
  21. log.Info("runPlusOne start ...")
  22. if wip {
  23. c.gitlab.CreateMRNote(event.Project.ID, int(event.MergeRequest.IID), fmt.Sprintf("<pre>警告:当前MR处于WIP状态,请待开发结束后再review!</pre>"))
  24. return
  25. }
  26. if author, err = c.gitlab.UserName(authorID); err != nil {
  27. log.Error("%+v", err)
  28. return
  29. }
  30. log.Info("runPlusOne notification author: %s", author)
  31. if author != "" {
  32. go func() {
  33. notification.MailPlusOne(author, reviewer, url, commit, sourceBranch, targetBranch)
  34. }()
  35. go func() {
  36. notification.WechatPlusOne(c.dao, author, reviewer, url, commit, sourceBranch, targetBranch)
  37. }()
  38. }
  39. return
  40. }
  41. func reviewedOwner(owners []string, reviewedUsers []string, username string) (isowner bool, reviewed bool) {
  42. for _, owner := range owners {
  43. if strings.EqualFold(owner, username) || strings.EqualFold(owner, "all") {
  44. return true, true
  45. }
  46. }
  47. for _, owner := range owners {
  48. for _, user := range reviewedUsers {
  49. if strings.EqualFold(user, owner) {
  50. return false, true
  51. }
  52. }
  53. }
  54. return false, false
  55. }
  56. func (c *Command) reviewedUsers(projID int, mrIID int) (reviewedUsers []string, err error) {
  57. var awardUsers []string
  58. if reviewedUsers, err = c.gitlab.PlusUsernames(projID, mrIID); err != nil {
  59. return
  60. }
  61. if awardUsers, err = c.gitlab.AwardEmojiUsernames(projID, mrIID); err != nil {
  62. return
  63. }
  64. OUTER:
  65. for _, au := range awardUsers {
  66. for _, mu := range reviewedUsers {
  67. if au == mu {
  68. continue OUTER
  69. }
  70. }
  71. reviewedUsers = append(reviewedUsers, au)
  72. }
  73. return
  74. }
  75. func reviewedNum(reviewers []string, reviewedUsers []string) (num int) {
  76. for _, reviewer := range reviewers {
  77. for _, user := range reviewedUsers {
  78. if strings.EqualFold(user, reviewer) || strings.EqualFold(reviewer, "all") {
  79. num++
  80. }
  81. }
  82. }
  83. return
  84. }