Skip to content

Commit

Permalink
Merge pull request #87 from svanoort/feature-workflow-compatibility
Browse files Browse the repository at this point in the history
[JENKINS-26050] Fix Workflow compatibility for Parameterized Trigger
  • Loading branch information
svanoort committed Aug 10, 2015
2 parents df0ee6e + b60a16e commit 5f04921
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 69 deletions.
22 changes: 22 additions & 0 deletions pom.xml
Expand Up @@ -63,6 +63,27 @@
<version>1.3</version>
<scope>test</scope>
</dependency>

<!-- Workflow plugin imports are used to test compatibility -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -160,6 +181,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<workflow.version>1.4</workflow.version>
</properties>
</project>

Expand Up @@ -9,8 +9,10 @@
import hudson.model.BuildListener;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.Run;
import jenkins.model.ParameterizedJobMixIn;
import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.collect.ArrayListMultimap;
Expand Down Expand Up @@ -60,7 +62,14 @@ public ListMultimap<AbstractProject, Future<AbstractBuild>> perform2(AbstractBui
}

@Override
protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, List<Action> list) throws InterruptedException, IOException {
public ListMultimap<Job, Future<Run>> perform3(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
ListMultimap<Job, Future<Run>> futures = super.perform3(build, launcher, listener);
if(block==null) return ArrayListMultimap.create();
return futures;
}

@Override
protected Future schedule(AbstractBuild<?, ?> build, Job project, List<Action> list) throws InterruptedException, IOException {
if (block!=null) {
while (true) {
// add DifferentiatingAction to make sure this doesn't get merged with something else,
Expand All @@ -70,7 +79,7 @@ protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, Li
// if we fail to add the item to the queue, wait and retry.
// it also means we have to force quiet period = 0, or else it'll never leave the queue
Future f = schedule(build, project, 0, list);
//when a project is disabled or the configuration is not yet saved f will always be null and we'ure caught in a loop, therefore we need to check for it
// When a project is disabled or the configuration is not yet saved f will always be null and we're caught in a loop, therefore we need to check for it
if (f!=null || (f==null && !project.isBuildable())){
return f;
}
Expand Down
Expand Up @@ -11,13 +11,15 @@
import hudson.model.DependencyGraph;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.model.Job;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class BuildTrigger extends Notifier implements DependecyDeclarer {
Expand Down Expand Up @@ -48,20 +50,41 @@ public BuildStepMonitor getRequiredMonitorService() {
}

@Override @SuppressWarnings("deprecation")
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {

HashSet<BuildTriggerConfig> alreadyFired = new HashSet<BuildTriggerConfig>();

// If this project has non-abstract projects, we need to fire them
for (BuildTriggerConfig config : configs) {
boolean hasNonAbstractProject = false;

List<Job> jobs = config.getJobs(build.getRootBuild().getProject().getParent(), build.getEnvironment(listener));
for (Job j : jobs) {
if (!(j instanceof AbstractProject)) {
hasNonAbstractProject = true;
break;
}
}
// Fire this config's projects if not already fired
if (hasNonAbstractProject) {
config.perform(build, launcher, listener);
alreadyFired.add(config);
}
}

if (canDeclare(build.getProject())) {
// job will get triggered by dependency graph, so we have to capture buildEnvironment NOW before
// hudson.model.AbstractBuild.AbstractBuildExecution#cleanUp is called and reset
EnvVars env = build.getEnvironment(listener);
build.addAction(new CapturedEnvironmentAction(env));
} else {
} else { // Not using dependency graph
for (BuildTriggerConfig config : configs) {
config.perform(build, launcher, listener);
if (!alreadyFired.contains(config)) {
config.perform(build, launcher, listener);
}
}
}


return true;
}

Expand All @@ -70,9 +93,12 @@ public void buildDependencyGraph(AbstractProject owner, DependencyGraph graph) {
// Can only add dependencies in Hudson 1.341 or higher
if (!canDeclare(owner)) return;

for (BuildTriggerConfig config : configs)
for (AbstractProject project : config.getProjectList(owner.getParent(),null))
for (BuildTriggerConfig config : configs) {
List<AbstractProject> projectList = config.getProjectList(owner.getParent(), null);
for (AbstractProject project : projectList) {
ParameterizedDependency.add(owner, project, config, graph);
}
}
}

private boolean canDeclare(AbstractProject owner) {
Expand Down

0 comments on commit 5f04921

Please sign in to comment.