prototype of pre-build yaml hash injection

This commit is contained in:
Ben 2024-03-25 13:48:20 +01:00
parent 87ec67778b
commit 03a3ccb4de
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
5 changed files with 60 additions and 1 deletions

View File

@ -1,4 +1,4 @@
using Core;
using Core;
using KubernetesWorkflow.Types;
namespace CodexPlugin
@ -9,6 +9,8 @@ namespace CodexPlugin
private readonly IPluginTools tools;
private readonly CodexLogLevel defaultLogLevel = CodexLogLevel.Trace;
private const string OpenApiYamlHash = "<CODEX_OPENAPI_HASH_HERE>";
public CodexPlugin(IPluginTools tools)
{
codexStarter = new CodexStarter(tools);

View File

@ -34,4 +34,8 @@
<ProjectReference Include="..\MetricsPlugin\MetricsPlugin.csproj" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="dotnet run -p $(ProjectDir)\..\CodexPluginPrebuild" />
</Target>
</Project>

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,36 @@
using System.Security.Cryptography;
public static class Program
{
private const string OpenApiFile = "../CodexPlugin/openapi.yaml";
private const string Search = "<CODEX_OPENAPI_HASH_HERE>";
private const string TargetFile = "CodexPlugin.cs";
private static string CreateHash()
{
var fileBytes = File.ReadAllBytes(OpenApiFile);
var sha = SHA256.Create();
var hash = sha.ComputeHash(fileBytes);
return BitConverter.ToString(hash);
}
private static void SearchAndReplace(string hash)
{
var lines = File.ReadAllLines(TargetFile);
lines = lines.Select(l => l.Replace(Search, hash)).ToArray();
File.WriteAllLines(TargetFile, lines);
}
public static void Main(string[] args)
{
Console.WriteLine("Injecting hash of 'openapi.yaml'...");
// This hash is used to verify that the Codex docker image being used is compatible
// with the openapi.yaml being used by the Codex plugin.
// If the openapi.yaml files don't match, an exception is thrown.
var hash = CreateHash();
SearchAndReplace(hash);
Console.WriteLine("Done!");
}
}

View File

@ -59,6 +59,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GethConnector", "Framework\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordRewards", "Framework\DiscordRewards\DiscordRewards.csproj", "{B07820C4-309F-4454-BCC1-1D4902C9C67B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodexPluginPrebuild", "ProjectPlugins\CodexPluginPrebuild\CodexPluginPrebuild.csproj", "{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -161,6 +163,10 @@ Global
{B07820C4-309F-4454-BCC1-1D4902C9C67B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B07820C4-309F-4454-BCC1-1D4902C9C67B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B07820C4-309F-4454-BCC1-1D4902C9C67B}.Release|Any CPU.Build.0 = Release|Any CPU
{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -190,6 +196,7 @@ Global
{570C0DBE-0EF1-47B5-9A3B-E1F7895722A5} = {7591C5B3-D86E-4AE4-8ED2-B272D17FE7E3}
{F730DA73-1C92-4107-BCFB-D33759DAB0C3} = {81AE04BC-CBFA-4E6F-B039-8208E9AFAAE7}
{B07820C4-309F-4454-BCC1-1D4902C9C67B} = {81AE04BC-CBFA-4E6F-B039-8208E9AFAAE7}
{88C212E9-308A-46A4-BAAD-468E8EBD8EDF} = {8F1F1C2A-E313-4E0C-BE40-58FB0BA91124}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {237BF0AA-9EC4-4659-AD9A-65DEB974250C}