Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Encoding HTML newline as <br/>
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Nov 1, 2017
1 parent d52398c commit 86ebeb8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
Doc

[d52398cd9b66ea6](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d52398cd9b66ea6) Tomas Bjerre *2017-10-06 15:45:02*

## 3.15
### GitHub [#251](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/251) Plugin causing crash and stopping pushing to bitbucket after upgrading.
Removing migration code for legacy settings
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/se/bjurr/prnfb/service/PrnfbRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ String getRenderedStringResolved(
if (encodeFor == URL) {
try {
replaceWith = encode(resolved, UTF_8.name());
} catch (UnsupportedEncodingException e) {
} catch (final UnsupportedEncodingException e) {
propagate(e);
}
} else if (encodeFor == HTML) {
replaceWith = StringEscapeUtils.escapeHtml4(resolved);
replaceWith = StringEscapeUtils.escapeHtml4(resolved).replaceAll("(\r\n|\n)", "<br />");
} else {
replaceWith = resolved;
}
try {
replaceWith = Matcher.quoteReplacement(replaceWith);
string = string.replaceAll(regExpStr, replaceWith);
} catch (IllegalArgumentException e) {
} catch (final IllegalArgumentException e) {
throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e);
}
return string;
Expand Down Expand Up @@ -146,7 +146,7 @@ private String renderVariable(
if (resolved == null) {
resolved = "";
}
} catch (Exception e) {
} catch (final Exception e) {
LOG.error("Error when resolving " + variable, e);
}
return getRenderedStringResolved(string, encodeFor, regExpStr, resolved);
Expand Down
35 changes: 24 additions & 11 deletions src/test/java/se/bjurr/prnfb/service/PrnfbRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public HttpResponse invoke(UrlInvoker toInvoke) {
HttpResponse response = null;
try {
response = new HttpResponse(new URI("http://fake.om/"), 200, "theResponse");
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
e.printStackTrace();
}
toInvoke.setResponse(response);
Expand All @@ -104,7 +104,7 @@ public HttpResponse invoke(UrlInvoker toInvoke) {

@Test
public void testThatEverythingCanBeRendered() throws UnsupportedEncodingException {
String actual =
final String actual =
sut.getRenderedStringResolved(
"asd ${" + EVERYTHING_URL.name() + "} asd",
encodeFor,
Expand All @@ -121,7 +121,7 @@ public void testThatEverythingCanBeRendered() throws UnsupportedEncodingExceptio
shouldAcceptAnyCertificate,
securityService));

for (PrnfbVariable v : PrnfbVariable.values()) {
for (final PrnfbVariable v : PrnfbVariable.values()) {
if (v != EVERYTHING_URL && v != PULL_REQUEST_DESCRIPTION) {
assertThat(actual) //
.containsOnlyOnce(v.name() + "=${" + v.name() + "}") //
Expand All @@ -136,7 +136,7 @@ public void testThatEverythingCanBeRendered() throws UnsupportedEncodingExceptio

@Test
public void testThatDollarInStringCanBeRendered() throws UnsupportedEncodingException {
String actual =
final String actual =
sut.getRenderedStringResolved(
"asd ${" + PULL_REQUEST_TITLE.name() + "} asd",
encodeFor,
Expand All @@ -150,7 +150,7 @@ public void testThatDollarInStringCanBeRendered() throws UnsupportedEncodingExce

@Test
public void testThatIfAVariableChrashesOnResolveItWillBeEmpty() {
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_AUTHOR_EMAIL + "} string",
encodeFor,
Expand All @@ -168,7 +168,7 @@ public void testThatIfAVariableChrashesResolvedToNullOnResolveItWillBeEmpty() {
.thenReturn(user);
when(pullRequest.getAuthor().getUser().getEmailAddress()) //
.thenReturn(null);
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_AUTHOR_EMAIL + "} string",
encodeFor,
Expand Down Expand Up @@ -196,7 +196,7 @@ public void testThatInjectionUrlCanBeRendered() throws ValidationException {
variables,
securityService);

String actual =
final String actual =
sut.render(
"my ${" + INJECTION_URL_VALUE + "} string",
encodeFor,
Expand All @@ -209,7 +209,7 @@ public void testThatInjectionUrlCanBeRendered() throws ValidationException {
@Test
public void testThatMergeCommitCanBeRenderedIfExists() {
variables.put(PULL_REQUEST_MERGE_COMMIT, Suppliers.ofInstance("mergeHash"));
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_MERGE_COMMIT + "} string",
encodeFor,
Expand All @@ -221,7 +221,7 @@ public void testThatMergeCommitCanBeRenderedIfExists() {

@Test
public void testThatMergeCommitCanBeRenderedIfNotExists() {
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_MERGE_COMMIT + "} string",
encodeFor,
Expand All @@ -233,7 +233,7 @@ public void testThatMergeCommitCanBeRenderedIfNotExists() {

@Test
public void testThatStringCanBeRendered() {
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_FROM_HASH + "} string",
encodeFor,
Expand All @@ -246,7 +246,7 @@ public void testThatStringCanBeRendered() {
@Test
public void testThatStringCanBeRenderedForUrl() {
variables.put(PULL_REQUEST_COMMENT_TEXT, Suppliers.ofInstance("the comment"));
String actual =
final String actual =
sut.render(
"my ${" + PULL_REQUEST_COMMENT_TEXT + "} string",
ENCODE_FOR.URL,
Expand All @@ -255,4 +255,17 @@ public void testThatStringCanBeRenderedForUrl() {
assertThat(actual) //
.isEqualTo("my the+comment string");
}

@Test
public void testThatStringCanBeRenderedForHtml() {
variables.put(PULL_REQUEST_COMMENT_TEXT, Suppliers.ofInstance("the\ncomment \""));
final String actual =
sut.render(
"my ${" + PULL_REQUEST_COMMENT_TEXT + "} string",
ENCODE_FOR.HTML,
clientKeyStore,
shouldAcceptAnyCertificate);
assertThat(actual) //
.isEqualTo("my the<br />comment &quot; string");
}
}

0 comments on commit 86ebeb8

Please sign in to comment.