Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #93 from gssiyankai/master
    JENKINS-23691  Support regex/replace attribute in ${CHANGES} token to modify changes message
  • Loading branch information
slide committed Jul 6, 2014
2 parents 684ab89 + bd7adee commit 2d37c7a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
Expand Up @@ -52,6 +52,10 @@ abstract public class AbstractChangesSinceContent
public boolean showDependencies = false;
@Parameter
public String dateFormat;
@Parameter
public String regex;
@Parameter
public String replace;

@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
Expand Down Expand Up @@ -100,6 +104,8 @@ private <P extends AbstractProject<P, B>, B extends AbstractBuild<P, B>> void ap
final ChangesSinceLastBuildContent changes = new ChangesSinceLastBuildContent(changesFormat, pathFormat, showPaths);
changes.showDependencies = showDependencies;
changes.dateFormat = dateFormat;
changes.regex = regex;
changes.replace = replace;

Util.printf(buf, format, new Util.PrintfSpec() {
public boolean printSpec(StringBuffer buf, char formatChar) {
Expand Down
Expand Up @@ -37,6 +37,10 @@ public class ChangesSinceLastBuildContent extends DataBoundTokenMacro {
public boolean showDependencies = false;
@Parameter
public String dateFormat;
@Parameter
public String regex;
@Parameter
public String replace;

public ChangesSinceLastBuildContent() {

Expand Down Expand Up @@ -125,6 +129,9 @@ public boolean printSpec(StringBuffer buf, char formatChar) {
}
case 'm': {
String m = entry.getMsg();
if(!StringUtils.isEmpty(regex) && !StringUtils.isEmpty(replace)) {
m = m.replaceAll(regex, replace);
}
buf.append(m);
if (m == null || !m.endsWith("\n")) {
buf.append('\n');
Expand Down
Expand Up @@ -6,7 +6,7 @@ dd() {
dd(_("If true, changes to projects this build depends on are shown. Defaults to false"))

dt("showPaths")
dd(_("If true, the paths, modifued by a commit are shown. Defaults to false"))
dd(_("If true, the paths, modified by a commit are shown. Defaults to false"))

dt("format")
dd() {
Expand Down Expand Up @@ -34,5 +34,9 @@ dd() {
dt("pathFormat")
dd(_("A string containing %p to indicate how to print paths. Defaults to "
+"\"\\\\t%p\\\\n\""))
dt("regex")
dd(_("A regular expression."))
dt("replace")
dd(_("A replacement for all substrings of the change message that match the given regular expression."))
}
}
Expand Up @@ -149,6 +149,20 @@ public void testTypeFormatStringWithAffectedFiles()
assertEquals("[Ash Lux] Changes for a successful build.\n" + "\tPATH1\tadd - The file was added\n" + "\tPATH2\tdelete - The file was removed\n" + "\tPATH3\tedit - The file was modified\n" + "\n", content);
}

@Test
public void testRegexReplace()
throws Exception {
changesSinceLastBuildContent.regex = "<defectId>(DEFECT-[0-9]+)</defectId><message>(.*)</message>";
changesSinceLastBuildContent.replace = "[$1] $2";
changesSinceLastBuildContent.format = "%m\\n";

AbstractBuild currentBuild = createBuildWithAffectedFiles(Result.SUCCESS, 42, "<defectId>DEFECT-666</defectId><message>Initial commit</message>");

String content = changesSinceLastBuildContent.evaluate(currentBuild, listener, ChangesSinceLastBuildContent.MACRO_NAME);

assertEquals("[DEFECT-666] Initial commit\n\n", content);
}

private AbstractBuild createBuild(Result result, int buildNumber, String message) {
AbstractBuild build = mock(AbstractBuild.class);
when(build.getResult()).thenReturn(result);
Expand Down
Expand Up @@ -197,6 +197,31 @@ public void testWhenShowPathsIsTrueShouldPrintPath()
+ "\tPATH2\n" + "\tPATH3\n" + "\n" + "\n" + "Changes for Build #42\n"
+ "[Ash Lux] Changes for a successful build.\n" + "\tPATH1\n" + "\tPATH2\n" + "\tPATH3\n" + "\n" + "\n", contentStr);
}

@Test
public void testRegexReplace()
throws Exception {
content.regex = "<defectId>(DEFECT-[0-9]+)</defectId><message>(.*)</message>";
content.replace = "[$1] $2";
content.changesFormat = "%m\\n";

AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "<defectId>DEFECT-666</defectId><message>Changes for a failed build.</message>");

AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "<defectId>DEFECT-666</defectId><message>Changes for a successful build.</message>");
when(currentBuild.getPreviousBuild()).thenReturn(failureBuild);
when(failureBuild.getNextBuild()).thenReturn(currentBuild);

String contentStr = content.evaluate(currentBuild, listener, ChangesSinceLastSuccessfulBuildContent.MACRO_NAME);

Assert.assertEquals("Changes for Build #41\n"
+ "[DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ "[DEFECT-666] Changes for a successful build.\n"
+ "\n"
+ "\n", contentStr);
}

private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) {
AbstractBuild build = mock(AbstractBuild.class);
Expand Down
Expand Up @@ -199,6 +199,31 @@ public void testWhenShowPathsIsTrueShouldPrintPath()
+ "\tPATH2\n" + "\tPATH3\n" + "\n" + "\n" + "Changes for Build #42\n"
+ "[Ash Lux] Changes for a successful build.\n" + "\tPATH1\n" + "\tPATH2\n" + "\tPATH3\n" + "\n" + "\n", contentStr);
}

@Test
public void testRegexReplace()
throws Exception {
content.regex = "<defectId>(DEFECT-[0-9]+)</defectId><message>(.*)</message>";
content.replace = "[$1] $2";
content.changesFormat = "%m\\n";

AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "<defectId>DEFECT-666</defectId><message>Changes for a failed build.</message>");

AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "<defectId>DEFECT-666</defectId><message>Changes for a successful build.</message>");
when(currentBuild.getPreviousBuild()).thenReturn(failureBuild);
when(failureBuild.getNextBuild()).thenReturn(currentBuild);

String contentStr = content.evaluate(currentBuild, listener, ChangesSinceLastUnstableBuildContent.MACRO_NAME);

assertEquals("Changes for Build #41\n"
+ "[DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ "[DEFECT-666] Changes for a successful build.\n"
+ "\n"
+ "\n", contentStr);
}

private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) {
AbstractBuild build = mock(AbstractBuild.class);
Expand Down

0 comments on commit 2d37c7a

Please sign in to comment.