Skip to content
Permalink
Browse files
[JENKINS-38865] Null safety and fix Script Block toGroovy
  • Loading branch information
stephenc committed Oct 13, 2016
1 parent 738d393 commit dc354c72807779cab2ac420b385843b39192f8e8
@@ -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);
}
@@ -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
@@ -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
@@ -34,7 +36,9 @@ public String toGroovy() {
@Override
public void removeSourceLocation() {
super.removeSourceLocation();
value.removeSourceLocation();
if (value != null) {
value.removeSourceLocation();
}
}

public ModelASTValue getValue() {
@@ -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() {
@@ -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();
@@ -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);

0 comments on commit dc354c7

Please sign in to comment.