notifications_handler.go 3.4 KB

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