Skip to content

Commit

Permalink
JENKINS-53868 Ability to show date and time as absolute values in pip…
Browse files Browse the repository at this point in the history
…eline views

* Added as a configurable option
* Refactored: Moved common functionality to core package
  • Loading branch information
tommysdk committed Nov 22, 2018
1 parent e9b9959 commit 481bd35
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 39 deletions.
81 changes: 81 additions & 0 deletions src/main/java/se/diabol/jenkins/core/PipelineView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
This file is part of Delivery Pipeline Plugin.
Delivery Pipeline Plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Delivery Pipeline Plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Delivery Pipeline Plugin.
If not, see <http://www.gnu.org/licenses/>.
*/
package se.diabol.jenkins.core;

import org.acegisecurity.AuthenticationException;
import se.diabol.jenkins.pipeline.trigger.TriggerException;

import java.util.List;

/**
* Interface defining common methods for delivery pipeline views.
* Note that Jenkins does <b>NOT</b> support the following:
* <ul>
* <li>Java 8 interface default methods</li>
* <li><tt>@Exported</tt> annotations on interface methods - these needs to be set on the method in the
* implementing class</li>
* </ul>
*/
public interface PipelineView {

void triggerManual(String projectName, String upstreamName, String buildId)
throws TriggerException, AuthenticationException;

void triggerRebuild(String projectName, String buildId);

void abortBuild(String projectName, String buildId) throws TriggerException, AuthenticationException;

/**
* Whether to allow a new pipeline to be started from the pipeline view.
* Expected to be annotated with <tt>@Exported</tt> for consumption from the view page.
*
* @return whether to allow a new pipeline to be started from the pipeline view.
*/
boolean isAllowPipelineStart();

/**
* Whether to allow running pipelines to be aborted from the pipeline view.
* Expected to be annotated with <tt>@Exported</tt> for consumption from the view page.
*
* @return whether to allow pipelines to be aborted from the pipeline view.
*/
boolean isAllowAbort();

/**
* A date time string representing when the pipeline view was last updated.
* Expected to be annotated with <tt>@Exported</tt> for consumption from the view page.
*
* @return a string representation on when the pipeline view was last updated.
*/
String getLastUpdated();

/**
* Exposes any error that is currently associated with the pipeline view.
*
* @return the error currently associated with this pipeline view.
*/
String getError();

/**
* Resolve the pipelines associated with this view.
* Expected to be annotated with <tt>@Exported</tt> for consumption from the view page.
*
* @return a list of components associated with this pipeline view.
*/
List<? extends GenericComponent> getPipelines();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
along with Delivery Pipeline Plugin.
If not, see <http://www.gnu.org/licenses/>.
*/
package se.diabol.jenkins.pipeline;
package se.diabol.jenkins.core;

import org.acegisecurity.AuthenticationException;
import se.diabol.jenkins.pipeline.trigger.TriggerException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public interface PipelineView {
public final class TimestampFormat {

void triggerManual(String projectName, String upstreamName, String buildId)
throws TriggerException, AuthenticationException;
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

void triggerRebuild(String projectName, String buildId);
private TimestampFormat() {
}

void abortBuild(String projectName, String buildId) throws TriggerException, AuthenticationException;
public static String formatTimestamp(long timestamp) {
return DATE_TIME_FORMAT.format(new Date(timestamp));
}
}
20 changes: 18 additions & 2 deletions src/main/java/se/diabol/jenkins/pipeline/DeliveryPipelineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.export.Exported;
import se.diabol.jenkins.core.PipelineView;
import se.diabol.jenkins.core.TimestampFormat;
import se.diabol.jenkins.pipeline.domain.Component;
import se.diabol.jenkins.pipeline.domain.Pipeline;
import se.diabol.jenkins.pipeline.domain.PipelineException;
Expand All @@ -58,7 +60,6 @@
import se.diabol.jenkins.pipeline.trigger.TriggerException;
import se.diabol.jenkins.pipeline.util.FullScreen;
import se.diabol.jenkins.pipeline.util.JenkinsUtil;
import se.diabol.jenkins.pipeline.util.PipelineUtils;
import se.diabol.jenkins.pipeline.util.ProjectUtil;

import java.io.IOException;
Expand Down Expand Up @@ -110,6 +111,7 @@ public class DeliveryPipelineView extends View implements PipelineView {
private boolean showStaticAnalysisResults = false;
private boolean linkRelative = false;
private boolean pagingEnabled = false;
private boolean showAbsoluteDateTime = false;
private boolean showAggregatedChanges = false;
private String aggregatedChangesGroupingPattern = null;
private String theme = DEFAULT_THEME;
Expand Down Expand Up @@ -204,6 +206,7 @@ public void setShowAggregatedPipeline(boolean showAggregatedPipeline) {
}

@Exported
@Override
public boolean isAllowPipelineStart() {
return allowPipelineStart;
}
Expand All @@ -213,6 +216,7 @@ public void setAllowPipelineStart(boolean allowPipelineStart) {
}

@Exported
@Override
public boolean isAllowAbort() {
return allowAbort;
}
Expand Down Expand Up @@ -328,11 +332,13 @@ public Api getApi() {
}

@Exported
@Override
public String getLastUpdated() {
return PipelineUtils.formatTimestamp(System.currentTimeMillis());
return TimestampFormat.formatTimestamp(System.currentTimeMillis());
}

@Exported
@Override
public String getError() {
return error;
}
Expand Down Expand Up @@ -395,6 +401,15 @@ public void setPagingEnabled(boolean pagingEnabled) {
this.pagingEnabled = pagingEnabled;
}

@Exported
public boolean isShowAbsoluteDateTime() {
return showAbsoluteDateTime;
}

public void setShowAbsoluteDateTime(boolean showAbsoluteDateTime) {
this.showAbsoluteDateTime = showAbsoluteDateTime;
}

@Exported
public boolean isShowAggregatedChanges() {
return showAggregatedChanges;
Expand Down Expand Up @@ -521,6 +536,7 @@ protected static String withoutFolderPrefix(final String projectName) {
}

@Exported
@Override
public List<Component> getPipelines() {
try {
LOG.fine("Getting pipelines");
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/se/diabol/jenkins/pipeline/PipelineApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import se.diabol.jenkins.core.PipelineView;
import se.diabol.jenkins.pipeline.trigger.TriggerException;

import java.io.IOException;
import javax.servlet.ServletException;

public class PipelineApi extends Api {

private final PipelineView view;
Expand Down Expand Up @@ -83,7 +81,7 @@ public void doInputStep(StaplerRequest request,
StaplerResponse response,
@QueryParameter String project,
@QueryParameter String upstream,
@QueryParameter String buildId) throws IOException, ServletException {
@QueryParameter String buildId) {
doManualStep(request, response, project, upstream, buildId);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/se/diabol/jenkins/pipeline/domain/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import hudson.model.Result;
import org.kohsuke.stapler.export.Exported;
import se.diabol.jenkins.core.GenericPipeline;
import se.diabol.jenkins.core.TimestampFormat;
import se.diabol.jenkins.pipeline.domain.task.Task;
import se.diabol.jenkins.pipeline.sort.BuildStartTimeComparator;
import se.diabol.jenkins.pipeline.util.PipelineUtils;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -298,7 +298,7 @@ public List<Pipeline> createPipelineLatest(int noOfPipelines,
Component component) throws PipelineException {
List<Pipeline> result = new ArrayList<>();
if (firstProject.isInQueue()) {
String pipeLineTimestamp = PipelineUtils.formatTimestamp(firstProject.getQueueItem().getInQueueSince());
String pipeLineTimestamp = TimestampFormat.formatTimestamp(firstProject.getQueueItem().getInQueueSince());
List<Stage> pipelineStages = new ArrayList<>();
for (Stage stage : getStages()) {
pipelineStages.add(stage.createLatestStage(context, null));
Expand Down Expand Up @@ -341,7 +341,7 @@ protected List<Pipeline> getPipelines(Iterator it, ItemGroup context, int startI
List<Change> pipelineChanges = Change.getChanges(firstBuild);
Set<UserInfo> contributors = showChanges ? UserInfo.getContributors(pipelineChanges) : null;

String pipeLineTimestamp = PipelineUtils.formatTimestamp(firstBuild.getTimeInMillis());
String pipeLineTimestamp = TimestampFormat.formatTimestamp(firstBuild.getTimeInMillis());
List<Stage> pipelineStages = new ArrayList<>();
Pipeline pipeline = this;
if (showUpstream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.core.TimestampFormat;
import se.diabol.jenkins.pipeline.domain.status.promotion.AbstractPromotionStatusProvider;
import se.diabol.jenkins.pipeline.domain.status.promotion.PromotionStatus;
import se.diabol.jenkins.pipeline.util.PipelineUtils;
import se.diabol.jenkins.pipeline.util.ProjectUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -91,7 +91,7 @@ public long getLastActivity() {
@Exported
public String getTimestamp() {
if (lastActivity != -1) {
return PipelineUtils.formatTimestamp(lastActivity);
return TimestampFormat.formatTimestamp(lastActivity);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
*/
package se.diabol.jenkins.pipeline.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public final class PipelineUtils {
Expand All @@ -29,11 +26,6 @@ public final class PipelineUtils {
private PipelineUtils() {
}

public static String formatTimestamp(long timestamp) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
return format.format(new Date(timestamp));
}

public static long getRandom() {
return RANDOM.nextLong();
}
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/se/diabol/jenkins/workflow/WorkflowPipelineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import se.diabol.jenkins.core.PipelineView;
import se.diabol.jenkins.core.TimestampFormat;
import se.diabol.jenkins.pipeline.PipelineApi;
import se.diabol.jenkins.pipeline.PipelineView;
import se.diabol.jenkins.pipeline.domain.Change;
import se.diabol.jenkins.pipeline.domain.PipelineException;
import se.diabol.jenkins.pipeline.sort.ComponentComparatorDescriptor;
import se.diabol.jenkins.pipeline.sort.GenericComponentComparator;
import se.diabol.jenkins.pipeline.trigger.TriggerException;
import se.diabol.jenkins.pipeline.util.JenkinsUtil;
import se.diabol.jenkins.pipeline.util.PipelineUtils;
import se.diabol.jenkins.pipeline.util.ProjectUtil;
import se.diabol.jenkins.workflow.model.Component;
import se.diabol.jenkins.workflow.model.Pipeline;
Expand Down Expand Up @@ -89,6 +89,7 @@ public class WorkflowPipelineView extends View implements PipelineView {
private boolean allowPipelineStart = false;
private boolean allowAbort = false;
private boolean showChanges = false;
private boolean showAbsoluteDateTime = false;
private String theme = DEFAULT_THEME;
private int maxNumberOfVisiblePipelines = -1;
@Deprecated
Expand Down Expand Up @@ -147,6 +148,7 @@ public void setSorting(String sorting) {
}

@Exported
@Override
public boolean isAllowPipelineStart() {
return allowPipelineStart;
}
Expand All @@ -156,6 +158,7 @@ public void setAllowPipelineStart(boolean allowPipelineStart) {
}

@Exported
@Override
public boolean isAllowAbort() {
return allowAbort;
}
Expand All @@ -172,6 +175,15 @@ public void setShowChanges(boolean showChanges) {
this.showChanges = showChanges;
}

@Exported
public boolean isShowAbsoluteDateTime() {
return showAbsoluteDateTime;
}

public void setShowAbsoluteDateTime(boolean showAbsoluteDateTime) {
this.showAbsoluteDateTime = showAbsoluteDateTime;
}

public String getTheme() {
return this.theme == null ? DEFAULT_THEME : this.theme;
}
Expand Down Expand Up @@ -208,8 +220,9 @@ public void setComponentSpecs(List<ComponentSpec> componentSpecs) {
}

@Exported
@Override
public String getLastUpdated() {
return PipelineUtils.formatTimestamp(System.currentTimeMillis());
return TimestampFormat.formatTimestamp(System.currentTimeMillis());
}

@Exported
Expand All @@ -221,8 +234,8 @@ public void setLinkToConsoleLog(boolean linkToConsoleLog) {
this.linkToConsoleLog = linkToConsoleLog;
}

@Override
@Exported
@Override
public String getDescription() {
if (super.description == null) {
setDescription(this.description);
Expand All @@ -236,11 +249,13 @@ public void setDescription(String description) {
}

@Exported
@Override
public String getError() {
return error;
}

@Exported
@Override
public List<Component> getPipelines() {
try {
LOG.fine("Getting pipelines");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/se/diabol/jenkins/workflow/model/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.kohsuke.stapler.export.Exported;
import se.diabol.jenkins.core.GenericPipeline;
import se.diabol.jenkins.core.TimestampFormat;
import se.diabol.jenkins.pipeline.domain.Change;
import se.diabol.jenkins.pipeline.domain.PipelineException;
import se.diabol.jenkins.pipeline.domain.TriggerCause;
import se.diabol.jenkins.pipeline.domain.UserInfo;
import se.diabol.jenkins.pipeline.util.PipelineUtils;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,7 +127,7 @@ public long getLastActivity() {
}

public static Pipeline resolve(WorkflowJob project, WorkflowRun build) throws PipelineException {
String pipelineTimestamp = PipelineUtils.formatTimestamp(build.getTimeInMillis());
String pipelineTimestamp = TimestampFormat.formatTimestamp(build.getTimeInMillis());

List<FlowNode> stageNodes = FlowNodeUtil.getStageNodes(build.getExecution());
return new Pipeline(project.getName(),
Expand Down
Loading

0 comments on commit 481bd35

Please sign in to comment.