Skip to content

octokit/webhooks.net

Octokit.Webhooks

GitHub Workflow StatusOctokit.Webhooks NuGet Package VersionOctokit.Webhooks NuGet Package DownloadsOpenSSF Scorecard

Libraries to handle GitHub Webhooks in .NET applications.

Usage

ASP.NET Core

  1. dotnet add package Octokit.Webhooks.AspNetCore

  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:

    publicsealedclassMyWebhookEventProcessor:WebhookEventProcessor{protectedoverrideValueTaskProcessPullRequestWebhookAsync(WebhookHeadersheaders,PullRequestEventpullRequestEvent,PullRequestActionaction,CancellationTokencancellationToken=default){ ...}}
  3. Register your implementation of WebhookEventProcessor:

    builder.Services.AddSingleton<WebhookEventProcessor,MyWebhookEventProcessor>();
  4. 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.

Azure Functions

NOTE: Support is only provided for isolated process Azure Functions.

  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:

    publicsealedclassMyWebhookEventProcessor:WebhookEventProcessor{protectedoverrideValueTaskProcessPullRequestWebhookAsync(WebhookHeadersheaders,PullRequestEventpullRequestEvent,PullRequestActionaction,CancellationTokencancellationToken=default){ ...}}
  3. Register your implementation of WebhookEventProcessor:

    .ConfigureServices(collection =>{ ...collection.AddSingleton<WebhookEventProcessor,MyWebhookEventProcessor>(); ...})
  4. Configure the webhook function:

    newHostBuilder() ....ConfigureGitHubWebhooks() ....Build();

ConfigureGitHubWebhooks() either takes an optional parameter:

  • secret. The secret you have configured in GitHub, if you have set this up.

or:

  • configure. A function that takes an IConfiguration instance and expects the secret you have configured in GitHub in return.

The function is available on the /api/github/webhooks endpoint.

Thanks

License

All packages in this repository are licensed under the MIT license.

Packages

 
 
 

Contributors 28

Languages