Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-48344] Lightweight timestamp annotations #25

Merged
merged 16 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>1.0-beta-7</version>
</extension>
</extensions>
2 changes: 2 additions & 0 deletions .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-Pconsume-incrementals
-Pmight-produce-incrementals
56 changes: 49 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.37</version>
<version>3.25</version>
<relativePath />
</parent>

<artifactId>timestamper</artifactId>
<version>1.8.11-SNAPSHOT</version>
<version>${revision}${changelist}</version>
<packaging>hpi</packaging>

<name>Timestamper</name>
Expand All @@ -36,7 +37,7 @@
<connection>scm:git:git://github.com/jenkinsci/timestamper-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/timestamper-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/timestamper-plugin</url>
<tag>HEAD</tag>
<tag>${scmTag}</tag>
</scm>

<repositories>
Expand All @@ -61,8 +62,11 @@
</distributionManagement>

<properties>
<jenkins.version>1.608</jenkins.version> <!-- SimpleBuildWrapper.createLoggerDecorator -->
<java.level>7</java.level>
<revision>1.8.11</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.121.1</jenkins.version>
Copy link
Member

@dwnusbaum dwnusbaum Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a jump, although required for the JEP-210 updates. Here are stats for the plugin's usage by Jenkins core version: http://stats.jenkins.io/pluginversions/timestamper.html. According to those stats, 89.63% of users of timestamper-1.8.10 are on Jenkins 2.121.1 or newer, so this looks reasonable to me.

<java.level>8</java.level>
<useBeta>true</useBeta>
</properties>

<build>
Expand All @@ -75,6 +79,7 @@
<threshold>Low</threshold>
</configuration>
</plugin>
<!-- TODO pending https://github.com/andrena/macker-maven-plugin/pull/6 this is incompatible with Incrementals: target/target/generated-sources/localizer/hudson/plugins/timestamper/Messages.java:[20,8] duplicate class: hudson.plugins.timestamper.Messages
<plugin>
<groupId>de.andrena.tools.macker</groupId>
<artifactId>macker-maven-plugin</artifactId>
Expand All @@ -91,15 +96,20 @@
</execution>
</executions>
</plugin>
-->
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.7</version>
<optional>true</optional>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.30</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -137,5 +147,37 @@
<version>2.4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.26</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.58</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.22</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.17</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
34 changes: 17 additions & 17 deletions src/main/java/hudson/plugins/timestamper/TimestamperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
import javax.annotation.CheckForNull;

import org.apache.commons.lang.time.DurationFormatUtils;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.ExtensionList;
import hudson.plugins.timestamper.pipeline.GlobalDecorator;
import jenkins.YesNoMaybe;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

/**
* Global configuration for the Timestamper plug-in, as shown on the Jenkins Configure System page.
Expand All @@ -52,14 +50,7 @@ public final class TimestamperConfig extends GlobalConfiguration {
* @return the Timestamper configuration, or {@code null} if Jenkins has been shut down
*/
public static TimestamperConfig get() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins != null) {
TimestamperConfig config = jenkins.getDescriptorByType(TimestamperConfig.class);
if (config != null) {
return config;
}
}
return null;
return ExtensionList.lookupSingleton(TimestamperConfig.class);
}

/** The default {@link #timestampFormat}. */
Expand All @@ -80,6 +71,11 @@ public static TimestamperConfig get() {
*/
@CheckForNull private String elapsedTimeFormat;

/**
* Whether to activate {@link GlobalDecorator}.
*/
private boolean allPipelines;

/** Constructor. */
public TimestamperConfig() {
load();
Expand All @@ -101,6 +97,7 @@ public String getSystemTimeFormat() {
*/
public void setSystemTimeFormat(@CheckForNull String timestampFormat) {
this.timestampFormat = timestampFormat;
save();
}

/**
Expand All @@ -119,13 +116,16 @@ public String getElapsedTimeFormat() {
*/
public void setElapsedTimeFormat(@CheckForNull String elapsedTimeFormat) {
this.elapsedTimeFormat = elapsedTimeFormat;
save();
}

public boolean isAllPipelines() {
return allPipelines;
}

/** {@inheritDoc} */
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws Descriptor.FormException {
req.bindJSON(this, json);
public void setAllPipelines(boolean allPipelines) {
this.allPipelines = allPipelines;
save();
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* The MIT License
*
* Copyright 2018 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.plugins.timestamper.pipeline;

import hudson.Extension;
import hudson.MarkupText;
import hudson.console.ConsoleAnnotator;
import hudson.console.ConsoleAnnotatorFactory;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.plugins.timestamper.Timestamp;
import hudson.plugins.timestamper.format.TimestampFormat;
import hudson.plugins.timestamper.format.TimestampFormatProvider;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.graph.FlowNode;

/**
* Interprets marks added by {@link GlobalDecorator}.
*/
public final class GlobalAnnotator extends ConsoleAnnotator<Object> {

private static final long serialVersionUID = 1;

private static final Logger LOGGER = Logger.getLogger(GlobalAnnotator.class.getName());

@Override
public ConsoleAnnotator<Object> annotate(Object context, MarkupText text) {
Run<?, ?> build;
if (context instanceof Run) {
build = (Run<?, ?>) context;
} else if (context instanceof FlowNode) {
FlowExecutionOwner owner = ((FlowNode) context).getExecution().getOwner();
if (owner == null) {
return null;
}
Queue.Executable executable;
try {
executable = owner.getExecutable();
} catch (IOException x) {
LOGGER.log(Level.FINE, null, x);
return null;
}
if (executable instanceof Run) {
build = (Run) executable;
} else {
return null;
}
} else {
return null;
}
long buildStartTime = build.getStartTimeInMillis();
String html = text.toString(true);
int start;
if (html.startsWith("<span class=\"pipeline-new-node\" ")) { // cf. LogStorage.startStep
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unpleasant that this is necessary. Not sure if it is solvable upstream. You would probably expect that the MarkupText is what is produced before this span is added by FileLogStorage.

start = html.indexOf('>') + 1;
} else {
start = 0;
}
if (html.startsWith("[", start)) {
int end = html.indexOf(']', start);
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);
TimestampFormat format = TimestampFormatProvider.get();
format.markup(text, timestamp);
text.addMarkup(0, 26, "<span style=\"display: none\">", "</span>");
} catch (DateTimeParseException x) {
// something else, ignore
}
}
}
return this;
}

@Extension
public static final class Factory extends ConsoleAnnotatorFactory<Object> {

@Override
public ConsoleAnnotator<Object> newInstance(Object context) {
if (context instanceof Run && context instanceof FlowExecutionOwner.Executable) {
return new GlobalAnnotator();
} else if (context instanceof FlowNode) {
return new GlobalAnnotator();
}
// Note that prior to 2.145, we actually get FlowNode.class here rather than a FlowNode, so there is no per-step annotation.
return null;
}
}

}
Loading