From 6451c6d1b4b0ca7ee0db21fa16204744dda6470f Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Mon, 16 Jan 2023 10:07:19 +0100 Subject: [PATCH] When looking up on other branches, start with latest build and not the previous one * Build hyperlink formatting * Adjust unit test accordingly --- .../ParallelTestExecutor.java | 24 ++++++++++--------- .../ParallelTestExecutorUnitTest.java | 2 ++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutor.java b/src/main/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutor.java index 7675156d..a882abdc 100644 --- a/src/main/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutor.java +++ b/src/main/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutor.java @@ -5,10 +5,10 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.AbortException; import hudson.Extension; -import hudson.ExtensionList; import hudson.FilePath; import hudson.Launcher; import hudson.Util; +import hudson.console.ModelHyperlinkNote; import hudson.model.*; import hudson.plugins.parameterizedtrigger.*; import hudson.tasks.BuildStepDescriptor; @@ -348,8 +348,10 @@ static private void collect(TestResult r, Map data) { private static TestResult findPreviousTestResult(Run b, TaskListener listener) { Job project = b.getParent(); - TestResult result = getTestResult(project, b, listener); + // Look for test results starting with the previous build + TestResult result = getTestResult(project, b.getPreviousBuild(), listener); if (result == null) { + // Look for test results from the target branch builds if this is a change request. SCMHead head = SCMHead.HeadByItem.findHead(project); if (head instanceof ChangeRequestSCMHead) { SCMHead target = ((ChangeRequestSCMHead) head).getTarget(); @@ -366,19 +368,19 @@ private static TestResult findPreviousTestResult(Run b, TaskListener liste private static TestResult getTestResult(Job originProject, Run b, TaskListener listener) { TestResult result = null; for (int i = 0; i < NUMBER_OF_BUILDS_TO_SEARCH; i++) {// limit the search to a small number to avoid loading too much - b = b.getPreviousBuild(); if (b == null) break; - if(!RESULTS_OF_BUILDS_TO_CONSIDER.contains(b.getResult()) || b.isBuilding()) continue; + if (!RESULTS_OF_BUILDS_TO_CONSIDER.contains(b.getResult()) || b.isBuilding()) continue; AbstractTestResultAction tra = b.getAction(AbstractTestResultAction.class); - if (tra == null) continue; - - Object o = tra.getResult(); - if (o instanceof TestResult) { - listener.getLogger().printf("Using build %s#%d as reference%n", originProject != b.getParent() ? b.getParent().getFullName() : "", b.getNumber()); - result = (TestResult) o; - break; + if (tra != null) { + Object o = tra.getResult(); + if (o instanceof TestResult) { + listener.getLogger().printf("Using build %s as reference%n", ModelHyperlinkNote.encodeTo('/' + b.getUrl(), originProject != b.getParent() ? b.getFullDisplayName() : b.getDisplayName())); + result = (TestResult) o; + break; + } } + b = b.getPreviousBuild(); } return result; } diff --git a/src/test/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutorUnitTest.java b/src/test/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutorUnitTest.java index 5b1070cb..93370834 100644 --- a/src/test/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutorUnitTest.java +++ b/src/test/java/org/jenkinsci/plugins/parallel_test_executor/ParallelTestExecutorUnitTest.java @@ -57,6 +57,8 @@ public class ParallelTestExecutorUnitTest { public void setUp() throws Exception { when(build.getPreviousBuild()).thenReturn((Run)previousBuild); when(previousBuild.getResult()).thenReturn(Result.SUCCESS); + when(previousBuild.getUrl()).thenReturn("job/some-project/1"); + when(previousBuild.getDisplayName()).thenReturn("#1"); when(listener.getLogger()).thenReturn(System.err); when(previousBuild.getAction(eq(AbstractTestResultAction.class))).thenReturn(action); }