|
| 1 | +using System.Globalization; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Sigtran.NET.Core.Utilities; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Describes a retained commercial evidence run approval report. |
| 9 | +/// </summary> |
| 10 | +public sealed class SigtranCommercialEvidenceRunApprovalReportWriteResult |
| 11 | +{ |
| 12 | + /// <summary>Creates a retained commercial evidence run approval report result.</summary> |
| 13 | + /// <param name="manifest">The approval manifest rendered into the report.</param> |
| 14 | + /// <param name="reportPath">The retained report path.</param> |
| 15 | + /// <param name="reportSha256">The retained report SHA-256 digest.</param> |
| 16 | + /// <param name="writtenAtUtc">The UTC report write time.</param> |
| 17 | + public SigtranCommercialEvidenceRunApprovalReportWriteResult( |
| 18 | + SigtranCommercialEvidenceRunApprovalManifest manifest, |
| 19 | + string reportPath, |
| 20 | + string reportSha256, |
| 21 | + DateTimeOffset writtenAtUtc) |
| 22 | + { |
| 23 | + Manifest = manifest ?? throw new ArgumentNullException(nameof(manifest)); |
| 24 | + ReportPath = string.IsNullOrWhiteSpace(reportPath) ? throw new ArgumentException("Report path is required.", nameof(reportPath)) : reportPath; |
| 25 | + ReportSha256 = string.IsNullOrWhiteSpace(reportSha256) ? throw new ArgumentException("Report SHA-256 digest is required.", nameof(reportSha256)) : reportSha256; |
| 26 | + WrittenAtUtc = writtenAtUtc.Offset == TimeSpan.Zero ? writtenAtUtc : writtenAtUtc.ToUniversalTime(); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary>The approval manifest rendered into the report.</summary> |
| 30 | + public SigtranCommercialEvidenceRunApprovalManifest Manifest { get; } |
| 31 | + |
| 32 | + /// <summary>The retained report path.</summary> |
| 33 | + public string ReportPath { get; } |
| 34 | + |
| 35 | + /// <summary>The retained report SHA-256 digest.</summary> |
| 36 | + public string ReportSha256 { get; } |
| 37 | + |
| 38 | + /// <summary>The UTC report write time.</summary> |
| 39 | + public DateTimeOffset WrittenAtUtc { get; } |
| 40 | + |
| 41 | + /// <summary>Whether the report exists on disk.</summary> |
| 42 | + public bool ReportExists => File.Exists(ReportPath); |
| 43 | + |
| 44 | + /// <summary>The retained report size in bytes.</summary> |
| 45 | + public long ReportSizeBytes => ReportExists ? new FileInfo(ReportPath).Length : 0; |
| 46 | + |
| 47 | + /// <summary>Whether the report digest is a valid SHA-256 hex value.</summary> |
| 48 | + public bool HasValidReportDigest => ReportSha256.Length == 64 |
| 49 | + && ReportSha256.All(Uri.IsHexDigit); |
| 50 | + |
| 51 | + /// <summary>Whether the report write time is normalized to UTC.</summary> |
| 52 | + public bool HasUtcWriteTime => WrittenAtUtc.Offset == TimeSpan.Zero; |
| 53 | + |
| 54 | + /// <summary>Whether the retained report is ready for promotion package creation.</summary> |
| 55 | + public bool IsReady => Manifest.IsReady |
| 56 | + && ReportExists |
| 57 | + && ReportSizeBytes > 0 |
| 58 | + && HasValidReportDigest |
| 59 | + && HasUtcWriteTime; |
| 60 | + |
| 61 | + /// <summary>Formats a compact approval report write summary.</summary> |
| 62 | + /// <returns>The approval report write summary.</returns> |
| 63 | + public string Describe() |
| 64 | + { |
| 65 | + return $"commercialEvidenceRunApprovalReportReady={IsReady} report={ReportPath}"; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// <summary> |
| 70 | +/// Provides retained commercial evidence run approval report helpers. |
| 71 | +/// </summary> |
| 72 | +public static class SigtranCommercialEvidenceRunApprovalReportWriters |
| 73 | +{ |
| 74 | + /// <summary>Writes a retained Markdown approval report.</summary> |
| 75 | + /// <param name="manifest">The approval manifest to render.</param> |
| 76 | + /// <param name="outputDirectory">The output directory.</param> |
| 77 | + /// <param name="writtenAtUtc">The UTC report write time.</param> |
| 78 | + /// <returns>The retained approval report write result.</returns> |
| 79 | + public static SigtranCommercialEvidenceRunApprovalReportWriteResult WriteReport( |
| 80 | + SigtranCommercialEvidenceRunApprovalManifest manifest, |
| 81 | + string outputDirectory, |
| 82 | + DateTimeOffset writtenAtUtc) |
| 83 | + { |
| 84 | + ArgumentNullException.ThrowIfNull(manifest); |
| 85 | + string directory = string.IsNullOrWhiteSpace(outputDirectory) ? throw new ArgumentException("Output directory is required.", nameof(outputDirectory)) : outputDirectory; |
| 86 | + Directory.CreateDirectory(directory); |
| 87 | + |
| 88 | + string reportPath = Path.Combine(directory, "commercial-evidence-run-approval-report.md"); |
| 89 | + string report = RenderMarkdown(manifest, writtenAtUtc); |
| 90 | + File.WriteAllText(reportPath, report, Encoding.UTF8); |
| 91 | + string digest = ComputeSha256(File.ReadAllBytes(reportPath)); |
| 92 | + |
| 93 | + return new(manifest, reportPath, digest, writtenAtUtc); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary>Renders a Markdown approval report.</summary> |
| 97 | + /// <param name="manifest">The approval manifest to render.</param> |
| 98 | + /// <param name="writtenAtUtc">The UTC report write time.</param> |
| 99 | + /// <returns>The rendered Markdown report.</returns> |
| 100 | + public static string RenderMarkdown( |
| 101 | + SigtranCommercialEvidenceRunApprovalManifest manifest, |
| 102 | + DateTimeOffset writtenAtUtc) |
| 103 | + { |
| 104 | + ArgumentNullException.ThrowIfNull(manifest); |
| 105 | + DateTimeOffset writtenAt = writtenAtUtc.Offset == TimeSpan.Zero ? writtenAtUtc : writtenAtUtc.ToUniversalTime(); |
| 106 | + SigtranCommercialEvidenceApprovedRunTarget target = manifest.Checklist.Target; |
| 107 | + StringBuilder builder = new(); |
| 108 | + |
| 109 | + builder.AppendLine("# Commercial Evidence Run Approval Report"); |
| 110 | + builder.AppendLine(); |
| 111 | + builder.Append("- Run id: `").Append(target.RunId).AppendLine("`"); |
| 112 | + builder.Append("- Package version: `").Append(target.PackageVersion).AppendLine("`"); |
| 113 | + builder.Append("- Source commit: `").Append(target.SourceCommit).AppendLine("`"); |
| 114 | + builder.Append("- Artifact root: `").Append(target.ArtifactRoot).AppendLine("`"); |
| 115 | + builder.Append("- Checklist digest: `").Append(manifest.ChecklistSha256).AppendLine("`"); |
| 116 | + builder.Append("- Manifest ready: `").Append(manifest.IsReady).AppendLine("`"); |
| 117 | + builder.Append("- Written at UTC: `").Append(writtenAt.ToString("O", CultureInfo.InvariantCulture)).AppendLine("`"); |
| 118 | + builder.AppendLine(); |
| 119 | + builder.AppendLine("## Reviewer Approvals"); |
| 120 | + builder.AppendLine(); |
| 121 | + |
| 122 | + foreach (SigtranCommercialEvidenceReviewerApproval approval in manifest.Approvals) |
| 123 | + { |
| 124 | + builder.Append("- `").Append(approval.Role).Append("`: `") |
| 125 | + .Append(approval.Approved) |
| 126 | + .Append("` by `") |
| 127 | + .Append(approval.ReviewerName) |
| 128 | + .AppendLine("`"); |
| 129 | + } |
| 130 | + |
| 131 | + return builder.ToString(); |
| 132 | + } |
| 133 | + |
| 134 | + private static string ComputeSha256(byte[] content) |
| 135 | + { |
| 136 | + return Convert.ToHexString(SHA256.HashData(content)).ToLowerInvariant(); |
| 137 | + } |
| 138 | +} |
0 commit comments