|
| 1 | +namespace Sigtran.NET.Core.Utilities; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Describes publication credential readiness for a package artifact set. |
| 5 | +/// </summary> |
| 6 | +public sealed class SigtranPackagePublicationCredentialReadiness |
| 7 | +{ |
| 8 | + /// <summary>Creates package publication credential readiness.</summary> |
| 9 | + /// <param name="artifactSet">The package publication artifact set.</param> |
| 10 | + /// <param name="credentialPolicy">The publication credential policy.</param> |
| 11 | + /// <param name="availableSecretNames">The available secret names.</param> |
| 12 | + /// <param name="evaluatedAtUtc">The UTC evaluation time.</param> |
| 13 | + public SigtranPackagePublicationCredentialReadiness( |
| 14 | + SigtranPackagePublicationArtifactSet artifactSet, |
| 15 | + SigtranPublicationCredentialPolicy credentialPolicy, |
| 16 | + IReadOnlySet<string> availableSecretNames, |
| 17 | + DateTimeOffset evaluatedAtUtc) |
| 18 | + { |
| 19 | + ArtifactSet = artifactSet ?? throw new ArgumentNullException(nameof(artifactSet)); |
| 20 | + CredentialPolicy = credentialPolicy ?? throw new ArgumentNullException(nameof(credentialPolicy)); |
| 21 | + ArgumentNullException.ThrowIfNull(availableSecretNames); |
| 22 | + AvailableSecretNames = availableSecretNames.ToHashSet(StringComparer.Ordinal); |
| 23 | + MissingSecretNames = CredentialPolicy.GetMissingSecrets(AvailableSecretNames); |
| 24 | + EvaluatedAtUtc = evaluatedAtUtc.Offset == TimeSpan.Zero ? evaluatedAtUtc : evaluatedAtUtc.ToUniversalTime(); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary>The package publication artifact set.</summary> |
| 28 | + public SigtranPackagePublicationArtifactSet ArtifactSet { get; } |
| 29 | + |
| 30 | + /// <summary>The publication credential policy.</summary> |
| 31 | + public SigtranPublicationCredentialPolicy CredentialPolicy { get; } |
| 32 | + |
| 33 | + /// <summary>The available secret names.</summary> |
| 34 | + public IReadOnlySet<string> AvailableSecretNames { get; } |
| 35 | + |
| 36 | + /// <summary>The missing secret names.</summary> |
| 37 | + public IReadOnlyList<string> MissingSecretNames { get; } |
| 38 | + |
| 39 | + /// <summary>The UTC evaluation time.</summary> |
| 40 | + public DateTimeOffset EvaluatedAtUtc { get; } |
| 41 | + |
| 42 | + /// <summary>Whether the evaluation time is normalized to UTC.</summary> |
| 43 | + public bool HasUtcEvaluationTime => EvaluatedAtUtc.Offset == TimeSpan.Zero; |
| 44 | + |
| 45 | + /// <summary>Whether every required publication secret is available by name.</summary> |
| 46 | + public bool HasRequiredSecrets => MissingSecretNames.Count == 0; |
| 47 | + |
| 48 | + /// <summary>Whether the NuGet API key secret is available by name.</summary> |
| 49 | + public bool HasNuGetApiKey => HasSecret(SigtranPublicationCredentialKind.NuGetApiKey); |
| 50 | + |
| 51 | + /// <summary>Whether signing certificate secrets are available by name.</summary> |
| 52 | + public bool HasSigningSecrets => HasSecret(SigtranPublicationCredentialKind.SigningCertificate) |
| 53 | + && HasSecret(SigtranPublicationCredentialKind.SigningCertificatePassword); |
| 54 | + |
| 55 | + /// <summary>Whether credential readiness can move into evidence assembly.</summary> |
| 56 | + public bool IsReadyForEvidenceAssembly => ArtifactSet.IsReadyForCredentialEvaluation |
| 57 | + && CredentialPolicy.RequiresCommercialSecrets |
| 58 | + && HasRequiredSecrets |
| 59 | + && HasUtcEvaluationTime; |
| 60 | + |
| 61 | + /// <summary>Formats a compact credential readiness summary.</summary> |
| 62 | + /// <returns>The credential readiness summary.</returns> |
| 63 | + public string Describe() |
| 64 | + { |
| 65 | + return $"packagePublicationCredentialsReady={IsReadyForEvidenceAssembly} missingSecrets={MissingSecretNames.Count} hasNuGetApiKey={HasNuGetApiKey} hasSigningSecrets={HasSigningSecrets}"; |
| 66 | + } |
| 67 | + |
| 68 | + private bool HasSecret(SigtranPublicationCredentialKind kind) |
| 69 | + { |
| 70 | + return CredentialPolicy.Credentials |
| 71 | + .Where(credential => credential.Kind == kind) |
| 72 | + .All(credential => AvailableSecretNames.Contains(credential.SecretName)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// <summary> |
| 77 | +/// Provides package publication credential readiness helpers. |
| 78 | +/// </summary> |
| 79 | +public static class SigtranPackagePublicationCredentialReadinessEvaluator |
| 80 | +{ |
| 81 | + /// <summary>Evaluates package publication credential readiness using the default publication policy.</summary> |
| 82 | + /// <param name="artifactSet">The package publication artifact set.</param> |
| 83 | + /// <param name="availableSecretNames">The available secret names.</param> |
| 84 | + /// <param name="evaluatedAtUtc">The UTC evaluation time.</param> |
| 85 | + /// <returns>The package publication credential readiness.</returns> |
| 86 | + public static SigtranPackagePublicationCredentialReadiness EvaluateDefault( |
| 87 | + SigtranPackagePublicationArtifactSet artifactSet, |
| 88 | + IReadOnlySet<string> availableSecretNames, |
| 89 | + DateTimeOffset evaluatedAtUtc) |
| 90 | + { |
| 91 | + return new( |
| 92 | + artifactSet, |
| 93 | + SigtranPublicationCredentials.CreateDefaultPolicy(), |
| 94 | + availableSecretNames, |
| 95 | + evaluatedAtUtc); |
| 96 | + } |
| 97 | +} |
0 commit comments