Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into JENKINS-37778
Browse files Browse the repository at this point in the history
 Conflicts:
	pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
  • Loading branch information
rsandell committed Nov 8, 2016
2 parents bec0c4b + ec6f0ea commit 44c8249
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 50 deletions.
@@ -0,0 +1,62 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

/**
* Represents the special step which are executed without validation against the declarative subset.
* @see ModelASTScriptBlock
* @see ModelASTWhen
*/
public abstract class AbstractModelASTCodeBlock extends ModelASTStep {

protected AbstractModelASTCodeBlock(Object sourceLocation, String name) {
super(sourceLocation);
this.setName(name);
}

@Override
public String toGroovy() {
StringBuilder result = new StringBuilder(getName()).append(" {\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
public String toString() {
return getClass().getSimpleName() + "{" +
"name='" + getName() + '\'' +
", args=" + getArgs() +
"}";
}
}
Expand Up @@ -5,33 +5,8 @@
*
* @author Andrew Bayer
*/
public class ModelASTScriptBlock extends ModelASTStep {
public class ModelASTScriptBlock extends AbstractModelASTCodeBlock {
public ModelASTScriptBlock(Object sourceLocation) {
super(sourceLocation);
this.setName("script");
super(sourceLocation, "script");
}

@Override
public String toGroovy() {
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
public String toString() {
return "ModelASTScriptBlock{" +
"name='" + getName() + '\'' +
", args=" + getArgs() +
"}";
}

}
Expand Up @@ -18,6 +18,7 @@ public final class ModelASTStage extends ModelASTElement {
private ModelASTAgent agent;
private List<ModelASTBranch> branches = new ArrayList<ModelASTBranch>();
private ModelASTPostStage post;
private ModelASTWhen when;
private ModelASTTools tools;
private ModelASTEnvironment environment;

Expand All @@ -37,6 +38,9 @@ public JSONObject toJSON() {
if (agent != null) {
o.accumulate("agent", agent.toJSON());
}
if (when != null) {
o.accumulate("when", when.toJSON());
}

if (post != null) {
o.accumulate("post", post.toJSON());
Expand All @@ -62,6 +66,9 @@ public void validate(final ModelValidator validator) {
if (agent != null) {
agent.validate(validator);
}
if (when != null) {
when.validate(validator);
}
if (post != null) {
post.validate(validator);
}
Expand All @@ -81,6 +88,9 @@ public String toGroovy() {
if (agent != null) {
result.append(agent.toGroovy());
}
if (when != null) {
result.append(when.toGroovy());
}
if (tools != null) {
result.append(tools.toGroovy());
}
Expand Down Expand Up @@ -125,6 +135,9 @@ public void removeSourceLocation() {
if (agent != null) {
agent.removeSourceLocation();
}
if (when != null) {
when.removeSourceLocation();
}
if (post != null) {
post.removeSourceLocation();
}
Expand Down Expand Up @@ -168,6 +181,14 @@ public void setPost(ModelASTPostStage post) {
this.post = post;
}

public ModelASTWhen getWhen() {
return when;
}

public void setWhen(ModelASTWhen when) {
this.when = when;
}

public ModelASTTools getTools() {
return tools;
}
Expand All @@ -189,6 +210,7 @@ public String toString() {
return "ModelASTStage{" +
"name='" + name + '\'' +
", agent=" + agent +
", when=" + when +
", branches=" + branches +
", post=" + post +
", tools=" + tools +
Expand Down Expand Up @@ -219,6 +241,10 @@ public boolean equals(Object o) {
if (getPost() != null ? !getPost().equals(that.getPost()) : that.getPost() != null) {
return false;
}
if (getWhen() != null ? !getWhen().equals(that.getWhen()) : that.getWhen() != null) {
return false;
}

if (getTools() != null ? !getTools().equals(that.getTools()) : that.getTools() != null) {
return false;
}
Expand All @@ -235,6 +261,7 @@ public int hashCode() {
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getAgent() != null ? getAgent().hashCode() : 0);
result = 31 * result + (getBranches() != null ? getBranches().hashCode() : 0);
result = 31 * result + (getWhen() != null ? getWhen().hashCode() : 0);
result = 31 * result + (getPost() != null ? getPost().hashCode() : 0);
result = 31 * result + (getTools() != null ? getTools().hashCode() : 0);
result = 31 * result + (getEnvironment() != null ? getEnvironment().hashCode() : 0);
Expand Down
@@ -0,0 +1,53 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import net.sf.json.JSONObject;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
* Represents a block for when/if a {@link ModelASTStage} will be executed or not.
*/
public class ModelASTWhen extends AbstractModelASTCodeBlock {
public ModelASTWhen(Object sourceLocation) {
super(sourceLocation, "when");
}

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

@Override
public void validate(ModelValidator validator) {
super.validate(validator);
validator.validateElement(this);
}
}
Expand Up @@ -45,6 +45,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTools;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTrigger;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTriggers;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhen;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWrapper;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWrappers;

Expand All @@ -70,6 +71,8 @@ public interface ModelValidator {

boolean validateElement(ModelASTStep step);

boolean validateElement(ModelASTWhen when);

boolean validateElement(ModelASTMethodCall methodCall);

boolean validateElement(ModelASTJobProperties jobProperties);
Expand Down
Expand Up @@ -48,6 +48,8 @@ public class Stage implements NestedModel, Serializable {

PostStage post

StepsBlock when

Tools tools

Environment environment
Expand Down Expand Up @@ -82,6 +84,11 @@ public class Stage implements NestedModel, Serializable {
return this
}

Stage when(StepsBlock when) {
this.when = when
return this
}

Stage tools(Tools tools) {
this.tools = tools
return this
Expand Down
Expand Up @@ -61,6 +61,7 @@ import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPostBuild
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTScriptBlock
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTools
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTreeStep
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhen
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWrapper
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWrappers
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ErrorCollector
Expand Down Expand Up @@ -200,6 +201,12 @@ class JSONParser {
}
}

if (j.has("when")) {
def object = j.getJSONObject("when")
if (!object.isNullObject()) {
stage.when = parseWhen(object)
}
}
return stage

}
Expand Down Expand Up @@ -417,6 +424,13 @@ class JSONParser {
return scriptBlock
}

public @CheckForNull ModelASTWhen parseWhen(JSONObject j) {
ModelASTWhen scriptBlock = new ModelASTWhen(j)
scriptBlock.args = parseArgumentList(j.getJSONObject("arguments"))

return scriptBlock
}

public @CheckForNull ModelASTTreeStep parseTreeStep(JSONObject j) {
ModelASTTreeStep step = new ModelASTTreeStep(j)
step.name = j.getString("name")
Expand Down

0 comments on commit 44c8249

Please sign in to comment.