Some checks failed
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Docs CI / lint-and-preview (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Findings Ledger CI / build-test (push) Has been cancelled
Findings Ledger CI / migration-validation (push) Has been cancelled
Findings Ledger CI / generate-manifest (push) Has been cancelled
Lighthouse CI / Lighthouse Audit (push) Has been cancelled
Lighthouse CI / Axe Accessibility Audit (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Reachability Corpus Validation / validate-corpus (push) Has been cancelled
Reachability Corpus Validation / validate-ground-truths (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Reachability Corpus Validation / determinism-check (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
- Introduced `all-edge-reasons.json` to test edge resolution reasons in .NET. - Added `all-visibility-levels.json` to validate method visibility levels in .NET. - Created `dotnet-aspnetcore-minimal.json` for a minimal ASP.NET Core application. - Included `go-gin-api.json` for a Go Gin API application structure. - Added `java-spring-boot.json` for the Spring PetClinic application in Java. - Introduced `legacy-no-schema.json` for legacy application structure without schema. - Created `node-express-api.json` for an Express.js API application structure.
121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
using StellaOps.AirGap.Importer.Versioning;
|
|
|
|
namespace StellaOps.Cli.Services;
|
|
|
|
internal sealed class FileBundleVersionStore : IBundleVersionStore
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
|
{
|
|
WriteIndented = true
|
|
};
|
|
|
|
private readonly string _stateDirectory;
|
|
private readonly ILogger<FileBundleVersionStore> _logger;
|
|
|
|
public FileBundleVersionStore(string stateDirectory, ILogger<FileBundleVersionStore> logger)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(stateDirectory);
|
|
_stateDirectory = stateDirectory;
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public async Task<BundleVersionRecord?> GetCurrentAsync(
|
|
string tenantId,
|
|
string bundleType,
|
|
CancellationToken ct = default)
|
|
{
|
|
var history = await GetHistoryInternalAsync(tenantId, bundleType, ct).ConfigureAwait(false);
|
|
return history
|
|
.OrderByDescending(record => record.ActivatedAt)
|
|
.ThenByDescending(record => record.VersionString, StringComparer.Ordinal)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public async Task UpsertAsync(BundleVersionRecord record, CancellationToken ct = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(record);
|
|
|
|
Directory.CreateDirectory(_stateDirectory);
|
|
|
|
var path = GetStatePath(record.TenantId, record.BundleType);
|
|
var history = await GetHistoryInternalAsync(record.TenantId, record.BundleType, ct).ConfigureAwait(false);
|
|
|
|
history.Add(record);
|
|
|
|
var ordered = history
|
|
.OrderBy(r => r.ActivatedAt)
|
|
.ThenBy(r => r.VersionString, StringComparer.Ordinal)
|
|
.ToList();
|
|
|
|
var tempPath = path + ".tmp";
|
|
await using (var stream = File.Create(tempPath))
|
|
{
|
|
await JsonSerializer.SerializeAsync(stream, ordered, JsonOptions, ct).ConfigureAwait(false);
|
|
}
|
|
|
|
File.Copy(tempPath, path, overwrite: true);
|
|
File.Delete(tempPath);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<BundleVersionRecord>> GetHistoryAsync(
|
|
string tenantId,
|
|
string bundleType,
|
|
int limit = 10,
|
|
CancellationToken ct = default)
|
|
{
|
|
var history = await GetHistoryInternalAsync(tenantId, bundleType, ct).ConfigureAwait(false);
|
|
return history
|
|
.OrderByDescending(r => r.ActivatedAt)
|
|
.ThenByDescending(r => r.VersionString, StringComparer.Ordinal)
|
|
.Take(Math.Max(0, limit))
|
|
.ToArray();
|
|
}
|
|
|
|
private async Task<List<BundleVersionRecord>> GetHistoryInternalAsync(
|
|
string tenantId,
|
|
string bundleType,
|
|
CancellationToken ct)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(tenantId);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(bundleType);
|
|
|
|
var path = GetStatePath(tenantId, bundleType);
|
|
if (!File.Exists(path))
|
|
{
|
|
return new List<BundleVersionRecord>();
|
|
}
|
|
|
|
try
|
|
{
|
|
await using var stream = File.OpenRead(path);
|
|
var records = await JsonSerializer.DeserializeAsync<List<BundleVersionRecord>>(stream, JsonOptions, ct).ConfigureAwait(false);
|
|
return records ?? new List<BundleVersionRecord>();
|
|
}
|
|
catch (Exception ex) when (ex is IOException or JsonException)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to read bundle version history from {Path}", path);
|
|
return new List<BundleVersionRecord>();
|
|
}
|
|
}
|
|
|
|
private string GetStatePath(string tenantId, string bundleType)
|
|
{
|
|
var safeTenant = SanitizePathSegment(tenantId);
|
|
var safeBundleType = SanitizePathSegment(bundleType);
|
|
return Path.Combine(_stateDirectory, $"bundle-versions__{safeTenant}__{safeBundleType}.json");
|
|
}
|
|
|
|
private static string SanitizePathSegment(string value)
|
|
{
|
|
var trimmed = value.Trim().ToLowerInvariant();
|
|
var invalid = Path.GetInvalidFileNameChars();
|
|
var chars = trimmed
|
|
.Select(c => invalid.Contains(c) || c == '/' || c == '\\' || char.IsWhiteSpace(c) ? '_' : c)
|
|
.ToArray();
|
|
return new string(chars);
|
|
}
|
|
}
|
|
|