Libraries to handle GitHub Webhooks in .NET applications.
-
dotnet add package Octokit.Webhooks.AspNetCore -
Create a class that derives from
WebhookEventProcessorand override any of the virtual methods to handle webhooks from GitHub. For example, to handle Pull Request webhooks:public sealed class MyWebhookEventProcessor : WebhookEventProcessor { protected override ValueTask ProcessPullRequestWebhookAsync( WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default) { ... } }
-
Register your implementation of
WebhookEventProcessor:builder.Services.AddSingleton<WebhookEventProcessor, MyWebhookEventProcessor>();
-
Map the webhook endpoint:
app.UseEndpoints(endpoints => { ... endpoints.MapGitHubWebhooks(); ... });
MapGitHubWebhooks() takes two optional parameters:
path. Defaults to/api/github/webhooks, the URL of the endpoint to use for GitHub.secret. The secret you have configured in GitHub, if you have set this up.
NOTE: Support is only provided for isolated process Azure Functions.
-
dotnet add package Octokit.Webhooks.AzureFunctions -
Create a class that derives from
WebhookEventProcessorand override any of the virtual methods to handle webhooks from GitHub. For example, to handle Pull Request webhooks:public sealed class MyWebhookEventProcessor : WebhookEventProcessor { protected override ValueTask ProcessPullRequestWebhookAsync( WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default) { ... } }
-
Register your implementation of
WebhookEventProcessorand configure the webhook function:var builder = FunctionsApplication.CreateBuilder(args); builder.Services.AddSingleton<WebhookEventProcessor, MyWebhookEventProcessor>(); builder.ConfigureGitHubWebhooks(); builder.Build().Run();
ConfigureGitHubWebhooks has two overloads:
ConfigureGitHubWebhooks(string? secret = null). Pass the secret you have configured in GitHub, if you have set this up.ConfigureGitHubWebhooks(Func<IConfiguration, string> configure). Resolve the secret from configuration at runtime.
The function is available on the /api/github/webhooks endpoint.
All packages in this repository are licensed under the MIT license.