Skip to content

Commit

Permalink
[JENKINS-19146] Use runner (On evaluation failure) to evaluate a cond…
Browse files Browse the repository at this point in the history
…ition.
  • Loading branch information
ikedam committed Aug 16, 2013
1 parent 7148295 commit 9b58ca9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;

import org.jenkins_ci.plugins.run_condition.RunCondition;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;

import hudson.model.AbstractProject;
import hudson.model.DependencyGraph;
Expand All @@ -40,10 +41,12 @@ public class ConditionalDependencyGraphWrapper extends DependencyGraph
{
private DependencyGraph graph;
private RunCondition condition;
private BuildStepRunner runner;

public ConditionalDependencyGraphWrapper(DependencyGraph graph, RunCondition condition) {
public ConditionalDependencyGraphWrapper(DependencyGraph graph, RunCondition condition, BuildStepRunner runner) {
this.graph = graph;
this.condition = condition;
this.runner = runner;
}

/**
Expand All @@ -53,7 +56,7 @@ public ConditionalDependencyGraphWrapper(DependencyGraph graph, RunCondition con
*/
@Override
public void addDependency(Dependency dep) {
graph.addDependency(new ConditionalDependencyWrapper(dep, condition));
graph.addDependency(new ConditionalDependencyWrapper(dep, condition, runner));
}

@Override
Expand Down
Expand Up @@ -23,19 +23,25 @@
*/
package org.jenkins_ci.plugins.flexible_publish;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jenkins_ci.plugins.run_condition.RunCondition;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;

import hudson.Launcher;
import hudson.model.DependencyGraph.Dependency;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.NullStream;

/**
Expand All @@ -45,11 +51,13 @@ public class ConditionalDependencyWrapper extends Dependency {
private static Logger LOGGER = Logger.getLogger(ConditionalDependencyWrapper.class.getName());
private Dependency dep;
private RunCondition condition;
private BuildStepRunner runner;

public ConditionalDependencyWrapper(Dependency dep, RunCondition condition) {
public ConditionalDependencyWrapper(Dependency dep, RunCondition condition, BuildStepRunner runner) {
super(dep.getUpstreamProject(), dep.getDownstreamProject());
this.dep = dep;
this.condition = condition;
this.runner = runner;
}

/**
Expand All @@ -74,7 +82,14 @@ public boolean shouldTriggerBuild(AbstractBuild build,
}

try {
if (condition.runPerform(build, buildListener)) {
MarkPerformedBuilder marker = new MarkPerformedBuilder();

// launcher is not used by condition or runner or marker,
// this never cause NPE.
Launcher launcher = null;
runner.perform(condition, marker, build, launcher, buildListener);

if (marker.isPerformed()) {
return dep.shouldTriggerBuild(build, listener, actions);
} else {
return false;
Expand Down Expand Up @@ -115,4 +130,41 @@ public AbstractProject getUpstreamProject() {
public boolean pointsItself() {
return dep.pointsItself();
}

/**
* Used with {@link BuildStepRunner}.
*
* Stores whether perform is executed.
*/
private static class MarkPerformedBuilder extends Builder {
private boolean performed = false;

public boolean isPerformed() {
return performed;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
performed = true;
return true;
}

private static final Descriptor<Builder> DESCRIPTOR =
new BuildStepDescriptor<Builder>() {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "Builder to mark whether executed";
}
};
@Override
public Descriptor<Builder> getDescriptor() {
return DESCRIPTOR;
}
};
}
Expand Up @@ -136,7 +136,7 @@ public Descriptor<? extends BuildStep> getDefaultPublisher() {
public void buildDependencyGraph(AbstractProject owner, DependencyGraph graph) {
if (publisher instanceof DependecyDeclarer) {
((DependecyDeclarer)publisher).buildDependencyGraph(owner,
new ConditionalDependencyGraphWrapper(graph, condition));
new ConditionalDependencyGraphWrapper(graph, condition, runner));
}
}

Expand Down

0 comments on commit 9b58ca9

Please sign in to comment.