|
| 1 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 2 | +using PolicyComposition.Mutations; |
| 3 | +using PolicyComposition.Policies; |
| 4 | +using PolicyComposition.State; |
| 5 | + |
| 6 | +namespace PolicyComposition.Scenarios; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Demonstrates the <c>AllOf</c> composition mode for reusable governance gates. |
| 10 | +/// </summary> |
| 11 | +/// <remarks> |
| 12 | +/// This scenario is intentionally small: it creates a release state, builds a |
| 13 | +/// release submission mutation, evaluates the approval gate, and prints the |
| 14 | +/// composed result. The goal is to show how the merged decision contains the |
| 15 | +/// final state, audit side effects, and composition metadata. |
| 16 | +/// </remarks> |
| 17 | +internal static class AllOfScenario |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Evaluates the approval gate against a sample release submission. |
| 21 | + /// </summary> |
| 22 | + /// <remarks> |
| 23 | + /// The mutation carries approval count, emergency flag, and environment data |
| 24 | + /// through its execution context so the composed policies can make their own |
| 25 | + /// decisions without needing extra helper state. |
| 26 | + /// </remarks> |
| 27 | + public static async Task Run() |
| 28 | + { |
| 29 | + var state = new ReleaseGateState("release-42", "Draft", "platform"); |
| 30 | + var mutation = new SubmitReleaseMutation( |
| 31 | + releaseName: state.ReleaseName, |
| 32 | + approvals: 2, |
| 33 | + emergency: false, |
| 34 | + environment: "staging"); |
| 35 | + |
| 36 | + var decision = await ReleaseGovernancePolicies.ApprovalGate().EvaluateAsync(mutation, state); |
| 37 | + |
| 38 | + WriteDecision(decision); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Writes the important parts of the composed decision to the console. |
| 43 | + /// </summary> |
| 44 | + /// <param name="decision">The composed policy decision returned by the gate.</param> |
| 45 | + private static void WriteDecision(PolicyDecision decision) |
| 46 | + { |
| 47 | + Console.WriteLine("[AnyOf] Reusable override gate"); |
| 48 | + |
| 49 | + Console.WriteLine($" allowed: {decision.IsAllowed}"); |
| 50 | + Console.WriteLine($" reason: {decision.Reason}"); |
| 51 | + Console.WriteLine($" mode: {decision.Metadata!["PolicyComposition.Mode"]}"); |
| 52 | + |
| 53 | + if (decision.Modifications is not null && |
| 54 | + decision.Modifications.TryGetValue("State", out var value) && |
| 55 | + value is ReleaseGateState state) |
| 56 | + { |
| 57 | + Console.WriteLine($" stage: {state.Stage}"); |
| 58 | + Console.WriteLine($" owner: {state.Owner}"); |
| 59 | + } |
| 60 | + |
| 61 | + if (decision.Modifications is not null && |
| 62 | + decision.Modifications.TryGetValue("SideEffects", out var sideEffects) && |
| 63 | + sideEffects is IEnumerable<object> effects) |
| 64 | + { |
| 65 | + Console.WriteLine($" sideEffects: {effects.Count()}"); |
| 66 | + } |
| 67 | + |
| 68 | + Console.WriteLine(); |
| 69 | + } |
| 70 | +} |
0 commit comments