Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-40370] Make stage.when more declarative
- Loading branch information
Showing
16 changed files
with
631 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTWhenExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/** | ||
* Code expression {@link ModelASTStage} will be executed or not. | ||
*/ | ||
public class ModelASTWhenExpression extends AbstractModelASTCodeBlock { | ||
public ModelASTWhenExpression(Object sourceLocation) { | ||
super(sourceLocation, "expression"); | ||
} | ||
|
||
@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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/StageConditionals.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.model | ||
|
||
import groovy.transform.EqualsAndHashCode | ||
import groovy.transform.ToString | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional | ||
import org.jenkinsci.plugins.workflow.cps.CpsScript | ||
import org.jenkinsci.plugins.workflow.steps.StepContext | ||
|
||
/** | ||
* The {@link Stage#when} block. | ||
*/ | ||
@ToString | ||
@EqualsAndHashCode | ||
class StageConditionals implements MethodsToList<DeclarativeStageConditional>, Serializable { | ||
private List<DeclarativeStageConditional> conditions; | ||
|
||
StageConditionals(List<DeclarativeStageConditional> conditions) { | ||
this.conditions = conditions | ||
} | ||
|
||
public boolean evaluate(StepContext context, CpsScript script) { | ||
for (int i = 0; i < conditions.size(); i++) { | ||
DeclarativeStageConditional cond = conditions[i]; | ||
if (!cond.evaluate(context, script)) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...java/org/jenkinsci/plugins/pipeline/modeldefinition/when/DeclarativeStageConditional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.when; | ||
|
||
import hudson.AbortException; | ||
import hudson.ExtensionPoint; | ||
import hudson.model.AbstractDescribableImpl; | ||
import org.jenkinsci.plugins.workflow.cps.CpsScript; | ||
import org.jenkinsci.plugins.workflow.cps.CpsThread; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* Conditionals for when to run a stage. | ||
*/ | ||
public abstract class DeclarativeStageConditional extends AbstractDescribableImpl<DeclarativeStageConditional> implements Serializable, ExtensionPoint { | ||
|
||
public abstract boolean evaluate(CpsScript script) throws InterruptedException, AbortException; | ||
|
||
@Override | ||
public DeclarativeStageConditionalDescriptor getDescriptor() { | ||
return (DeclarativeStageConditionalDescriptor) super.getDescriptor(); | ||
} | ||
|
||
@Nullable | ||
protected <T> T getContextVariable(Class<T> key) throws IOException, InterruptedException { | ||
CpsThread current = CpsThread.current(); | ||
if (current != null) { | ||
throw new IllegalStateException("Needs to be called within a CPS Thread"); | ||
} | ||
StepExecution step = current.getStep(); | ||
if (step != null) { | ||
return step.getContext().get(key); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.