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

notification: add OnUserRolesUpdated #58

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
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")
Loading