Skip to content

Commit

Permalink
move some more logic
Browse files Browse the repository at this point in the history
  • Loading branch information
timbussmann committed May 22, 2024
1 parent b4537ba commit bb4f88a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
19 changes: 1 addition & 18 deletions Annoy-o-Bot/CallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<IActionResult> Run(
{
var cosmosWrapper = new CosmosClientWrapper(cosmosContainer);

if (!IsGitCommitCallback(req))
if (!GitHubCallbackRequest.IsGitCommitCallback(req, log))
{
return new OkResult();
}
Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 6 additions & 0 deletions Annoy-o-Bot/GitHub/Callbacks/CallbackModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace Annoy_o_Bot.GitHub.Callbacks
{
public class CallbackModel
{
public static CallbackModel FromJson(string json)
{
var requestObject = JsonConvert.DeserializeObject<CallbackModel>(json);
return requestObject;
}

public InstallationModel Installation { get; set; }
public RepositoryModel Repository { get; set; }
public CommitModel[] Commits { get; set; }
Expand Down
20 changes: 19 additions & 1 deletion Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CallbackModel> Validate(HttpRequest callbackRequest, string gitHubSecret)
{
if (!callbackRequest.Headers.TryGetValue("X-Hub-Signature-256", out var sha256SignatureHeaderValue))
Expand All @@ -20,7 +38,7 @@ public static async Task<CallbackModel> 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)
Expand Down
13 changes: 0 additions & 13 deletions Annoy-o-Bot/GitHub/Callbacks/RequestParser.cs

This file was deleted.

0 comments on commit bb4f88a

Please sign in to comment.