Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implement `SbomIngestServiceCollectionExtensionsTests` to verify the SBOM ingestion pipeline exports snapshots correctly. - Create `SbomIngestTransformerTests` to ensure the transformation produces expected nodes and edges, including deduplication of license nodes and normalization of timestamps. - Add `SbomSnapshotExporterTests` to test the export functionality for manifest, adjacency, nodes, and edges. - Introduce `VexOverlayTransformerTests` to validate the transformation of VEX nodes and edges. - Set up project file for the test project with necessary dependencies and configurations. - Include JSON fixture files for testing purposes.
54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace StellaOps.EvidenceLocker.Infrastructure.Db;
|
|
|
|
internal sealed class MigrationScript
|
|
{
|
|
private static readonly Regex VersionRegex = new(@"^(?<version>\d{3,})[_-]", RegexOptions.Compiled);
|
|
|
|
private MigrationScript(int version, string name, string sql)
|
|
{
|
|
Version = version;
|
|
Name = name;
|
|
Sql = sql;
|
|
Sha256 = ComputeSha256(sql);
|
|
}
|
|
|
|
public int Version { get; }
|
|
|
|
public string Name { get; }
|
|
|
|
public string Sql { get; }
|
|
|
|
public string Sha256 { get; }
|
|
|
|
public static bool TryCreate(string resourceName, string sql, [NotNullWhen(true)] out MigrationScript? script)
|
|
{
|
|
var fileName = resourceName.Split('.').Last();
|
|
var match = VersionRegex.Match(fileName);
|
|
|
|
if (!match.Success || !int.TryParse(match.Groups["version"].Value, out var version))
|
|
{
|
|
script = null;
|
|
return false;
|
|
}
|
|
|
|
script = new MigrationScript(version, fileName, sql);
|
|
return true;
|
|
}
|
|
|
|
private static string ComputeSha256(string sql)
|
|
{
|
|
var normalized = NormalizeLineEndings(sql);
|
|
var bytes = Encoding.UTF8.GetBytes(normalized);
|
|
var hash = SHA256.HashData(bytes);
|
|
return Convert.ToHexString(hash).ToLowerInvariant();
|
|
}
|
|
|
|
private static string NormalizeLineEndings(string value)
|
|
=> value.Replace("\r\n", "\n", StringComparison.Ordinal);
|
|
}
|