Skip to content

Commit

Permalink
[JENKINS-30167] Added an option for driving a concatenation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanouse committed Aug 28, 2015
1 parent 2efd758 commit 1910cb6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ work/
.project
.classpath
.settings
.idea/
20 changes: 12 additions & 8 deletions pom.xml
Expand Up @@ -13,14 +13,18 @@
<url>http://wiki.jenkins-ci.org/display/JENKINS/Description+Setter+Plugin</url>

<developers>
<developer>
<id>michael1010</id>
<name>Michael</name>
</developer>
<developer>
<id>huybrechts</id>
<name>Tom Huybrechts</name>
</developer>
<developer>
<id>michael1010</id>
<name>Michael</name>
</developer>
<developer>
<id>huybrechts</id>
<name>Tom Huybrechts</name>
</developer>
<developer>
<id>pjanouse</id>
<name>Pavel Janousek</name>
</developer>
</developers>

<scm>
Expand Down
Expand Up @@ -23,19 +23,21 @@ public class DescriptionSetterBuilder extends Builder {

private final String regexp;
private final String description;
private final boolean appendMode;

@DataBoundConstructor
public DescriptionSetterBuilder(String regexp, String description) {
public DescriptionSetterBuilder(String regexp, String description, boolean appendMode) {
this.regexp = regexp;
this.description = Util.fixEmptyAndTrim(description);
this.appendMode = appendMode;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException {

return DescriptionSetterHelper.setDescription(build, listener, regexp,
description);
description, appendMode);
}

@Extension
Expand Down
@@ -1,6 +1,5 @@
package hudson.plugins.descriptionsetter;

import hudson.EnvVars;
import hudson.model.BuildListener;
import hudson.model.ParameterValue;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -30,7 +29,7 @@ public class DescriptionSetterHelper {
/**
* Sets the description on the given build based on the specified regular
* expression and description.
*
*
* @param build the build whose description to set.
* @param listener the build listener to report events to.
* @param regexp the regular expression to apply to the build log.
Expand All @@ -42,7 +41,25 @@ public class DescriptionSetterHelper {
public static boolean setDescription(AbstractBuild<?, ?> build,
BuildListener listener, String regexp, String description)
throws InterruptedException {
return setDescription(build, listener, regexp, description, true);
}

/**
* Sets the description on the given build based on the specified regular
* expression and description.
*
* @param build the build whose description to set.
* @param listener the build listener to report events to.
* @param regexp the regular expression to apply to the build log.
* @param description the description to set.
* @param appendMode if true, description is added to the current one
* @return true, regardless of if the regular expression matched and a
* description could be set or not.
* @throws InterruptedException if the build is interrupted by the user.
*/
public static boolean setDescription(AbstractBuild<?, ?> build,
BuildListener listener, String regexp, String description, boolean appendMode)
throws InterruptedException {
try {
Matcher matcher;
String result = null;
Expand All @@ -67,15 +84,9 @@ public static boolean setDescription(AbstractBuild<?, ?> build,

build.addAction(new DescriptionSetterAction(result));
if(build.getDescription() == null)
{
build.setDescription(result);
}
else
{
String oldDescr = build.getDescription();
String newDescr = oldDescr + "<br />" + result;
build.setDescription(newDescr);
}
build.setDescription((appendMode ? build.getDescription() + "<br />" : "") + result);

setEnvironmentVariable(result, build);

Expand Down
Expand Up @@ -39,6 +39,7 @@ public class DescriptionSetterPublisher extends Recorder implements

private final String descriptionForFailed;
private final boolean setForMatrix;
private final boolean appendMode;

@Deprecated
private transient boolean setForFailed = false;
Expand All @@ -49,12 +50,13 @@ public class DescriptionSetterPublisher extends Recorder implements
@DataBoundConstructor
public DescriptionSetterPublisher(String regexp, String regexpForFailed,
String description, String descriptionForFailed,
boolean setForMatrix) {
boolean setForMatrix, boolean appendMode) {
this.regexp = regexp;
this.regexpForFailed = regexpForFailed;
this.description = Util.fixEmptyAndTrim(description);
this.descriptionForFailed = Util.fixEmptyAndTrim(descriptionForFailed);
this.setForMatrix = setForMatrix;
this.appendMode = appendMode;
}

public BuildStepMonitor getRequiredMonitorService() {
Expand All @@ -69,13 +71,14 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
&& build.getResult().isWorseThan(Result.UNSTABLE);
return DescriptionSetterHelper.setDescription(build, listener,
useUnstable ? regexpForFailed : regexp,
useUnstable ? descriptionForFailed : description);
useUnstable ? descriptionForFailed : description,
appendMode);
}

private Object readResolve() throws ObjectStreamException {
if (explicitNotRegexp) {
return new DescriptionSetterPublisher(null, null, regexp,
setForFailed ? regexpForFailed : null, false);
setForFailed ? regexpForFailed : null, false, false);
} else {
return this;
}
Expand Down Expand Up @@ -152,6 +155,7 @@ public MatrixAggregator createAggregator(final MatrixBuild build,
@Override
public boolean endRun(MatrixRun run) throws InterruptedException,
IOException {

if (build.getDescription() == null
&& run.getDescription() != null) {
build.setDescription(run.getDescription());
Expand All @@ -171,4 +175,6 @@ public boolean isSetForMatrix() {
return setForMatrix;
}

public boolean isAppendMode() { return appendMode; }

}
Expand Up @@ -6,4 +6,7 @@
<f:entry title="${%Description}" field="description">
<f:textbox />
</f:entry>
<f:entry title="${%Append to description instead of resetting it}" field="appendMode">
<f:checkbox />
</f:entry>
</j:jelly>
Expand Up @@ -18,6 +18,9 @@
<f:checkbox />
</f:entry>
</j:if>
<f:entry title="${%Append to description instead of resetting it}" field="appendMode">
<f:checkbox />
</f:entry>
</f:advanced>

</j:jelly>
Expand Up @@ -67,14 +67,34 @@ public void testNullMatch2() throws Exception {
"^Prefix: (\\S+)( .*)?$", "Match=(\\1)\\2"));
}

public void testAppendDescriptionInBuilder() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new DescriptionSetterBuilder("", "test1", false));
project.getBuildersList().add(new DescriptionSetterBuilder("", "test2", true));
FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals("test1<br />test2", build.getDescription());
}

public void testRewriteDescriptionInBuilder() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new DescriptionSetterBuilder("", "test1", false));
project.getBuildersList().add(new DescriptionSetterBuilder("", "test2", false));
FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals("test2", build.getDescription());
}

private String getDescription(String text, Result result, String regexp,
String description) throws Exception {
String description, boolean appendMode) throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new TestBuilder(text, result));
project.getBuildersList().add(
new DescriptionSetterBuilder(regexp, description));
new DescriptionSetterBuilder(regexp, description, appendMode));
FreeStyleBuild build = project.scheduleBuild2(0).get();
return build.getDescription();
}

private String getDescription(String text, Result result, String regexp,
String description) throws Exception {
return getDescription(text, result, regexp, description, true);
}
}
Expand Up @@ -77,16 +77,39 @@ public void testNullMatch2() throws Exception {
"Match=(\\1)\\2", null));
}

public void testAppendDescriptionInPublisher() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new DescriptionSetterBuilder("", "test1", false));
project.getPublishersList().add(
new DescriptionSetterPublisher("", "", "test2", "", false, true));
FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals("test1<br />test2", build.getDescription());
}

public void testRewriteDescriptionInPublisher() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new DescriptionSetterBuilder("", "test1", false));
project.getPublishersList().add(
new DescriptionSetterPublisher("", "", "test2", "", false, false));
FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals("test2", build.getDescription());
}

private String getDescription(String text, Result result, String regexp,
String regexpForFailed, String description,
String descriptionForFailed) throws Exception {
String descriptionForFailed, boolean appendMode) throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new TestBuilder(text, result));
project.getPublishersList().add(
new DescriptionSetterPublisher(regexp, regexpForFailed,
description, descriptionForFailed, false));
description, descriptionForFailed, false, appendMode));
FreeStyleBuild build = project.scheduleBuild2(0).get();
return build.getDescription();
}

private String getDescription(String text, Result result, String regexp,
String regexpForFailed, String description,
String descriptionForFailed) throws Exception {
return getDescription(text, result, regexp, regexpForFailed, description, descriptionForFailed, true);
}
}

0 comments on commit 1910cb6

Please sign in to comment.