|
| 1 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 2 | +using PolicyComposition.Policies.Approval; |
| 3 | +using PolicyComposition.Policies.Deployment; |
| 4 | +using PolicyComposition.Policies.Emergency; |
| 5 | +using PolicyComposition.Policies.Shared; |
| 6 | +using PolicyComposition.State; |
| 7 | + |
| 8 | +namespace PolicyComposition.Policies; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Named composed policy sets used by the release governance example. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This class acts as the composition root for the example. It keeps the concrete |
| 15 | +/// child policies isolated from the scenarios and exposes reusable policy sets that |
| 16 | +/// demonstrate the three composition modes: |
| 17 | +/// <list type="bullet"> |
| 18 | +/// <item><description><c>AllOf</c> for mandatory approval gates.</description></item> |
| 19 | +/// <item><description><c>AnyOf</c> for emergency fallback flows.</description></item> |
| 20 | +/// <item><description><c>Priority</c> for deterministic ordered selection.</description></item> |
| 21 | +/// </list> |
| 22 | +/// </remarks> |
| 23 | +internal static class ReleaseGovernancePolicies |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Builds the standard approval gate used for release promotion. |
| 27 | + /// </summary> |
| 28 | + /// <remarks> |
| 29 | + /// The gate combines: |
| 30 | + /// <list type="bullet"> |
| 31 | + /// <item><description><see cref="RequireApprovalsPolicy"/> to enforce the approval threshold.</description></item> |
| 32 | + /// <item><description><see cref="AddAuditTrailPolicy"/> to add traceability once the gate succeeds.</description></item> |
| 33 | + /// </list> |
| 34 | + /// The composed policy only succeeds when both child policies can contribute |
| 35 | + /// without a conflict and the approval requirement is satisfied. |
| 36 | + /// </remarks> |
| 37 | + public static IMutationPolicy<ReleaseGateState> ApprovalGate() => |
| 38 | + ModularityKit.Mutator.Abstractions.Policies.PolicyComposition.AllOf( |
| 39 | + "ReleaseApprovalGate", |
| 40 | + [ |
| 41 | + new RequireApprovalsPolicy(), |
| 42 | + new AddAuditTrailPolicy() |
| 43 | + ], |
| 44 | + priority: 500, |
| 45 | + description: "Requires two approvals and adds audit metadata."); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Builds the emergency gate that prefers an explicit override and falls back to approvals. |
| 49 | + /// </summary> |
| 50 | + /// <remarks> |
| 51 | + /// The gate combines: |
| 52 | + /// <list type="bullet"> |
| 53 | + /// <item><description><see cref="EmergencyOverridePolicy"/> for explicit emergency approval.</description></item> |
| 54 | + /// <item><description><see cref="ApprovalFallbackPolicy"/> for the approval-based fallback path.</description></item> |
| 55 | + /// </list> |
| 56 | + /// The first allowed branch wins, which makes the result deterministic while |
| 57 | + /// still allowing a reusable emergency path to be expressed in one place. |
| 58 | + /// </remarks> |
| 59 | + public static IMutationPolicy<ReleaseGateState> EmergencyGate() => |
| 60 | + ModularityKit.Mutator.Abstractions.Policies.PolicyComposition.AnyOf( |
| 61 | + "ReleaseEmergencyGate", |
| 62 | + [ |
| 63 | + new EmergencyOverridePolicy(), |
| 64 | + new ApprovalFallbackPolicy() |
| 65 | + ], |
| 66 | + priority: 400, |
| 67 | + description: "Chooses the emergency override branch when available."); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Builds the deployment gate that evaluates production checks before the default path. |
| 71 | + /// </summary> |
| 72 | + /// <remarks> |
| 73 | + /// The gate combines: |
| 74 | + /// <list type="bullet"> |
| 75 | + /// <item><description><see cref="ProductionGuardPolicy"/> to block production releases early.</description></item> |
| 76 | + /// <item><description><see cref="DefaultDeploymentPolicy"/> as the fallback branch for non-production releases.</description></item> |
| 77 | + /// </list> |
| 78 | + /// Because this is a priority composition, the first decisive policy short-circuits |
| 79 | + /// the rest of the chain. |
| 80 | + /// </remarks> |
| 81 | + public static IMutationPolicy<ReleaseGateState> DeploymentGate() => |
| 82 | + ModularityKit.Mutator.Abstractions.Policies.PolicyComposition.Priority( |
| 83 | + "ReleaseDeploymentGate", |
| 84 | + [ |
| 85 | + new ProductionGuardPolicy(), |
| 86 | + new DefaultDeploymentPolicy() |
| 87 | + ], |
| 88 | + priority: 300, |
| 89 | + description: "Uses a production guard first, then falls back to the default deployment path."); |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Builds a composed gate that intentionally conflicts on the owner field. |
| 93 | + /// </summary> |
| 94 | + /// <remarks> |
| 95 | + /// The gate composes two <see cref="SetOwnerPolicy"/> instances that target the |
| 96 | + /// same state field with different values. The example uses this to show that |
| 97 | + /// the composition layer detects conflicting mutation results explicitly instead |
| 98 | + /// of silently picking one branch. |
| 99 | + /// </remarks> |
| 100 | + public static IMutationPolicy<ReleaseGateState> ConflictingOwnerGate() => |
| 101 | + ModularityKit.Mutator.Abstractions.Policies.PolicyComposition.AllOf( |
| 102 | + "ReleaseOwnerConflictGate", |
| 103 | + [ |
| 104 | + new SetOwnerPolicy("platform"), |
| 105 | + new SetOwnerPolicy("security") |
| 106 | + ], |
| 107 | + priority: 200, |
| 108 | + description: "Demonstrates explicit conflict handling when two policies set the same field differently."); |
| 109 | +} |
0 commit comments