incident_notifier.go 3.0 KB

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