Skip to content

Commit

Permalink
Scan code with sonarcloud (#5)
Browse files Browse the repository at this point in the history
Enable sonarcloud inspaction.
  • Loading branch information
kdebisschop authored Feb 19, 2020
1 parent 03ee493 commit 39686d2
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 94 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Run unit tests
run: ./gradlew test
- name: Determine coverage
run: ./gradlew jacocoTestReport
- name: Scan with Sonar
run: ./gradlew sonar
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Build with Gradle
run: ./gradlew build
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
plugins {
id 'pl.allegro.tech.build.axion-release' version '1.10.1'
id "org.sonarqube" version "2.8"
id 'jacoco'
id 'java'
}

sonarqube {
properties {
property "sonar.projectKey", "kdebisschop_rundeck-conditional-logic-plugin"
property "sonar.organization", "kdebisschop"
property "sonar.host.url", "https://sonarcloud.io"
property "sonar.login", "807fd8589d7244bd324b5fecdfca9614d8a40c8c"
}
}

jacocoTestReport {
reports {
xml.enabled true
csv.enabled false
html.destination file("${buildDir}/jacocoHtml")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
Expand Down
119 changes: 83 additions & 36 deletions src/main/java/com/bioraft/rundeck/conditional/IfElse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.dtolabs.rundeck.core.dispatcher.ContextView;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;

import java.util.Map;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
* step variable.
Expand All @@ -45,6 +47,11 @@ public class IfElse {

private PluginStepContext ctx;

private Map<String, Object> cfg;

/** If specified, also create a variable in global export context. */
private boolean elevate = false;

/**
* Constructor sets PluginStepContext.
*
Expand All @@ -54,6 +61,16 @@ public IfElse(PluginStepContext ctx) {
this.ctx = ctx;
}

public IfElse setElevate(boolean elevate) {
this.elevate = elevate;
return this;
}

public IfElse setCfg(Map<String, Object> cfg) {
this.cfg = cfg;
return this;
}

/**
* Add ifTrue value to SharedOutputContext if condition passes, otherwise add
* ifFalse if not null.
Expand All @@ -65,63 +82,93 @@ public IfElse(PluginStepContext ctx) {
* @param comparisonValue The value to test against.
* @param ifTrue The value to return if comparison is true.
* @param ifFalse The value to return if comparison is false.
* @param elevateToGlobal If specified, also create a variable in global export
* context.
*/
public void ifElse(String group, String name, String testValue, String operator, String comparisonValue,
String ifTrue, String ifFalse, boolean elevateToGlobal) {
String ifTrue, String ifFalse) {

group = cfg.getOrDefault("group", group).toString();
name = cfg.getOrDefault("name", name).toString();
testValue = cfg.getOrDefault("testValue", testValue).toString();
operator = cfg.getOrDefault("operator", operator).toString();
comparisonValue = cfg.getOrDefault("comparisonValue", comparisonValue).toString();
ifTrue = cfg.getOrDefault("ifTrue", ifTrue).toString();
ifFalse = cfg.getOrDefault("ifFalse", ifFalse).toString();

String value;
String matched = null;
String matched;

if (operator.equals(STRING_EQ) && testValue.endsWith(comparisonValue)) {
matched = STRING_EQ;
if (operator.equals(STRING_EQ) && testValue.equals(comparisonValue)) {
matched = STRING_EQ;
} else if (operator.equals(STRING_NE) && !testValue.equals(comparisonValue)) {
matched = STRING_NE;
} else if (operator.equals(STRING_BEG) && testValue.startsWith(comparisonValue)) {
matched = STRING_BEG;
} else if (operator.equals(STRING_END) && testValue.endsWith(comparisonValue)) {
matched = STRING_END;
} else if (operator.equals(STRING_LT) && testValue.compareTo(comparisonValue) < 0) {
matched = STRING_LT;
} else if (operator.equals(STRING_LE) && testValue.compareTo(comparisonValue) <= 0) {
matched = STRING_LE;
} else if (operator.equals(STRING_GE) && testValue.compareTo(comparisonValue) >= 0) {
matched = STRING_GE;
} else if (operator.equals(STRING_GT) && testValue.compareTo(comparisonValue) > 0) {
matched = STRING_GT;
} else if (operator.equals(NUMBER_LT) && Double.parseDouble(testValue) < Double.parseDouble(comparisonValue)) {
matched = NUMBER_LT;
} else if (operator.equals(NUMBER_LE) && Double.parseDouble(testValue) <= Double.parseDouble(comparisonValue)) {
matched = NUMBER_LE;
} else if (operator.equals(NUMBER_GE) && Double.parseDouble(testValue) >= Double.parseDouble(comparisonValue)) {
matched = NUMBER_GE;
} else if (operator.equals(NUMBER_GT) && Double.parseDouble(testValue) > Double.parseDouble(comparisonValue)) {
matched = NUMBER_GT;
} else if (operator.equals(NUMBER_EQ) && Double.parseDouble(testValue) == Double.parseDouble(comparisonValue)) {
matched = NUMBER_EQ;
} else if (operator.equals(NUMBER_NE) && Double.parseDouble(testValue) != Double.parseDouble(comparisonValue)) {
matched = NUMBER_NE;
matched = STRING_NE;
} else {
matched = compareString(operator, testValue, comparisonValue);
}

if (matched != null) {
value = ifTrue;
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + matched + ", returning ifTrue value.");
} else {
if (matched.equals("")) {
matched = compareNumeric(operator, testValue, comparisonValue);
}

if (matched.equals("")) {
if (ifFalse == null || ifFalse.length() == 0) {
ctx.getLogger().log(Constants.DEBUG_LEVEL, "No match, default is empty.");
return;
}
ctx.getLogger().log(Constants.DEBUG_LEVEL, "No match, using default.");
value = ifFalse;
} else {
value = ifTrue;
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + matched + ", returning ifTrue value.");
}

ctx.getOutputContext().addOutput(group, name, value);
if (elevateToGlobal) {
if (elevate) {
String groupName = group + "." + name;
ctx.getOutputContext().addOutput(ContextView.global(), "export", groupName, value);
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Elevating to globsal ${export." + groupName + "}.");
}
}

private String compareString(String operator, String testValue, String comparisonValue) {
if (operator.equals(STRING_BEG) && testValue.startsWith(comparisonValue)) {
return STRING_BEG;
} else if (operator.equals(STRING_END) && testValue.endsWith(comparisonValue)) {
return STRING_END;
}

int compare = testValue.compareTo(comparisonValue);
if (operator.equals(STRING_LT) && compare < 0) {
return STRING_LT;
} else if (operator.equals(STRING_LE) && compare <= 0) {
return STRING_LE;
} else if (operator.equals(STRING_GE) && compare >= 0) {
return STRING_GE;
} else if (operator.equals(STRING_GT) && compare > 0) {
return STRING_GT;
}
return "";
}

private String compareNumeric(String operator, String testValue, String comparisonValue) {
try {
double testDouble = Double.parseDouble(testValue);
double comparisonDouble = Double.parseDouble(comparisonValue);
if (operator.equals(NUMBER_LT) && testDouble < comparisonDouble) {
return NUMBER_LT;
} else if (operator.equals(NUMBER_LE) && testDouble <= comparisonDouble) {
return NUMBER_LE;
} else if (operator.equals(NUMBER_GE) && testDouble >= comparisonDouble) {
return NUMBER_GE;
} else if (operator.equals(NUMBER_GT) && testDouble > comparisonDouble) {
return NUMBER_GT;
} else if (operator.equals(NUMBER_EQ) && testDouble == comparisonDouble) {
return NUMBER_EQ;
} else if (operator.equals(NUMBER_NE) && testDouble != comparisonDouble) {
return NUMBER_NE;
}
} catch (Exception e) {
return "";
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class IfElseNodeStepPlugin implements NodeStepPlugin {
@PluginProperty(title = "Operator", description = "Comparison operator", required = true)
@SelectValues(values = { IfElse.STRING_EQ, IfElse.STRING_NE, IfElse.STRING_LT, IfElse.STRING_LE, IfElse.STRING_GE,
IfElse.STRING_GT, IfElse.STRING_BEG, IfElse.STRING_END, IfElse.NUMBER_EQ, IfElse.NUMBER_NE,
IfElse.NUMBER_LT, IfElse.NUMBER_LE, IfElse.NUMBER_GE, IfElse.NUMBER_GT }, freeSelect = false)
IfElse.NUMBER_LT, IfElse.NUMBER_LE, IfElse.NUMBER_GE, IfElse.NUMBER_GT })
private String operator;

@PluginProperty(title = "Comparison Value", description = "Second test value", required = true)
Expand All @@ -62,31 +62,23 @@ public class IfElseNodeStepPlugin implements NodeStepPlugin {
@PluginProperty(title = "If True", description = "Value to assign if comparison is true", required = true)
private String ifTrue;

@PluginProperty(title = "If False", description = "Value to assign if comparison is false", required = false)
@PluginProperty(title = "If False", description = "Value to assign if comparison is false")
private String ifFalse;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)")
private boolean elevateToGlobal;

@Override
public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INodeEntry node)
throws NodeStepException {

String group = cfg.getOrDefault("group", this.group).toString();
String name = cfg.getOrDefault("name", this.name).toString();
String testValue = cfg.getOrDefault("testValue", this.testValue).toString();
String operator = cfg.getOrDefault("operator", this.operator).toString();
String comparisonValue = cfg.getOrDefault("comparisonValue", this.comparisonValue).toString();
String ifTrue = cfg.getOrDefault("ifTrue", this.ifTrue).toString();
boolean elevateToGlobal = (boolean) cfg.getOrDefault("elevateToGlobal", this.elevateToGlobal);
if (cfg.containsKey("ifFalse") && cfg.get("ifFalse") != null) {
ifFalse = cfg.getOrDefault("ifFalse", this.ifFalse).toString();
}

ctx.getLogger().log(Constants.DEBUG_LEVEL,
"Setting " + group + "." + name + " based on " + testValue + " " + operator + " " + comparisonValue);

(new IfElse(ctx)).ifElse(group, name, testValue, operator, comparisonValue, ifTrue, ifFalse, elevateToGlobal);
elevateToGlobal = (boolean) cfg.getOrDefault("elevateToGlobal", this.elevateToGlobal);

String message = "Setting " + group + "." + name + " based on " + testValue + " " + operator + " " + comparisonValue;
ctx.getLogger().log(Constants.DEBUG_LEVEL, message);

(new IfElse(ctx)).setElevate(elevateToGlobal).setCfg(cfg)
.ifElse(group, name, testValue, operator, comparisonValue, ifTrue, ifFalse);
}

}
28 changes: 10 additions & 18 deletions src/main/java/com/bioraft/rundeck/conditional/IfElseStepPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class IfElseStepPlugin implements StepPlugin {
IfElse.STRING_LE, IfElse.STRING_GE, IfElse.STRING_GT,
IfElse.STRING_BEG, IfElse.STRING_END, IfElse.NUMBER_EQ,
IfElse.NUMBER_NE, IfElse.NUMBER_LT, IfElse.NUMBER_LE,
IfElse.NUMBER_GE, IfElse.NUMBER_GT }, freeSelect = false)
IfElse.NUMBER_GE, IfElse.NUMBER_GT })
private String operator;

@PluginProperty(title = "Comparison Value", description = "Second test value", required = true)
Expand All @@ -63,30 +63,22 @@ public class IfElseStepPlugin implements StepPlugin {
@PluginProperty(title = "If True", description = "Value to assign if comparison is true", required = true)
private String ifTrue;

@PluginProperty(title = "If False", description = "Value to assign if comparison is false", required = false)
@PluginProperty(title = "If False", description = "Value to assign if comparison is false")
private String ifFalse;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)")
private boolean elevateToGlobal;

@Override
public void executeStep(final PluginStepContext ctx, final Map<String, Object> cfg) throws StepException {

String group = cfg.getOrDefault("group", this.group).toString();
String name = cfg.getOrDefault("name", this.name).toString();
String testValue = cfg.getOrDefault("testValue", this.testValue).toString();
String operator = cfg.getOrDefault("operator", this.operator).toString();
String comparisonValue = cfg.getOrDefault("comparisonValue", this.comparisonValue).toString();
String ifTrue = cfg.getOrDefault("ifTrue", this.ifTrue).toString();
boolean elevateToGlobal = (boolean) cfg.getOrDefault("elevateToGlobal", this.elevateToGlobal);
if (cfg.containsKey("ifFalse") && cfg.get("ifFalse") != null) {
ifFalse = cfg.getOrDefault("ifFalse", this.ifFalse).toString();
}

ctx.getLogger().log(Constants.DEBUG_LEVEL,
"Setting " + group + "." + name + " based on " + testValue + " " + operator + " " + comparisonValue);

(new IfElse(ctx)).ifElse(group, name, testValue, operator, comparisonValue, ifTrue, ifFalse, elevateToGlobal);
elevateToGlobal = (boolean) cfg.getOrDefault("elevateToGlobal", this.elevateToGlobal);

String message = "Setting " + group + "." + name + " based on " + testValue + " " + operator + " " + comparisonValue;
ctx.getLogger().log(Constants.DEBUG_LEVEL, message);

(new IfElse(ctx)).setElevate(elevateToGlobal).setCfg(cfg)
.ifElse(group, name, testValue, operator, comparisonValue, ifTrue, ifFalse);
}

}
9 changes: 5 additions & 4 deletions src/main/java/com/bioraft/rundeck/conditional/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
public class Switch {

public static final String CFG_DEFAULT_VALUE = "defaultValue";

private PluginStepContext ctx;

/**
Expand Down Expand Up @@ -72,7 +74,6 @@ public void switchCase(String group, String name, String cases, String test, Str
} catch (JsonProcessingException e) {
ctx.getLogger().log(Constants.ERR_LEVEL, "Failed to parse cases.");
ctx.getLogger().log(Constants.ERR_LEVEL, e.getMessage());
e.printStackTrace();
throw e;
}
}
Expand Down Expand Up @@ -126,11 +127,11 @@ public static String ensureStringIsJsonObject(String string) {
if (string == null) {
return "";
}
String trimmed = string.replaceFirst("^\\s*\\{?", "{").replaceFirst("\\s*$", "");
return trimmed + (trimmed.endsWith("}") ? "" : "}");
String trimmed = string.trim().replaceFirst(",[\\s}]*$", "");
return (trimmed.startsWith("{") ? "" : "{") + trimmed + (trimmed.endsWith("}") ? "" : "}");
}

enum Causes implements FailureReason {
InvalidJSON
INVALID_JSON
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;
import static com.bioraft.rundeck.conditional.Switch.CFG_DEFAULT_VALUE;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
Expand Down Expand Up @@ -76,18 +77,16 @@ public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INod
name = cfg.getOrDefault("name", this.name).toString();
cases = cfg.getOrDefault("cases", this.cases).toString();
testValue = cfg.getOrDefault("testValue", this.testValue).toString();
if (cfg.containsKey("elevateToGlobal")) {
elevateToGlobal = cfg.get("elevateToGlobal").equals("true");
}
elevateToGlobal = cfg.getOrDefault("elevateToGlobal", String.valueOf(elevateToGlobal)).equals("true");

boolean globalHasDefault = defaultValue != null && defaultValue.length() > 0;
boolean cfgHasDefault = cfg.containsKey("defaultValue") && cfg.get("defaultValue") != null;
boolean cfgHasDefault = cfg.containsKey(CFG_DEFAULT_VALUE) && cfg.get(CFG_DEFAULT_VALUE) != null;
if (cfgHasDefault) {
this.defaultValue = cfg.get("defaultValue").toString();
this.defaultValue = cfg.get(CFG_DEFAULT_VALUE).toString();
}

ctx.getLogger().log(Constants.DEBUG_LEVEL,
"Setting " + group + "." + name + " based on " + testValue + " " + cases);
String message = "Setting " + group + "." + name + " based on " + testValue + " " + cases;
ctx.getLogger().log(Constants.DEBUG_LEVEL, message);

try {
if (cfgHasDefault || globalHasDefault) {
Expand All @@ -96,7 +95,7 @@ public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INod
(new Switch(ctx)).switchCase(group, name, cases, testValue, elevateToGlobal);
}
} catch (JsonProcessingException e) {
throw new NodeStepException(e.getMessage(), Switch.Causes.InvalidJSON, node.getNodename());
throw new NodeStepException(e.getMessage(), Switch.Causes.INVALID_JSON, node.getNodename());
}
}

Expand Down
Loading

0 comments on commit 39686d2

Please sign in to comment.