Skip to content

Commit

Permalink
When looking up on other branches, start with latest build and not th…
Browse files Browse the repository at this point in the history
…e previous one

* Build hyperlink formatting
* Adjust unit test accordingly
  • Loading branch information
Vlatombe committed Jan 16, 2023
1 parent 21b93f9 commit 6451c6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -348,8 +348,10 @@ static private void collect(TestResult r, Map<String, TestClass> 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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 6451c6d

Please sign in to comment.