Skip to content

Commit

Permalink
notification: add OnUserRolesUpdated (#58)
Browse files Browse the repository at this point in the history
Adds the `UserRolesUpdated` notification event and handleReceive logic
Part of CORE-206
## Test plan
CI
  • Loading branch information
jac authored Aug 26, 2024
1 parent e673be4 commit d6a8a50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions notifications/v1/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ type SubscriberHandlers struct {
// MUST make sure the error is surfaced (by either returning or logging the
// error) to be retried or to a human operator.
OnUserDeleted func(ctx context.Context, data *UserDeletedData) error
// OnUserRolesUpdated is called when a "UserRolesUpdated" notification is received.
//
// It indicates that a user's roles have been updated for a particular service.
// The notification data does not specify whether roles have been granted or revoked.
// If the service's roles are relevant to the subscriber the user's current roles can
// be retrieved from the SAMS API.
OnUserRolesUpdated func(ctx context.Context, data *UserRolesUpdatedData) error
}

type ReceiveSettings = pubsub.ReceiveSettings
Expand All @@ -201,6 +208,16 @@ func (s *subscriber) handleReceive(ctx context.Context, name string, metadata js
}

return "handled", s.handlers.OnUserDeleted(ctx, &data)
case nameUserRolesUpdated:
if s.handlers.OnUserRolesUpdated == nil {
return "skipped", nil
}
var data UserRolesUpdatedData
if err := json.Unmarshal(metadata, &data); err != nil {
return "malformed_message", errors.Wrap(err, "unmarshal metadata")
}

return "handled", s.handlers.OnUserRolesUpdated(ctx, &data)
}

// Unknown message type
Expand Down
14 changes: 12 additions & 2 deletions notifications/v1/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package v1

import "go.opentelemetry.io/otel"
import (
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/services"
"go.opentelemetry.io/otel"
)

// ⚠️ WARNING: These types MUST match the SAMS implementation, at
// backend/internal/notification/types.go

const (
nameUserDeleted = "UserDeleted"
nameUserDeleted = "UserDeleted"
nameUserRolesUpdated = "UserRolesUpdated"
)

// UserDeletedData contains information of a "UserDeleted" notification.
Expand All @@ -17,4 +21,10 @@ type UserDeletedData struct {
Email string `json:"email"`
}

// UserRolesUpdatedData contains information of a "UserRolesUpdated" notification.
type UserRolesUpdatedData struct {
AccountID string `json:"account_id"`
Service services.Service `json:"service"`
}

var tracer = otel.Tracer("sams.notifications.v1")

0 comments on commit d6a8a50

Please sign in to comment.