Skip to content

Commit 45ccee6

Browse files
authored
chore: Add cancellationToken to all methods (#708)
1 parent c1db98b commit 45ccee6

File tree

5 files changed

+509
-428
lines changed

5 files changed

+509
-428
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Libraries to handle GitHub Webhooks in .NET applications.
5656
```C#
5757
public sealed class MyWebhookEventProcessor : WebhookEventProcessor
5858
{
59-
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
59+
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
6060
{
6161
...
6262
}

samples/AspNetCore/MyWebhookEventProcessor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
namespace AspNetCore;
1+
namespace AspNetCore;
22

3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Logging;
56
using Octokit.Webhooks;
@@ -8,17 +9,17 @@
89

910
public class MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) : WebhookEventProcessor
1011
{
11-
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
12+
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
1213
{
1314
switch (action)
1415
{
1516
case PullRequestActionValue.Opened:
1617
logger.LogInformation("pull request opened");
17-
await Task.Delay(1000);
18+
await Task.Delay(1000, cancellationToken);
1819
break;
1920
default:
2021
logger.LogInformation("Some other pull request event");
21-
await Task.Delay(1000);
22+
await Task.Delay(1000, cancellationToken);
2223
break;
2324
}
2425
}

samples/AzureFunctions/MyWebhookEventProcessor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace AzureFunctions;
22

3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Logging;
56
using Octokit.Webhooks;
@@ -8,17 +9,17 @@ namespace AzureFunctions;
89

910
public class MyWebhookEventProcessor(ILogger<MyWebhookEventProcessor> logger) : WebhookEventProcessor
1011
{
11-
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
12+
protected override async Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action, CancellationToken cancellationToken = default)
1213
{
1314
switch (action)
1415
{
1516
case PullRequestActionValue.Opened:
1617
logger.LogInformation("pull request opened");
17-
await Task.Delay(1000);
18+
await Task.Delay(1000, cancellationToken);
1819
break;
1920
default:
2021
logger.LogInformation("Some other pull request event");
21-
await Task.Delay(1000);
22+
await Task.Delay(1000, cancellationToken);
2223
break;
2324
}
2425
}

src/Octokit.Webhooks.AspNetCore/GitHubWebhookExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
namespace Octokit.Webhooks.AspNetCore;
1+
namespace Octokit.Webhooks.AspNetCore;
22

33
using System;
44
using System.Globalization;
55
using System.IO;
66
using System.Net.Mime;
77
using System.Security.Cryptography;
88
using System.Text;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Builder;
1112
using Microsoft.AspNetCore.Http;
@@ -23,7 +24,8 @@ public static partial class GitHubWebhookExtensions
2324
public static IEndpointConventionBuilder MapGitHubWebhooks(
2425
this IEndpointRouteBuilder endpoints,
2526
string path = "/api/github/webhooks",
26-
string? secret = null)
27+
string? secret = null,
28+
bool cancelOnRequestAborted = false)
2729
{
2830
var options = endpoints.ServiceProvider.GetService<IOptionsMonitor<GitHubWebhookOptions>>();
2931
return endpoints.MapPost(
@@ -58,7 +60,7 @@ public static IEndpointConventionBuilder MapGitHubWebhooks(
5860
try
5961
{
6062
var service = context.RequestServices.GetRequiredService<WebhookEventProcessor>();
61-
await service.ProcessWebhookAsync(context.Request.Headers, body)
63+
await service.ProcessWebhookAsync(context.Request.Headers, body, cancelOnRequestAborted ? context.RequestAborted : CancellationToken.None)
6264
.ConfigureAwait(false);
6365
context.Response.StatusCode = 200;
6466
}

0 commit comments

Comments
 (0)