Skip to content

Commit

Permalink
test actualisation2
Browse files Browse the repository at this point in the history
  • Loading branch information
mackmarton committed Sep 5, 2024
1 parent f80c541 commit 4e19c99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ public async Task AppStartupSucceeds()
var host = new HostBuilder()
.ConfigureServices(services =>
{
// Register any services that are needed for the test
services.AddSingleton<Services.IGitHubClientFactory, Services.GitHubClientFactory>();
// Add more service registrations as needed
})
.Build();

await host.StartAsync();

// Additional assertions or operations can be performed here

await host.StopAsync();
}
}
Expand Down
37 changes: 26 additions & 11 deletions github-monitor/Ahk.GitHub.Monitor/GitHubMonitorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class GitHubMonitorFunction
private readonly IOptions<GitHubMonitorConfig> config;
private readonly ILogger<GitHubMonitorFunction> logger;

public GitHubMonitorFunction(IEventDispatchService eventDispatchService, IOptions<GitHubMonitorConfig> config, ILogger<GitHubMonitorFunction> logger)
public GitHubMonitorFunction(IEventDispatchService eventDispatchService, IOptions<GitHubMonitorConfig> config,
ILogger<GitHubMonitorFunction> logger)
{
this.eventDispatchService = eventDispatchService;
this.config = config;
Expand All @@ -27,27 +28,40 @@ public GitHubMonitorFunction(IEventDispatchService eventDispatchService, IOption

[Function("github-webhook")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData request)
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
HttpRequestData request)
{
if (string.IsNullOrEmpty(config.Value.GitHubWebhookSecret))
return new ObjectResult(new { error = "GitHub secret not configured" }) { StatusCode = StatusCodes.Status500InternalServerError };
return new ObjectResult(new { error = "GitHub secret not configured" })
{
StatusCode = StatusCodes.Status500InternalServerError
};

if (string.IsNullOrEmpty(config.Value.GitHubAppId) || string.IsNullOrEmpty(config.Value.GitHubAppPrivateKey))
return new ObjectResult(new { error = "GitHub App ID/Token not configured" }) { StatusCode = StatusCodes.Status500InternalServerError };
if (string.IsNullOrEmpty(config.Value.GitHubAppId) ||
string.IsNullOrEmpty(config.Value.GitHubAppPrivateKey))
return new ObjectResult(new { error = "GitHub App ID/Token not configured" })
{
StatusCode = StatusCodes.Status500InternalServerError
};

string eventName = request.Headers.GetValues("X-GitHub-Event").FirstOrDefault();
string deliveryId = request.Headers.GetValues("X-GitHub-Delivery").FirstOrDefault();
string receivedSignature = request.Headers.GetValues("X-Hub-Signature-256").FirstOrDefault();
request.Headers.TryGetValues("X-GitHub-Event", out var eventNameValues);
string eventName = eventNameValues?.FirstOrDefault();
request.Headers.TryGetValues("X-GitHub-Delivery", out var deliveryIdValues);
string deliveryId = deliveryIdValues?.FirstOrDefault();
request.Headers.TryGetValues("X-Hub-Signature-256", out var signatureValues);
string receivedSignature = signatureValues?.FirstOrDefault();

logger.LogInformation("Webhook delivery: Delivery id = '{DeliveryId}', Event name = '{EventName}'", deliveryId, eventName);
logger.LogInformation("Webhook delivery: Delivery id = '{DeliveryId}', Event name = '{EventName}'",
deliveryId, eventName);

if (string.IsNullOrEmpty(eventName))
return new BadRequestObjectResult(new { error = "X-GitHub-Event header missing" });
if (string.IsNullOrEmpty(receivedSignature))
return new BadRequestObjectResult(new { error = "X-Hub-Signature-256 header missing" });

string requestBody = await request.ReadAsStringAsync();
if (!GitHubSignatureValidator.IsSignatureValid(requestBody, receivedSignature, config.Value.GitHubWebhookSecret))
if (!GitHubSignatureValidator.IsSignatureValid(requestBody, receivedSignature,
config.Value.GitHubWebhookSecret))
return new BadRequestObjectResult(new { error = "Payload signature not valid" });

return await runCore(eventName, deliveryId, requestBody);
Expand All @@ -60,7 +74,8 @@ private async Task<IActionResult> runCore(string eventName, string deliveryId, s
try
{
await eventDispatchService.Process(eventName, requestBody, webhookResult, logger);
logger.LogInformation("Webhook delivery processed succesfully with Delivery id = '{DeliveryId}'", deliveryId);
logger.LogInformation("Webhook delivery processed succesfully with Delivery id = '{DeliveryId}'",
deliveryId);
}
catch (Exception ex)
{
Expand Down

0 comments on commit 4e19c99

Please sign in to comment.