These libraries help with handling GitHub events in .NET applications.
If you're running inside of GitHub Actions, you can simply access the event data that triggered the workflow by doing this:
GitHubEvent gitHubEvent = GitHubEvent.FromGitHubActions();
if (gitHubEvent is not null)
{
// Triggered from inside GitHub Actions
}
Assuming your web hook lives in ASP.NET Core, simply do the following:
-
dotnet add package Terrajobst.GitHubEvents.AspNetCore
-
Add a class that implements from
IGitHubEventProcessor
to handle events from GitHub:public sealed class MyGitHubEventProcessor : IGitHubEventProcessor { private readonly TelemetryClient _telemetryClient; public MyGitHubEventProcessor(TelemetryClient telemetryClient) { _telemetryClient = telemetryClient; } public void Process(GitHubEvent @event) { _telemetryClient.GetMetric("github_" + @event.Kind) .TrackValue(1.0); } }
-
Modify your
ConfigureServices()
method to register an implementation forIGitHubEventProcessor
:public void ConfigureServices(IServiceCollection services) { ... services.AddSingleton<IGitHubEventProcessor, MyGitHubEventProcessor>(); ... }
-
Modify your
Configure()
method to map the web hook end point:app.UseEndpoints(endpoints => { ... endpoints.MapGitHubWebHook(); ... });
MapGitHubWebHook()
takes two optional parameters:
pattern
. Defaults to/github-webhook
, the URL of the end point to use for GitHub.secret
. The secret you have configured in GitHub, if you have set this up.
$ dotnet tool install Terrajobst.GitHubEvents.AspNetCore -g --add-source https://nuget.pkg.github.com/terrajobst/index.json