Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #49 from abayer/jenkins-31576
[FIXED JENKINS-31576] Add currentBuild.upstreamBuilds
  • Loading branch information
abayer committed Jan 4, 2018
2 parents 5ea4e13 + f0b548c commit da2ca81
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -226,6 +226,33 @@ public class RunWrapperTest {
});
}

@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) {
Expand Down

0 comments on commit da2ca81

Please sign in to comment.