Skip to content

Commit

Permalink
java.lang.NumberFormatException: When using parametrized scheduler (#510
Browse files Browse the repository at this point in the history
)

* ActiveNotifier MessageBuilder: handle { in messages outside links (#509)

* ActiveNotifier MessageBuilder: add test coverage
  • Loading branch information
jsoref authored and timja committed Feb 7, 2019
1 parent 15aea13 commit 60e8a8a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolea

public static class MessageBuilder {

private static final Pattern aTag = Pattern.compile("(?i)<a([^>]+)>(.+?)</a>|(\\{)");
private static final Pattern aTag = Pattern.compile("(?i)<a([^>]+)>(.+?)</a>|([{%])");
private static final Pattern href = Pattern.compile("\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))");
private static final String BACK_TO_NORMAL_STATUS_MESSAGE = "Back to normal",
STILL_FAILING_STATUS_MESSAGE = "Still Failing",
Expand Down Expand Up @@ -527,12 +527,16 @@ private String[] extractReplaceLinks(Matcher aTag, StringBuffer sb) {
String escapeThis = aTag.group(3);
if (escapeThis != null) {
aTag.appendReplacement(sb, String.format("{%s}", size++));
links.add("{");
links.add(escapeThis);
} else {
aTag.appendReplacement(sb, String.format("{%s}", size++));
links.add(String.format("<%s|%s>", url.group(1).replaceAll("\"", ""), aTag.group(2)));
}
}
} else {
String escapeThis = aTag.group(3);
aTag.appendReplacement(sb, String.format("{%s}", size++));
links.add(escapeThis);
}
}
aTag.appendTail(sb);
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/jenkins/plugins/slack/MessageBuilderEscapeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package jenkins.plugins.slack;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.ItemGroup;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;

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

public class MessageBuilderEscapeTest {

private static ActiveNotifier.MessageBuilder messageBuilder;

@BeforeClass
public static void setupMessageBuilder() {
AbstractBuild build = mock(AbstractBuild.class);
AbstractProject project = mock(AbstractProject.class);
AbstractProject job = mock(AbstractProject.class);
ItemGroup group = mock(ItemGroup.class);
SlackNotifier notifier = mock(SlackNotifier.class);

when(build.getDisplayName()).thenReturn("build");
when(build.getProject()).thenReturn(project);
when(build.getParent()).thenReturn(job);
when(job.getParent()).thenReturn(group);
when(group.getFullDisplayName()).thenReturn("group");
when(project.getParent()).thenReturn(group);

when(job.getFullDisplayName()).thenReturn("job");
when(project.getFullDisplayName()).thenReturn("project");

messageBuilder = new ActiveNotifier.MessageBuilder(notifier, build);
}

@Test
public void testEscapeAnchor() throws IOException {
String input = "<a href='target'>test</a>";
String expected = "<'target'|test>";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapePercent() throws IOException {
String input = "hello % world";
String expected = "hello % world";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapeBraces() throws IOException {
String input = "something { is } odd";
String expected = "something { is } odd";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapeBracesInLink() throws IOException {
String input = "<a href='target'>test { case }</a>";
String expected = "<'target'|test { case }>";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}
}

0 comments on commit 60e8a8a

Please sign in to comment.