Skip to content
Permalink
Browse files
Fix JENKINS-20692
Added %a and %d to the pathFormat formatting options.

- %a will display the getName of the EditType
- %d will display the getDescription of the EditType

If getAffectedFiles is not implemented by the SCM, then "Unknown" will be put displayed for each %a anf %d
  • Loading branch information
slide committed Jan 29, 2014
1 parent 2f2f9cb commit 6d57206bb4b541a6504d77b08ad7fcabcfe59790
@@ -8,6 +8,7 @@
import hudson.plugins.emailext.Util;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.AffectedFile;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
@@ -131,21 +132,49 @@ public boolean printSpec(StringBuffer buf, char formatChar) {
return true;
}
case 'p': {
Collection<String> affectedPaths = entry.getAffectedPaths();
for (final String affectedPath : affectedPaths) {
Util.printf(buf, pathFormatString, new Util.PrintfSpec() {
public boolean printSpec(StringBuffer buf, char formatChar) {
if (formatChar == 'p') {
buf.append(affectedPath);
return true;
} else {
return false;
try {
Collection<? extends AffectedFile> affectedFiles = entry.getAffectedFiles();
for (final AffectedFile file : affectedFiles) {
Util.printf(buf, pathFormatString, new Util.PrintfSpec() {
public boolean printSpec(StringBuffer buf, char formatChar) {
if (formatChar == 'p') {
buf.append(file.getPath());
return true;
} else if(formatChar == 'a') {
buf.append(file.getEditType().getName());
return true;
} else if(formatChar == 'd') {
buf.append(file.getEditType().getDescription());
return true;
} else {
return false;
}
}
});
}
} catch(UnsupportedOperationException e) {
Collection<String> affectedPaths = entry.getAffectedPaths();
for (final String affectedPath : affectedPaths) {
Util.printf(buf, pathFormatString, new Util.PrintfSpec() {
public boolean printSpec(StringBuffer buf, char formatChar) {
if (formatChar == 'p') {
buf.append(affectedPath);
return true;
} else if(formatChar == 'a') {
buf.append("Unknown");
return true;
} else if(formatChar == 'd') {
buf.append("Unknown");
return true;
} else {
return false;
}
}
}
});
});
}
}
return true;
}
}
case 'r': {
try {
buf.append(entry.getCommitId());
@@ -5,6 +5,8 @@
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.EditType;
import hudson.util.StreamTaskListener;
import org.junit.Before;
import org.junit.Test;
@@ -118,6 +120,32 @@ public void testDateFormatString()

assertEquals("Oct 21, 2013 19:39:00", content);
}

@Test
public void testTypeFormatStringWithNoGetAffectedFiles()
throws Exception {
changesSinceLastBuildContent.showPaths = true;
changesSinceLastBuildContent.pathFormat = "\t%p\t%a\n";

AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build.");

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

assertEquals("[Ash Lux] Changes for a successful build.\n" + "\tPATH1\tUnknown\n" + "\tPATH2\tUnknown\n" + "\tPATH3\tUnknown\n" + "\n", content);
}

@Test
public void testTypeFormatStringWithAffectedFiles()
throws Exception {
changesSinceLastBuildContent.showPaths = true;
changesSinceLastBuildContent.pathFormat = "\t%p\t%a - %d\n";

AbstractBuild currentBuild = createBuildWithAffectedFiles(Result.SUCCESS, 42, "Changes for a successful build.");

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

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);
}

private AbstractBuild createBuild(Result result, int buildNumber, String message) {
AbstractBuild build = mock(AbstractBuild.class);
@@ -128,6 +156,16 @@ private AbstractBuild createBuild(Result result, int buildNumber, String message

return build;
}

private AbstractBuild createBuildWithAffectedFiles(Result result, int buildNumber, String message) {
AbstractBuild build = mock(AbstractBuild.class);
when(build.getResult()).thenReturn(result);
ChangeLogSet changes1 = createChangeLogWithAffectedFiles(message);
when(build.getChangeSet()).thenReturn(changes1);
when(build.getNumber()).thenReturn(buildNumber);

return build;
}

public ChangeLogSet createChangeLog(String message) {
ChangeLogSet changes = mock(ChangeLogSet.class);
@@ -139,7 +177,18 @@ public ChangeLogSet createChangeLog(String message) {

return changes;
}

public ChangeLogSet createChangeLogWithAffectedFiles(String message) {
ChangeLogSet changes = mock(ChangeLogSet.class);

List<ChangeLogSet.Entry> entries = new LinkedList<ChangeLogSet.Entry>();
ChangeLogSet.Entry entry = new ChangeLogEntryWithAffectedFiles(message, "Ash Lux");
entries.add(entry);
when(changes.iterator()).thenReturn(entries.iterator());

return changes;
}

public static class ChangeLogEntry
extends ChangeLogSet.Entry {

@@ -173,7 +222,7 @@ public Collection<String> getAffectedPaths() {
}
};
}

@Override
public String getCommitId() {
return "REVISION";
@@ -185,4 +234,42 @@ public long getTimestamp() {
return 1382409540000L;
}
}

public static class TestAffectedFile implements AffectedFile {

private final String path;
private final EditType type;

public TestAffectedFile(String path, EditType type) {
this.path = path;
this.type = type;
}

public String getPath() {
return path;
}

public EditType getEditType() {
return type;
}
}

public static class ChangeLogEntryWithAffectedFiles
extends ChangeLogEntry {

public ChangeLogEntryWithAffectedFiles(String message, String author) {
super(message, author);
}

@Override
public Collection<AffectedFile> getAffectedFiles() {
return new ArrayList<AffectedFile>() {
{
add(new TestAffectedFile("PATH1", EditType.ADD));
add(new TestAffectedFile("PATH2", EditType.DELETE));
add(new TestAffectedFile("PATH3", EditType.EDIT));
}
};
}
}
}

0 comments on commit 6d57206

Please sign in to comment.