Skip to content

Commit

Permalink
change LoadReminders
Browse files Browse the repository at this point in the history
  • Loading branch information
timbussmann committed Aug 13, 2024
1 parent bb4f88a commit fd61b83
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions Annoy-o-Bot/CallbackHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Annoy_o_Bot.CosmosDB;
Expand Down Expand Up @@ -75,25 +74,33 @@ public async Task<IActionResult> Run(
async Task ApplyReminderDefinitions(FileChanges reminderChanges, CallbackModel requestObject,
IGitHubRepository githubClient, CosmosClientWrapper cosmosWrapper, FileChanges fileChanges)
{
var newReminders = await LoadReminders(reminderChanges.New, requestObject, githubClient);
foreach (var (fileName, reminder) in newReminders)
foreach (var reminderPath in reminderChanges.New)
{
await CreateNewReminder(cosmosWrapper, requestObject, reminder, fileName, githubClient);
var newReminder = await LoadReminder(requestObject, githubClient, reminderPath);
if (newReminder != null)
{
await CreateNewReminder(cosmosWrapper, requestObject, newReminder, reminderPath, githubClient);
}
}

var updatedReminders = await LoadReminders(reminderChanges.Updated, requestObject, githubClient);
foreach (var (fileName, updatedReminder) in updatedReminders)
foreach (var reminderPath in reminderChanges.Updated)
{
var existingReminder = await cosmosWrapper.LoadReminder(fileName, requestObject.Installation.Id, requestObject.Repository.Id);
var existingReminderDefinition = await LoadReminder(requestObject, githubClient, reminderPath);
if (existingReminderDefinition == null)
{
continue;
}

var existingReminder = await cosmosWrapper.LoadReminder(reminderPath, requestObject.Installation.Id, requestObject.Repository.Id);
if (existingReminder is null)
{
await CreateNewReminder(cosmosWrapper, requestObject, updatedReminder, fileName, githubClient);
await CreateNewReminder(cosmosWrapper, requestObject, existingReminderDefinition, reminderPath, githubClient);
}
else
{
existingReminder.Reminder = updatedReminder;
existingReminder.Reminder = existingReminderDefinition;
// recalculate next reminder due time from scratch:
existingReminder.NextReminder = new DateTime(updatedReminder.Date.Ticks, DateTimeKind.Utc);
existingReminder.NextReminder = new DateTime(existingReminderDefinition.Date.Ticks, DateTimeKind.Utc);
if (existingReminder.LastReminder >= existingReminder.NextReminder)
{
// reminder start date is in the past, re-calculate next reminder due date with interval based on new start date.
Expand All @@ -102,7 +109,7 @@ async Task ApplyReminderDefinitions(FileChanges reminderChanges, CallbackModel r

await cosmosWrapper.AddOrUpdateReminder(existingReminder);
await githubClient.CreateComment(requestObject.HeadCommit.Id,
$"Updated reminder '{updatedReminder.Title}' for {existingReminder.NextReminder:D}");
$"Updated reminder '{existingReminderDefinition.Title}' for {existingReminder.NextReminder:D}");
}
}

Expand All @@ -112,11 +119,17 @@ await githubClient.CreateComment(requestObject.HeadCommit.Id,
async Task ValidateReminderDefinitions(FileChanges reminderChanges, CallbackModel requestObject,
IGitHubRepository githubClient)
{
List<(string, ReminderDefinition)> newReminders;
var atLeastOneReminderChecked = false;
try
{
newReminders = await LoadReminders(reminderChanges.New, requestObject, githubClient);
newReminders.AddRange(await LoadReminders(reminderChanges.Updated, requestObject, githubClient));
foreach (var reminder in reminderChanges.New.Concat(reminderChanges.Updated))
{
var reminderDefinition = await LoadReminder(requestObject, githubClient, reminder);
if (reminderDefinition != null)
{
atLeastOneReminderChecked = true;
}
}
}
catch (Exception e)
{
Expand All @@ -132,7 +145,7 @@ await TryCreateCheckRun(githubClient, requestObject.Repository.Id,
throw;
}

if (newReminders.Any())
if (atLeastOneReminderChecked)
{
await TryCreateCheckRun(githubClient, requestObject.Repository.Id,
new NewCheckRun("annoy-o-bot", requestObject.HeadCommit.Id)
Expand Down Expand Up @@ -170,24 +183,18 @@ private static async Task TryCreateCheckRun(IGitHubRepository installationClient
}
}

static async Task<List<(string, ReminderDefinition)>> LoadReminders(ICollection<string> filePaths, CallbackModel requestObject, IGitHubRepository installationClient)
static async Task<ReminderDefinition?> LoadReminder(CallbackModel requestObject, IGitHubRepository installationClient, string filePath)
{
var results = new List<(string, ReminderDefinition)>(filePaths.Count); // potentially lower but never higher than number of files
foreach (var filePath in filePaths)
var parser = ReminderParser.GetParser(filePath);
if (parser == null)
{
var parser = ReminderParser.GetParser(filePath);
if (parser == null)
{
// unsupported file type
continue;
}

var content = await installationClient.ReadFileContent(filePath, requestObject.Ref);
var reminder = parser.Parse(content);
results.Add((filePath, reminder));
// unsupported file type
return null;
}

return results;
var content = await installationClient.ReadFileContent(filePath, requestObject.Ref);
var reminder = parser.Parse(content);
return reminder;
}

async Task DeleteRemovedReminders(ICollection<string> deletedFiles, CosmosClientWrapper cosmosWrapper, CallbackModel requestObject, IGitHubRepository client)
Expand Down

0 comments on commit fd61b83

Please sign in to comment.