diff --git a/src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs b/src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs index 88fc9c2c4f..5f2a5660ef 100644 --- a/src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs +++ b/src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs @@ -25,17 +25,16 @@ public record WebhookMessage(Guid EventId, DateOnly? ExpiresOn = null) : DownloadableEventMessage(EventId, EventType, Event, InstanceId, InstanceName, CreatedAt, SasUrl, ExpiresOn); +// the schema for the event grid message is defined here: +// https://learn.microsoft.com/en-us/azure/event-grid/event-schema#event-schema public record WebhookMessageEventGrid( [property: JsonPropertyName("dataVersion")] string DataVersion, string Subject, [property: JsonPropertyName("EventType")] EventType EventType, [property: JsonPropertyName("eventTime")] DateTimeOffset EventTime, Guid Id, - [property: TypeDiscrimnatorAttribute("EventType", typeof(EventTypeProvider))] - [property: JsonConverter(typeof(BaseEventConverter))] - BaseEvent Data, - Uri SasUrl, - String Version = "1.0"); + WebhookMessage Data + ); public record WebhookMessageLog( [RowKey] Guid EventId, diff --git a/src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs b/src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs index 3228f2d1a2..cb43a536e5 100644 --- a/src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs +++ b/src/ApiService/ApiService/onefuzzlib/WebhookOperations.cs @@ -137,13 +137,12 @@ public async Task Ping(Webhook webhook) { var eventData = eventDataResult.OkV; string data; + var instanceId = await _context.Containers.GetInstanceId(); + var webhookMessage = new WebhookMessage(WebhookId: webhookId, EventId: eventId, EventType: eventType, Event: webhookEvent, InstanceId: instanceId, InstanceName: _context.Creds.GetInstanceName(), CreatedAt: eventData.CreatedAt, SasUrl: eventData.SasUrl); if (messageFormat != null && messageFormat == WebhookMessageFormat.EventGrid) { - var eventGridMessage = new[] { new WebhookMessageEventGrid(Id: eventId, Data: webhookEvent, DataVersion: "1.0.0", Subject: _context.Creds.GetInstanceName(), EventType: eventType, EventTime: DateTimeOffset.UtcNow, SasUrl: eventData.SasUrl) }; + var eventGridMessage = new[] { new WebhookMessageEventGrid(Id: eventId, Data: webhookMessage, DataVersion: "2.0.0", Subject: _context.Creds.GetInstanceName(), EventType: eventType, EventTime: DateTimeOffset.UtcNow) }; data = JsonSerializer.Serialize(eventGridMessage, options: EntityConverter.GetJsonSerializerOptions()); } else { - var instanceId = await _context.Containers.GetInstanceId(); - var webhookMessage = new WebhookMessage(WebhookId: webhookId, EventId: eventId, EventType: eventType, Event: webhookEvent, InstanceId: instanceId, InstanceName: _context.Creds.GetInstanceName(), CreatedAt: eventData.CreatedAt, SasUrl: eventData.SasUrl); - data = JsonSerializer.Serialize(webhookMessage, options: EntityConverter.GetJsonSerializerOptions()); } diff --git a/src/ApiService/Tests/OrmModelsTest.cs b/src/ApiService/Tests/OrmModelsTest.cs index c4dadc0db1..e78e7e1638 100644 --- a/src/ApiService/Tests/OrmModelsTest.cs +++ b/src/ApiService/Tests/OrmModelsTest.cs @@ -303,16 +303,19 @@ public static Gen WebhookMessage() { } public static Gen WebhookMessageEventGrid() { - return Arb.Generate>().Select( + var gen1 = Arb.Generate>(); + var gen2 = WebhookMessage(); + return gen1.Zip(gen2) + + .Select( arg => new WebhookMessageEventGrid( - DataVersion: arg.Item1, - Subject: arg.Item2, - EventType: arg.Item3.GetEventType(), - Data: arg.Item3, - Id: arg.Item4, - EventTime: arg.Item5, - SasUrl: arg.Item6 + DataVersion: arg.Item1.Item1, + Subject: arg.Item1.Item2, + EventType: arg.Item1.Item3.GetEventType(), + Data: arg.Item2, + Id: arg.Item1.Item4, + EventTime: arg.Item1.Item5 ) ); }