incident_notifier.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package sendgrid
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/porter-dev/porter/api/types"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/internal/notifier"
  8. "github.com/sendgrid/sendgrid-go"
  9. "github.com/sendgrid/sendgrid-go/helpers/mail"
  10. )
  11. type IncidentNotifier struct {
  12. opts *IncidentNotifierOpts
  13. }
  14. type IncidentNotifierOpts struct {
  15. *SharedOpts
  16. IncidentAlertTemplateID string
  17. IncidentResolvedTemplateID string
  18. Users []*models.User
  19. }
  20. func NewIncidentNotifier(opts *IncidentNotifierOpts) notifier.IncidentNotifier {
  21. return &IncidentNotifier{opts}
  22. }
  23. func (s *IncidentNotifier) NotifyNew(incident *types.Incident, url string) error {
  24. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  25. request.Method = "POST"
  26. personalizations := make([]*mail.Personalization, 0)
  27. resourceKind := "application"
  28. if strings.ToLower(string(incident.InvolvedObjectKind)) == "job" {
  29. resourceKind = "job"
  30. }
  31. templData := map[string]interface{}{
  32. "incident_text": incident.Summary,
  33. "app_url": url,
  34. "subject": fmt.Sprintf("Your %s %s crashed on Porter", resourceKind, incident.ReleaseName),
  35. "preheader": incident.Summary,
  36. "created_at": fmt.Sprintf("%s", incident.CreatedAt.Format("Jan 2, 2006 at 3:04pm (MST)")),
  37. }
  38. for _, user := range s.opts.Users {
  39. personalizations = append(personalizations, &mail.Personalization{
  40. To: []*mail.Email{
  41. {
  42. Address: user.Email,
  43. },
  44. },
  45. DynamicTemplateData: templData,
  46. })
  47. }
  48. sgMail := &mail.SGMailV3{
  49. Personalizations: personalizations,
  50. From: &mail.Email{
  51. Address: s.opts.SenderEmail,
  52. Name: "Porter Notifications",
  53. },
  54. TemplateID: s.opts.IncidentAlertTemplateID,
  55. }
  56. request.Body = mail.GetRequestBody(sgMail)
  57. _, err := sendgrid.API(request)
  58. return err
  59. }
  60. func (s *IncidentNotifier) NotifyResolved(incident *types.Incident, url string) error {
  61. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  62. request.Method = "POST"
  63. personalizations := make([]*mail.Personalization, 0)
  64. templData := map[string]interface{}{
  65. "incident_resolved_text": fmt.Sprintf("[Resolved] The incident for application %s has been resolved. The incident text was:\n\n:%s", incident.ReleaseName, incident.Summary),
  66. "app_url": url,
  67. "subject": fmt.Sprintf("[Resolved] The incident for application %s has been resolved", incident.ReleaseName),
  68. "preheader": incident.Summary,
  69. "resolved_at": fmt.Sprintf("%s", incident.UpdatedAt.Format("Jan 2, 2006 at 3:04pm (MST)")),
  70. }
  71. for _, user := range s.opts.Users {
  72. personalizations = append(personalizations, &mail.Personalization{
  73. To: []*mail.Email{
  74. {
  75. Address: user.Email,
  76. },
  77. },
  78. DynamicTemplateData: templData,
  79. })
  80. }
  81. sgMail := &mail.SGMailV3{
  82. Personalizations: personalizations,
  83. From: &mail.Email{
  84. Address: s.opts.SenderEmail,
  85. Name: "Porter Notifications",
  86. },
  87. TemplateID: s.opts.IncidentResolvedTemplateID,
  88. }
  89. request.Body = mail.GetRequestBody(sgMail)
  90. _, err := sendgrid.API(request)
  91. return err
  92. }