Skip to content
Permalink
Browse files

Merge pull request #41 from jenkinsci/JENKINS-37781

[JENKINS-37781] Adding Stage.when to specify if a stage should run
  • Loading branch information...
rsandell committed Nov 8, 2016
2 parents eee41f8 + 8c76660 commit ec6f0ea92278c82b854b65710d4fc0e8f65f3413
Showing with 537 additions and 74 deletions.
  1. +62 −0 ...i/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/AbstractModelASTCodeBlock.java
  2. +2 −27 ...del-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTScriptBlock.java
  3. +27 −0 ...ine-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTStage.java
  4. +53 −0 ...line-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTWhen.java
  5. +3 −0 ...el-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.java
  6. +8 −1 ...odel-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Stage.groovy
  7. +14 −0 ...efinition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy
  8. +38 −7 ...finition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy
  9. +11 −0 ...rc/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidatorImpl.groovy
  10. +16 −0 pipeline-model-definition/src/main/resources/ast-schema.json
  11. +54 −39 ...inition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
  12. +114 −0 ...-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java
  13. +48 −0 pipeline-model-definition/src/test/resources/when/simpleWhen.groovy
  14. +43 −0 pipeline-model-definition/src/test/resources/when/whenEmpty.groovy
  15. +44 −0 pipeline-model-definition/src/test/resources/when/whenException.groovy
@@ -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() +
"}";
}
}
@@ -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() +
"}";
}

}
@@ -18,6 +18,7 @@
private ModelASTAgent agent;
private List<ModelASTBranch> branches = new ArrayList<ModelASTBranch>();
private ModelASTPostStage post;
private ModelASTWhen when;
private ModelASTTools tools;
private ModelASTEnvironment environment;

@@ -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());
@@ -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);
}
@@ -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());
}
@@ -125,6 +135,9 @@ public void removeSourceLocation() {
if (agent != null) {
agent.removeSourceLocation();
}
if (when != null) {
when.removeSourceLocation();
}
if (post != null) {
post.removeSourceLocation();
}
@@ -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;
}
@@ -189,6 +210,7 @@ public String toString() {
return "ModelASTStage{" +
"name='" + name + '\'' +
", agent=" + agent +
", when=" + when +
", branches=" + branches +
", post=" + post +
", tools=" + tools +
@@ -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;
}
@@ -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);
@@ -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);
}
}
@@ -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;

@@ -70,6 +71,8 @@

boolean validateElement(ModelASTStep step);

boolean validateElement(ModelASTWhen when);

boolean validateElement(ModelASTMethodCall methodCall);

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

PostStage post

StepsBlock when

Tools tools

Environment environment
@@ -68,7 +70,7 @@ public class Stage implements NestedModel, Serializable {
this.agent = new Agent(s)
return this
}

Stage steps(StepsBlock s) {
this.steps = s
return this
@@ -79,6 +81,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
@@ -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
@@ -200,6 +201,12 @@ class JSONParser {
}
}

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

}
@@ -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")

0 comments on commit ec6f0ea

Please sign in to comment.
You can’t perform that action at this time.