Skip to content
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 {
}
if err := SendNotification(notificationContext, payload, celEnv); err != nil {
}
Comment on lines +50 to +53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty error blocks silently swallow errors that should be surfaced to the client. Consider handling both error cases by returning them like:

if err != nil {
    return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "failed to get env for event: %v", err))
}
if err := SendNotification(notificationContext, payload, celEnv); err != nil {
    return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "failed to send notification: %v", err))
}

This provides better visibility into failures and maintains consistent error handling patterns.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.


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