Skip to content

Commit

Permalink
Prefer the original long buildStartTime parameter as it is more amena…
Browse files Browse the repository at this point in the history
…ble to unit testing.
  • Loading branch information
basil committed Mar 2, 2020
1 parent 908c53b commit cd08a08
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public TimestampLogFileLine readLine() throws IOException {
// If a timestamps file is not present, attempt to read the timestamp from the log file.
// The log file is decorated with GlobalDecorator for Pipeline builds of version 1.9 or
// later.
timestamp = GlobalAnnotator.parseTimestamp(logFileLine, build).orElse(null);
timestamp =
GlobalAnnotator.parseTimestamp(logFileLine, build.getStartTimeInMillis())
.orElse(null);
if (timestamp != null) {
// If we succeeded, then the log file was decorated by GlobalDecorator. Strip the
// timestamp decoration from the front of the line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public ConsoleAnnotator<Object> annotate(Object context, MarkupText text) {
} else {
return null;
}
parseTimestamp(text.getText(), build)
long buildStartTime = build.getStartTimeInMillis();
parseTimestamp(text.getText(), buildStartTime)
.ifPresent(
timestamp -> {
TimestampFormat format = TimestampFormatProvider.get();
Expand All @@ -90,14 +91,14 @@ public ConsoleAnnotator<Object> annotate(Object context, MarkupText text) {

/** Parse this line for a timestamp if such a timestamp is present. */
@Restricted(NoExternalUse.class)
public static Optional<Timestamp> parseTimestamp(String text, Run<?, ?> build) {
public static Optional<Timestamp> parseTimestamp(String text, long buildStartTime) {
if (text.startsWith("[")) {
int end = text.indexOf(']');
if (end != -1) {
try {
long millisSinceEpoch = ZonedDateTime.parse(text.substring(1, end), GlobalDecorator.UTC_MILLIS).toInstant().toEpochMilli();
// Alternately: Instant.parse(text.substring(1, end)).toEpochMilli()
Timestamp timestamp = new Timestamp(millisSinceEpoch - build.getStartTimeInMillis(), millisSinceEpoch);
Timestamp timestamp = new Timestamp(millisSinceEpoch - buildStartTime, millisSinceEpoch);
return Optional.of(timestamp);
} catch (DateTimeParseException x) {
// something else, ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public void timestamperApi() throws Exception {
r.assertLogContains("foo", build);
List<String> unstampedLines = new ArrayList<>();
for (String line : build.getLog(Integer.MAX_VALUE)) {
assertTrue(GlobalAnnotator.parseTimestamp(line, build).isPresent());
assertTrue(
GlobalAnnotator.parseTimestamp(line, build.getStartTimeInMillis()).isPresent());
unstampedLines.add(line.substring(27));
}
TimestamperApiTestUtil.timestamperApi(build, unstampedLines);
Expand All @@ -165,7 +166,7 @@ public void timestamperStep() throws Exception {
assertEquals(
line,
line.contains("foo"),
GlobalAnnotator.parseTimestamp(line, build).isPresent());
GlobalAnnotator.parseTimestamp(line, build.getStartTimeInMillis()).isPresent());
}
}
}

0 comments on commit cd08a08

Please sign in to comment.