Skip to content

Commit

Permalink
Merge pull request #175 from jgreffe/JENKINS-72474
Browse files Browse the repository at this point in the history
[JENKINS-72474] Additional informations on BFA scans
  • Loading branch information
rsandell authored Dec 18, 2023
2 parents de48348 + 8363544 commit 13c1349
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<FailureCause> causes = PluginImpl.getInstance().getKnowledgeBase().getCauses();
List<FoundFailureCause> foundCauseListToLog = findCauses(causes, build, scanLog);
Expand Down Expand Up @@ -262,7 +263,10 @@ public static void scan(Run build, PrintStream scanLog) {
}
}
} catch (Exception e) {
scanLogAction.setExceptionMessage(e.toString());

Check warning on line 266 in src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 266 is not covered by tests
logger.log(Level.SEVERE, "Could not scan build " + build, e);
} finally {
scanLogAction.finished();
}
}

Expand Down Expand Up @@ -591,8 +595,12 @@ private static List<FoundFailureCause> 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.getExceptionMessage() == null) {

Check warning on line 601 in src/main/java/com/sonyericsson/jenkins/plugins/bfa/BuildFailureScanner.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 601 is only partially covered, 2 branches are missing
logAction.setExceptionMessage(e.toString());
}
} finally {
if (reader != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
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;

/**
* The Action for adding a link to the analysis for each run.
*/
@ExportedBean
public class ScanLogAction implements RunAction2 {

/**
Expand All @@ -20,6 +25,12 @@ public class ScanLogAction implements RunAction2 {

private transient Run run;

private long startTime = System.currentTimeMillis();

private Long endTime;

private String exceptionMessage;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -64,6 +75,48 @@ 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.
*/
@Restricted(NoExternalUse.class)
protected void finished() {
this.endTime = System.currentTimeMillis();
}

/**
* Get the exception message if any.
* @return the first exception faced during scan
*/
public String getExceptionMessage() {
return exceptionMessage;
}

/**
* Set an exception.
* @param exceptionMessage the exception message to set
*/
public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.getExceptionMessage());

FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class);
assertNotNull(action);
List<FoundFailureCause> causeListFromAction = action.getFoundFailureCauses();
Expand All @@ -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<FreeStyleBuild> 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.getExceptionMessage());

FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class);
assertNotNull(action);
}

/**
* Happy test that should find one generic failure indication in the build.
*
Expand Down

0 comments on commit 13c1349

Please sign in to comment.