-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add handler for notification by id (#4280)
Co-authored-by: Feroze Mohideen <[email protected]>
- Loading branch information
Showing
16 changed files
with
526 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package notifications | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/porter-dev/porter/internal/models" | ||
|
||
"github.com/porter-dev/porter/internal/porter_app/notifications" | ||
|
||
"github.com/porter-dev/porter/api/server/shared/requestutils" | ||
|
||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/types" | ||
"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" | ||
) | ||
|
||
// GetNotificationHandler is the handler for the POST /notifications/{notification_config_id} endpoint | ||
type GetNotificationHandler struct { | ||
handlers.PorterHandlerReadWriter | ||
} | ||
|
||
// NewNotificationHandler returns a new GetNotificationHandler | ||
func NewNotificationHandler( | ||
config *config.Config, | ||
decoderValidator shared.RequestDecoderValidator, | ||
writer shared.ResultWriter, | ||
) *GetNotificationHandler { | ||
return &GetNotificationHandler{ | ||
PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), | ||
} | ||
} | ||
|
||
// GetNotificationRequest is the request object for the /notifications/{notification_id} endpoint | ||
type GetNotificationRequest struct{} | ||
|
||
// NotificationResponse is the response object for the notifications endpoint | ||
type NotificationResponse struct { | ||
// Notifications are the notifications associated with the app revision | ||
Notification notifications.Notification `json:"notification"` | ||
} | ||
|
||
// ServeHTTP returns a notification by id | ||
func (n *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "serve-notification") | ||
defer span.End() | ||
|
||
project, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
notificationID, reqErr := requestutils.GetURLParamString(r, types.URLParamNotificationID) | ||
if reqErr != nil { | ||
e := telemetry.Error(ctx, span, nil, "error parsing notification id from url") | ||
n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest)) | ||
return | ||
} | ||
|
||
telemetry.WithAttributes(span, | ||
telemetry.AttributeKV{Key: "notification-id", Value: notificationID}, | ||
) | ||
|
||
request := &GetNotificationRequest{} | ||
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 | ||
} | ||
|
||
event, err := n.Repo().PorterAppEvent().NotificationByID(ctx, notificationID) | ||
if err != nil { | ||
e := telemetry.Error(ctx, span, nil, "error getting notification by id") | ||
n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest)) | ||
return | ||
} | ||
|
||
// check project scope indirectly with deployment target | ||
deploymentTarget, err := n.Repo().DeploymentTarget().DeploymentTarget(project.ID, event.DeploymentTargetID.String()) | ||
if err != nil || deploymentTarget == nil || deploymentTarget.ID == uuid.Nil { | ||
e := telemetry.Error(ctx, span, err, "notification is not in project scope") | ||
n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest)) | ||
return | ||
} | ||
|
||
notification, err := notifications.NotificationFromPorterAppEvent(event) | ||
if err != nil { | ||
e := telemetry.Error(ctx, span, nil, "error converting app event to notification") | ||
n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError)) | ||
return | ||
} | ||
|
||
resp := &NotificationResponse{ | ||
Notification: *notification, | ||
} | ||
|
||
n.WriteResult(w, r, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.