Skip to content
Permalink
Browse files

JENKINS-23691

Support regex/replace attributes in ${CHANGES} token to modify changes message
  • Loading branch information...
gssiyankai committed Jul 5, 2014
1 parent 77e907d commit bd7adee64ffc608bb2365d66066501696a0f5dde
@@ -52,6 +52,10 @@
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)
@@ -100,6 +104,8 @@ public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String
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) {
@@ -37,6 +37,10 @@
public boolean showDependencies = false;
@Parameter
public String dateFormat;
@Parameter
public String regex;
@Parameter
public String replace;

public ChangesSinceLastBuildContent() {

@@ -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');
@@ -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."))
}
}
@@ -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);
@@ -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);
@@ -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);

0 comments on commit bd7adee

Please sign in to comment.
You can’t perform that action at this time.