From 72ad57b343d8e918449f3cf3973f37cc4f65c766 Mon Sep 17 00:00:00 2001 From: James Cotter Date: Mon, 26 Aug 2024 12:32:36 +0100 Subject: [PATCH 1/2] notification: add OnUserRolesUpdated --- notifications/v1/subscriber.go | 17 +++++++++++++++++ notifications/v1/types.go | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/notifications/v1/subscriber.go b/notifications/v1/subscriber.go index dc0baea..65503bd 100644 --- a/notifications/v1/subscriber.go +++ b/notifications/v1/subscriber.go @@ -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 @@ -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 diff --git a/notifications/v1/types.go b/notifications/v1/types.go index f8f4ebc..f648b3a 100644 --- a/notifications/v1/types.go +++ b/notifications/v1/types.go @@ -6,7 +6,8 @@ import "go.opentelemetry.io/otel" // backend/internal/notification/types.go const ( - nameUserDeleted = "UserDeleted" + nameUserDeleted = "UserDeleted" + nameUserRolesUpdated = "UserRolesUpdated" ) // UserDeletedData contains information of a "UserDeleted" notification. @@ -17,4 +18,10 @@ type UserDeletedData struct { Email string `json:"email"` } +// UserRolesUpdatedData contains information of a "UserRolesUpdated" notification. +type UserRolesUpdatedData struct { + AccountID string `json:"account_id"` + Service string `json:"service"` +} + var tracer = otel.Tracer("sams.notifications.v1") From b14f309349ed42cd6e41fd5fb105e3387d03261b Mon Sep 17 00:00:00 2001 From: James Cotter Date: Mon, 26 Aug 2024 12:39:24 +0100 Subject: [PATCH 2/2] use services.Service type --- notifications/v1/types.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/notifications/v1/types.go b/notifications/v1/types.go index f648b3a..b21e89d 100644 --- a/notifications/v1/types.go +++ b/notifications/v1/types.go @@ -1,6 +1,9 @@ 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 @@ -20,8 +23,8 @@ type UserDeletedData struct { // UserRolesUpdatedData contains information of a "UserRolesUpdated" notification. type UserRolesUpdatedData struct { - AccountID string `json:"account_id"` - Service string `json:"service"` + AccountID string `json:"account_id"` + Service services.Service `json:"service"` } var tracer = otel.Tracer("sams.notifications.v1")