| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package notifications
- import (
- "net/http"
- "connectrpc.com/connect"
- "github.com/porter-dev/porter/api/server/shared/requestutils"
- porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
- "github.com/porter-dev/porter/api/server/shared/apierrors"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/telemetry"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/config"
- )
- // GetNotificationConfigHandler is the handler for the POST /notifications/{notification_config_id} endpoint
- type GetNotificationConfigHandler struct {
- handlers.PorterHandlerReadWriter
- }
- // NewNotificationConfigHandler returns a new GetNotificationConfigHandler
- func NewNotificationConfigHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *GetNotificationConfigHandler {
- return &GetNotificationConfigHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- // GetNotificationConfigRequest is the request object for the /notifications/{notification_config_id} endpoint
- type GetNotificationConfigRequest struct{}
- // GetNotificationConfigResponse is the response object for the /notifications/{notification_config_id} endpoint
- type GetNotificationConfigResponse struct {
- Config Config `json:"config"`
- }
- // ServeHTTP updates a notification config
- func (n *GetNotificationConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ctx, span := telemetry.NewSpan(r.Context(), "serve-notification-config")
- defer span.End()
- project, _ := ctx.Value(types.ProjectScope).(*models.Project)
- notificationConfigID, reqErr := requestutils.GetURLParamUint(r, types.URLParamNotificationConfigID)
- if reqErr != nil {
- e := telemetry.Error(ctx, span, nil, "error parsing event id from url")
- n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
- return
- }
- telemetry.WithAttributes(span,
- telemetry.AttributeKV{Key: "notification-config-id", Value: notificationConfigID},
- )
- request := &GetNotificationConfigRequest{}
- if ok := n.DecodeAndValidate(w, r, request); !ok {
- err := telemetry.Error(ctx, span, nil, "error decoding request")
- n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
- return
- }
- configReq := connect.NewRequest(&porterv1.NotificationConfigRequest{
- ProjectId: int64(project.ID),
- NotificationConfigId: int64(notificationConfigID),
- })
- ccpResp, err := n.Config().ClusterControlPlaneClient.NotificationConfig(ctx, configReq)
- if err != nil {
- err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
- n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
- return
- }
- if ccpResp == nil || ccpResp.Msg == nil {
- err := telemetry.Error(ctx, span, nil, "ccp response or msg is nil")
- n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
- return
- }
- response := &GetNotificationConfigResponse{
- Config: configFromProto(ccpResp.Msg.Config),
- }
- n.WriteResult(w, r, response)
- }
- func configFromProto(proto *porterv1.NotificationConfig) Config {
- if proto == nil {
- return Config{}
- }
- var statuses []Status
- for _, status := range proto.Statuses {
- statuses = append(statuses, Status{transformProtoToStatusString[status]})
- }
- var types []Type
- for _, t := range proto.EventTypes {
- types = append(types, Type{transformProtoToTypeString[t]})
- }
- var mention string
- if proto.SlackConfig != nil && len(proto.SlackConfig.Mentions) > 0 {
- mention = proto.SlackConfig.Mentions[0]
- }
- return Config{
- Statuses: statuses,
- Mention: mention,
- Types: types,
- }
- }
- var transformProtoToStatusString = map[porterv1.EnumNotificationStatus]string{
- porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_SUCCESSFUL: "successful",
- porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_FAILED: "failed",
- porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_PROGRESSING: "progressing",
- }
- var transformProtoToTypeString = map[porterv1.EnumNotificationEventType]string{
- porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_DEPLOY: "deploy",
- porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_PREDEPLOY: "pre-deploy",
- porterv1.EnumNotificationEventType_ENUM_NOTIFICATION_EVENT_TYPE_BUILD: "build",
- }
|