From 69d8fe47ff9d60cd885aaeb3e0b91b4fcfe6ba6e Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 11 Aug 2017 11:03:42 -0400 Subject: [PATCH] [FIXED JENKINS-46082] API will include culprits again. (cherry picked from commit a975f7227ee58d3f274b82e3d9b615b297f30fa0) --- .../main/java/hudson/model/AbstractBuild.java | 6 +++++ .../java/hudson/model/AbstractBuildTest.java | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 9644c2c0ded7..c8674fb02865 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -324,6 +324,12 @@ public FilePath[] getModuleRoots() { return culprits; } + @Override + @Exported + @Nonnull public Set getCulprits() { + return RunWithSCM.super.getCulprits(); + } + @Override public boolean shouldCalculateCulprits() { return getCulpritIds() == null; diff --git a/test/src/test/java/hudson/model/AbstractBuildTest.java b/test/src/test/java/hudson/model/AbstractBuildTest.java index df845c06adc1..fad2b3dcb73c 100644 --- a/test/src/test/java/hudson/model/AbstractBuildTest.java +++ b/test/src/test/java/hudson/model/AbstractBuildTest.java @@ -24,6 +24,7 @@ package hudson.model; import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.WebResponse; import hudson.EnvVars; import hudson.Launcher; import hudson.model.queue.QueueTaskFuture; @@ -43,6 +44,8 @@ import hudson.tasks.LogRotatorTest; import hudson.tasks.Recorder; import hudson.util.OneShotEvent; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.CaptureEnvironmentBuilder; @@ -53,6 +56,7 @@ import org.jvnet.hudson.test.TestBuilder; import org.jvnet.hudson.test.TestExtension; import org.jvnet.hudson.test.UnstableBuilder; +import org.xml.sax.SAXException; /** * Unit tests of {@link AbstractBuild}. @@ -125,12 +129,31 @@ public void rawConsoleOutput() throws Exception { assertThat(rsp.getWebResponse().getContentAsString(), containsString(out)); } - private void assertCulprits(AbstractBuild b, String... expectedIds) { + private void assertCulprits(AbstractBuild b, String... expectedIds) throws IOException, SAXException { Set actual = new TreeSet<>(); for (User u : b.getCulprits()) { actual.add(u.getId()); } assertEquals(actual, new TreeSet<>(Arrays.asList(expectedIds))); + + if (expectedIds.length > 0) { + JenkinsRule.WebClient wc = j.createWebClient(); + WebResponse response = wc.goTo(b.getUrl() + "api/json?tree=culprits[id]", "application/json").getWebResponse(); + JSONObject json = JSONObject.fromObject(response.getContentAsString()); + + Object culpritsArray = json.get("culprits"); + assertNotNull(culpritsArray); + assertTrue(culpritsArray instanceof JSONArray); + Set fromApi = new TreeSet<>(); + for (Object o : ((JSONArray)culpritsArray).toArray()) { + assertTrue(o instanceof JSONObject); + Object id = ((JSONObject)o).get("id"); + if (id instanceof String) { + fromApi.add((String)id); + } + } + assertEquals(fromApi, new TreeSet<>(Arrays.asList(expectedIds))); + } } @Test