Skip to content

Commit

Permalink
fix(conditions): invalid stageEnabled expressions disable stage (#3249)
Browse files Browse the repository at this point in the history
  • Loading branch information
asher authored and mergify[bot] committed Oct 24, 2019
1 parent 4024b77 commit 4d290d0
Showing 1 changed file with 16 additions and 3 deletions.
Expand Up @@ -19,6 +19,7 @@
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static org.apache.commons.lang3.StringUtils.isBlank;

import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor;
import java.util.Map;
Expand Down Expand Up @@ -58,17 +59,25 @@ public static boolean isOptional(
return false;
}

if ("expression".equals(optionalType) && isNullOrEmpty(stageEnabled.get("expression"))) {
log.warn(
"No expression found on stage, treating as non-optional (executionId: {}, stageId: {}",
stage.getExecution().getId(),
stage.getId());
return false;
}

try {
return !stage
.mapTo("/stageEnabled", OPTIONAL_STAGE_TYPES.get(optionalType))
.evaluate(stage, contextParameterProcessor);
} catch (InvalidExpression e) {
log.warn(
"Unable to determine stage optionality, reason: {} (executionId: {}, stageId: {})",
"{}, skipping stage (executionId: {}, stageId: {})",
e.getMessage(),
stage.getExecution().getId(),
stage.getId());
return false;
return true;
}
}

Expand Down Expand Up @@ -112,13 +121,17 @@ public boolean evaluate(Stage stage, ContextParameterProcessor contextParameterP
if (!asList("true", "false").contains(expression.toLowerCase())) {
// expression failed to evaluate successfully
throw new InvalidExpression(
format("Expression '%s' could not be evaluated", this.expression));
format("Conditional Expression '%s' could not be evaluated", this.expression));
}

return Boolean.valueOf(expression);
}
}

private static boolean isNullOrEmpty(Object object) {
return object == null || object instanceof String && isBlank((String) object);
}

static class InvalidExpression extends RuntimeException {
InvalidExpression(String message) {
super(message);
Expand Down

0 comments on commit 4d290d0

Please sign in to comment.