|
| 1 | +namespace Sigtran.NET.Core.Utilities; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Describes a package artifact that can participate in publication evaluation. |
| 5 | +/// </summary> |
| 6 | +public sealed class SigtranPackagePublicationArtifact |
| 7 | +{ |
| 8 | + /// <summary>Creates a package publication artifact reference.</summary> |
| 9 | + /// <param name="kind">The package artifact kind.</param> |
| 10 | + /// <param name="path">The retained artifact path.</param> |
| 11 | + /// <param name="sha256">The retained artifact SHA-256 digest.</param> |
| 12 | + /// <param name="sizeBytes">The retained artifact size in bytes.</param> |
| 13 | + /// <param name="required">Whether the artifact is required for publication.</param> |
| 14 | + public SigtranPackagePublicationArtifact( |
| 15 | + SigtranPackageArtifactKind kind, |
| 16 | + string path, |
| 17 | + string sha256, |
| 18 | + long sizeBytes, |
| 19 | + bool required) |
| 20 | + { |
| 21 | + Kind = kind; |
| 22 | + Path = string.IsNullOrWhiteSpace(path) ? throw new ArgumentException("Artifact path is required.", nameof(path)) : path; |
| 23 | + Sha256 = string.IsNullOrWhiteSpace(sha256) ? throw new ArgumentException("SHA-256 digest is required.", nameof(sha256)) : sha256; |
| 24 | + SizeBytes = sizeBytes >= 0 ? sizeBytes : throw new ArgumentOutOfRangeException(nameof(sizeBytes)); |
| 25 | + Required = required; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary>The package artifact kind.</summary> |
| 29 | + public SigtranPackageArtifactKind Kind { get; } |
| 30 | + |
| 31 | + /// <summary>The retained artifact path.</summary> |
| 32 | + public string Path { get; } |
| 33 | + |
| 34 | + /// <summary>The retained artifact SHA-256 digest.</summary> |
| 35 | + public string Sha256 { get; } |
| 36 | + |
| 37 | + /// <summary>The retained artifact size in bytes.</summary> |
| 38 | + public long SizeBytes { get; } |
| 39 | + |
| 40 | + /// <summary>Whether the artifact is required for publication.</summary> |
| 41 | + public bool Required { get; } |
| 42 | + |
| 43 | + /// <summary>Whether the artifact digest is a valid SHA-256 hex value.</summary> |
| 44 | + public bool HasValidDigest => Sha256.Length == 64 |
| 45 | + && Sha256.All(Uri.IsHexDigit); |
| 46 | + |
| 47 | + /// <summary>Whether the artifact has a non-empty retained size.</summary> |
| 48 | + public bool HasContent => SizeBytes > 0; |
| 49 | + |
| 50 | + /// <summary>Whether the artifact can participate in publication evaluation.</summary> |
| 51 | + public bool IsReady => !Required || (HasValidDigest && HasContent); |
| 52 | +} |
| 53 | + |
| 54 | +/// <summary> |
| 55 | +/// Binds package artifacts to a package publication request. |
| 56 | +/// </summary> |
| 57 | +public sealed class SigtranPackagePublicationArtifactSet |
| 58 | +{ |
| 59 | + /// <summary>Creates a package publication artifact set.</summary> |
| 60 | + /// <param name="request">The package publication request.</param> |
| 61 | + /// <param name="packageId">The package identifier.</param> |
| 62 | + /// <param name="artifacts">The retained package artifacts.</param> |
| 63 | + public SigtranPackagePublicationArtifactSet( |
| 64 | + SigtranPackagePublicationRequest request, |
| 65 | + string packageId, |
| 66 | + IReadOnlyList<SigtranPackagePublicationArtifact> artifacts) |
| 67 | + { |
| 68 | + Request = request ?? throw new ArgumentNullException(nameof(request)); |
| 69 | + PackageId = string.IsNullOrWhiteSpace(packageId) ? throw new ArgumentException("Package id is required.", nameof(packageId)) : packageId; |
| 70 | + ArgumentNullException.ThrowIfNull(artifacts); |
| 71 | + Artifacts = artifacts.Count == 0 ? throw new ArgumentException("At least one package artifact is required.", nameof(artifacts)) : artifacts.ToArray(); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary>The package publication request.</summary> |
| 75 | + public SigtranPackagePublicationRequest Request { get; } |
| 76 | + |
| 77 | + /// <summary>The package identifier.</summary> |
| 78 | + public string PackageId { get; } |
| 79 | + |
| 80 | + /// <summary>The retained package artifacts.</summary> |
| 81 | + public IReadOnlyList<SigtranPackagePublicationArtifact> Artifacts { get; } |
| 82 | + |
| 83 | + /// <summary>Whether the artifact set includes a NuGet package.</summary> |
| 84 | + public bool IncludesPackage => Artifacts.Any(static artifact => artifact.Kind == SigtranPackageArtifactKind.Package); |
| 85 | + |
| 86 | + /// <summary>Whether the artifact set includes a symbols package.</summary> |
| 87 | + public bool IncludesSymbolPackage => Artifacts.Any(static artifact => artifact.Kind == SigtranPackageArtifactKind.SymbolPackage); |
| 88 | + |
| 89 | + /// <summary>Whether each artifact kind appears only once.</summary> |
| 90 | + public bool UsesUniqueArtifactKinds => Artifacts |
| 91 | + .Select(static artifact => artifact.Kind) |
| 92 | + .Distinct() |
| 93 | + .Count() == Artifacts.Count; |
| 94 | + |
| 95 | + /// <summary>Whether required artifacts have valid digests and retained content.</summary> |
| 96 | + public bool RequiredArtifactsAreReady => Artifacts |
| 97 | + .Where(static artifact => artifact.Required) |
| 98 | + .All(static artifact => artifact.IsReady); |
| 99 | + |
| 100 | + /// <summary>Whether artifact paths match the requested package version.</summary> |
| 101 | + public bool PathsMatchRequestedVersion => Artifacts.All(artifact => |
| 102 | + artifact.Path.Contains($"{PackageId}.{Request.PackageVersion}", StringComparison.OrdinalIgnoreCase)); |
| 103 | + |
| 104 | + /// <summary>Whether the artifacts are ready for credential evaluation.</summary> |
| 105 | + public bool IsReadyForCredentialEvaluation => Request.IsReadyForArtifactBinding |
| 106 | + && IncludesPackage |
| 107 | + && IncludesSymbolPackage |
| 108 | + && UsesUniqueArtifactKinds |
| 109 | + && RequiredArtifactsAreReady |
| 110 | + && PathsMatchRequestedVersion; |
| 111 | + |
| 112 | + /// <summary>Creates a package integrity manifest from the bound publication artifacts.</summary> |
| 113 | + /// <returns>The package integrity manifest.</returns> |
| 114 | + public SigtranPackageIntegrityManifest ToIntegrityManifest() |
| 115 | + { |
| 116 | + SigtranPackageIntegrityManifest manifest = new(); |
| 117 | + foreach (SigtranPackagePublicationArtifact artifact in Artifacts) |
| 118 | + { |
| 119 | + manifest.Add(new SigtranPackageIntegrityEntry(artifact.Kind, artifact.Path, artifact.Sha256)); |
| 120 | + } |
| 121 | + |
| 122 | + return manifest; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary>Formats a compact package publication artifact set summary.</summary> |
| 126 | + /// <returns>The package publication artifact set summary.</returns> |
| 127 | + public string Describe() |
| 128 | + { |
| 129 | + return $"packagePublicationArtifactsReady={IsReadyForCredentialEvaluation} packageId={PackageId} artifacts={Artifacts.Count}"; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// <summary> |
| 134 | +/// Provides package publication artifact set helpers. |
| 135 | +/// </summary> |
| 136 | +public static class SigtranPackagePublicationArtifacts |
| 137 | +{ |
| 138 | + /// <summary>Creates a package publication artifact set.</summary> |
| 139 | + /// <param name="request">The package publication request.</param> |
| 140 | + /// <param name="packageId">The package identifier.</param> |
| 141 | + /// <param name="artifacts">The retained package artifacts.</param> |
| 142 | + /// <returns>The package publication artifact set.</returns> |
| 143 | + public static SigtranPackagePublicationArtifactSet Create( |
| 144 | + SigtranPackagePublicationRequest request, |
| 145 | + string packageId, |
| 146 | + IReadOnlyList<SigtranPackagePublicationArtifact> artifacts) |
| 147 | + { |
| 148 | + return new(request, packageId, artifacts); |
| 149 | + } |
| 150 | +} |
0 commit comments