Skip to content
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ Libraries to handle GitHub Webhooks in .NET applications.
```C#
public sealed class MyWebhookEventProcessor : WebhookEventProcessor
{
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
protected override Task ProcessPullRequestWebhookAsync(
WebhookHeaders headers,
PullRequestEvent pullRequestEvent,
PullRequestAction action,
CancellationToken cancellationToken = default)
{
...
}
Expand Down Expand Up @@ -48,15 +52,19 @@ Libraries to handle GitHub Webhooks in .NET applications.

### Azure Functions

**NOTE**: Support is only provided for [isolated process Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide).
**NOTE**: Support is only provided for [isolated process Azure Functions](https://learn.microsoft.com/azure/azure-functions/dotnet-isolated-process-guide).

1. `dotnet add package Octokit.Webhooks.AzureFunctions`
2. Create a class that derives from `WebhookEventProcessor` and override any of the virtual methods to handle webhooks from GitHub. For example, to handle Pull Request webhooks:

```C#
public sealed class MyWebhookEventProcessor : WebhookEventProcessor
{
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
protected override Task ProcessPullRequestWebhookAsync(
WebhookHeaders headers,
PullRequestEvent pullRequestEvent,
PullRequestAction action,
CancellationToken cancellationToken = default)
{
...
}
Expand Down
6 changes: 5 additions & 1 deletion samples/AspNetCore/MyWebhookEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace AspNetCore;

public class MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) : WebhookEventProcessor
{
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
protected override async Task ProcessPullRequestWebhookAsync(
WebhookHeaders headers,
PullRequestEvent pullRequestEvent,
PullRequestAction action,
CancellationToken cancellationToken = default)
{
switch (action)
{
Expand Down
6 changes: 5 additions & 1 deletion samples/AzureFunctions/MyWebhookEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace AzureFunctions;

public class MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) : WebhookEventProcessor
{
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
protected override async Task ProcessPullRequestWebhookAsync(
WebhookHeaders headers,
PullRequestEvent pullRequestEvent,
PullRequestAction action,
CancellationToken cancellationToken = default)
{
switch (action)
{
Expand Down
5 changes: 2 additions & 3 deletions src/Octokit.Webhooks.AspNetCore/GitHubWebhookExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public static partial class GitHubWebhookExtensions
public static IEndpointConventionBuilder MapGitHubWebhooks(
this IEndpointRouteBuilder endpoints,
string path = "/api/github/webhooks",
string? secret = null,
bool cancelOnRequestAborted = false)
string? secret = null)
{
var options = endpoints.ServiceProvider.GetService<IOptionsMonitor<GitHubWebhookOptions>>();
return endpoints.MapPost(
Expand Down Expand Up @@ -60,7 +59,7 @@ public static IEndpointConventionBuilder MapGitHubWebhooks(
try
{
var service = context.RequestServices.GetRequiredService<WebhookEventProcessor>();
await service.ProcessWebhookAsync(context.Request.Headers, body, cancelOnRequestAborted ? context.RequestAborted : CancellationToken.None)
await service.ProcessWebhookAsync(context.Request.Headers, body, context.RequestAborted)
.ConfigureAwait(false);
context.Response.StatusCode = 200;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed partial class GitHubWebhooksHttpFunction(IOptions<GitHubWebhooksOp
kv => kv.Key,
kv => new StringValues([.. kv.Value]),
StringComparer.OrdinalIgnoreCase);
await service.ProcessWebhookAsync(headers, body)
await service.ProcessWebhookAsync(headers, body, ctx.CancellationToken)
.ConfigureAwait(false);
return req.CreateResponse(HttpStatusCode.OK);
}
Expand Down
Loading