notifications_handler.go 3.2 KB

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