From b381cc3f0660aab49ce51f3840bf3abca3e19edf Mon Sep 17 00:00:00 2001 From: Julien Greffe Date: Thu, 14 Dec 2023 15:16:27 +0100 Subject: [PATCH 1/3] [JENKINS-72474] Additional informations on BFA scans --- .../plugins/bfa/BuildFailureScanner.java | 12 ++++- .../jenkins/plugins/bfa/ScanLogAction.java | 50 +++++++++++++++++++ .../bfa/BuildFailureScannerHudsonTest.java | 31 ++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java index 3e9afcd7..f9ea0f77 100644 --- a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java +++ b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java @@ -171,7 +171,8 @@ public static void scanIfNotScanned(final Run build, final PrintStream scanLog) * @param scanLog log to write information to. */ public static void scan(Run build, PrintStream scanLog) { - build.addOrReplaceAction(new ScanLogAction()); + ScanLogAction scanLogAction = new ScanLogAction(); + build.addOrReplaceAction(scanLogAction); try { Collection causes = PluginImpl.getInstance().getKnowledgeBase().getCauses(); List foundCauseListToLog = findCauses(causes, build, scanLog); @@ -262,7 +263,10 @@ public static void scan(Run build, PrintStream scanLog) { } } } catch (Exception e) { + scanLogAction.setException(e); logger.log(Level.SEVERE, "Could not scan build " + build, e); + } finally { + scanLogAction.finished(); } } @@ -591,8 +595,12 @@ private static List parseSingleLineCauses(Run build, build, reader, LOG_FILE_NAME)); - } catch (IOException e) { + } catch (Exception e) { logToScanLog(scanLog, "Exception during parsing file: " + e); + ScanLogAction logAction = build.getAction(ScanLogAction.class); + if (logAction != null && logAction.getException() == null) { + logAction.setException(e); + } } finally { if (reader != null) { try { diff --git a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java index 417b915a..400a7437 100644 --- a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java +++ b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java @@ -7,10 +7,13 @@ import java.nio.charset.StandardCharsets; import jenkins.model.RunAction2; import org.apache.commons.io.FileUtils; +import org.kohsuke.stapler.export.Exported; +import org.kohsuke.stapler.export.ExportedBean; /** * The Action for adding a link to the analysis for each run. */ +@ExportedBean public class ScanLogAction implements RunAction2 { /** @@ -20,6 +23,12 @@ public class ScanLogAction implements RunAction2 { private transient Run run; + private long startTime = System.currentTimeMillis(); + + private Long endTime; + + private Exception exception; + /** * {@inheritDoc} */ @@ -64,6 +73,47 @@ public String getLogText() throws IOException { return FileUtils.readFileToString(new File(run.getRootDir(), FILE_NAME), StandardCharsets.UTF_8); } + /** + * The start time of the current scan. + * @return time in milliseconds {@link System#currentTimeMillis} + */ + @Exported + public long getStartTime() { + return startTime; + } + + /** + * The end time of the current scan. + * @return time in milliseconds {@link System#currentTimeMillis} + */ + @Exported + public Long getEndTime() { + return endTime; + } + + /** + * To call when the scan is finished. + */ + public void finished() { + this.endTime = System.currentTimeMillis(); + } + + /** + * Get the exception if any. + * @return the first exception faced during scan + */ + public Exception getException() { + return exception; + } + + /** + * Set an exception. + * @param exception the exception to set + */ + public void setException(Exception exception) { + this.exception = exception; + } + /** * {@inheritDoc} */ diff --git a/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java b/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java index 39654d23..f9f2bd52 100644 --- a/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java +++ b/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java @@ -129,6 +129,12 @@ public void testOneIndicationFound() throws Exception { FreeStyleBuild build = future.get(10, TimeUnit.SECONDS); jenkins.assertBuildStatus(Result.FAILURE, build); + ScanLogAction scanLogAction = build.getAction(ScanLogAction.class); + assertNotNull(scanLogAction); + assertNotNull(scanLogAction.getStartTime()); + assertNotNull(scanLogAction.getEndTime()); + assertNull(scanLogAction.getException()); + FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class); assertNotNull(action); List causeListFromAction = action.getFoundFailureCauses(); @@ -155,6 +161,31 @@ public void testOneIndicationFound() throws Exception { assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Error").getCount()); } + /** + * Test when an exception occurred during scan. + * @throws Exception if so. + */ + @Test + public void testExceptionDuringParsing() throws Exception { + FreeStyleProject project = createProject(); + + FailureCause failureCause = configureCauseAndIndication(new BuildLogIndication("(wrong pattern")); + + QueueTaskFuture future = project.scheduleBuild2(0, new Cause.UserIdCause()); + + FreeStyleBuild build = future.get(10, TimeUnit.SECONDS); + jenkins.assertBuildStatus(Result.FAILURE, build); + + ScanLogAction scanLogAction = build.getAction(ScanLogAction.class); + assertNotNull(scanLogAction); + assertNotNull(scanLogAction.getStartTime()); + assertNotNull(scanLogAction.getEndTime()); + assertNotNull(scanLogAction.getException()); + + FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class); + assertNotNull(action); + } + /** * Happy test that should find one generic failure indication in the build. * From aa846cfd31d12d4a4743aab405af584504ac769c Mon Sep 17 00:00:00 2001 From: Julien Greffe Date: Mon, 18 Dec 2023 14:14:11 +0100 Subject: [PATCH 2/3] [JENKINS-72474] Apply suggestions --- .../plugins/bfa/BuildFailureScanner.java | 6 +++--- .../jenkins/plugins/bfa/ScanLogAction.java | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java index f9ea0f77..3763a849 100644 --- a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java +++ b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java @@ -263,7 +263,7 @@ public static void scan(Run build, PrintStream scanLog) { } } } catch (Exception e) { - scanLogAction.setException(e); + scanLogAction.setExceptionMessage(e.toString()); logger.log(Level.SEVERE, "Could not scan build " + build, e); } finally { scanLogAction.finished(); @@ -598,8 +598,8 @@ private static List parseSingleLineCauses(Run build, } catch (Exception e) { logToScanLog(scanLog, "Exception during parsing file: " + e); ScanLogAction logAction = build.getAction(ScanLogAction.class); - if (logAction != null && logAction.getException() == null) { - logAction.setException(e); + if (logAction != null && logAction.getExceptionMessage() == null) { + logAction.setExceptionMessage(e.toString()); } } finally { if (reader != null) { diff --git a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java index 400a7437..aa3c3e85 100644 --- a/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java +++ b/src/main/java/com/sonyericsson/jenkins/plugins/bfa/ScanLogAction.java @@ -7,6 +7,8 @@ import java.nio.charset.StandardCharsets; import jenkins.model.RunAction2; import org.apache.commons.io.FileUtils; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; @@ -27,7 +29,7 @@ public class ScanLogAction implements RunAction2 { private Long endTime; - private Exception exception; + private String exceptionMessage; /** * {@inheritDoc} @@ -94,24 +96,25 @@ public Long getEndTime() { /** * To call when the scan is finished. */ - public void finished() { + @Restricted(NoExternalUse.class) + protected void finished() { this.endTime = System.currentTimeMillis(); } /** - * Get the exception if any. + * Get the exception message if any. * @return the first exception faced during scan */ - public Exception getException() { - return exception; + public String getExceptionMessage() { + return exceptionMessage; } /** * Set an exception. - * @param exception the exception to set + * @param exceptionMessage the exception message to set */ - public void setException(Exception exception) { - this.exception = exception; + public void setExceptionMessage(String exceptionMessage) { + this.exceptionMessage = exceptionMessage; } /** From 8363544c17b137f58857c8fcba9bf15456b4a2e9 Mon Sep 17 00:00:00 2001 From: Julien Greffe Date: Mon, 18 Dec 2023 14:24:50 +0100 Subject: [PATCH 3/3] [JENKINS-72474] Apply suggestions --- .../jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java b/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java index f9f2bd52..c24fc9e9 100644 --- a/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java +++ b/src/test/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScannerHudsonTest.java @@ -133,7 +133,7 @@ public void testOneIndicationFound() throws Exception { assertNotNull(scanLogAction); assertNotNull(scanLogAction.getStartTime()); assertNotNull(scanLogAction.getEndTime()); - assertNull(scanLogAction.getException()); + assertNull(scanLogAction.getExceptionMessage()); FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class); assertNotNull(action); @@ -180,7 +180,7 @@ public void testExceptionDuringParsing() throws Exception { assertNotNull(scanLogAction); assertNotNull(scanLogAction.getStartTime()); assertNotNull(scanLogAction.getEndTime()); - assertNotNull(scanLogAction.getException()); + assertNotNull(scanLogAction.getExceptionMessage()); FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class); assertNotNull(action);