Skip to content

Commit

Permalink
Implement liquid templating for comment bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangiebel committed Jun 4, 2023
1 parent f8d8bce commit 1e523bb
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Configuration/GithubConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SS14.MapServer.Configuration;
namespace SS14.GithubApiHelper.Configuration;

public sealed class GithubConfiguration
{
Expand All @@ -8,4 +8,5 @@ public sealed class GithubConfiguration
public string? AppName { get; set; }
public string? AppPrivateKeyLocation { get; set; }
public int? AppId { get; set; }
}
public string? TemplateLocation { get; set; }
}
21 changes: 21 additions & 0 deletions Extensions/GithubTemplateServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Fluid;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SS14.GithubApiHelper.Services;

namespace SS14.GithubApiHelper.Extensions;

public static class GithubTemplateServiceExtension
{
public static void AddGithubTemplating(this IServiceCollection services)
{
services.AddSingleton<FluidParser>();
services.AddSingleton<GithubTemplateService>();
}

public static async Task PreloadGithubTemplates(this WebApplication application)
{
var templateService = application.Services.GetService<GithubTemplateService>();
await templateService?.LoadTemplates()!;
}
}
1 change: 1 addition & 0 deletions SS14.GithubApiHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Fluid.Core" Version="2.4.0" />
<PackageReference Include="GitHubJwt" Version="0.0.6" />
<PackageReference Include="Octokit" Version="6.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Services/AbstractGithubApiService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using Octokit;
using SS14.GithubApiHelper.Configuration;
using SS14.GithubApiHelper.Helpers;
using SS14.MapServer.Configuration;
using SS14.MapServer.Exceptions;
using ILogger = Serilog.ILogger;

Expand Down
96 changes: 96 additions & 0 deletions Services/GithubTemplateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Diagnostics;
using System.Globalization;
using Fluid;
using Microsoft.Extensions.Configuration;
using Serilog;
using SS14.GithubApiHelper.Configuration;

namespace SS14.GithubApiHelper.Services;

public sealed class GithubTemplateService
{
private const string TemplateFileSearchPattern = "*.liquid";

private readonly FluidParser _parser;

private readonly GithubConfiguration _configuration = new();
private readonly Dictionary<string, IFluidTemplate> _templates = new();
private readonly ILogger _log;

public GithubTemplateService(FluidParser parser, IConfiguration configuration)
{
_parser = parser;
configuration.Bind(GithubConfiguration.Name, _configuration);
_log = Log.ForContext<GithubTemplateService>();
}

public async Task LoadTemplates()
{
var path = _configuration.TemplateLocation;

if (path == null)
{
_log.Error("Tried to load templates without template path configured [Github.TemplateLocation]");
return;
}

if (!Directory.Exists(path))
{
_log.Error("Template path doesn't exist: {TemplatePath}", path);
return;
}

_log.Information("Preloading github message templates");

var directory = new DirectoryInfo(path);

var enumerationOptions = new EnumerationOptions
{
RecurseSubdirectories = true,
MaxRecursionDepth = 5
};

foreach (var templateFile in directory.EnumerateFiles(TemplateFileSearchPattern, enumerationOptions))
{
var rawTemplate = await File.ReadAllTextAsync(templateFile.FullName);
if (!_parser.TryParse(rawTemplate, out var template, out var error))
{
_log.Error(
"Failed to parse template: {TemplateName}.\n{ErrorMessage}",
templateFile.Name,
error
);
continue;
}

var templateName = Path.GetRelativePath(path, templateFile.FullName);
templateName = templateName.Replace(Path.GetExtension(templateName), "");
_templates.Add(templateName, template);
}

_log.Information("Loaded {TemplateCount} templates", _templates.Count);
}

public async Task<string> RenderTemplate(string templateName, object? model = null, CultureInfo? culture = null)
{
model ??= new { };
culture ??= CultureInfo.InvariantCulture;

if (!_templates.TryGetValue(templateName, out var template))
{
_log.Error("No template with name: {TemplateName}", templateName);
return "";
}

var context = new TemplateContext(model)
{
CultureInfo = culture,
Options =
{
Greedy = true
}
};

return await template.RenderAsync(context);
}
}

0 comments on commit 1e523bb

Please sign in to comment.