get_notification_config.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // GetNotificationConfigHandler is the handler for the POST /notifications/{notification_config_id} endpoint
  16. type GetNotificationConfigHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewNotificationConfigHandler returns a new GetNotificationConfigHandler
  20. func NewNotificationConfigHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *GetNotificationConfigHandler {
  25. return &GetNotificationConfigHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // GetNotificationConfigRequest is the request object for the /notifications/{notification_config_id} endpoint
  30. type GetNotificationConfigRequest struct{}
  31. // GetNotificationConfigResponse is the response object for the /notifications/{notification_config_id} endpoint
  32. type GetNotificationConfigResponse struct {
  33. Config Config `json:"config"`
  34. }
  35. // ServeHTTP updates a notification config
  36. func (n *GetNotificationConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  37. ctx, span := telemetry.NewSpan(r.Context(), "serve-notification-config")
  38. defer span.End()
  39. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  40. notificationConfigID, reqErr := requestutils.GetURLParamUint(r, types.URLParamNotificationConfigID)
  41. if reqErr != nil {
  42. e := telemetry.Error(ctx, span, nil, "error parsing event id from url")
  43. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  44. return
  45. }
  46. telemetry.WithAttributes(span,
  47. telemetry.AttributeKV{Key: "notification-config-id", Value: notificationConfigID},
  48. )
  49. request := &GetNotificationConfigRequest{}
  50. if ok := n.DecodeAndValidate(w, r, request); !ok {
  51. err := telemetry.Error(ctx, span, nil, "error decoding request")
  52. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  53. return
  54. }
  55. configReq := connect.NewRequest(&porterv1.NotificationConfigRequest{
  56. ProjectId: int64(project.ID),
  57. NotificationConfigId: int64(notificationConfigID),
  58. })
  59. ccpResp, err := n.Config().ClusterControlPlaneClient.NotificationConfig(ctx, configReq)
  60. if err != nil {
  61. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  62. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  63. return
  64. }
  65. if ccpResp == nil || ccpResp.Msg == nil {
  66. err := telemetry.Error(ctx, span, nil, "ccp response or msg is nil")
  67. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  68. return
  69. }
  70. response := &GetNotificationConfigResponse{
  71. Config: configFromProto(ccpResp.Msg.Config),
  72. }
  73. n.WriteResult(w, r, response)
  74. }
  75. func configFromProto(proto *porterv1.NotificationConfig) Config {
  76. if proto == nil {
  77. return Config{}
  78. }
  79. var statuses []Status
  80. for _, status := range proto.Statuses {
  81. statuses = append(statuses, Status{transformProtoToStatusString[status]})
  82. }
  83. var types []Type
  84. for _, t := range proto.EventTypes {
  85. types = append(types, Type{transformProtoToTypeString[t]})
  86. }
  87. var mention string
  88. if proto.SlackConfig != nil && len(proto.SlackConfig.Mentions) > 0 {
  89. mention = proto.SlackConfig.Mentions[0]
  90. }
  91. return Config{
  92. Statuses: statuses,
  93. Mention: mention,
  94. Types: types,
  95. }
  96. }
  97. var transformProtoToStatusString = map[porterv1.EnumNotificationStatus]string{
  98. porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_SUCCESSFUL: "successful",
  99. porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_FAILED: "failed",
  100. porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_PROGRESSING: "progressing",
  101. }
  102. var transformProtoToTypeString = map[porterv1.EnumNotificationEventType]string{
  103. porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_DEPLOY: "deploy",
  104. porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_PREDEPLOY: "pre-deploy",
  105. porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_BUILD: "build",
  106. }