Skip to content

Commit

Permalink
[JENKINS-63900] Allow overwriting default x axis label per job. (#38)
Browse files Browse the repository at this point in the history
* Add new field for overwriting x axis label per build

* Add xAxisLabel to projectAction

* Add x axis label for robot step

* Check if build overwrites x axis label

* Add help texts for overwriting x axis
  • Loading branch information
asimell committed Jan 14, 2021
1 parent 35a2605 commit 99b1854
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/main/java/hudson/plugins/robot/AggregatedRobotAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import hudson.plugins.robot.model.RobotResult;
import hudson.util.ChartUtil;
import hudson.util.Graph;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand Down Expand Up @@ -92,8 +93,8 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp)

if (req.checkIfModified(t, rsp))
return;

String labelFormat = RobotConfig.getInstance().getXAxisLabelFormat();
String label = getChildBuildAction(build).getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createTestResultsGraphForTestObject(getResult(),
Boolean.valueOf(req.getParameter("zoomSignificant")),
false, Boolean.valueOf(req.getParameter("hd")),
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/hudson/plugins/robot/RobotBuildAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class RobotBuildAction extends AbstractTestResultAction<RobotBuildAction>
private final boolean enableCache;
private Run<?, ?> build;
private RobotResult result;
private String xAxisLabel;

static {
XSTREAM.alias("result",RobotResult.class);
Expand All @@ -82,14 +83,15 @@ public class RobotBuildAction extends AbstractTestResultAction<RobotBuildAction>
* @param enableCache Whether we want to enable caching or not
*/
public RobotBuildAction(Run<?, ?> build, RobotResult result,
String outputPath, TaskListener listener, String logFileLink, String logHtmlLink, boolean enableCache) {
String outputPath, TaskListener listener, String logFileLink, String logHtmlLink, boolean enableCache, String xAxisLabel) {
super();
super.onAttached(build);
this.build = build;
this.outputPath = outputPath;
this.logFileLink = logFileLink;
this.logHtmlLink = logHtmlLink;
this.enableCache = enableCache;
this.xAxisLabel = xAxisLabel;
setResult(result, listener);
}

Expand Down Expand Up @@ -219,6 +221,10 @@ public Object getTarget(){
return getResult();
}

public String getxAxisLabel() {
return xAxisLabel;
}

/**
* Serves Robot html report via robot url. Shows not found page if file is missing.
* @param req StaplerRequest
Expand Down Expand Up @@ -268,7 +274,7 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp)
if (maxBuildsReq == null || maxBuildsReq.isEmpty())
maxBuildsReq = "0"; // show all builds by default

String labelFormat = RobotConfig.getInstance().getXAxisLabelFormat();
String labelFormat = StringUtils.isBlank(xAxisLabel) ? RobotConfig.getInstance().getXAxisLabelFormat() : xAxisLabel;
Graph g = RobotGraphHelper.createTestResultsGraphForTestObject(getResult(),
Boolean.valueOf(req.getParameter("zoomSignificant")), false,
Boolean.valueOf(req.getParameter("hd")),
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/hudson/plugins/robot/RobotPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class RobotPublisher extends Recorder implements Serializable,
final private double passThreshold;
final private double unstableThreshold;
private String[] otherFiles;
final private String overwriteXAxisLabel;
final private boolean enableCache;

//Default to true
Expand Down Expand Up @@ -95,7 +96,7 @@ public class RobotPublisher extends Recorder implements Serializable,
public RobotPublisher(String outputPath, String outputFileName,
boolean disableArchiveOutput, String reportFileName, String logFileName,
double passThreshold, double unstableThreshold,
boolean onlyCritical, String otherFiles, boolean enableCache) {
boolean onlyCritical, String otherFiles, boolean enableCache, String overwriteXAxisLabel) {
this.outputPath = outputPath;
this.outputFileName = outputFileName;
this.disableArchiveOutput = disableArchiveOutput;
Expand All @@ -105,6 +106,7 @@ public RobotPublisher(String outputPath, String outputFileName,
this.logFileName = logFileName;
this.onlyCritical = onlyCritical;
this.enableCache = enableCache;
this.overwriteXAxisLabel = overwriteXAxisLabel;

if (otherFiles != null) {
String[] filemasks = otherFiles.split(",");
Expand Down Expand Up @@ -203,6 +205,14 @@ public String getOtherFiles() {
return StringUtils.join(otherFiles, ",");
}

/**
* Gets the value of overwriteXAxisLabel
* @return X axis label for the trend
*/
public String getOverwriteXAxisLabel() {
return overwriteXAxisLabel;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -260,7 +270,7 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_assigning());

RobotBuildAction action = new RobotBuildAction(build, result, FILE_ARCHIVE_DIR, listener, expandedReportFileName, expandedLogFileName, enableCache);
RobotBuildAction action = new RobotBuildAction(build, result, FILE_ARCHIVE_DIR, listener, expandedReportFileName, expandedLogFileName, enableCache, overwriteXAxisLabel);
build.addAction(action);

// set RobotProjectAction as project action for Blue Ocean
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/hudson/plugins/robot/RobotStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class RobotStep extends Step {
private @CheckForNull String[] otherFiles;
private boolean enableCache = true;
private boolean onlyCritical = true;
private @CheckForNull String overwriteXAxisLabel;



Expand Down Expand Up @@ -89,6 +90,10 @@ public boolean getEnableCache() {
public boolean getOnlyCritical() {
return this.onlyCritical;
}

public String getOverwriteXAxisLabel() {
return this.overwriteXAxisLabel;
}

@DataBoundSetter
public void setReportFileName(String reportFileName) {
Expand Down Expand Up @@ -142,6 +147,11 @@ public void setOtherFiles(String otherFiles) {
}
}

@DataBoundSetter
public void setOverwriteXAxisLabel(String overwriteXAxisLabel) {
this.overwriteXAxisLabel = overwriteXAxisLabel;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new RobotStepExecution(this, context);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/robot/RobotStepExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RobotStepExecution extends SynchronousNonBlockingStepExecution<Void
@Override protected Void run() throws Exception {
FilePath workspace = getContext().get(FilePath.class);
workspace.mkdirs();
RobotPublisher rp = new RobotPublisher(step.getOutputPath(), step.getOutputFileName(), step.getDisableArchiveOutput(), step.getReportFileName(), step.getLogFileName(), step.getPassThreshold(), step.getUnstableThreshold(), step.getOnlyCritical(), step.getOtherFiles(), step.getEnableCache());
RobotPublisher rp = new RobotPublisher(step.getOutputPath(), step.getOutputFileName(), step.getDisableArchiveOutput(), step.getReportFileName(), step.getLogFileName(), step.getPassThreshold(), step.getUnstableThreshold(), step.getOnlyCritical(), step.getOtherFiles(), step.getEnableCache(), step.getOverwriteXAxisLabel());
rp.perform(getContext().get(Run.class), workspace, getContext().get(Launcher.class), getContext().get(TaskListener.class));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand Down Expand Up @@ -265,7 +266,8 @@ public int getAge(){
public void doGraph(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if(!isNeedToGenerate(req, rsp)) return;
String labelFormat = RobotConfig.getInstance().getXAxisLabelFormat();
String label = getParentAction().getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createTestResultsGraphForTestObject(this, false, true,
Boolean.parseBoolean(req.getParameter("hd")), false, false, labelFormat,
Integer.parseInt(req.getParameter("maxBuildsToShow")));
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/hudson/plugins/robot/model/RobotTestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public String getDurationDiff(RobotTestObject comparable){
public void doGraph(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if(!isNeedToGenerate(req, rsp)) return;
String labelFormat = RobotConfig.getInstance().getXAxisLabelFormat();
String label = parentAction.getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createTestResultsGraphForTestObject(this,
Boolean.parseBoolean(req.getParameter("zoomSignificant")),
false, Boolean.parseBoolean(req.getParameter("hd")),
Expand All @@ -273,7 +274,8 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp)
public void doDurationGraph(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if(!isNeedToGenerate(req, rsp)) return;
String labelFormat = RobotConfig.getInstance().getXAxisLabelFormat();
String label = parentAction.getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createDurationGraphForTestObject(this,
req.hasParameter("hd"),
Integer.parseInt(req.getParameter("maxBuildsToShow")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ limitations under the License.
<f:entry title="${%advanced.enableCache}" description="${%advanced.enableCache.description}" field="enableCache">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%advanced.overwriteXAxisLabel}" description="${%advanced.overwriteXAxisLabel.description}" field="overwriteXAxisLabel">
<f:textbox />
</f:entry>
</f:advanced>
<f:entry title="${%thresholds.label}" help="/plugin/robot/help-thresholds.html">
<table width="100%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ advanced.otherfiles=Other files to copy
advanced.otherfiles.description=Comma separated list of robot related artifacts to be saved
advanced.enableCache=Enable cache
advanced.enableCache.description=Enable cache for test results
advanced.overwriteXAxisLabel=X-axis label
advanced.overwriteXAxisLabel.description=Overwrite default x-axis label for publish trend
thresholds.label=Thresholds for build result
thresholds.onlycritical=Use thresholds for critical tests only
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
Copyright 2008-2014 Nokia Solutions and Networks Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div>
<p>Pattern to format x axis label in trend graphs. You can use $build for build number beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ limitations under the License.
<f:entry title="${%advanced.enableCache}" description="${%advanced.enableCache.description}" field="enableCache">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%advanced.overwriteXAxisLabel}" description="${%advanced.overwriteXAxisLabel.description}" field="overwriteXAxisLabel">
<f:textbox />
</f:entry>
</f:advanced>
<f:entry title="${%thresholds.label}" help="/plugin/robot/help-thresholds.html">
<table width="100%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ advanced.otherfiles=Other files to copy
advanced.otherfiles.description=Comma separated list of robot related artifacts to be saved
advanced.enableCache=Enable cache
advanced.enableCache.description=Enable cache for test results
advanced.overwriteXAxisLabel=X-axis label
advanced.overwriteXAxisLabel.description=Overwrite default x-axis label for publish trend
thresholds.label=Thresholds for build result
thresholds.onlycritical=Use thresholds for critical tests only
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
Copyright 2008-2014 Nokia Solutions and Networks Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div>
<p>Pattern to format x axis label in trend graphs. You can use $build for build number beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testShouldDisplayGraph() throws IOException {
when(build.getProject()).thenReturn(p);
when(build.getRootDir()).thenReturn(tmpDir);
RobotResult result = mock(RobotResult.class);
RobotBuildAction buildAction = new RobotBuildAction(build, result, "", null, null, null, false);
RobotBuildAction buildAction = new RobotBuildAction(build, result, "", null, null, null, false, "");
when(build.getAction(RobotBuildAction.class)).thenReturn(buildAction);
when(p.getLastBuild()).thenReturn(build);

Expand All @@ -76,7 +76,7 @@ public void testShouldGetLastBuildAction() throws IOException{
when(buildWithAction.getProject()).thenReturn(p);
when(buildWithAction.getRootDir()).thenReturn(tmpDir);
RobotResult result = mock(RobotResult.class);
RobotBuildAction buildAction = new RobotBuildAction(buildWithAction, result, "", null, null, null, false);
RobotBuildAction buildAction = new RobotBuildAction(buildWithAction, result, "", null, null, null, false, "");
when(buildWithAction.getAction(RobotBuildAction.class)).thenReturn(buildAction);

when(p.getLastBuild()).thenReturn(lastBuild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class RobotPublisherSystemTest {
public void testRoundTripConfig() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "testRoundTripConfig");
RobotPublisher before = new RobotPublisher("a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
false);
false, "");
p.getPublishersList().add(before);
j.configRoundtrip(p);
RobotPublisher after = p.getPublishersList().get(RobotPublisher.class);
Expand All @@ -70,7 +70,7 @@ public void testRoundTripConfig() throws Exception {
public void testConfigView() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "testConfigView");
RobotPublisher before = new RobotPublisher("a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
false);
false, "");
p.getPublishersList().add(before);
HtmlPage page = j.createWebClient().getPage(p, "configure");
WebAssert.assertTextPresent(page, "Publish Robot Framework");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/robot/RobotPublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setUp() throws Exception {
}

private RobotPublisher getRobotPublisher(double passThreshold, double unstableThreshold) {
return new RobotPublisher("", "", false, "", "", passThreshold, unstableThreshold, onlyCritical, "", false);
return new RobotPublisher("", "", false, "", "", passThreshold, unstableThreshold, onlyCritical, "", false, "");
}

@Test
Expand Down

0 comments on commit 99b1854

Please sign in to comment.