Skip to content

Commit

Permalink
JENKINS-54139 Jacoco: don't automatically publish report if more than…
Browse files Browse the repository at this point in the history
… 1 Maven module has generated a jacoco report
  • Loading branch information
Cyrille Le Clerc committed Oct 22, 2018
1 parent a42a374 commit 75ce8e2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 69 deletions.
Expand Up @@ -93,8 +93,8 @@ public void process(@Nonnull final StepContext context, @Nonnull final Element m
final Launcher launcher = context.get(Launcher.class);

Set<String> concordionOutputDirPatterns = new HashSet<String>();
concordionOutputDirPatterns.addAll(findConcordionOutputDirPatterns(XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, SUREFIRE_ID, SUREFIRE_GOAL)));
concordionOutputDirPatterns.addAll(findConcordionOutputDirPatterns(XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, FAILSAFE_ID, FAILSAFE_GOAL)));
concordionOutputDirPatterns.addAll(findConcordionOutputDirPatterns(XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, SUREFIRE_ID, SUREFIRE_GOAL, "MojoSucceeded", "MojoFailed")));
concordionOutputDirPatterns.addAll(findConcordionOutputDirPatterns(XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, FAILSAFE_ID, FAILSAFE_GOAL, "MojoSucceeded", "MojoFailed")));

if (concordionOutputDirPatterns.isEmpty()) {
if (LOGGER.isLoggable(Level.FINE)) {
Expand Down
Expand Up @@ -173,7 +173,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
Launcher launcher = context.get(Launcher.class);


List<Element> findbugsEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, "org.codehaus.mojo", "findbugs-maven-plugin", "findbugs");
List<Element> findbugsEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, "org.codehaus.mojo", "findbugs-maven-plugin", "findbugs", "MojoSucceeded", "MojoFailed");

if (findbugsEvents.isEmpty()) {
LOGGER.log(Level.FINE, "No org.codehaus.mojo:findbugs-maven-plugin:findbugs execution found");
Expand All @@ -190,10 +190,6 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE


for (Element findBugsTestEvent : findbugsEvents) {
String findBugsEventType = findBugsTestEvent.getAttribute("type");
if (!findBugsEventType.equals("MojoSucceeded") && !findBugsEventType.equals("MojoFailed")) {
continue;
}

Element pluginElt = XmlUtils.getUniqueChildElement(findBugsTestEvent, "plugin");
Element xmlOutputDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "xmlOutputDirectory");
Expand Down
Expand Up @@ -85,8 +85,8 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
listener = new StreamBuildListener((OutputStream) System.err);
}

List<Element> invokerRunRunEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, ARTIFACT_ID, RUN_GOAL);
List<Element> invokerRunIntegrationTestEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, ARTIFACT_ID, INTEGRATION_TEST_GOAL);
List<Element> invokerRunRunEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, ARTIFACT_ID, RUN_GOAL, "MojoSucceeded", "MojoFailed");
List<Element> invokerRunIntegrationTestEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, ARTIFACT_ID, INTEGRATION_TEST_GOAL, "MojoSucceeded", "MojoFailed");

if (invokerRunRunEvents.isEmpty() && invokerRunIntegrationTestEvents.isEmpty()) {
if (LOGGER.isLoggable(Level.FINE)) {
Expand Down Expand Up @@ -118,10 +118,6 @@ private void executeReporter(StepContext context, TaskListener listener, List<El
Launcher launcher = context.get(Launcher.class);

for (Element testEvent : testEvents) {
String surefireEventType = testEvent.getAttribute("type");
if (!surefireEventType.equals("MojoSucceeded") && !surefireEventType.equals("MojoFailed")) {
continue;
}
Element projectElt = XmlUtils.getUniqueChildElement(testEvent, "project");
Element pluginElt = XmlUtils.getUniqueChildElement(testEvent, "plugin");
Element reportsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "reportsDirectory");
Expand Down
Expand Up @@ -123,11 +123,16 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

List<Element> jacocoPrepareAgentEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, "org.jacoco", "jacoco-maven-plugin", "prepare-agent");
List<Element> jacocoPrepareAgentEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, "org.jacoco", "jacoco-maven-plugin", "prepare-agent", "MojoSucceeded", "MojoFailed");

if (jacocoPrepareAgentEvents.isEmpty()) {
LOGGER.log(Level.FINE, "No org.jacoco:jacoco-maven-plugin:prepare-agent execution found");
return;
} else if (jacocoPrepareAgentEvents.size() > 1) { // JENKINS-54139
if (LOGGER.isLoggable(Level.FINE))
listener.getLogger().print("[withMaven - Jacoco] More than 1 Maven module (" + jacocoPrepareAgentEvents.size() + ") generated a Jacoco code coverage report, " +
"skip automatic collect of Jacoco reports as the Jenkins Jacoco report is not designed to render multiple reports per build");
return;
}
try {
Class.forName("hudson.plugins.jacoco.JacocoPublisher");
Expand All @@ -140,10 +145,6 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE


for (Element jacocoPrepareAgentEvent : jacocoPrepareAgentEvents) {
String jacocoPrepareAgentEventType = jacocoPrepareAgentEvent.getAttribute("type");
if (!jacocoPrepareAgentEventType.equals("MojoSucceeded") && !jacocoPrepareAgentEventType.equals("MojoFailed")) {
continue;
}

Element buildElement = XmlUtils.getUniqueChildElementOrNull(jacocoPrepareAgentEvent, "project", "build");
if (buildElement == null) {
Expand Down
Expand Up @@ -196,8 +196,8 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
return;
}

List<Element> sureFireTestEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, SUREFIRE_ID, SUREFIRE_GOAL);
List<Element> failSafeTestEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, FAILSAFE_ID, FAILSAFE_GOAL);
List<Element> sureFireTestEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, SUREFIRE_ID, SUREFIRE_GOAL, "MojoSucceeded", "MojoFailed");
List<Element> failSafeTestEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, GROUP_ID, FAILSAFE_ID, FAILSAFE_GOAL, "MojoSucceeded", "MojoFailed");

executeReporter(context, listener, sureFireTestEvents, SUREFIRE_ID + ":" + SUREFIRE_GOAL);
executeReporter(context, listener, failSafeTestEvents, FAILSAFE_ID + ":" + FAILSAFE_GOAL);
Expand All @@ -224,10 +224,6 @@ private void executeReporter(StepContext context, TaskListener listener, List<El
*/

for (Element testEvent : testEvents) {
String surefireEventType = testEvent.getAttribute("type");
if (!surefireEventType.equals("MojoSucceeded") && !surefireEventType.equals("MojoFailed")) {
continue;
}
Element pluginElt = XmlUtils.getUniqueChildElement(testEvent, "plugin");
Element reportsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "reportsDirectory");
Element projectElt = XmlUtils.getUniqueChildElement(testEvent, "project");
Expand Down
Expand Up @@ -171,7 +171,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

List<Element> spotbugsEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, "com.github.spotbugs", "spotbugs-maven-plugin", "spotbugs");
List<Element> spotbugsEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, "com.github.spotbugs", "spotbugs-maven-plugin", "spotbugs", "MojoSucceeded", "MojoFailed");

if (spotbugsEvents.isEmpty()) {
LOGGER.log(Level.FINE, "No com.github.spotbugs:spotbugs-maven-plugin:spotbugs execution found");
Expand Down
Expand Up @@ -261,64 +261,25 @@ public static Element getArtifactDeployedEvent(@Nonnull List<Element> artifactDe
</ExecutionEvent>
*/
@Nonnull
public static List<Element> getExecutionEvents(@Nonnull Element mavenSpyLogs, String pluginGroupId, String pluginArtifactId, String pluginGoal) {
List<Element> result = new ArrayList<>();
for (Element executionEventElt : getChildrenElements(mavenSpyLogs, "ExecutionEvent")) {
Element pluginElt = XmlUtils.getUniqueChildElementOrNull(executionEventElt, "plugin");
if (pluginElt == null) {

} else {
if (pluginElt.getAttribute("groupId").equals(pluginGroupId) &&
pluginElt.getAttribute("artifactId").equals(pluginArtifactId) &&
pluginElt.getAttribute("goal").equals(pluginGoal)) {
result.add(executionEventElt);
} else {

}
}
public static List<Element> getExecutionEventsByPlugin(@Nonnull Element mavenSpyLogs, String pluginGroupId, String pluginArtifactId, String pluginGoal, String... eventType) {
Set<String> eventTypes = new HashSet<>(Arrays.asList(eventType));

}
return result;
}

/*
<ExecutionEvent type="MojoSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2017-02-02 23:03:17.06">
<project artifactIdId="supplychain-portal" groupId="com.acmewidgets.supplychain" name="supplychain-portal" version="0.0.7" />
<plugin executionId="default-test" goal="test" groupId="org.apache.maven.plugins" artifactId="maven-surefire-plugin" version="2.18.1">
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
</plugin>
</ExecutionEvent>
*/

/**
*
* @param mavenSpyLogs
* @param eventType e.g. "MojoSucceeded"
* @param pluginGroupId e.g. "org.apache.maven.plugins" artifactId=
* @param pluginArtifactId e.g. "maven-surefire-plugin"
* @param pluginGoal e.g. "test"
* @return
*/
@Nonnull
public static List<Element> getExecutionEvents(@Nonnull Element mavenSpyLogs, String eventType, String pluginGroupId, String pluginArtifactId, String pluginGoal) {
List<Element> result = new ArrayList<>();
for (Element executionEventElt : getChildrenElements(mavenSpyLogs, "ExecutionEvent")) {

if (executionEventElt.getAttribute("type").equals(eventType)) {
if (eventTypes.contains(executionEventElt.getAttribute("type"))) {
Element pluginElt = XmlUtils.getUniqueChildElementOrNull(executionEventElt, "plugin");
if (pluginElt == null) {
// ignore unexpected

} else {
if (pluginElt.getAttribute("groupId").equals(pluginGroupId) &&
pluginElt.getAttribute("artifactId").equals(pluginArtifactId) &&
pluginElt.getAttribute("goal").equals(pluginGoal)) {
result.add(executionEventElt);
} else {
// ignore non matching plugin

}
}
} else {
// ignore not supported event type
}

}
Expand Down
Expand Up @@ -276,6 +276,19 @@ public void test_getArtifactDeployedEvent() throws Exception {
Assert.assertThat(repositoryUrl, Matchers.is("https://nexus.beescloud.com/content/repositories/snapshots/"));
}

@Test
public void test_getExecutionEventsByPlugin() throws Exception {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml");
in.getClass(); // check non null
Element mavenSpyLogs = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in).getDocumentElement();

List<Element> executionEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogs, "org.apache.maven.plugins", "maven-deploy-plugin","deploy", "MojoSucceeded", "MojoFailed");

Assert.assertThat(executionEvents.size(), Matchers.is(1));
Element deployExecutionEvent = executionEvents.get(0);
Assert.assertThat(deployExecutionEvent.getAttribute("type"), Matchers.is("MojoSucceeded"));
}

@Test
public void test_listGeneratedArtifacts() throws Exception {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml");
Expand Down

0 comments on commit 75ce8e2

Please sign in to comment.