From bb4f88a215d38d0360f59df1db96640aa8b01ea8 Mon Sep 17 00:00:00 2001 From: Tim Bussmann Date: Wed, 22 May 2024 22:51:31 +0200 Subject: [PATCH] move some more logic --- Annoy-o-Bot/CallbackHandler.cs | 19 +----------------- Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs | 6 ++++++ .../GitHub/Callbacks/GitHubCallbackRequest.cs | 20 ++++++++++++++++++- Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs | 13 ------------ 4 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs diff --git a/Annoy-o-Bot/CallbackHandler.cs b/Annoy-o-Bot/CallbackHandler.cs index 0ab1fb2..9f88f98 100644 --- a/Annoy-o-Bot/CallbackHandler.cs +++ b/Annoy-o-Bot/CallbackHandler.cs @@ -31,7 +31,7 @@ public async Task Run( { var cosmosWrapper = new CosmosClientWrapper(cosmosContainer); - if (!IsGitCommitCallback(req)) + if (!GitHubCallbackRequest.IsGitCommitCallback(req, log)) { return new OkResult(); } @@ -143,23 +143,6 @@ await TryCreateCheckRun(githubClient, requestObject.Repository.Id, } } - bool IsGitCommitCallback(HttpRequest req) - { - if (!req.Headers.TryGetValue("X-GitHub-Event", out var callbackEvent) || callbackEvent != "push") - { - // Check for known callback types that we don't care - if (callbackEvent != "check_suite") // ignore check_suite events - { - // record unknown callback types to further analyze them - log.LogWarning($"Non-push callback. 'X-GitHub-Event': '{callbackEvent}'"); - } - - return false; - } - - return true; - } - async Task CreateNewReminder(CosmosClientWrapper cosmosWrapper, CallbackModel requestObject, ReminderDefinition reminderDefinition, string fileName, IGitHubRepository githubClient) { diff --git a/Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs b/Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs index b74ed3d..5d7f14d 100644 --- a/Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs +++ b/Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs @@ -7,6 +7,12 @@ namespace Annoy_o_Bot.GitHub.Callbacks { public class CallbackModel { + public static CallbackModel FromJson(string json) + { + var requestObject = JsonConvert.DeserializeObject(json); + return requestObject; + } + public InstallationModel Installation { get; set; } public RepositoryModel Repository { get; set; } public CommitModel[] Commits { get; set; } diff --git a/Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs b/Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs index 87b83e5..509c7a6 100644 --- a/Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs +++ b/Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs @@ -4,11 +4,29 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; namespace Annoy_o_Bot.GitHub.Callbacks; public class GitHubCallbackRequest { + public static bool IsGitCommitCallback(HttpRequest callbackRequest, ILogger log) + { + if (!callbackRequest.Headers.TryGetValue("X-GitHub-Event", out var callbackEvent) || callbackEvent != "push") + { + // Check for known callback types that we don't care + if (callbackEvent != "check_suite") // ignore check_suite events + { + // record unknown callback types to further analyze them + log.LogWarning($"Non-push callback. 'X-GitHub-Event': '{callbackEvent}'"); + } + + return false; + } + + return true; + } + public static async Task Validate(HttpRequest callbackRequest, string gitHubSecret) { if (!callbackRequest.Headers.TryGetValue("X-Hub-Signature-256", out var sha256SignatureHeaderValue)) @@ -20,7 +38,7 @@ public static async Task Validate(HttpRequest callbackRequest, st await ValidateSignature(requestBody, gitHubSecret, sha256SignatureHeaderValue.ToString().Replace("sha256=", "")); - return RequestParser.ParseJson(requestBody); + return CallbackModel.FromJson(requestBody); } public static async Task ValidateSignature(string signedText, string secret, string sha256Signature) diff --git a/Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs b/Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs deleted file mode 100644 index 048ad60..0000000 --- a/Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Newtonsoft.Json; - -namespace Annoy_o_Bot.GitHub.Callbacks -{ - public class RequestParser - { - public static CallbackModel ParseJson(string requestBody) - { - var requestObject = JsonConvert.DeserializeObject(requestBody); - return requestObject; - } - } -} \ No newline at end of file