Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test notification endpoint #934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions echo/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/flanksource/incident-commander/connection"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/logs"
"github.com/flanksource/incident-commander/notification"
"github.com/flanksource/incident-commander/playbook"
"github.com/flanksource/incident-commander/rbac"
"github.com/flanksource/incident-commander/snapshot"
Expand Down Expand Up @@ -109,6 +110,8 @@ func New(ctx context.Context) *echov4.Echo {

playbook.RegisterRoutes(e)
connection.RegisterRoutes(e)
notification.RegisterRoutes(e)

e.POST("/agent/generate", agent.GenerateAgent, rbac.Authorization(rbac.ObjectAgentCreate, rbac.ActionWrite))
e.POST("/logs", logs.LogsHandler)
return e
Expand Down
56 changes: 56 additions & 0 deletions notification/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package notification

import (
"net/http"
"time"

dutyAPI "github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
"github.com/flanksource/postq"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

func RegisterRoutes(e *echo.Echo) {
apiGroup := e.Group("/notifications")
apiGroup.POST("/test", TestNotification)
}

func TestNotification(c echo.Context) error {
ctx := c.Request().Context().(context.Context)

var reqData struct {
ID uuid.UUID `json:"id"`
EventName string `json:"eventName"`
}
if err := c.Bind(&reqData); err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINVALID, "invalid request: %v", err))
}

e := postq.Event{
Name: reqData.EventName,
Properties: map[string]string{"id": reqData.ID.String(), "event_name": reqData.EventName},
CreatedAt: time.Now(),
}

if err := addNotificationEvent(ctx, e); err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "unable to create notification event: %v", err))
}

var payload NotificationEventPayload
payload.FromMap(e.Properties)

ctx.Debugf("[notification.send] %s ", payload.EventName)

notificationContext := NewContext(ctx, payload.NotificationID)
notificationContext.WithSource(payload.EventName, payload.ID)

originalEvent := postq.Event{Name: payload.EventName, CreatedAt: payload.EventCreatedAt}
celEnv, err := getEnvForEvent(ctx, originalEvent, e.Properties)
if err != nil {

Check failure on line 50 in notification/controllers.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)
}
if err := SendNotification(notificationContext, payload, celEnv); err != nil {

Check failure on line 52 in notification/controllers.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)
}

return c.JSON(http.StatusOK, dutyAPI.HTTPSuccess{Message: "success"})
}
Loading