Skip to content
Permalink
Browse files

[JENKINS-37792] Per stage post build steps

  • Loading branch information
rsandell committed Oct 14, 2016
1 parent b7a3ec6 commit 061eebc2c0af2fc92420ccc746c2f8349727a34e
Showing with 648 additions and 104 deletions.
  1. +84 −0 ...groovy/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTBuildConditionsContainer.groovy
  2. +4 −27 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTNotifications.groovy
  3. +4 −27 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPostBuild.groovy
  4. +33 −0 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPostStage.groovy
  5. +10 −0 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTStage.groovy
  6. +34 −0 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/PostStage.groovy
  7. +1 −1 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy
  8. +20 −0 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Stage.groovy
  9. +22 −7 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy
  10. +23 −14 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy
  11. +6 −27 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.groovy
  12. +3 −0 src/main/resources/ast-schema.json
  13. +24 −1 src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
  14. +229 −0 src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java
  15. 0 src/test/resources/postStage/failingNotifications.groovy
  16. +44 −0 src/test/resources/postStage/globalAndLocalAlways.groovy
  17. +68 −0 src/test/resources/postStage/localAll.groovy
  18. +39 −0 src/test/resources/postStage/localAlways.groovy
@@ -0,0 +1,84 @@
/*
* 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator

/**
* Represents a list of {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition} and {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.StepsBlock} pairs to be called, depending on whether the build
* condition is satisfied, at the end of the build or a stage.
* Corresponds to {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.Notifications} or {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.PostBuild}
*
* @see ModelASTNotifications
* @see ModelASTPostBuild
*
* @author Robert Sandell <rsandell@cloudbees.com>.
*/
@ToString(includeSuper = true, includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public abstract class ModelASTBuildConditionsContainer extends ModelASTElement {
List<ModelASTBuildCondition> conditions = []

protected ModelASTBuildConditionsContainer(Object sourceLocation) {
super(sourceLocation)
}

/*package*/ abstract String getName();

@Override
public JSONObject toJSON() {
JSONArray a = new JSONArray()
conditions.each { c ->
a.add(c.toJSON())
}

return new JSONObject()
.accumulate("conditions", a)
}

protected void _validate(ModelValidator validator) {
conditions.each { c ->
c?.validate(validator)
}
}

@Override
public String toGroovy() {
return "${getName()} {\n${conditions.collect { it.toGroovy() }.join("\n")}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
conditions.each { c ->
c.removeSourceLocation()
}
}
}
@@ -42,43 +42,20 @@ import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator
@ToString(includeSuper = true, includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public final class ModelASTNotifications extends ModelASTElement {
List<ModelASTBuildCondition> conditions = []
public final class ModelASTNotifications extends ModelASTBuildConditionsContainer {

public ModelASTNotifications(Object sourceLocation) {
super(sourceLocation)
}

@Override
public JSONObject toJSON() {
JSONArray a = new JSONArray()
conditions.each { c ->
a.add(c.toJSON())
}

return new JSONObject()
.accumulate("conditions", a)
/*package*/ String getName() {
return "notifications";
}

@Override
public void validate(ModelValidator validator) {
validator.validateElement(this)
conditions.each { c ->
c?.validate(validator)
}
}

@Override
public String toGroovy() {
return "notifications {\n${conditions.collect { it.toGroovy() }.join("\n")}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()

conditions.each { c ->
c.removeSourceLocation()
}
_validate(validator)
}
}
@@ -43,43 +43,20 @@ import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator
@ToString(includeSuper = true, includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public final class ModelASTPostBuild extends ModelASTElement {
List<ModelASTBuildCondition> conditions = []
public final class ModelASTPostBuild extends ModelASTBuildConditionsContainer {

public ModelASTPostBuild(Object sourceLocation) {
super(sourceLocation)
}

@Override
public JSONObject toJSON() {
JSONArray a = new JSONArray()
conditions.each { c ->
a.add(c.toJSON())
}

return new JSONObject()
.accumulate("conditions", a)
/*package*/ String getName() {
return "postBuild";
}

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

conditions.each { c ->
c?.validate(validator)
}
}

@Override
public String toGroovy() {
return "postBuild {\n${conditions.collect { it.toGroovy() }.join("\n")}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
conditions.each { c ->
c.removeSourceLocation()
}
_validate(validator)
}
}
@@ -0,0 +1,33 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildConditionsContainer
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator

/**
* Represents a list of {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition} and {@link org.jenkinsci.plugins.pipeline.modeldefinition.model.StepsBlock} pairs to be called, depending on whether the build
* condition is satisfied, at the end of the stage.
*
* @author Robert Sandell &lt;rsandell@cloudbees.com&gt;.
*/
@ToString(includeSuper = true, includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public final class ModelASTPostStage extends ModelASTBuildConditionsContainer {
public ModelASTPostStage(Object sourceLocation) {
super(sourceLocation)
}

@Override
/*package*/ String getName() {
return "post"
}

@Override
public void validate(ModelValidator validator) {
validator.validateElement(this)
_validate(validator)
}
}
@@ -22,6 +22,7 @@ public final class ModelASTStage extends ModelASTElement {
String name
ModelASTAgent agent
List<ModelASTBranch> branches = []
ModelASTPostStage post;

public ModelASTStage(Object sourceLocation) {
super(sourceLocation)
@@ -39,6 +40,9 @@ public final class ModelASTStage extends ModelASTElement {
if (agent != null) {
o.accumulate("agent", agent.toJSON())
}
if (post != null) {
o.accumulate("post", post.toJSON())
}
return o
}

@@ -49,6 +53,7 @@ public final class ModelASTStage extends ModelASTElement {
b?.validate(validator)
}
agent?.validate(validator)
post?.validate(validator)
}

@Override
@@ -72,6 +77,10 @@ public final class ModelASTStage extends ModelASTElement {

retString.append("}\n")

if (post != null) {
retString.append(post.toGroovy())
}

retString.append("}\n")

return retString.toString()
@@ -84,5 +93,6 @@ public final class ModelASTStage extends ModelASTElement {
b.removeSourceLocation()
}
agent?.removeSourceLocation()
post?.removeSourceLocation()
}
}
@@ -0,0 +1,34 @@
/*
* 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings

/**
*
* @author Robert Sandell &lt;rsandell@cloudbees.com&gt;.
*/
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public class PostStage extends AbstractBuildConditionResponder<PostStage> {
}
@@ -176,7 +176,7 @@ public class Root implements NestedModel, Serializable {
* @param runWrapperObj The {@link RunWrapper} for the build.
* @return A list of closures from the responder which have had their conditions satisfied.
*/
private List<Closure> satisfiedConditionsForField(AbstractBuildConditionResponder r, Object runWrapperObj) {
/*package*/ List<Closure> satisfiedConditionsForField(AbstractBuildConditionResponder r, Object runWrapperObj) {
if (r != null) {
return r.satisfiedConditions(runWrapperObj)
} else {
@@ -47,6 +47,9 @@ public class Stage implements NestedModel, Serializable {
@Whitelisted
Agent agent

@Whitelisted
PostStage post

@Whitelisted
Stage name(String n) {
this.name = n
@@ -71,6 +74,12 @@ public class Stage implements NestedModel, Serializable {
return this
}

@Whitelisted
Stage post(PostStage post) {
this.post = post
return this
}

@Override
@Whitelisted
public void modelFromMap(Map<String,Object> m) {
@@ -79,4 +88,15 @@ public class Stage implements NestedModel, Serializable {
}
}

/**
* Returns a list of notification closures whose conditions have been satisfied and should be run.
*
* @param runWrapperObj The {@link org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper} for the build.
* @return a list of closures whose conditions have been satisfied.
*/
@Whitelisted
List<Closure> satisfiedPostStageConditions(Root root, Object runWrapperObj) {
return root.satisfiedConditionsForField(post, runWrapperObj)
}

}

0 comments on commit 061eebc

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