Skip to content

Commit

Permalink
simplify check
Browse files Browse the repository at this point in the history
  • Loading branch information
timbussmann committed Aug 13, 2024
1 parent 2ef3236 commit 9653343
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 0 additions & 2 deletions Annoy-o-Bot.Tests/Parser/JsonReminderParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using Annoy_o_Bot.Parser;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Xunit;

namespace Annoy_o_Bot.Tests.Parser
Expand Down
16 changes: 6 additions & 10 deletions Annoy-o-Bot/CallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ public async Task<IActionResult> Run(
var secret = configuration.GetValue<string>("WebhookSecret") ??
throw new Exception("Missing 'WebhookSecret' setting to validate GitHub callbacks.");
var commitModel = await GitHubCallbackRequest.Validate(req, secret, log);
if (commitModel == null)
{
return new OkResult();
}

if (commitModel.HeadCommit == null)
if (commitModel?.HeadCommit == null)
{
// no commits on push (e.g. branch delete)
return new OkResult();
Expand Down Expand Up @@ -71,7 +67,7 @@ public async Task<IActionResult> Run(
return new OkResult();
}

async Task ApplyReminderDefinitions(FileChanges reminderChanges, CallbackModel requestObject,
async Task ApplyReminderDefinitions(FileChanges reminderChanges, GitPushCallbackModel requestObject,
IGitHubRepository githubClient, CosmosClientWrapper cosmosWrapper, FileChanges fileChanges)
{
var newReminders = await LoadReminders(reminderChanges.New, requestObject, githubClient);
Expand Down Expand Up @@ -108,7 +104,7 @@ await githubClient.CreateComment(requestObject.HeadCommit.Id,
await DeleteRemovedReminders(fileChanges.Deleted, cosmosWrapper, requestObject, githubClient);
}

async Task ValidateReminderDefinitions(FileChanges reminderChanges, CallbackModel requestObject,
async Task ValidateReminderDefinitions(FileChanges reminderChanges, GitPushCallbackModel requestObject,
IGitHubRepository githubClient)
{
List<(string, ReminderDefinition)> newReminders;
Expand Down Expand Up @@ -142,7 +138,7 @@ await TryCreateCheckRun(githubClient, requestObject.Repository.Id,
}
}

async Task CreateNewReminder(CosmosClientWrapper cosmosWrapper, CallbackModel requestObject, ReminderDefinition reminderDefinition, string fileName,
async Task CreateNewReminder(CosmosClientWrapper cosmosWrapper, GitPushCallbackModel requestObject, ReminderDefinition reminderDefinition, string fileName,
IGitHubRepository githubClient)
{
var reminderDocument = ReminderDocument.New(
Expand All @@ -169,7 +165,7 @@ private static async Task TryCreateCheckRun(IGitHubRepository installationClient
}
}

static async Task<List<(string, ReminderDefinition)>> LoadReminders(ICollection<string> filePaths, CallbackModel requestObject, IGitHubRepository installationClient)
static async Task<List<(string, ReminderDefinition)>> LoadReminders(ICollection<string> filePaths, GitPushCallbackModel requestObject, IGitHubRepository installationClient)
{
var results = new List<(string, ReminderDefinition)>(filePaths.Count); // potentially lower but never higher than number of files
foreach (var filePath in filePaths)
Expand All @@ -189,7 +185,7 @@ private static async Task TryCreateCheckRun(IGitHubRepository installationClient
return results;
}

async Task DeleteRemovedReminders(ICollection<string> deletedFiles, CosmosClientWrapper cosmosWrapper, CallbackModel requestObject, IGitHubRepository client)
async Task DeleteRemovedReminders(ICollection<string> deletedFiles, CosmosClientWrapper cosmosWrapper, GitPushCallbackModel requestObject, IGitHubRepository client)
{
foreach (var deletedReminder in deletedFiles)
{
Expand Down
4 changes: 2 additions & 2 deletions Annoy-o-Bot/GitHub/Callbacks/GitHubCallbackRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Annoy_o_Bot.GitHub.Callbacks;

public class GitHubCallbackRequest
{
public static async Task<CallbackModel?> Validate(HttpRequest callbackRequest, string gitHubSecret, ILogger log)
public static async Task<GitPushCallbackModel?> Validate(HttpRequest callbackRequest, string gitHubSecret, ILogger log)
{
if (!IsGitCommitCallback(callbackRequest, log))
{
Expand All @@ -27,7 +27,7 @@ public class GitHubCallbackRequest

await ValidateSignature(requestBody, gitHubSecret, sha256SignatureHeaderValue.ToString().Replace("sha256=", ""));

return JsonConvert.DeserializeObject<CallbackModel>(requestBody);
return JsonConvert.DeserializeObject<GitPushCallbackModel>(requestBody);
}

static bool IsGitCommitCallback(HttpRequest callbackRequest, ILogger log)
Expand Down

0 comments on commit 9653343

Please sign in to comment.