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

Commit 40b9c33

Browse files
committed
Avoid crash when variable resolved to string with dollar sign #233
1 parent d23fe8f commit 40b9c33

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6+
### No issue
7+
doc
8+
9+
[d23fe8fe5b339d9](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d23fe8fe5b339d9) Tomas Bjerre *2017-07-16 05:55:13*
10+
11+
## 3.2
612
### GitHub [#226](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/226) REST API POST ISSUES
713
Correcting markdown in REST-API
814

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111

1212
import java.io.UnsupportedEncodingException;
1313
import java.util.Map;
14+
import java.util.regex.Matcher;
1415

1516
import org.apache.commons.lang3.StringEscapeUtils;
1617
import org.slf4j.Logger;
1718

19+
import se.bjurr.prnfb.http.ClientKeyStore;
20+
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
21+
import se.bjurr.prnfb.settings.PrnfbNotification;
22+
1823
import com.atlassian.bitbucket.pull.PullRequest;
1924
import com.atlassian.bitbucket.repository.RepositoryService;
2025
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
@@ -23,10 +28,6 @@
2328
import com.google.common.annotations.VisibleForTesting;
2429
import com.google.common.base.Supplier;
2530

26-
import se.bjurr.prnfb.http.ClientKeyStore;
27-
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
28-
import se.bjurr.prnfb.settings.PrnfbNotification;
29-
3031
public class PrnfbRenderer {
3132
public enum ENCODE_FOR {
3233
NONE,
@@ -91,6 +92,7 @@ String getRenderedStringResolved(
9192
replaceWith = resolved;
9293
}
9394
try {
95+
replaceWith = Matcher.quoteReplacement(replaceWith);
9496
string = string.replaceAll(regExpStr, replaceWith);
9597
} catch (IllegalArgumentException e) {
9698
throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public String resolve(
8282
for (PrnfbVariable v : PrnfbVariable.values()) {
8383
if (v != EVERYTHING_URL //
8484
&& v != PULL_REQUEST_DESCRIPTION) {
85-
parts.add(v.name() + "=\\${" + v.name() + "}");
85+
parts.add(v.name() + "=${" + v.name() + "}");
8686
}
8787
}
8888
Collections.sort(parts);

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_DESCRIPTION;
1414
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_FROM_HASH;
1515
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_MERGE_COMMIT;
16+
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_TITLE;
1617
import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder;
1718

1819
import java.io.UnsupportedEncodingException;
@@ -24,6 +25,15 @@
2425
import org.junit.Test;
2526
import org.mockito.Mock;
2627

28+
import se.bjurr.prnfb.http.ClientKeyStore;
29+
import se.bjurr.prnfb.http.HttpResponse;
30+
import se.bjurr.prnfb.http.Invoker;
31+
import se.bjurr.prnfb.http.UrlInvoker;
32+
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
33+
import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR;
34+
import se.bjurr.prnfb.settings.PrnfbNotification;
35+
import se.bjurr.prnfb.settings.ValidationException;
36+
2737
import com.atlassian.bitbucket.pull.PullRequest;
2838
import com.atlassian.bitbucket.pull.PullRequestParticipant;
2939
import com.atlassian.bitbucket.pull.PullRequestRef;
@@ -34,15 +44,6 @@
3444
import com.google.common.base.Supplier;
3545
import com.google.common.base.Suppliers;
3646

37-
import se.bjurr.prnfb.http.ClientKeyStore;
38-
import se.bjurr.prnfb.http.HttpResponse;
39-
import se.bjurr.prnfb.http.Invoker;
40-
import se.bjurr.prnfb.http.UrlInvoker;
41-
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
42-
import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR;
43-
import se.bjurr.prnfb.settings.PrnfbNotification;
44-
import se.bjurr.prnfb.settings.ValidationException;
45-
4647
public class PrnfbRendererTest {
4748

4849
@Mock private ApplicationUser applicationUser;
@@ -133,6 +134,20 @@ public void testThatEverythingCanBeRendered() throws UnsupportedEncodingExceptio
133134
.startsWith("asd ");
134135
}
135136

137+
@Test
138+
public void testThatDollarInStringCanBeRendered() throws UnsupportedEncodingException {
139+
String actual =
140+
sut.getRenderedStringResolved(
141+
"asd ${" + PULL_REQUEST_TITLE.name() + "} asd",
142+
encodeFor,
143+
sut.regexp(PULL_REQUEST_TITLE),
144+
"BNRSD-387 Fix circular reference logging on $host errors in RSD abstract client");
145+
146+
assertThat(actual) //
147+
.isEqualTo(
148+
"asd BNRSD-387 Fix circular reference logging on $host errors in RSD abstract client asd");
149+
}
150+
136151
@Test
137152
public void testThatIfAVariableChrashesOnResolveItWillBeEmpty() {
138153
String actual =

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
import org.junit.Test;
1515

16+
import se.bjurr.prnfb.http.ClientKeyStore;
17+
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
18+
import se.bjurr.prnfb.settings.PrnfbNotification;
19+
1620
import com.atlassian.bitbucket.pull.PullRequest;
1721
import com.atlassian.bitbucket.repository.RepositoryService;
1822
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
@@ -22,10 +26,6 @@
2226
import com.google.common.io.Files;
2327
import com.google.common.io.Resources;
2428

25-
import se.bjurr.prnfb.http.ClientKeyStore;
26-
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
27-
import se.bjurr.prnfb.settings.PrnfbNotification;
28-
2929
public class PrnfbVariableTest {
3030

3131
private ApplicationUser applicationUser;
@@ -80,7 +80,7 @@ public void testThatEverythingVariableIsResolvedToEveryOtherVariable() {
8080
for (PrnfbVariable v : PrnfbVariable.values()) {
8181
if (v != EVERYTHING_URL && v != PULL_REQUEST_DESCRIPTION) {
8282
assertThat(actual) //
83-
.containsOnlyOnce(v.name() + "=\\${" + v.name() + "}") //
83+
.containsOnlyOnce(v.name() + "=${" + v.name() + "}") //
8484
.doesNotContain(EVERYTHING_URL.name());
8585
}
8686
}

0 commit comments

Comments
 (0)