notification.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package notifications
  2. import (
  3. "net/http"
  4. "github.com/google/uuid"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/internal/porter_app/notifications"
  7. "github.com/porter-dev/porter/api/server/shared/requestutils"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/telemetry"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. )
  15. // GetNotificationHandler is the handler for the POST /notifications/{notification_config_id} endpoint
  16. type GetNotificationHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewNotificationHandler returns a new GetNotificationHandler
  20. func NewNotificationHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *GetNotificationHandler {
  25. return &GetNotificationHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // GetNotificationRequest is the request object for the /notifications/{notification_id} endpoint
  30. type GetNotificationRequest struct{}
  31. // NotificationResponse is the response object for the notifications endpoint
  32. type NotificationResponse struct {
  33. // Notifications are the notifications associated with the app revision
  34. Notification notifications.Notification `json:"notification"`
  35. }
  36. // ServeHTTP returns a notification by id
  37. func (n *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  38. ctx, span := telemetry.NewSpan(r.Context(), "serve-notification")
  39. defer span.End()
  40. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  41. notificationID, reqErr := requestutils.GetURLParamString(r, types.URLParamNotificationID)
  42. if reqErr != nil {
  43. e := telemetry.Error(ctx, span, nil, "error parsing notification id from url")
  44. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  45. return
  46. }
  47. telemetry.WithAttributes(span,
  48. telemetry.AttributeKV{Key: "notification-id", Value: notificationID},
  49. )
  50. request := &GetNotificationRequest{}
  51. if ok := n.DecodeAndValidate(w, r, request); !ok {
  52. err := telemetry.Error(ctx, span, nil, "error decoding request")
  53. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  54. return
  55. }
  56. event, err := n.Repo().PorterAppEvent().NotificationByID(ctx, notificationID)
  57. if err != nil {
  58. e := telemetry.Error(ctx, span, nil, "error getting notification by id")
  59. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  60. return
  61. }
  62. // check project scope indirectly with deployment target
  63. deploymentTarget, err := n.Repo().DeploymentTarget().DeploymentTarget(project.ID, event.DeploymentTargetID.String())
  64. if err != nil || deploymentTarget == nil || deploymentTarget.ID == uuid.Nil {
  65. e := telemetry.Error(ctx, span, err, "notification is not in project scope")
  66. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  67. return
  68. }
  69. notification, err := notifications.NotificationFromPorterAppEvent(event)
  70. if err != nil {
  71. e := telemetry.Error(ctx, span, nil, "error converting app event to notification")
  72. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  73. return
  74. }
  75. resp := &NotificationResponse{
  76. Notification: *notification,
  77. }
  78. n.WriteResult(w, r, resp)
  79. }