Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codepipeline): populate stage execution status (#3478)
Aniket Shukla129ba0a260d5
diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.javaindex 15f077f835..0eb6524896 100644--- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java+++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java@@ -146,6 +146,7 @@ public class CodePipelineService {execution.setStopRequested(false);execution.setAbandon(false);execution.setActionExecutions(new ArrayList<>());+ execution.setStageExecutionStatuses(new LinkedHashMap<>());putExecution(execution);executor.submit(() -> runExecution(pipeline, execution));}, () -> {@@ -299,6 +300,9 @@ public class CodePipelineService {execution.setStatus("Stopping");execution.setStatusSummary(request.path("reason").asText("Stop requested."));execution.setLastUpdateTime(now());+ if (execution.getCurrentStage() != null) {+ execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopping");+ }if (execution.isAbandon()) {execution.getActionExecutions().stream().filter(a -> "InProgress".equals(a.getStatus()))@@ -385,7 +389,8 @@ public class CodePipelineService {private ObjectNode getPipelineState(JsonNode request, String region, String account) {CodePipelinePipeline pipeline = requirePipeline(account, region, text(request, "name"));- CodePipelineExecution latest = executions(account, region, pipeline.getName()).stream().findFirst().orElse(null);+ List<CodePipelineExecution> pipelineExecutions = executions(account, region, pipeline.getName());+ CodePipelineExecution latest = pipelineExecutions.stream().findFirst().orElse(null);ObjectNode response = mapper.createObjectNode();response.put("pipelineName", pipeline.getName());response.put("pipelineVersion", pipeline.getVersion());@@ -414,11 +419,15 @@ public class CodePipelineService {.ifPresent(a -> actionState.set("latestExecution", actionStateNode(a)));}}- if (latest != null && stageName.equals(latest.getCurrentStage())) {+ CodePipelineExecution latestStageExecution = pipelineExecutions.stream()+ .filter(execution -> hasStageExecution(execution, stageName))+ .findFirst()+ .orElse(null);+ if (latestStageExecution != null) {ObjectNode latestExecution = state.putObject("latestExecution");- latestExecution.put("pipelineExecutionId", latest.getPipelineExecutionId());- latestExecution.put("status", latest.getStatus());- latestExecution.put("type", latest.getExecutionType());+ latestExecution.put("pipelineExecutionId", latestStageExecution.getPipelineExecutionId());+ latestExecution.put("status", stageExecutionStatus(latestStageExecution, stageName));+ latestExecution.put("type", latestStageExecution.getExecutionType());}}return response;@@ -495,9 +504,16 @@ public class CodePipelineService {"The approval action has already been approved or rejected.", 400);}- approval.setStatus("Approved".equals(status) ? "Succeeded" : "Failed");+ boolean approved = "Approved".equals(status);+ approval.setStatus(approved ? "Succeeded" : "Failed");approval.setSummary(summary);approval.setLastUpdateTime(now());+ if (!approved) {+ execution.setStatus("Failed");+ execution.setStatusSummary("Action " + actionName + " failed: " + summary);+ execution.getStageExecutionStatuses().put(stageName, "Failed");+ execution.setLastUpdateTime(now());+ }putExecution(execution);return mapper.createObjectNode().put("approvedAt", now());}@@ -742,17 +758,30 @@ public class CodePipelineService {if (finishIfStopped(execution)) {return;}- execution.setCurrentStage(stage.path("name").asText());+ String stageName = stage.path("name").asText();+ execution.setCurrentStage(stageName);+ execution.getStageExecutionStatuses().put(stageName, "InProgress");execution.setLastUpdateTime(now());putExecution(execution);runStage(pipeline, execution, stage);- if ("Failed".equals(execution.getStatus()) || finishIfStopped(execution)) {+ if ("Failed".equals(execution.getStatus())) {+ execution.getStageExecutionStatuses().put(stageName, "Failed");+ return;+ }+ if (finishIfStopped(execution)) {return;}+ execution.getStageExecutionStatuses().put(stageName, "Succeeded");+ execution.setCurrentStage(null);+ execution.setLastUpdateTime(now());+ putExecution(execution);}execution.setStatus("Succeeded");execution.setStatusSummary("Pipeline execution succeeded.");} catch (Exception e) {+ if (execution.getCurrentStage() != null) {+ execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Failed");+ }execution.setStatus("Failed");execution.setStatusSummary(e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());LOG.errorf(e, "CodePipeline execution %s failed", execution.getPipelineExecutionId());@@ -1052,6 +1081,9 @@ public class CodePipelineService {execution.setStatus("Stopped");execution.setStatusSummary("Pipeline execution stopped.");execution.setLastUpdateTime(now());+ if (execution.getCurrentStage() != null) {+ execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopped");+ }putExecution(execution);return true;}@@ -1312,6 +1344,32 @@ public class CodePipelineService {.toList();}+ private boolean hasStageExecution(CodePipelineExecution execution, String stage) {+ return execution.getStageExecutionStatuses().containsKey(stage)+ || !actionExecutionsForStage(execution, stage).isEmpty();+ }++ private String stageExecutionStatus(CodePipelineExecution execution, String stage) {+ List<ActionExecution> actions = actionExecutionsForStage(execution, stage);+ if (actions.stream().anyMatch(action -> "Failed".equals(action.getStatus()))) {+ return "Failed";+ }+ String trackedStatus = execution.getStageExecutionStatuses().get(stage);+ if (trackedStatus != null) {+ return trackedStatus;+ }+ if (actions.stream().anyMatch(action -> "InProgress".equals(action.getStatus()))) {+ return "InProgress";+ }+ if (actions.stream().anyMatch(action -> "Abandoned".equals(action.getStatus()))) {+ return "Stopped";+ }+ if (actions.stream().allMatch(action -> "Succeeded".equals(action.getStatus()))) {+ return "Succeeded";+ }+ return "Failed";+ }+private List<Map<String, Object>> objectList(JsonNode node) {if (node == null || !node.isArray()) {return new ArrayList<>();diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.javaindex d8cce7514c..8c9441b753 100644--- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java+++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java@@ -30,6 +30,7 @@ public class CodePipelineExecution {private List<Map<String, String>> variables = new ArrayList<>();private Map<String, String> trigger = new LinkedHashMap<>();private List<ActionExecution> actionExecutions = new ArrayList<>();+ private Map<String, String> stageExecutionStatuses = new LinkedHashMap<>();private String currentStage;private boolean stopRequested;private boolean abandon;@@ -163,6 +164,15 @@ public class CodePipelineExecution {this.actionExecutions = actionExecutions;}+ public Map<String, String> getStageExecutionStatuses() {+ return stageExecutionStatuses;+ }++ public void setStageExecutionStatuses(Map<String, String> stageExecutionStatuses) {+ this.stageExecutionStatuses = stageExecutionStatuses == null+ ? new LinkedHashMap<>() : stageExecutionStatuses;+ }+public String getCurrentStage() {return currentStage;}diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.javaindex 2e2e12cd7f..a00c60ffe1 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java@@ -262,7 +262,11 @@ class CodePipelineIntegrationTest {.then().statusCode(200).body("stageStates", hasSize(2))- .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded"));+ .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded"))+ .body("stageStates[0].latestExecution.pipelineExecutionId", equalTo(executionId))+ .body("stageStates[0].latestExecution.status", equalTo("Succeeded"))+ .body("stageStates[1].latestExecution.pipelineExecutionId", equalTo(executionId))+ .body("stageStates[1].latestExecution.status", equalTo("Succeeded"));post("ListActionExecutions", """{@@ -764,7 +768,133 @@ class CodePipelineIntegrationTest {.then().statusCode(200).body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Failed"))- .body("stageStates[0].actionStates[0].latestExecution.summary", equalTo("Rejected by test"));+ .body("stageStates[0].actionStates[0].latestExecution.summary", equalTo("Rejected by test"))+ .body("stageStates[0].latestExecution.pipelineExecutionId", equalTo(executionId2))+ .body("stageStates[0].latestExecution.status", equalTo("Failed"));+ }++ @Test+ void stageStatusTracksMultipleRunOrdersAndStopBeforeLaterGroup() throws Exception {+ String pipelineName = "multi-run-order-stage-status";+ post("CreatePipeline", pipeline(pipelineName, """+ {+ "name": "Approve",+ "actions": [+ {+ "name": "FirstApproval",+ "actionTypeId": {+ "category": "Approval",+ "owner": "AWS",+ "provider": "Manual",+ "version": "1"+ },+ "configuration": {},+ "runOrder": 1+ },+ {+ "name": "SecondApproval",+ "actionTypeId": {+ "category": "Approval",+ "owner": "AWS",+ "provider": "Manual",+ "version": "1"+ },+ "configuration": {},+ "runOrder": 2+ },+ {+ "name": "NeverStartedApproval",+ "actionTypeId": {+ "category": "Approval",+ "owner": "AWS",+ "provider": "Manual",+ "version": "1"+ },+ "configuration": {},+ "runOrder": 3+ }+ ]+ },+ {+ "name": "Deploy",+ "actions": [{+ "name": "PlaceholderAction",+ "actionTypeId": {+ "category": "Deploy",+ "owner": "AWS",+ "provider": "S3",+ "version": "1"+ },+ "configuration": {+ "BucketName": "codepipeline-artifacts",+ "ObjectKey": "placeholder"+ },+ "runOrder": 1+ }]+ }+ """))+ .then()+ .statusCode(200);++ String executionId = post("StartPipelineExecution", """+ {"name": "%s"}+ """.formatted(pipelineName))+ .then()+ .statusCode(200)+ .extract().path("pipelineExecutionId");++ String firstToken = waitForApprovalToken(pipelineName, 0);+ post("PutApprovalResult", """+ {+ "pipelineName": "%s",+ "stageName": "Approve",+ "actionName": "FirstApproval",+ "token": "%s",+ "result": {"status": "Approved", "summary": "First group complete"}+ }+ """.formatted(pipelineName, firstToken))+ .then()+ .statusCode(200);++ waitForApprovalToken(pipelineName, 1);+ post("GetPipelineState", """+ {"name": "%s"}+ """.formatted(pipelineName))+ .then()+ .statusCode(200)+ .body("stageStates[0].latestExecution.pipelineExecutionId", equalTo(executionId))+ .body("stageStates[0].latestExecution.status", equalTo("InProgress"))+ .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded"))+ .body("stageStates[0].actionStates[1].latestExecution.status", equalTo("InProgress"))+ .body("stageStates[0].actionStates[2].latestExecution", nullValue());++ post("StopPipelineExecution", """+ {+ "pipelineName": "%s",+ "pipelineExecutionId": "%s",+ "abandon": true,+ "reason": "Verify stage status"+ }+ """.formatted(pipelineName, executionId))+ .then()+ .statusCode(200)+ .body("pipelineExecutionId", equalTo(executionId));++ waitForExecution(pipelineName, executionId, "Stopped");+ post("GetPipelineState", """+ {"name": "%s"}+ """.formatted(pipelineName))+ .then()+ .statusCode(200)+ .body("stageStates[0].latestExecution.pipelineExecutionId", equalTo(executionId))+ .body("stageStates[0].latestExecution.status", equalTo("Stopped"))+ .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded"))+ .body("stageStates[0].actionStates[1].latestExecution.status", equalTo("Abandoned"))+ .body("stageStates[0].actionStates[2].latestExecution", nullValue());++ post("DeletePipeline", """+ {"name": "%s"}+ """.formatted(pipelineName)).then().statusCode(200);}@Test@@ -1094,13 +1224,18 @@ class CodePipelineIntegrationTest {}private String waitForApprovalToken(String pipelineName) throws Exception {+ return waitForApprovalToken(pipelineName, 0);+ }++ private String waitForApprovalToken(String pipelineName, int actionIndex) throws Exception {Instant deadline = Instant.now().plus(Duration.ofSeconds(5));String token;do {token = post("GetPipelineState", """{"name": "%s"}""".formatted(pipelineName))- .jsonPath().getString("stageStates[0].actionStates[0].latestExecution.token");+ .jsonPath().getString(+ "stageStates[0].actionStates[%d].latestExecution.token".formatted(actionIndex));if (token != null) {return token;}