Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-38865] Null safety and fix Script Block toGroovy
  • Loading branch information
stephenc committed Oct 13, 2016
1 parent 738d393 commit dc354c7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
Expand Up @@ -28,32 +28,26 @@ public ModelASTPipelineDef(Object sourceLocation) {
@Override
public JSONObject toJSON() {
JSONObject a = new JSONObject();
if (stages != null) {
a.put("stages", stages.toJSON());
}
if (notifications != null) {
a.put("notifications", notifications.toJSON());
}
if (postBuild != null) {
a.put("postBuild", postBuild.toJSON());
}
if (environment != null) {
a.put("environment", environment.toJSON());
}
if (agent != null) {
a.put("agent", agent.toJSON());
}
if (tools != null) {
a.put("tools", tools.toJSON());
}
if (jobProperties != null) {
a.put("stages", stages != null ? stages.toJSON() : null);
a.put("notifications", notifications != null ? notifications.toJSON() : null);
a.put("postBuild", postBuild != null ? postBuild.toJSON() : null);
a.put("environment", environment != null ? environment.toJSON() : null);
a.put("agent", agent != null ? agent.toJSON() : null);
a.put("tools", tools != null ? tools.toJSON() : null);
if (jobProperties != null && !jobProperties.getProperties().isEmpty()) {
a.put("jobProperties", jobProperties.toJSON());
} else {
a.put("jobProperties", null);
}
if (parameters != null) {
if (parameters != null && !parameters.getParameters().isEmpty()) {
a.put("parameters", parameters.toJSON());
} else {
a.put("parameters", null);
}
if (triggers != null) {
if (triggers != null && !triggers.getTriggers().isEmpty()) {
a.put("triggers", triggers.toJSON());
} else {
a.put("triggers", null);
}
return new JSONObject().accumulate("pipeline", a);
}
Expand Down
Expand Up @@ -13,7 +13,17 @@ public ModelASTScriptBlock(Object sourceLocation) {

@Override
public String toGroovy() {
return "script {\n" + getArgs().toGroovy() + "\n}\n";
StringBuilder result = new StringBuilder("script {\n");
if (getArgs() != null
&& getArgs() instanceof ModelASTSingleArgument
&& ((ModelASTSingleArgument) getArgs()).getValue()!=null
&& ((ModelASTSingleArgument) getArgs()).getValue().isLiteral()) {
result.append(((ModelASTSingleArgument) getArgs()).getValue().getValue());
} else if (getArgs() != null) {
result.append(getArgs().toGroovy());
}
result.append("\n}\n");
return result.toString();
}

@Override
Expand Down
Expand Up @@ -23,7 +23,9 @@ public Object toJSON() {
@Override
public void validate(ModelValidator validator) {
// Nothing to immediately validate here
value.validate(validator);
if (value != null) {
value.validate(validator);
}
}

@Override
Expand All @@ -34,7 +36,9 @@ public String toGroovy() {
@Override
public void removeSourceLocation() {
super.removeSourceLocation();
value.removeSourceLocation();
if (value != null) {
value.removeSourceLocation();
}
}

public ModelASTValue getValue() {
Expand Down
Expand Up @@ -40,25 +40,33 @@ public ModelASTStep(Object sourceLocation) {

@Override
public JSONObject toJSON() {
return new JSONObject().accumulate("name", name).accumulate("arguments", args.toJSON());
JSONObject o = new JSONObject();
o.accumulate("name", name);
if (args != null) {
o.accumulate("arguments", args.toJSON());
}
return o;
}

@Override
public void validate(ModelValidator validator) {
validator.validateElement(this);

args.validate(validator);
if (args != null) {
args.validate(validator);
}
}

@Override
public String toGroovy() {
return name + "(" + args.toGroovy() + ")";
return name + "(" + (args != null ? args.toGroovy() : "") + ")";
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
args.removeSourceLocation();
if (args != null) {
args.removeSourceLocation();
}
}

public String getName() {
Expand Down
Expand Up @@ -114,9 +114,9 @@ public String toGroovy() {
if (getValue() instanceof String) {
String str = (String) getValue();
if (str.indexOf('\n') == -1) {
return "'" + str.replace("'", "\\'") + "'";
return "'" + (str.replace("'", "\\'")) + "'";
} else {
return "'''" + str.replace("'''", "\\'\\'\\'") + "'''";
return "'''" + (str.replace("'''", "\\'\\'\\'")) + "'''";
}
} else {
return getValue().toString();
Expand Down
Expand Up @@ -302,6 +302,10 @@ class ModelValidatorImpl implements ModelValidator {
if (model != null) {
if (meth.args.any { it instanceof ModelASTKeyValueOrMethodCallPair }) {
meth.args.each { a ->
if (!(a instanceof ModelASTKeyValueOrMethodCallPair)) {
errorCollector.error(meth, "Can't mix named and unnamed parameter definition arguments")
return
}
ModelASTKeyValueOrMethodCallPair kvm = (ModelASTKeyValueOrMethodCallPair) a

def p = model.getParameter(kvm.key.key);
Expand Down

0 comments on commit dc354c7

Please sign in to comment.