Last active
March 4, 2022 05:56
-
-
Save JamieMagee/fb5ffc670bd4f7cdb3378eca33cfc12f to your computer and use it in GitHub Desktop.
Writing GitHub bots in .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package Octokit.Webhooks.AspNetCore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package Octokit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Octokit.Webhooks; | |
using Octokit.Webhooks.Events; | |
using Octokit.Webhooks.Events.IssueComment; | |
public sealed class MyWebhookEventProcessor : WebhookEventProcessor | |
{ | |
private readonly ILogger<MyWebhookEventProcessor> logger; | |
public MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) | |
{ | |
this.logger = logger; | |
} | |
protected override Task ProcessIssueCommentWebhookAsync(WebhookHeaders headers, IssueCommentEvent issueCommentEvent, IssueCommentAction action) | |
{ | |
this.logger.LogInformation(issueCommentEvent.Comment.Body); | |
return Task.CompletedTask; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private readonly GitHubClient client; | |
public MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) | |
{ | |
this.logger = logger; | |
this.client = new GitHubClient(new ProductHeaderValue("octokit-webhooks-sample")) | |
{ | |
Credentials = new Credentials("...") | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override async Task ProcessIssueCommentWebhookAsync(WebhookHeaders headers, IssueCommentEvent issueCommentEvent, IssueCommentAction action) | |
{ | |
this.logger.LogInformation(issueCommentEvent.Comment.Body); | |
await this.client.Issue.Comment.Create( | |
repositoryId: issueCommentEvent.Repository.Id, | |
number: (int)issueCommentEvent.Issue.Number, | |
newComment: "Hello, world!" | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet new webapi --output octokit-webhooks-sample |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
info: Microsoft.Hosting.Lifetime[14] | |
Now listening on: http://localhost:5002 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ssh -R 80:localhost:5002 [email protected] | |
... | |
b49b69845954b1.lhrtunnel.link tunneled with tls termination, https://b49b69845954b1.lhrtunnel.link |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
info: MyWebhookEventProcessor[0] | |
Test comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Octokit.Webhooks; | |
using Octokit.Webhooks.AspNetCore; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddSingleton<WebhookEventProcessor, MyWebhookEventProcessor>(); | |
var app = builder.Build(); | |
app.UseRouting(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapGitHubWebhooks(); | |
}); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment