notify_new_incident.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package cluster
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/notifier"
  15. "github.com/porter-dev/porter/internal/notifier/sendgrid"
  16. "github.com/porter-dev/porter/internal/notifier/slack"
  17. "github.com/porter-dev/porter/internal/repository"
  18. "gorm.io/gorm"
  19. )
  20. type NotifyNewIncidentHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. func NewNotifyNewIncidentHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *NotifyNewIncidentHandler {
  29. return &NotifyNewIncidentHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. func (c *NotifyNewIncidentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  36. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  37. if project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
  38. return
  39. }
  40. request := &types.Incident{}
  41. if ok := c.DecodeAndValidate(w, r, request); !ok {
  42. return
  43. }
  44. slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(cluster.ProjectID)
  45. rel, err := c.Repo().Release().ReadRelease(cluster.ID, request.ReleaseName, request.ReleaseNamespace)
  46. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. var notifConf *types.NotificationConfig
  51. if rel != nil && rel.NotificationConfig != 0 {
  52. conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
  53. if err != nil {
  54. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. notifConf = conf.ToNotificationConfigType()
  58. }
  59. users, err := getUsersByProjectID(c.Repo(), cluster.ProjectID)
  60. if err != nil {
  61. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  62. return
  63. }
  64. notifiers := make([]notifier.IncidentNotifier, 0)
  65. if c.Config().SlackConf != nil {
  66. notifiers = append(notifiers, slack.NewIncidentNotifier(slackInts...))
  67. }
  68. if sc := c.Config().ServerConf; sc.SendgridAPIKey != "" && sc.SendgridSenderEmail != "" && sc.SendgridIncidentAlertTemplateID != "" {
  69. notifiers = append(notifiers, sendgrid.NewIncidentNotifier(&sendgrid.IncidentNotifierOpts{
  70. SharedOpts: &sendgrid.SharedOpts{
  71. APIKey: c.Config().ServerConf.SendgridAPIKey,
  72. SenderEmail: c.Config().ServerConf.SendgridSenderEmail,
  73. },
  74. IncidentAlertTemplateID: sc.SendgridIncidentAlertTemplateID,
  75. Users: users,
  76. }))
  77. }
  78. multi := notifier.NewMultiIncidentNotifier(
  79. notifConf,
  80. notifiers...,
  81. )
  82. if !cluster.NotificationsDisabled {
  83. url := fmt.Sprintf(
  84. "%s/applications/%s/%s/%s?project_id=%d",
  85. c.Config().ServerConf.ServerURL,
  86. cluster.Name,
  87. request.ReleaseNamespace,
  88. request.ReleaseName,
  89. cluster.ProjectID,
  90. )
  91. if strings.ToLower(string(request.InvolvedObjectKind)) == "job" {
  92. url = fmt.Sprintf(
  93. "%s/jobs/%s/%s/%s?project_id=%d&job=%s",
  94. c.Config().ServerConf.ServerURL,
  95. cluster.Name,
  96. request.ReleaseNamespace,
  97. request.ReleaseName,
  98. cluster.ProjectID,
  99. request.InvolvedObjectName,
  100. )
  101. }
  102. err := multi.NotifyNew(request, url)
  103. if err != nil {
  104. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  105. return
  106. }
  107. }
  108. }
  109. func getUsersByProjectID(repo repository.Repository, projectID uint) ([]*models.User, error) {
  110. roles, err := repo.Project().ListProjectRoles(projectID)
  111. if err != nil {
  112. return nil, err
  113. }
  114. roleMap := make(map[uint]*models.Role)
  115. idArr := make([]uint, 0)
  116. for _, role := range roles {
  117. roleCp := role
  118. roleMap[role.UserID] = &roleCp
  119. idArr = append(idArr, role.UserID)
  120. }
  121. return repo.User().ListUsersByIDs(idArr)
  122. }