notification.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package notification
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime/debug"
  6. "strconv"
  7. "strings"
  8. "go-common/app/tool/saga/dao"
  9. "go-common/app/tool/saga/model"
  10. "go-common/app/tool/saga/service/mail"
  11. "go-common/app/tool/saga/service/wechat"
  12. "go-common/library/log"
  13. )
  14. // WechatAuthor send wechat message to original author
  15. func WechatAuthor(dao *dao.Dao, authorName string, url, sourceBranch, targetBranch string, comment string) (err error) {
  16. defer func() {
  17. if x := recover(); x != nil {
  18. log.Error("wechatAuthor: %+v %s", x, debug.Stack())
  19. }
  20. }()
  21. var (
  22. subject = fmt.Sprintf("[SAGA] MR ( %s ) merge 通知", sourceBranch)
  23. data = fmt.Sprintf("MR : %s \n ( %s -> %s ) 状态 %s", url, sourceBranch, targetBranch, comment)
  24. wct = wechat.New(dao)
  25. ctx = context.Background()
  26. )
  27. return wct.PushMsg(ctx, []string{authorName}, fmt.Sprintf("%s\n\n%s", subject, data))
  28. }
  29. // MailPipeline ...
  30. func MailPipeline(event *model.HookPipeline) (err error) {
  31. var (
  32. author = event.User.UserName
  33. branch = event.ObjectAttributes.Ref
  34. commit = event.ObjectAttributes.Sha
  35. title = ""
  36. url = ""
  37. status = "失败"
  38. )
  39. if event.Commit != nil {
  40. commitIndex := strings.LastIndex(event.Commit.URL, "commit")
  41. url = event.Commit.URL[:commitIndex] + "pipelines/" + strconv.FormatInt(event.ObjectAttributes.ID, 10)
  42. }
  43. if strings.Contains(event.Commit.Message, "\n") {
  44. title = event.Commit.Message[:strings.Index(event.Commit.Message, "\n")]
  45. } else {
  46. title = event.Commit.Message
  47. }
  48. if event.ObjectAttributes.Status == model.PipelineSuccess {
  49. status = "成功"
  50. }
  51. subject := fmt.Sprintf("[SAGA] Pipeline ( %s ) %s 通知", branch, status)
  52. data := fmt.Sprintf("Pipeline : %s \nCommit : %s\nCommitID : %s\n状态: 运行%s !", url, title, commit, status)
  53. addr := &model.MailAddress{
  54. Address: author + "@bilibili.com",
  55. Name: author,
  56. }
  57. if err = mail.SendMail2(addr, subject, data); err != nil {
  58. log.Error("%+v", err)
  59. }
  60. return
  61. }
  62. // WechatPipeline ...
  63. func WechatPipeline(dao *dao.Dao, event *model.HookPipeline) (err error) {
  64. var (
  65. wct = wechat.New(dao)
  66. ctx = context.Background()
  67. author = event.User.UserName
  68. branch = event.ObjectAttributes.Ref
  69. commit = event.ObjectAttributes.Sha
  70. title = ""
  71. url = ""
  72. status = "失败"
  73. )
  74. if event.Commit != nil {
  75. commitIndex := strings.LastIndex(event.Commit.URL, "commit")
  76. url = event.Commit.URL[:commitIndex] + "pipelines/" + strconv.FormatInt(event.ObjectAttributes.ID, 10)
  77. }
  78. if strings.Contains(event.Commit.Message, "\n") {
  79. title = event.Commit.Message[:strings.Index(event.Commit.Message, "\n")]
  80. } else {
  81. title = event.Commit.Message
  82. }
  83. if event.ObjectAttributes.Status == model.PipelineSuccess {
  84. status = "成功"
  85. }
  86. subject := fmt.Sprintf("[SAGA] Pipeline ( %s ) %s 通知", branch, status)
  87. data := fmt.Sprintf("Pipeline : %s \nCommit : %s\nCommitID : %s\n状态: 运行%s !", url, title, commit, status)
  88. if err = wct.PushMsg(ctx, []string{author}, fmt.Sprintf("%s\n\n%s", subject, data)); err != nil {
  89. log.Error("%+v", err)
  90. }
  91. return
  92. }
  93. // MailPlusOne ...
  94. func MailPlusOne(author, reviewer, url, commit, sourceBranch, targetBranch string) (err error) {
  95. var (
  96. subject = fmt.Sprintf("[SAGA] MR ( %s ) review 通知", sourceBranch)
  97. data = fmt.Sprintf("MR : %s \n ( %s -> %s ) commitID ( %s ) 已被 ( %s ) review!", url, sourceBranch, targetBranch, commit, reviewer)
  98. addr = &model.MailAddress{
  99. Address: author + "@bilibili.com",
  100. Name: author,
  101. }
  102. )
  103. if err = mail.SendMail2(addr, subject, data); err != nil {
  104. log.Error("%+v", err)
  105. }
  106. return
  107. }
  108. // WechatPlusOne ...
  109. func WechatPlusOne(dao *dao.Dao, author, reviewer, url, commit, sourceBranch, targetBranch string) (err error) {
  110. defer func() {
  111. if x := recover(); x != nil {
  112. log.Error("wechatPlusOne: %+v %s", x, debug.Stack())
  113. }
  114. }()
  115. var (
  116. subject = fmt.Sprintf("[SAGA] MR ( %s ) review 通知", sourceBranch)
  117. data = fmt.Sprintf("MR : %s \n ( %s -> %s ) commitID ( %s ) 已被 ( %s ) review!", url, sourceBranch, targetBranch, commit, reviewer)
  118. wct = wechat.New(dao)
  119. ctx = context.Background()
  120. )
  121. if err = wct.PushMsg(ctx, []string{author}, fmt.Sprintf("%s\n\n%s", subject, data)); err != nil {
  122. log.Error("%+v", err)
  123. }
  124. return
  125. }
  126. // func notifyAssign(assign string, authorName string, url, sourceBranch, targetBranch string) (err error) {
  127. // var (
  128. // subject = fmt.Sprintf("[SAGA] MR ( %s ) assign 通知", sourceBranch)
  129. // data = fmt.Sprintf("MR : %s \n ( %s -> %s ) 开发者 ( %s ) 已指派给您 !", url, sourceBranch, targetBranch, authorName)
  130. // addr = &model.MailAddress{
  131. // Address: assign + "@bilibili.com",
  132. // Name: assign,
  133. // }
  134. // )
  135. // if err = mail.SendMail2(addr, subject, data); err != nil {
  136. // log.Error("%+v", err)
  137. // }
  138. // return
  139. // }
  140. // func wechatAssign(dao *dao.Dao, assign string, authorName string, url, sourceBranch, targetBranch string) (err error) {
  141. // var (
  142. // subject = fmt.Sprintf("[SAGA] MR ( %s ) assign 通知", sourceBranch)
  143. // data = fmt.Sprintf("MR : %s \n ( %s -> %s ) 开发者 ( %s ) 已指派给您 !", url, sourceBranch, targetBranch, authorName)
  144. // wct = wechat.New(dao)
  145. // ctx = context.Background()
  146. // )
  147. // if err = wct.PushMsg(ctx, []string{assign}, fmt.Sprintf("%s\n\n%s", subject, data)); err != nil {
  148. // log.Error("wechatAssign failed, err (%s)", err.Error())
  149. // }
  150. // return
  151. // }
  152. // func notifyReviewer(reviewers []string, authorName, url, sourceBranch, targetBranch string) (err error) {
  153. // var (
  154. // subject = fmt.Sprintf("[SAGA] MR ( %s ) review 请求", sourceBranch)
  155. // data = fmt.Sprintf("MR : %s \n ( %s -> %s ) 开发者 ( %s ) 需要您的 review !\n请在 review 后留言 +1 !", url, sourceBranch, targetBranch, authorName)
  156. // )
  157. // for _, reviewer := range reviewers {
  158. // addr := &model.MailAddress{
  159. // Address: reviewer + "@bilibili.com",
  160. // Name: reviewer,
  161. // }
  162. // if err = mail.SendMail2(addr, subject, data); err != nil {
  163. // log.Error("%+v", err)
  164. // }
  165. // }
  166. // return
  167. // }
  168. // func wechatReviewer(dao *dao.Dao, reviewers []string, authorName, url, sourceBranch, targetBranch string) (err error) {
  169. // var (
  170. // subject = fmt.Sprintf("[SAGA] MR ( %s ) review 请求", sourceBranch)
  171. // data = fmt.Sprintf("MR : %s \n ( %s -> %s ) 开发者 ( %s ) 需要您的 review !\n请在 review 后留言 +1 !", url, sourceBranch, targetBranch, authorName)
  172. // wct = wechat.New(dao)
  173. // ctx = context.Background()
  174. // )
  175. // if err = wct.PushMsg(ctx, reviewers, fmt.Sprintf("%s\n\n%s", subject, data)); err != nil {
  176. // log.Error("%+v", err)
  177. // }
  178. // return
  179. // }