get_notifications.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package release
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/requestutils"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/models"
  11. "gorm.io/gorm"
  12. )
  13. type GetNotificationHandler struct {
  14. handlers.PorterHandlerReadWriter
  15. }
  16. func NewGetNotificationHandler(
  17. config *config.Config,
  18. writer shared.ResultWriter,
  19. ) *GetNotificationHandler {
  20. return &GetNotificationHandler{
  21. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  22. }
  23. }
  24. func (c *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  26. name, _ := requestutils.GetURLParamString(r, types.URLParamReleaseName)
  27. namespace := r.Context().Value(types.NamespaceScope).(string)
  28. release, err := c.Repo().Release().ReadRelease(cluster.ID, name, namespace)
  29. if err != nil {
  30. if err == gorm.ErrRecordNotFound {
  31. w.WriteHeader(http.StatusNotFound)
  32. return
  33. }
  34. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. res := &types.GetNotificationConfigResponse{
  38. NotificationConfig: &types.NotificationConfig{
  39. Enabled: true,
  40. Success: true,
  41. Failure: true,
  42. },
  43. }
  44. if release.NotificationConfig != 0 {
  45. notifConfig, err := c.Repo().NotificationConfig().ReadNotificationConfig(release.NotificationConfig)
  46. if err != nil {
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. res.NotificationConfig = notifConfig.ToNotificationConfigType()
  51. }
  52. c.WriteResult(w, r, res)
  53. }