Skip to content

Commit

Permalink
Fix JENKINS-20324 - Add default parameter for CHANGES, CHANGES_SINCE_…
Browse files Browse the repository at this point in the history
…LAST_SUCCESS and CHANGES_SINCE_LAST_UNSTABLE tokens
  • Loading branch information
Jeff MAURY committed Jul 25, 2014
1 parent f8f1af2 commit bfaadca
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 9 deletions.
Expand Up @@ -56,6 +56,8 @@ abstract public class AbstractChangesSinceContent
public String regex;
@Parameter
public String replace;
@Parameter(alias="default")
public String def = ChangesSinceLastBuildContent.DEFAULT_DEFAULT_VALUE;

@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
Expand Down Expand Up @@ -106,6 +108,7 @@ private <P extends AbstractProject<P, B>, B extends AbstractBuild<P, B>> void ap
changes.dateFormat = dateFormat;
changes.regex = regex;
changes.replace = replace;
changes.def = def;

Util.printf(buf, format, new Util.PrintfSpec() {
public boolean printSpec(StringBuffer buf, char formatChar) {
Expand Down
Expand Up @@ -26,6 +26,7 @@ public class ChangesSinceLastBuildContent extends DataBoundTokenMacro {
public static final String FORMAT_DEFAULT_VALUE = "[%a] %m\\n";
public static final String PATH_FORMAT_DEFAULT_VALUE = "\\t%p\\n";
public static final String FORMAT_DEFAULT_VALUE_WITH_PATHS = "[%a] %m%p\\n";
public static final String DEFAULT_DEFAULT_VALUE = "No changes\n";
public static final String MACRO_NAME = "CHANGES";
@Parameter
public boolean showPaths = false;
Expand All @@ -41,6 +42,8 @@ public class ChangesSinceLastBuildContent extends DataBoundTokenMacro {
public String regex;
@Parameter
public String replace;
@Parameter(alias="default")
public String def = DEFAULT_DEFAULT_VALUE;

public ChangesSinceLastBuildContent() {

Expand Down Expand Up @@ -73,21 +76,31 @@ public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String
}

StringBuffer buf = new StringBuffer();
for (ChangeLogSet.Entry entry : build.getChangeSet()) {
Util.printf(buf, format, new ChangesSincePrintfSpec(entry, pathFormat, dateFormatter));
}

if (showDependencies) {
if (!build.getChangeSet().isEmptySet()) {
for (ChangeLogSet.Entry entry : build.getChangeSet()) {
Util.printf(buf, format, new ChangesSincePrintfSpec(entry,
pathFormat, dateFormatter));
}
} else {
buf.append(def);
}
if (showDependencies) {
AbstractBuild previousBuild = ExtendedEmailPublisher.getPreviousBuild(build, listener);
if (previousBuild != null) {
for (Entry<AbstractProject, DependencyChange> e : build.getDependencyChanges(previousBuild).entrySet()) {
buf.append("\n=======================\n");
buf.append("\nChanges in ").append(e.getKey().getName())
.append(":\n");
for (AbstractBuild<?, ?> b : e.getValue().getBuilds()) {
for (ChangeLogSet.Entry entry : b.getChangeSet()) {
Util.printf(buf, format, new ChangesSincePrintfSpec(entry, pathFormat, dateFormatter));
}
if (!b.getChangeSet().isEmptySet()) {
for (ChangeLogSet.Entry entry : b.getChangeSet()) {
Util.printf(buf, format,
new ChangesSincePrintfSpec(entry,
pathFormat, dateFormatter));
}
} else {
buf.append(def);
}
}
}
}
Expand Down
Expand Up @@ -163,6 +163,27 @@ public void testRegexReplace()
assertEquals("[DEFECT-666] Initial commit\n\n", content);
}

@Test
public void testShouldPrintDefaultMessageWhenNoChanges()
throws Exception {
AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);

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

assertEquals(ChangesSinceLastBuildContent.DEFAULT_DEFAULT_VALUE, content);
}

@Test
public void testShouldPrintMessageWhenNoChanges()
throws Exception {
changesSinceLastBuildContent.def = "another default message\n";
AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);

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

assertEquals("another default message\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 @@ -208,6 +229,7 @@ public ChangeLogSet createEmptyChangeLog() {
ChangeLogSet changes = mock(ChangeLogSet.class);
List<ChangeLogSet.Entry> entries = Collections.emptyList();
when(changes.iterator()).thenReturn(entries.iterator());
when(changes.isEmptySet()).thenReturn(true);

return changes;
}
Expand Down
Expand Up @@ -223,6 +223,47 @@ public void testRegexReplace()
+ "\n", contentStr);
}

@Test
public void testShouldPrintDefaultMessageWhenNoChanges()
throws Exception {
AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "[DEFECT-666] Changes for a failed build.");

AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);
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"
+ "[Ash Lux] [DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ ChangesSinceLastBuildContent.DEFAULT_DEFAULT_VALUE
+ "\n", contentStr);
}

@Test
public void testShouldPrintMessageWhenNoChanges()
throws Exception {
content.def = "another default message\n";
AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "[DEFECT-666] Changes for a failed build.");

AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);
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"
+ "[Ash Lux] [DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ "another default message\n"
+ "\n", contentStr);
}

private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) {
AbstractBuild build = mock(AbstractBuild.class);
when(build.getResult()).thenReturn(result);
Expand All @@ -247,6 +288,7 @@ public ChangeLogSet createEmptyChangeLog() {
ChangeLogSet changes = mock(ChangeLogSet.class);
List<ChangeLogSet.Entry> entries = Collections.emptyList();
when(changes.iterator()).thenReturn(entries.iterator());
when(changes.isEmptySet()).thenReturn(true);

return changes;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import hudson.scm.ChangeLogSet;
import hudson.util.StreamTaskListener;
import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -18,8 +19,8 @@
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import static junit.framework.Assert.assertEquals;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -225,6 +226,47 @@ public void testRegexReplace()
+ "\n", contentStr);
}

@Test
public void testShouldPrintDefaultMessageWhenNoChanges()
throws Exception {
AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "[DEFECT-666] Changes for a failed build.");

AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);
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"
+ "[Ash Lux] [DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ ChangesSinceLastBuildContent.DEFAULT_DEFAULT_VALUE
+ "\n", contentStr);
}

@Test
public void testShouldPrintMessageWhenNoChanges()
throws Exception {
content.def = "another default message\n";
AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "[DEFECT-666] Changes for a failed build.");

AbstractBuild currentBuild = createBuildWithNoChanges(Result.SUCCESS, 42);
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"
+ "[Ash Lux] [DEFECT-666] Changes for a failed build.\n"
+ "\n"
+ "\n"
+ "Changes for Build #42\n"
+ "another default message\n"
+ "\n", contentStr);
}

private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) {
AbstractBuild build = mock(AbstractBuild.class);
when(build.getResult()).thenReturn(result);
Expand All @@ -239,6 +281,7 @@ public ChangeLogSet createEmptyChangeLog() {
ChangeLogSet changes = mock(ChangeLogSet.class);
List<ChangeLogSet.Entry> entries = Collections.emptyList();
when(changes.iterator()).thenReturn(entries.iterator());
when(changes.isEmptySet()).thenReturn(true);

return changes;
}
Expand Down

0 comments on commit bfaadca

Please sign in to comment.