diff --git a/src/main/java/io/floci/aws/services/codepipeline/CodePipelineService.java b/src/main/java/io/floci/aws/services/codepipeline/CodePipelineService.java index 2bec8f15a7..ec4d201035 100644 --- a/src/main/java/io/floci/aws/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/floci/aws/services/codepipeline/CodePipelineService.java @@ -735,7 +735,7 @@ public class CodePipelineService { String executionId = request.path("filter").path("pipelineExecutionId").asText(null); List actions = executions(account, region, pipelineName).stream() .filter(e -> executionId == null || executionId.equals(e.getPipelineExecutionId())) - .flatMap(e -> e.getActionExecutions().stream()) + .flatMap(e -> actionExecutionsSnapshot(e).stream()) .sorted(Comparator.comparing(ActionExecution::getStartTime, Comparator.nullsLast(Comparator.reverseOrder()))) .toList(); @@ -840,14 +840,14 @@ public class CodePipelineService { CodePipelinePipeline pipeline = requirePipeline(account, region, pipelineName); CodePipelineExecution execution = executions(account, region, pipelineName).stream() - .filter(e -> e.getActionExecutions().stream() + .filter(e -> actionExecutionsSnapshot(e).stream() .anyMatch(a -> stageName.equals(a.getStageName()) && actionName.equals(a.getActionName()) && token.equals(a.getToken()))) .findFirst() .orElse(null); - ActionExecution approval = execution == null ? null : execution.getActionExecutions().stream() + ActionExecution approval = execution == null ? null : actionExecutionsSnapshot(execution).stream() .filter(a -> stageName.equals(a.getStageName()) && actionName.equals(a.getActionName())) .filter(a -> token.equals(a.getToken())) .findFirst() @@ -2588,7 +2588,7 @@ public class CodePipelineService { if (!execution.isStopRequested()) { return false; } - boolean anyRunning = execution.getActionExecutions().stream() + boolean anyRunning = actionExecutionsSnapshot(execution).stream() .anyMatch(a -> "InProgress".equals(a.getStatus())); if (!execution.isAbandon() && anyRunning) { return false; @@ -2770,6 +2770,12 @@ public class CodePipelineService { throw new AwsException("StageNotFoundException", "Stage not found: " + stageName, 400); } + private List actionExecutionsSnapshot(CodePipelineExecution execution) { + synchronized (execution) { + return List.copyOf(execution.getActionExecutions()); + } + } + private Map latestActionStatuses(CodePipelineExecution execution, String stageName) { Map statuses = new LinkedHashMap<>(); synchronized (execution) { @@ -2896,14 +2902,14 @@ public class CodePipelineService { private String pipelineExecutionId(ActionExecution action) { return executionStore.scanAllAccounts().stream() - .filter(e -> e.getActionExecutions().stream() + .filter(e -> actionExecutionsSnapshot(e).stream() .anyMatch(a -> action.getActionExecutionId().equals(a.getActionExecutionId()))) .map(CodePipelineExecution::getPipelineExecutionId) .findFirst().orElse(""); } private List actionExecutionsForStage(CodePipelineExecution execution, String stage) { - return execution.getActionExecutions().stream() + return actionExecutionsSnapshot(execution).stream() .filter(a -> stage.equals(a.getStageName())) .toList(); } diff --git a/src/test/java/io/floci/aws/services/codepipeline/CodePipelineServiceTest.java b/src/test/java/io/floci/aws/services/codepipeline/CodePipelineServiceTest.java index ea29558d3c..5407a80610 100644 --- a/src/test/java/io/floci/aws/services/codepipeline/CodePipelineServiceTest.java +++ b/src/test/java/io/floci/aws/services/codepipeline/CodePipelineServiceTest.java @@ -1174,6 +1174,128 @@ class CodePipelineServiceTest { } } + @Test + void listActionsIncludesAnActionRegisteredWhileTheRequestWaits() throws Exception { + String pipeline = "list-during-action-registration"; + try { + CodePipelineExecution execution = createWaitingApprovalExecution(pipeline); + ActionExecution registering = approvalAction(); + JsonNode response = handleWhileRegisteringAction("ListActionExecutions", """ + {"pipelineName":"%s"} + """.formatted(pipeline), execution, registering); + + JsonNode actions = response.path("actionExecutionDetails"); + assertEquals(1, actions.size()); + assertEquals(registering.getActionExecutionId(), actions.get(0).path("actionExecutionId").asText()); + assertEquals(execution.getPipelineExecutionId(), actions.get(0).path("pipelineExecutionId").asText()); + } finally { + service.shutdown(); + } + } + + @Test + void approvalAcceptsAnActionRegisteredWhileTheRequestWaits() throws Exception { + String pipeline = "approve-during-action-registration"; + try { + CodePipelineExecution execution = createWaitingApprovalExecution(pipeline); + ActionExecution registering = approvalAction(); + JsonNode response = handleWhileRegisteringAction("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "External", + "actionName": "Approve", + "token": "%s", + "result": {"status": "Approved", "summary": "accepted"} + } + """.formatted(pipeline, registering.getToken()), execution, registering); + + assertTrue(response.path("approvedAt").isNumber()); + assertEquals("Succeeded", registering.getStatus()); + assertEquals("accepted", registering.getSummary()); + } finally { + service.shutdown(); + } + } + + @Test + void stageStateIncludesAnActionRegisteredOnAnOlderExecution() throws Exception { + String pipeline = "state-during-action-registration"; + try { + CodePipelineExecution execution = createWaitingApprovalExecution(pipeline); + waitingExecution(storage, pipeline, "newer-execution", 2, "External"); + JsonNode response = handleWhileRegisteringAction("GetPipelineState", """ + {"name":"%s"} + """.formatted(pipeline), execution, approvalAction()); + + JsonNode stage = response.path("stageStates").get(0); + assertEquals("External", stage.path("stageName").asText()); + assertEquals(execution.getPipelineExecutionId(), + stage.path("latestExecution").path("pipelineExecutionId").asText()); + assertEquals("InProgress", stage.path("latestExecution").path("status").asText()); + } finally { + service.shutdown(); + } + } + + @Test + void stageSuccessFilterExcludesAnActionRegisteredWhileTheRequestWaits() throws Exception { + String pipeline = "filter-during-action-registration"; + try { + CodePipelineExecution execution = createWaitingApprovalExecution(pipeline); + JsonNode response = handleWhileRegisteringAction("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": {"stageName": "External"}} + } + """.formatted(pipeline), execution, approvalAction()); + + assertTrue(response.path("pipelineExecutionSummaries").isEmpty()); + } finally { + service.shutdown(); + } + } + + private CodePipelineExecution createWaitingApprovalExecution(String pipeline) throws Exception { + createExternalActionPipeline(service, pipeline, """ + {"name":"Approve","actionTypeId":{ + "category":"Approval","owner":"AWS","provider":"Manual","version":"1"}} + """); + return waitingExecution(storage, pipeline, "registering-execution", 1, "External"); + } + + private ActionExecution approvalAction() { + ActionExecution action = new ActionExecution(); + action.setActionExecutionId("registering-action"); + action.setStageName("External"); + action.setActionName("Approve"); + action.setCategory("Approval"); + action.setOwner("AWS"); + action.setProvider("Manual"); + action.setStatus("InProgress"); + action.setStartTime(1.0); + action.setToken("approval-token"); + return action; + } + + private JsonNode handleWhileRegisteringAction(String operation, String body, + CodePipelineExecution execution, + ActionExecution registering) throws Exception { + try (ExecutorService requests = Executors.newSingleThreadExecutor()) { + AtomicReference requestThread = new AtomicReference<>(); + Future response; + synchronized (execution) { + response = requests.submit(() -> { + requestThread.set(Thread.currentThread()); + return handle(operation, body); + }); + ThreadStates.awaitBlockedOrDone(requestThread, response); + // The worker finishes registering the action before the waiting reader acquires its monitor. + execution.getActionExecutions().add(registering); + } + return response.get(5, TimeUnit.SECONDS); + } + } + private CodePipelineService serviceWith(CodeBuildService codeBuildService, CodeDeployService codeDeployService) { return new CodePipelineService(