main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "strings"
  6. "go-common/app/tool/ci/lib/mail"
  7. "go-common/library/log"
  8. "github.com/BurntSushi/toml"
  9. )
  10. type sendInfo struct {
  11. SenderName string
  12. SendTitle string
  13. SendContent string
  14. ExtraData string
  15. }
  16. type receiverInfo struct {
  17. ReceiverName []string
  18. }
  19. type config struct {
  20. Title string
  21. SendInfo sendInfo
  22. ReceiverInfo receiverInfo
  23. }
  24. func mailToml(cPath string) (conf config, err error) {
  25. if _, err = toml.DecodeFile(cPath, &conf); err != nil {
  26. log.Error("Error(%v)", err)
  27. }
  28. return
  29. }
  30. func main() {
  31. var (
  32. filePath string
  33. ciSendTo string
  34. pipeStatus string
  35. eConf config
  36. sendTo []string
  37. sendContent string
  38. extraData string
  39. err error
  40. ciProjectURL = os.Getenv("CI_PROJECT_URL")
  41. ciPipelineId = os.Getenv("CI_PIPELINE_ID")
  42. ciUserEmail = os.Getenv("GITLAB_USER_EMAIL")
  43. sourceBranch = os.Getenv("CI_COMMIT_REF_NAME")
  44. )
  45. //log init
  46. logConfig := &log.Config{
  47. Stdout: true,
  48. }
  49. log.Init(logConfig)
  50. //pipeline url
  51. if ciProjectURL == "" {
  52. log.Warn("Error: Not CI_PROJECT_URL")
  53. }
  54. pipelineURL := ciProjectURL + "/pipelines/" + ciPipelineId
  55. log.Info("url: %v", pipelineURL)
  56. //send email data from config files
  57. flag.StringVar(&filePath, "configPath", "", "config path, eg: /data/gitlab/email.toml")
  58. flag.StringVar(&ciSendTo, "sendTo", "", "send to email, eg: jiangkai@bilibili.com,tangyongqiang@bilibili.com")
  59. flag.StringVar(&pipeStatus, "pipeStatus", "failed", "pipeStatus, only failed or success")
  60. flag.StringVar(&extraData, "extraData", "", "email contents")
  61. flag.Parse()
  62. if ciSendTo != "" {
  63. matchList := strings.Split(ciSendTo, ",")
  64. sendTo = matchList
  65. } else {
  66. if filePath != "" {
  67. if eConf, err = mailToml(filePath); err != nil {
  68. log.Warn("Warn(%v)", err)
  69. }
  70. sendTo = eConf.ReceiverInfo.ReceiverName
  71. sendContent = eConf.SendInfo.SendContent
  72. extraData = eConf.SendInfo.ExtraData
  73. } else {
  74. sendTo = []string{ciUserEmail}
  75. }
  76. }
  77. // delete saga send mail
  78. if strings.Contains(ciUserEmail, "zzjs") {
  79. log.Info("Saga exc pipeline")
  80. } else {
  81. sendmail.SendMail(sendTo, pipelineURL, sendContent, sourceBranch, extraData, pipeStatus)
  82. }
  83. }