Skip to content
Permalink
Browse files

Merge pull request #8 from fredg02/master

[FIXED JENKINS-11116] and [FIXED JENKINS-11117]
  • Loading branch information...
abayer committed Dec 3, 2011
2 parents c8a57a2 + 559c3a0 commit 320f7f0b4d5c12f585b5aada39006c406e8b3ef7
@@ -12,6 +12,9 @@
import hudson.model.Run;
import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
@@ -48,6 +51,13 @@ public BlockingBehaviour getBlock() {
return r;
}

@Override
public ListMultimap<AbstractProject, Future<AbstractBuild>> perform2(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
ListMultimap<AbstractProject, Future<AbstractBuild>> futures = super.perform2(build, launcher, listener);
if(block==null) return ArrayListMultimap.create();
return futures;
}

@Override
protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, List<Action> list) throws InterruptedException, IOException {
if (block!=null) {
@@ -1,6 +1,8 @@
package hudson.plugins.parameterizedtrigger;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import hudson.EnvVars;
import hudson.Extension;
@@ -219,6 +221,30 @@ private static ParametersAction getDefaultParameters(AbstractProject<?,?> projec
return Collections.emptyList();
}

public ListMultimap<AbstractProject, Future<AbstractBuild>> perform2(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables());

try {
if (getCondition().isMet(build.getResult())) {
ListMultimap<AbstractProject, Future<AbstractBuild>> futures = ArrayListMultimap.create();

for (List<AbstractBuildParameters> addConfigs : getDynamicBuildParameters(build, listener)) {
List<Action> actions = getBaseActions(ImmutableList.<AbstractBuildParameters>builder().addAll(configs).addAll(addConfigs).build(), build, listener);
for (AbstractProject project : getProjectList(env)) {
List<Action> list = getBuildActions(actions, project);

futures.put(project, schedule(build, project, list));
}
}
return futures;
}
} catch (DontTriggerException e) {
// don't trigger on this configuration
}
return ArrayListMultimap.create();
}

/**
* @return
* Inner list represents a set of build parameters used together for one invocation of a project,
@@ -25,7 +25,6 @@

package hudson.plugins.parameterizedtrigger;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import hudson.AbortException;
import hudson.EnvVars;
@@ -45,8 +44,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@@ -84,38 +83,37 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables());

ListMultimap<BlockableBuildTriggerConfig,List<Future<AbstractBuild>>> futures = ArrayListMultimap.create();
for (BlockableBuildTriggerConfig config : configs) {
futures.put(config, config.perform(build, launcher, listener));
}

boolean buildStepResult = true;

try {
for (Entry<BlockableBuildTriggerConfig, List<Future<AbstractBuild>>> e : futures.entries()) {
int n=0;
BlockableBuildTriggerConfig config = e.getKey();
for (BlockableBuildTriggerConfig config : configs) {
ListMultimap<AbstractProject, Future<AbstractBuild>> futures = config.perform2(build, launcher, listener);
List<AbstractProject> projectList = config.getProjectList(env);

if(!projectList.isEmpty()){
AbstractProject p = projectList.get(n);
for (Future<AbstractBuild> f : e.getValue()) {
try {
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
AbstractBuild b = f.get();
listener.getLogger().println(HyperlinkNote.encodeTo('/'+ b.getUrl(), b.getFullDisplayName()) + " completed. Result was "+b.getResult());

if(buildStepResult && config.getBlock().mapBuildStepResult(b.getResult())) {
build.setResult(config.getBlock().mapBuildResult(b.getResult()));
//handle non-blocking configs
if(futures.isEmpty()){
listener.getLogger().println("Triggering projects: " + getProjectListAsString(projectList));
continue;
}
//handle blocking configs
for (AbstractProject p : projectList) {
for (Future<AbstractBuild> future : futures.get(p)) {
try {
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
AbstractBuild b = future.get();
listener.getLogger().println(HyperlinkNote.encodeTo('/'+ b.getUrl(), b.getFullDisplayName()) + " completed. Result was "+b.getResult());

if(buildStepResult && config.getBlock().mapBuildStepResult(b.getResult())) {
build.setResult(config.getBlock().mapBuildResult(b.getResult()));
} else {
buildStepResult = false;
}
} catch (CancellationException x) {
throw new AbortException(p.getFullDisplayName() +" aborted.");
}
else {
buildStepResult = false;
}
} catch (CancellationException x) {
throw new AbortException(p.getFullDisplayName() +" aborted.");
}
n++;
}
}
} else {
throw new AbortException("Build aborted. No projects to trigger. Check your configuration!");
}
@@ -127,6 +125,18 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
return buildStepResult;
}

private String getProjectListAsString(List<AbstractProject> projectList){
StringBuffer projectListString = new StringBuffer();
for (Iterator iterator = projectList.iterator(); iterator.hasNext();) {
AbstractProject project = (AbstractProject) iterator.next();
projectListString.append(HyperlinkNote.encodeTo('/'+ project.getUrl(), project.getFullDisplayName()));
if(iterator.hasNext()){
projectListString.append(", ");
}
}
return projectListString.toString();
}

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
@Override
@@ -0,0 +1,129 @@
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* 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 hudson.plugins.parameterizedtrigger.test;

import hudson.model.Cause.UserCause;
import hudson.model.ParametersAction;
import hudson.model.Project;
import hudson.model.StringParameterValue;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters;
import hudson.plugins.parameterizedtrigger.BlockableBuildTriggerConfig;
import hudson.plugins.parameterizedtrigger.BlockingBehaviour;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.BuildTriggerConfig;
import hudson.plugins.parameterizedtrigger.CurrentBuildParameters;
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.plugins.parameterizedtrigger.TriggerBuilder;

import java.util.ArrayList;
import java.util.List;

import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;

public class TriggerBuilderTest extends HudsonTestCase {

private BlockableBuildTriggerConfig createTriggerConfig(String projects) {
return new BlockableBuildTriggerConfig(projects, new BlockingBehaviour("never", "never", "never"), null);
}

public void testOrderOfLogEntries() throws Exception {
createFreeStyleProject("project1");
createFreeStyleProject("project2");
createFreeStyleProject("project3");
createFreeStyleProject("project4");
createFreeStyleProject("project5");
createFreeStyleProject("project6");

Project<?, ?> triggerProject = createFreeStyleProject("projectA");

TriggerBuilder triggerBuilder = new TriggerBuilder(createTriggerConfig("project1"));
triggerBuilder.getConfigs().add(createTriggerConfig("project2"));
triggerBuilder.getConfigs().add(createTriggerConfig("project3"));
triggerBuilder.getConfigs().add(createTriggerConfig("project4"));
triggerBuilder.getConfigs().add(createTriggerConfig("project5"));
triggerBuilder.getConfigs().add(createTriggerConfig("project6"));

triggerProject.getBuildersList().add(triggerBuilder);

triggerProject.scheduleBuild2(0, new UserCause()).get();

List<String> log = triggerProject.getLastBuild().getLog(20);
for (String string : log) {
System.out.println(string);
}
assertEquals("project1 #1 completed. Result was SUCCESS", log.get(2));
assertEquals("project2 #1 completed. Result was SUCCESS", log.get(4));
assertEquals("project3 #1 completed. Result was SUCCESS", log.get(6));
assertEquals("project4 #1 completed. Result was SUCCESS", log.get(8));
assertEquals("project5 #1 completed. Result was SUCCESS", log.get(10));
assertEquals("project6 #1 completed. Result was SUCCESS", log.get(12));

}

public void testWaitingForCompletion() throws Exception {
createFreeStyleProject("project1");
createFreeStyleProject("project2");
createFreeStyleProject("project3");

Project<?, ?> triggerProject = createFreeStyleProject("projectA");

TriggerBuilder triggerBuilder = new TriggerBuilder(createTriggerConfig("project1, project2, project3"));

triggerProject.getBuildersList().add(triggerBuilder);

triggerProject.scheduleBuild2(0, new UserCause()).get();

List<String> log = triggerProject.getLastBuild().getLog(20);
for (String string : log) {
System.out.println(string);
}
assertEquals("Waiting for the completion of project1", log.get(1));
assertEquals("Waiting for the completion of project2", log.get(3));
assertEquals("Waiting for the completion of project3", log.get(5));
}

public void testNonBlockingTrigger() throws Exception {
createFreeStyleProject("project1");
createFreeStyleProject("project2");
createFreeStyleProject("project3");

Project<?, ?> triggerProject = createFreeStyleProject("projectA");

BlockableBuildTriggerConfig config = new BlockableBuildTriggerConfig("project1, project2, project3", null, null);
TriggerBuilder triggerBuilder = new TriggerBuilder(config);

triggerProject.getBuildersList().add(triggerBuilder);

triggerProject.scheduleBuild2(0, new UserCause()).get();

List<String> log = triggerProject.getLastBuild().getLog(20);
for (String string : log) {
System.out.println(string);
}

assertEquals("Triggering projects: project1, project2, project3", log.get(1));
}

}

0 comments on commit 320f7f0

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