Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pipeline_template): Deal with whitespace in jinja module kv pairs #1773

Merged
merged 2 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class ModuleTag implements Tag {

Expand All @@ -60,7 +61,9 @@ public String getName() {

@Override
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
List<String> helper = new HelperStringTokenizer(tagNode.getHelpers()).splitComma(true).allTokens();
List<String> helper = collapseWhitespaceInTokenPairs(
new HelperStringTokenizer(tagNode.getHelpers()).allTokens()
);
if (helper.isEmpty()) {
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'module' expects ID as first parameter: " + helper, tagNode.getLineNumber());
}
Expand Down Expand Up @@ -164,4 +167,51 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
public String getEndTagName() {
return null;
}

/**
* Look at this ungodly code. It's gross. Thanks to poor foresight, we tokenize on
* whitespace, which can break concatenation, and definitely breaks usage of filters.
* Sooo, we take the tokenized module definition and best-guess our way through collapsing
* whitespace to arrive at the real key/value pairs that we later parse for populating
* the module's internal context.
*/
private static List<String> collapseWhitespaceInTokenPairs(List<String> tokens) {
List<String> combinedTokens = new ArrayList<>();
combinedTokens.add(tokens.get(0));

StringBuilder buffer = new StringBuilder();
// idx 0 is `moduleName`. Skip that guy.
for (int i = 1; i < tokens.size(); i++) {
String token = tokens.get(i);
if (token.contains("=")) {
if (buffer.length() > 0) {
combinedTokens.add(buffer.toString());
}
buffer = new StringBuilder();
combinedTokens.add(token);
} else {
String lastToken = combinedTokens.get(combinedTokens.size() - 1);
if (lastToken.contains("=") && !lastToken.endsWith(",")) {
buffer.append(combinedTokens.remove(combinedTokens.size() - 1));
}
buffer.append(token);
}
}

if (buffer.length() > 0) {
int i = combinedTokens.size() - 1;
combinedTokens.set(i, combinedTokens.get(i) + buffer.toString());
}

return combinedTokens.stream()
.map(ModuleTag::removeTrailingCommas)
.collect(Collectors.toList());
}

private static String removeTrailingCommas(String token) {
if (token.endsWith(",")) {
return token.substring(0, token.length()-1);
}
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ class ModuleTagSpec extends Specification {
[name: 'myStringVar', defaultValue: 'hello'] as NamedHashMap,
[name: 'myOtherVar'] as NamedHashMap,
[name: 'subject'] as NamedHashMap,
[name: 'job'] as NamedHashMap
[name: 'job'] as NamedHashMap,
[name: 'concat', type: 'object'] as NamedHashMap,
[name: 'filtered'] as NamedHashMap
],
definition: '{{myStringVar}} {{myOtherVar}}, {{subject}}. You triggered {{job}}')
definition: '{{myStringVar}} {{myOtherVar}}, {{subject}}. You triggered {{job}} {{concat}} {{filtered}}')
]
)
RenderContext context = new DefaultRenderContext('myApp', pipelineTemplate, [job: 'myJob', buildNumber: 1234])
context.variables.put("testerName", "Mr. Tester Testington")
context.variables.put("m", [myKey: 'myValue'])

when:
def result = renderer.render('{% module myModule myOtherVar=world, subject=testerName, job=trigger.job %}', context)
def result = renderer.render("{% module myModule myOtherVar=world, subject=testerName, job=trigger.job, concat=m['my' + 'Key'], filtered=trigger.nonExist|default('hello', True) %}", context)

then:
result == 'hello world, Mr. Tester Testington. You triggered myJob'
result == 'hello world, Mr. Tester Testington. You triggered myJob myValue hello'
}
}