Skip to content

Commit

Permalink
Merge pull request #30 from jenkinsci/JENKINS-37792
Browse files Browse the repository at this point in the history
[JENKINS-37792] Per stage post build steps
  • Loading branch information
abayer committed Oct 19, 2016
2 parents b7a3ec6 + a29727b commit 9517f02
Show file tree
Hide file tree
Showing 19 changed files with 742 additions and 116 deletions.
@@ -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()
}
}
}
Expand Up @@ -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)
}
}
Expand Up @@ -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)
}
}
Expand Up @@ -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)
Expand All @@ -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
}

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

@Override
Expand All @@ -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()
Expand All @@ -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> {
}
Expand Up @@ -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 {
Expand Down
Expand Up @@ -47,6 +47,9 @@ public class Stage implements NestedModel, Serializable {
@Whitelisted
Agent agent

@Whitelisted
PostStage post

@Whitelisted
Stage name(String n) {
this.name = n
Expand All @@ -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) {
Expand All @@ -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 9517f02

Please sign in to comment.