|
| 1 | +using ModularityKit.Mutator.Abstractions.Context; |
| 2 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 3 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 4 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Model; |
| 5 | +using ModularityKit.Mutator.Governance.Runtime.Approval.Execution; |
| 6 | +using ModularityKit.Mutator.Governance.Runtime.Storage; |
| 7 | + |
| 8 | +namespace ApprovalWorkflow.Scenarios; |
| 9 | + |
| 10 | +internal static class GovernanceApprovalWorkflowScenario |
| 11 | +{ |
| 12 | + public static async Task Run() |
| 13 | + { |
| 14 | + var store = new InMemoryMutationRequestStore(); |
| 15 | + var manager = new MutationRequestApprovalWorkflowManager(store); |
| 16 | + |
| 17 | + PrintSection("Submit Pending Approval Request"); |
| 18 | + var request = await store.Create(CreateApprovalRequest()); |
| 19 | + PrintRequest(request); |
| 20 | + |
| 21 | + PrintSection("Approve Step 1"); |
| 22 | + var aliceApproval = request.ApprovalRequirements.Single(requirement => requirement.ApproverId == "alice"); |
| 23 | + var afterAlice = await manager.ApproveRequirement( |
| 24 | + request.RequestId, |
| 25 | + aliceApproval.ApprovalId, |
| 26 | + MutationContext.User("alice", "Alice", "Manager approved")); |
| 27 | + PrintRequest(afterAlice); |
| 28 | + |
| 29 | + PrintSection("Approve Step 1 - Second Actor"); |
| 30 | + var bobApproval = afterAlice.ApprovalRequirements.Single(requirement => requirement.ApproverId == "bob"); |
| 31 | + var afterBob = await manager.ApproveRequirement( |
| 32 | + request.RequestId, |
| 33 | + bobApproval.ApprovalId, |
| 34 | + MutationContext.User("bob", "Bob", "Security approved")); |
| 35 | + PrintRequest(afterBob); |
| 36 | + |
| 37 | + PrintSection("Approve Step 2"); |
| 38 | + var carolApproval = afterBob.ApprovalRequirements.Single(requirement => requirement.ApproverId == "carol"); |
| 39 | + var afterCarol = await manager.ApproveRequirement( |
| 40 | + request.RequestId, |
| 41 | + carolApproval.ApprovalId, |
| 42 | + MutationContext.User("carol", "Carol", "Finance approved")); |
| 43 | + PrintRequest(afterCarol); |
| 44 | + } |
| 45 | + |
| 46 | + private static MutationRequest CreateApprovalRequest() |
| 47 | + { |
| 48 | + return MutationRequest.PendingApproval( |
| 49 | + stateId: "tenant-42:roles", |
| 50 | + stateType: "IamRoleState", |
| 51 | + mutationType: "GrantRoleMutation", |
| 52 | + intent: new MutationIntent |
| 53 | + { |
| 54 | + OperationName = "GrantRole", |
| 55 | + Category = "Security", |
| 56 | + Description = "Grant elevated role to tenant operator" |
| 57 | + }, |
| 58 | + context: MutationContext.User("requester", "Requester", "Need elevated access for incident"), |
| 59 | + requirements: |
| 60 | + [ |
| 61 | + PolicyRequirement.Approval("alice", "Manager approval"), |
| 62 | + new PolicyRequirement |
| 63 | + { |
| 64 | + Type = "Approval", |
| 65 | + Description = "Security review", |
| 66 | + Data = new |
| 67 | + { |
| 68 | + Approver = "bob", |
| 69 | + StepOrder = 1, |
| 70 | + Reason = "Security sign-off" |
| 71 | + } |
| 72 | + }, |
| 73 | + new PolicyRequirement |
| 74 | + { |
| 75 | + Type = "Approval", |
| 76 | + Description = "Finance review", |
| 77 | + Data = new |
| 78 | + { |
| 79 | + Approver = "carol", |
| 80 | + StepOrder = 2, |
| 81 | + Reason = "Budget sign-off" |
| 82 | + } |
| 83 | + } |
| 84 | + ], |
| 85 | + expectedStateVersion: "v10"); |
| 86 | + } |
| 87 | + |
| 88 | + private static void PrintSection(string title) |
| 89 | + { |
| 90 | + Console.WriteLine(); |
| 91 | + Console.WriteLine($"=== {title} ==="); |
| 92 | + } |
| 93 | + |
| 94 | + private static void PrintRequest(MutationRequest request) |
| 95 | + { |
| 96 | + Console.WriteLine($"Request status: {request.Status}"); |
| 97 | + Console.WriteLine($"Pending reason: {request.PendingReason?.ToString() ?? "-"}"); |
| 98 | + Console.WriteLine($"Revision: {request.Revision}"); |
| 99 | + Console.WriteLine("Approval requirements:"); |
| 100 | + |
| 101 | + foreach (var requirement in request.ApprovalRequirements.OrderBy(requirement => requirement.StepOrder).ThenBy(requirement => requirement.ApproverId)) |
| 102 | + { |
| 103 | + Console.WriteLine( |
| 104 | + $" - Step {requirement.StepOrder}: {requirement.ApproverId} => {requirement.Status}"); |
| 105 | + } |
| 106 | + |
| 107 | + var lastDecision = request.Decisions[^1]; |
| 108 | + Console.WriteLine($"Last decision: {lastDecision.Type} by {lastDecision.Context.ActorId ?? "system"}"); |
| 109 | + Console.WriteLine($"Reason: {lastDecision.Reason ?? "-"}"); |
| 110 | + } |
| 111 | +} |
0 commit comments