2
0

notifications_handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/internal/models"
  9. "gorm.io/gorm"
  10. )
  11. type HandleUpdateNotificationConfigForm struct {
  12. Payload struct {
  13. Enabled bool `json:"enabled"`
  14. Success bool `json:"success"`
  15. Failure bool `json:"failure"`
  16. } `json:"payload"`
  17. Namespace string `json:"namespace"`
  18. ClusterID uint `json:"cluster_id"`
  19. }
  20. // HandleUpdateNotificationConfig updates notification settings for a given release
  21. func (app *App) HandleUpdateNotificationConfig(w http.ResponseWriter, r *http.Request) {
  22. name := chi.URLParam(r, "name")
  23. form := &HandleUpdateNotificationConfigForm{}
  24. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  25. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  26. return
  27. }
  28. release, err := app.Repo.Release().ReadRelease(form.ClusterID, name, form.Namespace)
  29. if err != nil {
  30. if err == gorm.ErrRecordNotFound {
  31. w.WriteHeader(http.StatusNotFound)
  32. return
  33. }
  34. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  35. Code: ErrReleaseReadData,
  36. Errors: []string{"release not found"},
  37. }, w)
  38. }
  39. // either create a new notification config or update the current one
  40. newConfig := &models.NotificationConfig{
  41. Enabled: form.Payload.Enabled,
  42. Success: form.Payload.Success,
  43. Failure: form.Payload.Failure,
  44. }
  45. if release.NotificationConfig == 0 {
  46. newConfig, err = app.Repo.NotificationConfig().CreateNotificationConfig(newConfig)
  47. if err != nil {
  48. app.handleErrorInternal(err, w)
  49. return
  50. }
  51. release.NotificationConfig = newConfig.ID
  52. release, err = app.Repo.Release().UpdateRelease(release)
  53. } else {
  54. newConfig.ID = release.NotificationConfig
  55. newConfig, err = app.Repo.NotificationConfig().UpdateNotificationConfig(newConfig)
  56. }
  57. if err != nil {
  58. app.handleErrorInternal(err, w)
  59. return
  60. }
  61. w.WriteHeader(http.StatusOK)
  62. }
  63. // HandleGetNotificationConfig gets the notification config for a given release
  64. func (app *App) HandleGetNotificationConfig(w http.ResponseWriter, r *http.Request) {
  65. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  66. if err != nil || projID == 0 {
  67. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  68. return
  69. }
  70. vals, err := url.ParseQuery(r.URL.RawQuery)
  71. name := chi.URLParam(r, "name")
  72. namespace := vals["namespace"][0]
  73. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  74. if err != nil {
  75. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  76. Code: ErrReleaseReadData,
  77. Errors: []string{"release not found"},
  78. }, w)
  79. return
  80. }
  81. release, err := app.Repo.Release().ReadRelease(uint(clusterID), name, namespace)
  82. if err != nil {
  83. if err == gorm.ErrRecordNotFound {
  84. w.WriteHeader(http.StatusNotFound)
  85. return
  86. }
  87. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  88. Code: ErrReleaseReadData,
  89. Errors: []string{"release not found"},
  90. }, w)
  91. return
  92. }
  93. config := &models.NotificationConfigExternal{
  94. Enabled: true,
  95. Success: true,
  96. Failure: true,
  97. }
  98. if release.NotificationConfig != 0 {
  99. notifConfig, err := app.Repo.NotificationConfig().ReadNotificationConfig(release.NotificationConfig)
  100. if err != nil {
  101. app.handleErrorInternal(err, w)
  102. }
  103. config = notifConfig.Externalize()
  104. }
  105. err = json.NewEncoder(w).Encode(config)
  106. if err != nil {
  107. app.handleErrorInternal(err, w)
  108. }
  109. }