Skip to content

Commit

Permalink
Merge pull request #46 from ikedam/feature/JENKINS-18804_FixNPEonTrig…
Browse files Browse the repository at this point in the history
…geredBuildSelector

[JENKINS-18804] Fix NPE in TriggeredBuildSelector
  • Loading branch information
ikedam committed Oct 11, 2014
2 parents 9f7f31b + 0af0734 commit 22eb37c
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
Expand Up @@ -23,6 +23,9 @@
*/
package hudson.plugins.copyartifact;

import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -44,6 +47,7 @@
* @author Alan Harder
*/
public class TriggeredBuildSelector extends BuildSelector {
private static final Logger LOGGER = Logger.getLogger(TriggeredBuildSelector.class.getName());
/**
* Which build should be used if triggered by multiple upstream builds.
*
Expand Down Expand Up @@ -160,6 +164,14 @@ public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?>
} else {
// Figure out the parent job and do a recursive call to getBuild
Job<?,?> parentJob = Jenkins.getInstance().getItemByFullName(upstreamProject, Job.class);
if (parentJob == null) {
LOGGER.log(Level.WARNING, "Upstream project doesn't exist (may be removed): {0}", upstreamProject);
continue;
}
if (parentJob.getBuildByNumber(upstreamBuild) == null) {
LOGGER.log(Level.WARNING, "Upstream build doesn't exist (may be removed): {0} #{1}", new Object[]{upstreamProject, upstreamBuild});
continue;
}
Run<?,?> run = getBuild(
job,
env,
Expand Down
Expand Up @@ -33,11 +33,13 @@
import hudson.model.FreeStyleBuild;
import hudson.model.Result;
import hudson.plugins.copyartifact.testutils.FileWriteBuilder;
import hudson.plugins.copyartifact.testutils.RemoveUpstreamBuilder;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildTrigger;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

Expand Down Expand Up @@ -611,4 +613,151 @@ public void testIsUseNewest() throws Exception {
d.setGlobalUpstreamFilterStrategy(TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest);
assertFalse(new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest).isUseNewest());
}

@Bug(18804)
@Test
public void testUpstreamWasRemoved() throws Exception {
// upstream -> downstream
// Remove the build of upstream.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new RemoveUpstreamBuilder());
downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

assertNull(upstream.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}

// upstream -> intermediate -> downstream
// Remove the build of upstream.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject intermediate = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
intermediate.getFullName(),
false
));

intermediate.getBuildersList().add(new RemoveUpstreamBuilder());
intermediate.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

assertNull(upstream.getLastBuild());
j.assertBuildStatusSuccess(intermediate.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}

// upstream -> intermediate -> downstream
// Remove the build of intermediate.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject intermediate = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
intermediate.getFullName(),
false
));

intermediate.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new RemoveUpstreamBuilder());
downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

j.assertBuildStatusSuccess(upstream.getLastBuild());
assertNull(intermediate.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}
}
}
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* 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.copyartifact.testutils;

import java.io.IOException;

import jenkins.model.Jenkins;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.BuildListener;
import hudson.tasks.Builder;

/**
* Removes the upstream build.
* (Only removes the direct upstream build).
*/
public class RemoveUpstreamBuilder extends Builder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
for (Cause.UpstreamCause c: Util.filter(build.getCauses(), Cause.UpstreamCause.class)) {
Job<?,?> upstreamProject = Jenkins.getInstance().getItemByFullName(c.getUpstreamProject(), Job.class);
if (upstreamProject == null) {
listener.getLogger().println(String.format("Not Found: %s", c.getUpstreamProject()));
continue;
}

Run<?,?> upstreamBuild = upstreamProject.getBuildByNumber(c.getUpstreamBuild());
if (upstreamBuild == null) {
listener.getLogger().println(String.format("Not Found: %s - %d", upstreamProject.getFullName(), c.getUpstreamBuild()));
continue;
}

listener.getLogger().println(String.format("Removed: %s - %s", upstreamProject.getFullName(), upstreamBuild.getFullDisplayName()));
upstreamBuild.delete();
}
return true;
}
}

0 comments on commit 22eb37c

Please sign in to comment.