diff --git a/build.gradle b/build.gradle index 9bb814f..749e0bb 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ ext.pluginName = 'RunDeck Conditional Logic Plugin' ext.pluginDescription = 'Execute conditional logic in RunDeck workflows' scmVersion { - ignoreUncommittedChanges = true + ignoreUncommittedChanges = false tag { // Ignore tags that begin with , include all tags // if prefix is empty. diff --git a/src/main/java/com/bioraft/rundeck/conditional/Switch.java b/src/main/java/com/bioraft/rundeck/conditional/Switch.java index 4f670d8..c89a994 100644 --- a/src/main/java/com/bioraft/rundeck/conditional/Switch.java +++ b/src/main/java/com/bioraft/rundeck/conditional/Switch.java @@ -18,6 +18,12 @@ import com.dtolabs.rundeck.core.Constants; import com.dtolabs.rundeck.core.dispatcher.ContextView; import com.dtolabs.rundeck.plugins.step.PluginStepContext; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.Iterator; +import java.util.Map; /** * Workflow Node Step Plug-in to choose one of several values to uplift into a @@ -75,15 +81,22 @@ public void switchCase(String group, String name, String cases, String test, Str * @return True if matched, false otherwise. */ public boolean switchCase(String group, String name, String cases, String test, boolean elevate) { - for (String keyValue : cases.split(",")) { - String[] values = keyValue.split(":"); - String key = values[0].replaceAll("^\"|\"$", ""); - String value = values[1].replaceAll("^\"|\"$", ""); - if (test.equals(key)) { - addOutput(elevate, group, name, value); - ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + key + "."); - return true; + ObjectMapper objectMapper = new ObjectMapper(); + try { + JsonNode map = objectMapper.readTree(ensureStringIsJsonObject(cases)); + Iterator> iterator = map.fields(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + String key = entry.getKey(); + String value = entry.getValue().asText(); + if (test.equals(key)) { + addOutput(elevate, group, name, value); + ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + key + "."); + return true; + } } + } catch (JsonProcessingException e) { + return false; } return false; } @@ -104,4 +117,12 @@ private void addOutput(boolean elevate, String group, String name, String value) ctx.getLogger().log(Constants.DEBUG_LEVEL, "Elevating to globsal ${export." + groupName + "}."); } } + + public static String ensureStringIsJsonObject(String string) { + if (string == null) { + return ""; + } + String trimmed = string.replaceFirst("^\\s*\\{?", "{").replaceFirst("\\s*$", ""); + return trimmed + (trimmed.endsWith("}") ? "" : "}"); + } } diff --git a/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseNodeStepPluginTest.java b/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseNodeStepPluginTest.java index 4202f1e..98ae6a0 100644 --- a/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseNodeStepPluginTest.java +++ b/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseNodeStepPluginTest.java @@ -103,6 +103,7 @@ private void runTest(String expected, String testValue, Map case String name = "test"; StringBuffer caseString = new StringBuffer(); cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(",")); + caseString.setLength(caseString.length() - 1); Map configuration = new HashMap<>(); configuration.put("group", group); @@ -127,6 +128,7 @@ public void runTestNoDefault(Map configuration) Map cases = ImmutableMap.builder().put("k1", "v1").put("k2", "v2").build(); StringBuilder caseString = new StringBuilder(); cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(",")); + caseString.setLength(caseString.length() - 1); configuration.put("cases", caseString.toString()); configuration.put("group", group); diff --git a/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseStepPluginTest.java b/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseStepPluginTest.java index 79630e2..f27d33b 100644 --- a/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseStepPluginTest.java +++ b/src/test/java/com/bioraft/rundeck/conditional/SwitchCaseStepPluginTest.java @@ -99,6 +99,7 @@ private void runTest(String expected, String testValue, Map case String name = "test"; StringBuffer caseString = new StringBuffer(); cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(",")); + caseString.setLength(caseString.length() - 1); Map configuration = new HashMap<>(); configuration.put("group", group); @@ -122,6 +123,7 @@ public void runTestNoDefault(Map configuration) throws StepExcep Map cases = ImmutableMap.builder().put("k1", "v1").put("k2", "v2").build(); StringBuilder caseString = new StringBuilder(); cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(",")); + caseString.setLength(caseString.length() - 1); configuration.put("cases", caseString.toString()); configuration.put("group", group);