|
| 1 | +using ModularityKit.Mutator.Abstractions.Effects; |
| 2 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 3 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 4 | +using PolicyComposition.State; |
| 5 | + |
| 6 | +namespace PolicyComposition.Policies.Approval; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Blocks release promotion until the expected approval threshold is present. |
| 10 | +/// </summary> |
| 11 | +/// <remarks> |
| 12 | +/// The policy reads the approval count from mutation metadata, so the example can |
| 13 | +/// show how governance input travels alongside the mutation itself. |
| 14 | +/// When the threshold is met, the policy marks the release as approved and emits |
| 15 | +/// an audit side effect. When it is not met, the policy returns a requirement and |
| 16 | +/// error severity so the composed result can surface the missing approval. |
| 17 | +/// </remarks> |
| 18 | +internal sealed class RequireApprovalsPolicy : IMutationPolicy<ReleaseGateState> |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Stable policy identifier used in diagnostics and composition metadata. |
| 22 | + /// </summary> |
| 23 | + public string Name => "RequireApprovals"; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Higher than the audit only policy, so approval gating is evaluated first. |
| 27 | + /// </summary> |
| 28 | + public int Priority => 300; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Explains the minimum-approval requirement that this policy enforces. |
| 32 | + /// </summary> |
| 33 | + public string Description => "Requires at least two approvals before the release can proceed."; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Reads the approval count from mutation metadata and either allows or blocks the release. |
| 37 | + /// </summary> |
| 38 | + /// <param name="mutation">The mutation carrying government metadata.</param> |
| 39 | + /// <param name="state">The current release state.</param> |
| 40 | + /// <returns> |
| 41 | + /// An allowed decision when approvals are enough, otherwise a blocking |
| 42 | + /// decision with an approval requirement and error severity. |
| 43 | + /// </returns> |
| 44 | + public PolicyDecision Evaluate(IMutation<ReleaseGateState> mutation, ReleaseGateState state) |
| 45 | + { |
| 46 | + var approvals = GetInt32(mutation.Context.Metadata, "approvals"); |
| 47 | + |
| 48 | + return approvals >= 2 |
| 49 | + ? new PolicyDecision |
| 50 | + { |
| 51 | + IsAllowed = true, |
| 52 | + PolicyName = Name, |
| 53 | + Modifications = new Dictionary<string, object> |
| 54 | + { |
| 55 | + ["State"] = state with { Stage = "Approved" }, |
| 56 | + ["SideEffect"] = SideEffect.Create("audit", $"Release approved with {approvals} approvals.") |
| 57 | + }, |
| 58 | + Metadata = new Dictionary<string, object> |
| 59 | + { |
| 60 | + ["approvalCount"] = approvals |
| 61 | + } |
| 62 | + } |
| 63 | + : new PolicyDecision |
| 64 | + { |
| 65 | + IsAllowed = false, |
| 66 | + PolicyName = Name, |
| 67 | + Severity = PolicyDecisionSeverity.Error, |
| 68 | + Reason = $"Release requires at least two approvals; found {approvals}.", |
| 69 | + Requirements = |
| 70 | + [ |
| 71 | + PolicyRequirement.Approval("release-manager", "Two approvals are required before promotion.") |
| 72 | + ], |
| 73 | + Metadata = new Dictionary<string, object> |
| 74 | + { |
| 75 | + ["approvalCount"] = approvals |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + private static int GetInt32(IReadOnlyDictionary<string, object> metadata, string key) |
| 81 | + => metadata.TryGetValue(key, out var value) && value is int number ? number : 0; |
| 82 | +} |
0 commit comments