Skip to content

Commit

Permalink
Use MarkupText#getText instead of MarkupText#toString
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Mar 1, 2020
1 parent 4234dfa commit 68ea395
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ 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, 0, build.getStartTimeInMillis())
.orElse(null);
timestamp = GlobalAnnotator.parseTimestamp(logFileLine, build).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,18 +78,7 @@ public ConsoleAnnotator<Object> annotate(Object context, MarkupText text) {
} else {
return null;
}
long buildStartTime = build.getStartTimeInMillis();
String html = text.toString(true);
int start = 0;
// cf. LogStorage.startStep
if (html.startsWith("<span class=\"pipeline-new-node\" ", start)) {
start = html.indexOf('>', start) + 1;
}
// cf. AnsiHtmlOutputStream.setForegroundColor
if (html.startsWith("<span style=\"color", start)) {
start = html.indexOf('>', start) + 1;
}
parseTimestamp(html, start, buildStartTime)
parseTimestamp(text.getText(), build)
.ifPresent(
timestamp -> {
TimestampFormat format = TimestampFormatProvider.get();
Expand All @@ -99,19 +88,16 @@ public ConsoleAnnotator<Object> annotate(Object context, MarkupText text) {
return this;
}

/**
* Parse this line for a timestamp starting at position {@code start}, if such a timestamp is
* present.
*/
/** Parse this line for a timestamp if such a timestamp is present. */
@Restricted(NoExternalUse.class)
public static Optional<Timestamp> parseTimestamp(String html, int start, long buildStartTime) {
if (html.startsWith("[", start)) {
int end = html.indexOf(']', start);
public static Optional<Timestamp> parseTimestamp(String text, Run<?, ?> build) {
if (text.startsWith("[")) {
int end = text.indexOf(']');
if (end != -1) {
try {
long millisSinceEpoch = ZonedDateTime.parse(html.substring(start + 1, end), GlobalDecorator.UTC_MILLIS).toInstant().toEpochMilli();
// Alternately: Instant.parse(html.substring(start + 1, end)).toEpochMilli()
Timestamp timestamp = new Timestamp(millisSinceEpoch - buildStartTime, millisSinceEpoch);
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);
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,9 +144,7 @@ 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, 0, build.getStartTimeInMillis())
.isPresent());
assertTrue(GlobalAnnotator.parseTimestamp(line, build).isPresent());
unstampedLines.add(line.substring(27));
}
TimestamperApiTestUtil.timestamperApi(build, unstampedLines);
Expand All @@ -167,8 +165,7 @@ public void timestamperStep() throws Exception {
assertEquals(
line,
line.contains("foo"),
GlobalAnnotator.parseTimestamp(line, 0, build.getStartTimeInMillis())
.isPresent());
GlobalAnnotator.parseTimestamp(line, build).isPresent());
}
}
}

0 comments on commit 68ea395

Please sign in to comment.