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

Commit 86ebeb8

Browse files
committed
Encoding HTML newline as <br/>
1 parent d52398c commit 86ebeb8

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### No issue
7+
Doc
8+
9+
[d52398cd9b66ea6](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d52398cd9b66ea6) Tomas Bjerre *2017-10-06 15:45:02*
10+
511
## 3.15
612
### GitHub [#251](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/251) Plugin causing crash and stopping pushing to bitbucket after upgrading.
713
Removing migration code for legacy settings

src/main/java/se/bjurr/prnfb/service/PrnfbRenderer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@ String getRenderedStringResolved(
8383
if (encodeFor == URL) {
8484
try {
8585
replaceWith = encode(resolved, UTF_8.name());
86-
} catch (UnsupportedEncodingException e) {
86+
} catch (final UnsupportedEncodingException e) {
8787
propagate(e);
8888
}
8989
} else if (encodeFor == HTML) {
90-
replaceWith = StringEscapeUtils.escapeHtml4(resolved);
90+
replaceWith = StringEscapeUtils.escapeHtml4(resolved).replaceAll("(\r\n|\n)", "<br />");
9191
} else {
9292
replaceWith = resolved;
9393
}
9494
try {
9595
replaceWith = Matcher.quoteReplacement(replaceWith);
9696
string = string.replaceAll(regExpStr, replaceWith);
97-
} catch (IllegalArgumentException e) {
97+
} catch (final IllegalArgumentException e) {
9898
throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e);
9999
}
100100
return string;
@@ -146,7 +146,7 @@ private String renderVariable(
146146
if (resolved == null) {
147147
resolved = "";
148148
}
149-
} catch (Exception e) {
149+
} catch (final Exception e) {
150150
LOG.error("Error when resolving " + variable, e);
151151
}
152152
return getRenderedStringResolved(string, encodeFor, regExpStr, resolved);

src/test/java/se/bjurr/prnfb/service/PrnfbRendererTest.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public HttpResponse invoke(UrlInvoker toInvoke) {
9393
HttpResponse response = null;
9494
try {
9595
response = new HttpResponse(new URI("http://fake.om/"), 200, "theResponse");
96-
} catch (URISyntaxException e) {
96+
} catch (final URISyntaxException e) {
9797
e.printStackTrace();
9898
}
9999
toInvoke.setResponse(response);
@@ -104,7 +104,7 @@ public HttpResponse invoke(UrlInvoker toInvoke) {
104104

105105
@Test
106106
public void testThatEverythingCanBeRendered() throws UnsupportedEncodingException {
107-
String actual =
107+
final String actual =
108108
sut.getRenderedStringResolved(
109109
"asd ${" + EVERYTHING_URL.name() + "} asd",
110110
encodeFor,
@@ -121,7 +121,7 @@ public void testThatEverythingCanBeRendered() throws UnsupportedEncodingExceptio
121121
shouldAcceptAnyCertificate,
122122
securityService));
123123

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

137137
@Test
138138
public void testThatDollarInStringCanBeRendered() throws UnsupportedEncodingException {
139-
String actual =
139+
final String actual =
140140
sut.getRenderedStringResolved(
141141
"asd ${" + PULL_REQUEST_TITLE.name() + "} asd",
142142
encodeFor,
@@ -150,7 +150,7 @@ public void testThatDollarInStringCanBeRendered() throws UnsupportedEncodingExce
150150

151151
@Test
152152
public void testThatIfAVariableChrashesOnResolveItWillBeEmpty() {
153-
String actual =
153+
final String actual =
154154
sut.render(
155155
"my ${" + PULL_REQUEST_AUTHOR_EMAIL + "} string",
156156
encodeFor,
@@ -168,7 +168,7 @@ public void testThatIfAVariableChrashesResolvedToNullOnResolveItWillBeEmpty() {
168168
.thenReturn(user);
169169
when(pullRequest.getAuthor().getUser().getEmailAddress()) //
170170
.thenReturn(null);
171-
String actual =
171+
final String actual =
172172
sut.render(
173173
"my ${" + PULL_REQUEST_AUTHOR_EMAIL + "} string",
174174
encodeFor,
@@ -196,7 +196,7 @@ public void testThatInjectionUrlCanBeRendered() throws ValidationException {
196196
variables,
197197
securityService);
198198

199-
String actual =
199+
final String actual =
200200
sut.render(
201201
"my ${" + INJECTION_URL_VALUE + "} string",
202202
encodeFor,
@@ -209,7 +209,7 @@ public void testThatInjectionUrlCanBeRendered() throws ValidationException {
209209
@Test
210210
public void testThatMergeCommitCanBeRenderedIfExists() {
211211
variables.put(PULL_REQUEST_MERGE_COMMIT, Suppliers.ofInstance("mergeHash"));
212-
String actual =
212+
final String actual =
213213
sut.render(
214214
"my ${" + PULL_REQUEST_MERGE_COMMIT + "} string",
215215
encodeFor,
@@ -221,7 +221,7 @@ public void testThatMergeCommitCanBeRenderedIfExists() {
221221

222222
@Test
223223
public void testThatMergeCommitCanBeRenderedIfNotExists() {
224-
String actual =
224+
final String actual =
225225
sut.render(
226226
"my ${" + PULL_REQUEST_MERGE_COMMIT + "} string",
227227
encodeFor,
@@ -233,7 +233,7 @@ public void testThatMergeCommitCanBeRenderedIfNotExists() {
233233

234234
@Test
235235
public void testThatStringCanBeRendered() {
236-
String actual =
236+
final String actual =
237237
sut.render(
238238
"my ${" + PULL_REQUEST_FROM_HASH + "} string",
239239
encodeFor,
@@ -246,7 +246,7 @@ public void testThatStringCanBeRendered() {
246246
@Test
247247
public void testThatStringCanBeRenderedForUrl() {
248248
variables.put(PULL_REQUEST_COMMENT_TEXT, Suppliers.ofInstance("the comment"));
249-
String actual =
249+
final String actual =
250250
sut.render(
251251
"my ${" + PULL_REQUEST_COMMENT_TEXT + "} string",
252252
ENCODE_FOR.URL,
@@ -255,4 +255,17 @@ public void testThatStringCanBeRenderedForUrl() {
255255
assertThat(actual) //
256256
.isEqualTo("my the+comment string");
257257
}
258+
259+
@Test
260+
public void testThatStringCanBeRenderedForHtml() {
261+
variables.put(PULL_REQUEST_COMMENT_TEXT, Suppliers.ofInstance("the\ncomment \""));
262+
final String actual =
263+
sut.render(
264+
"my ${" + PULL_REQUEST_COMMENT_TEXT + "} string",
265+
ENCODE_FOR.HTML,
266+
clientKeyStore,
267+
shouldAcceptAnyCertificate);
268+
assertThat(actual) //
269+
.isEqualTo("my the<br />comment &quot; string");
270+
}
258271
}

0 commit comments

Comments
 (0)