update_notification_config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package notifications
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. "github.com/porter-dev/porter/api/server/shared/requestutils"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  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. // UpdateNotificationConfigHandler is the handler for the POST /notifications/{notification_config_id} endpoint
  16. type UpdateNotificationConfigHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewUpdateNotificationConfigHandler returns a new UpdateNotificationConfigHandler
  20. func NewUpdateNotificationConfigHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *UpdateNotificationConfigHandler {
  25. return &UpdateNotificationConfigHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // UpdateNotificationConfigRequest is the request object for the /notifications/{notification_config_id} endpoint
  30. type UpdateNotificationConfigRequest struct {
  31. Config Config `json:"config"`
  32. SlackIntegrationID uint `json:"slack_integration_id"`
  33. }
  34. // Config is the config object for the /notifications endpoint
  35. type Config struct {
  36. Mention string `json:"mention"`
  37. Statuses []Status `json:"statuses"`
  38. Types []Type `json:"types"`
  39. }
  40. // Status is a wrapper object over a string for zod validation
  41. type Status struct {
  42. Status string `json:"status"`
  43. }
  44. // Type is a wrapper object over a string for zod validation
  45. type Type struct {
  46. Type string `json:"type"`
  47. }
  48. // UpdateNotificationConfigResponse is the response object for the /notifications/{notification_config_id} endpoint
  49. type UpdateNotificationConfigResponse struct {
  50. ID uint `json:"id"`
  51. }
  52. // ServeHTTP updates a notification config
  53. func (n *UpdateNotificationConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  54. ctx, span := telemetry.NewSpan(r.Context(), "serve-notification-config-update")
  55. defer span.End()
  56. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  57. telemetry.WithAttributes(span,
  58. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  59. )
  60. notificationConfigID, reqErr := requestutils.GetURLParamUint(r, types.URLParamNotificationConfigID)
  61. if reqErr != nil {
  62. e := telemetry.Error(ctx, span, nil, "error parsing event id from url")
  63. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  64. return
  65. }
  66. telemetry.WithAttributes(span,
  67. telemetry.AttributeKV{Key: "notification-config-id", Value: notificationConfigID},
  68. )
  69. request := &UpdateNotificationConfigRequest{}
  70. if ok := n.DecodeAndValidate(w, r, request); !ok {
  71. err := telemetry.Error(ctx, span, nil, "error decoding request")
  72. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  73. return
  74. }
  75. updateReq := connect.NewRequest(&porterv1.UpdateNotificationConfigRequest{
  76. ProjectId: int64(project.ID),
  77. NotificationConfigId: int64(notificationConfigID),
  78. Config: configToProto(request.Config),
  79. SlackIntegrationId: int64(request.SlackIntegrationID),
  80. })
  81. updateResp, err := n.Config().ClusterControlPlaneClient.UpdateNotificationConfig(ctx, updateReq)
  82. if err != nil {
  83. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  84. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. return
  86. }
  87. if updateResp == nil || updateResp.Msg == nil {
  88. err := telemetry.Error(ctx, span, nil, "ccp response or msg is nil")
  89. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  90. return
  91. }
  92. response := &UpdateNotificationConfigResponse{
  93. ID: uint(updateResp.Msg.NotificationConfigId),
  94. }
  95. n.WriteResult(w, r, response)
  96. }
  97. func configToProto(config Config) *porterv1.NotificationConfig {
  98. var statuses []porterv1.EnumNotificationStatus
  99. for _, status := range config.Statuses {
  100. statuses = append(statuses, transformStatusStringToProto[status.Status])
  101. }
  102. var types []porterv1.EnumNotificationEventType
  103. for _, t := range config.Types {
  104. types = append(types, transformTypeStringToProto[t.Type])
  105. }
  106. return &porterv1.NotificationConfig{
  107. Statuses: statuses,
  108. EventTypes: types,
  109. SlackConfig: &porterv1.SlackConfig{Mentions: []string{config.Mention}},
  110. }
  111. }
  112. var transformStatusStringToProto = map[string]porterv1.EnumNotificationStatus{
  113. "successful": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_SUCCESSFUL,
  114. "failed": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_FAILED,
  115. "progressing": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_PROGRESSING,
  116. }
  117. var transformTypeStringToProto = map[string]porterv1.EnumNotificationEventType{
  118. "deploy": porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_DEPLOY,
  119. "build": porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_BUILD,
  120. "pre-deploy": porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_PREDEPLOY,
  121. }