Publishing events into an ASP.NET Core webhook with Octopus

public class DeploymentWebhookSubscription
{
public DateTime Timestamp { get; set; }
public string EventType { get; set; }
public Payload Payload { get; set; }
}
public class Filter
{
public List<string> Users { get; set; }
public List<string> Projects { get; set; }
public List<string> ProjectGroups { get; set; }
public List<string> Environments { get; set; }
public List<string> EventGroups { get; set; }
public List<string> EventCategories { get; set; }
public List<string> EventAgents { get; set; }
public List<string> Tenants { get; set; }
public List<string> Tags { get; set; }
public List<string> DocumentTypes { get; set; }
}
public class EventNotificationSubscription
{
public Filter Filter { get; set; }
public List<string> EmailTeams { get; set; }
public string EmailFrequencyPeriod { get; set; }
public int EmailPriority { get; set; }
public string EmailShowDatesInTimeZoneId { get; set; }
public string WebhookURI { get; set; }
public List<object> WebhookTeams { get; set; }
public string WebhookTimeout { get; set; }
public string WebhookHeaderKey { get; set; }
public string WebhookHeaderValue { get; set; }
public DateTime WebhookLastProcessed { get; set; }
public int WebhookLastProcessedEventAutoId { get; set; }
}
public class Self
{
}
public class Links
{
public Self Self { get; set; }
}
public class Subscription
{
public string Id { get; set; }
public string Name { get; set; }
public int Type { get; set; }
public bool IsDisabled { get; set; }
public EventNotificationSubscription EventNotificationSubscription { get; set; }
public string SpaceId { get; set; }
public Links Links { get; set; }
}
public class MessageReference
{
public string ReferencedDocumentId { get; set; }
public int StartIndex { get; set; }
public int Length { get; set; }
}
public class LibraryVariableSetSnapshot
{
public string LibraryVariableSetId { get; set; }
public string VariableSetSnapshotId { get; set; }
}
public class SelectedPackage
{
public string ActionName { get; set; }
public string PackageReferenceName { get; set; }
public string Version { get; set; }
}
public class VersionControlReference
{
}
public class DocumentContext
{
public string Id { get; set; }
public string Version { get; set; }
public DateTime Assembled { get; set; }
public List<object> ReleaseDefects { get; set; }
public string ProjectId { get; set; }
public string ProjectVariableSetSnapshotId { get; set; }
public string ProjectDeploymentProcessSnapshotId { get; set; }
public List<LibraryVariableSetSnapshot> LibraryVariableSetSnapshots { get; set; }
public List<SelectedPackage> SelectedPackages { get; set; }
public string ChannelId { get; set; }
public List<object> BuildInformation { get; set; }
public VersionControlReference VersionControlReference { get; set; }
public string SpaceId { get; set; }
}
public class ChangeDetails
{
public DocumentContext DocumentContext { get; set; }
public List<object> Differences { get; set; }
}
public class Event
{
public string Id { get; set; }
public List<string> RelatedDocumentIds { get; set; }
public string Category { get; set; }
public string UserId { get; set; }
public string Username { get; set; }
public bool IsService { get; set; }
public string IdentityEstablishedWith { get; set; }
public string UserAgent { get; set; }
public DateTime Occurred { get; set; }
public string Message { get; set; }
public string MessageHtml { get; set; }
public List<MessageReference> MessageReferences { get; set; }
public ChangeDetails ChangeDetails { get; set; }
public string SpaceId { get; set; }
public Links Links { get; set; }
}
public class Payload
{
public DateTime BatchProcessingDate { get; set; }
public Subscription Subscription { get; set; }
public Event Event { get; set; }
public string BatchId { get; set; }
public int TotalEventsInBatch { get; set; }
public int EventNumberInBatch { get; set; }
}
public class Event
{
public List<string> RelatedDocumentIds { get; set; }
//other properties removed for clarity
}
[HttpPost]
public ActionResult Post([FromBody] JObject payload)
{
private static readonly int DEPLOYMENT_ID_INDEX = 0;
try
{
var webhookSubscriptionPayload = payload.ToObject<DeploymentWebhookSubscription>();
if (webhookSubscriptionPayload != null)
{
string deploymentId = webhookSubscriptionPayload.Payload.Event.RelatedDocumentIds[DEPLOYMENT_ID_INDEX];
//lookup the deployment ID in your table and do something useful here
return Ok(jobId);
}
return StatusCode(StatusCodes.Status500InternalServerError);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
[HttpPost]
public ActionResult Post([FromBody] JObject payload)
{
private static readonly int DEPLOYMENT_ID_INDEX = 0;
try
{
var webhookSubscriptionPayload = payload.ToObject<DeploymentWebhookSubscription>();
if (webhookSubscriptionPayload != null)
{
//extract the header key and value from the payload
//and write some custom authentication logic
string headerKey = webhookSubscriptionPayload.Payload.Subscription.EventNotificationSubscription.WebhookHeaderKey;
string headerValue = webhookSubscriptionPayload.Payload.Subscription.EventNotificationSubscription.WebhookHeaderValue; //rest of your code goes here }
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dominic Burford

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.