Skip to content
Permalink
Browse files

[FIXED JENKINS-31576] Add currentBuild.upstreamBuilds

  • Loading branch information...
abayer committed Dec 18, 2017
1 parent 5ea4e13 commit f0b548c043256a7f385ea6188da924e87c0893ef
@@ -62,7 +62,7 @@
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>2.60.2</jenkins.version>
<jenkins.version>2.60.3</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<git-plugin.version>3.0.5</git-plugin.version>
@@ -88,7 +88,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.27</version>
<version>1.39</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
@@ -123,7 +123,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.33</version>
<version>2.42</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -26,6 +26,7 @@

import hudson.AbortException;
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
@@ -34,12 +35,14 @@
import hudson.security.ACLContext;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import jenkins.scm.RunWithSCM;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction;
@@ -226,6 +229,35 @@ public String getId() throws AbortException {
}
}

@Whitelisted
@Nonnull
public List<RunWrapper> getUpstreamBuilds() throws AbortException {
List<RunWrapper> upstreams = new ArrayList<>();
Run<?,?> build = build();
for (Cause c : build.getCauses()) {
if (c instanceof Cause.UpstreamCause) {
upstreams.addAll(upstreamCauseToRunWrappers((Cause.UpstreamCause)c));
}
}

return upstreams;
}

@Nonnull
private List<RunWrapper> upstreamCauseToRunWrappers(@Nonnull Cause.UpstreamCause cause) {
List<RunWrapper> upstreams = new ArrayList<>();
Run<?,?> r = cause.getUpstreamRun();
if (r != null) {
upstreams.add(new RunWrapper(r, false));
for (Cause c : cause.getUpstreamCauses()) {
if (c instanceof Cause.UpstreamCause) {
upstreams.addAll(upstreamCauseToRunWrappers((Cause.UpstreamCause) c));
}
}
}
return upstreams;
}

@SuppressWarnings("deprecation")
@Whitelisted
public @Nonnull String getAbsoluteUrl() throws AbortException {
@@ -226,6 +226,33 @@
});
}

@Issue("JENKINS-31576")
@Test
public void upstreamBuilds() {
r.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob first = r.j.createProject(WorkflowJob.class, "first-job");
WorkflowJob second = r.j.createProject(WorkflowJob.class, "second-job");
WorkflowJob third = r.j.createProject(WorkflowJob.class, "third-job");
first.setDefinition(new CpsFlowDefinition("build job: 'second-job'\n", true));
second.setDefinition(new CpsFlowDefinition("build job: 'third-job'\n", true));
third.setDefinition(new CpsFlowDefinition("currentBuild.upstreamBuilds?.each { b ->\n" +
" echo \"b: ${b.getFullDisplayName()}\"\n" +
"}\n", true));

WorkflowRun firstRun = r.j.buildAndAssertSuccess(first);
WorkflowRun secondRun = second.getBuildByNumber(1);
r.j.assertBuildStatusSuccess(secondRun);
WorkflowRun thirdRun = third.getBuildByNumber(1);
r.j.assertBuildStatusSuccess(thirdRun);

r.j.assertLogContains("b: " + firstRun.getFullDisplayName(), thirdRun);
r.j.assertLogContains("b: " + secondRun.getFullDisplayName(), thirdRun);
}
});
}

// Like org.hamcrest.text.MatchesPattern.matchesPattern(String) but doing a substring, not whole-string, match:
private static Matcher<String> containsRegexp(final String rx) {
return new SubstringMatcher(rx) {

0 comments on commit f0b548c

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