Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-29876] CheckForNull job in ReverseBuildTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaSha committed Oct 2, 2015
1 parent e2dccc3 commit 45e4e99
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/jenkins/triggers/ReverseBuildTrigger.java
Expand Up @@ -107,7 +107,7 @@ public Result getThreshold() {

private boolean shouldTrigger(Run upstreamBuild, TaskListener listener) {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
if (jenkins == null || job == null) {
return false;
}
// This checks Item.READ also on parent folders; note we are checking as the upstream auth currently:
Expand Down Expand Up @@ -147,7 +147,7 @@ private boolean shouldTrigger(Run upstreamBuild, TaskListener listener) {
}
}

@Override public void start(Job project, boolean newInstance) {
@Override public void start(@Nonnull Job project, boolean newInstance) {
super.start(project, newInstance);
SecurityContext orig = ACL.impersonate(ACL.SYSTEM);
try {
Expand Down
35 changes: 35 additions & 0 deletions test/src/test/java/jenkins/triggers/ReverseBuildTriggerTest.java
Expand Up @@ -39,6 +39,8 @@
import hudson.tasks.BuildTrigger;
import hudson.tasks.BuildTriggerTest;
import hudson.triggers.Trigger;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -48,9 +50,16 @@
import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.acegisecurity.Authentication;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.*;

import org.hamcrest.core.IsNull;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;

Expand Down Expand Up @@ -148,4 +157,30 @@ public class ReverseBuildTriggerTest {
assertEquals(new Cause.UpstreamCause((Run) b), downstream.getLastBuild().getCause(Cause.UpstreamCause.class));
}

@Issue("JENKINS-29876")
@Test
public void nullJobInTriggerNotCausesNPE() throws Exception {
final FreeStyleProject upstreamJob = r.createFreeStyleProject("upstream");

//job with trigger.job == null
final FreeStyleProject downstreamJob1 = r.createFreeStyleProject("downstream1");
final ReverseBuildTrigger reverseBuildTrigger = new ReverseBuildTrigger("upstream", Result.SUCCESS);
downstreamJob1.addTrigger(reverseBuildTrigger);
downstreamJob1.save();

//job with trigger.job != null
final FreeStyleProject downstreamJob2 = r.createFreeStyleProject("downstream2");
final ReverseBuildTrigger reverseBuildTrigger2 = new ReverseBuildTrigger("upstream", Result.SUCCESS);
downstreamJob2.addTrigger(reverseBuildTrigger2);
downstreamJob2.save();
r.configRoundtrip(downstreamJob2);

r.jenkins.rebuildDependencyGraph();
final FreeStyleBuild build = upstreamJob.scheduleBuild2(0).get();
r.waitUntilNoActivity();

r.assertLogNotContains("java.lang.NullPointerException", build);
assertThat("Build should be not triggered", downstreamJob1.getBuilds(), hasSize(0));
assertThat("Build should be triggered", downstreamJob2.getBuilds(), not(hasSize(0)));
}
}

0 comments on commit 45e4e99

Please sign in to comment.