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

Commit 6925ea2

Browse files
committed
Url encoding evaluated values when they are used in URL invocations #76
1 parent 294485e commit 6925ea2

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## 2.9
6+
* Url encoding evaluated values when they are used in URL invocations.
7+
58
## 2.8
69
* New variables
710
* ${PULL_REQUEST_MERGE_COMMIT} Hash of merged commit (only available for merged-event).

src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ public void notify(final PrnfbNotification notification, PrnfbPullRequestAction
159159

160160
Optional<String> postContent = absent();
161161
if (notification.getPostContent().isPresent()) {
162-
postContent = of(renderer.render(notification.getPostContent().get()));
162+
postContent = of(renderer.render(notification.getPostContent().get(), FALSE));
163163
}
164-
String renderedUrl = renderer.render(notification.getUrl());
164+
String renderedUrl = renderer.render(notification.getUrl(), TRUE);
165165
logger.info(notification.getName() + " > " //
166166
+ pullRequest.getFromRef().getId() + "(" + pullRequest.getFromRef().getLatestCommit() + ") -> " //
167167
+ pullRequest.getToRef().getId() + "(" + pullRequest.getToRef().getLatestCommit() + ")" + " " //
@@ -173,7 +173,7 @@ public void notify(final PrnfbNotification notification, PrnfbPullRequestAction
173173
.appendBasicAuth(notification);
174174
for (Header header : notification.getHeaders()) {
175175
urlInvoker//
176-
.withHeader(header.getName(), renderer.render(header.getValue()));
176+
.withHeader(header.getName(), renderer.render(header.getValue(), FALSE));
177177
}
178178
invoker.invoke(urlInvoker//
179179
.withProxyServer(notification.getProxyServer()) //
@@ -189,8 +189,8 @@ public boolean notificationTriggeredByAction(PrnfbNotification notification, Prn
189189
}
190190
if (notification.getFilterRegexp().isPresent()
191191
&& notification.getFilterString().isPresent()
192-
&& !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get()))
193-
.find()) {
192+
&& !compile(notification.getFilterRegexp().get()).matcher(
193+
renderer.render(notification.getFilterString().get(), FALSE)).find()) {
194194
return FALSE;
195195
}
196196

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package se.bjurr.prnfb.listener;
22

3+
import static com.google.common.base.Charsets.UTF_8;
4+
import static com.google.common.base.Throwables.propagate;
35
import static com.google.common.collect.Iterables.filter;
46
import static com.google.common.collect.Lists.newArrayList;
7+
import static java.net.URLEncoder.encode;
58
import static java.util.logging.Logger.getLogger;
69
import static java.util.regex.Pattern.compile;
710
import static se.bjurr.prnfb.listener.PrnfbRenderer.REPO_PROTOCOL.http;
811
import static se.bjurr.prnfb.listener.PrnfbRenderer.REPO_PROTOCOL.ssh;
912
import static se.bjurr.prnfb.listener.UrlInvoker.urlInvoker;
1013
import static se.bjurr.prnfb.listener.UrlInvoker.HTTP_METHOD.GET;
1114

15+
import java.io.UnsupportedEncodingException;
1216
import java.util.Map;
1317
import java.util.Set;
1418
import java.util.logging.Logger;
@@ -476,12 +480,17 @@ public PrnfbRenderer(PullRequest pullRequest, PrnfbPullRequestAction pullRequest
476480
this.variables = variables;
477481
}
478482

479-
public String render(String string) {
483+
public String render(String string, Boolean forUrl) {
480484
for (final PrnfbVariable variable : PrnfbVariable.values()) {
481485
final String regExpStr = "\\$\\{" + variable.name() + "\\}";
482486
if (string.contains(regExpStr.replaceAll("\\\\", ""))) {
483-
string = string.replaceAll(regExpStr, variable.resolve(pullRequest, pullRequestAction, ApplicationUser,
484-
repositoryService, propertiesService, prnfbNotification, variables));
487+
try {
488+
String resolved = variable.resolve(pullRequest, pullRequestAction, ApplicationUser, repositoryService,
489+
propertiesService, prnfbNotification, variables);
490+
string = string.replaceAll(regExpStr, forUrl ? encode(resolved, UTF_8.name()) : resolved);
491+
} catch (UnsupportedEncodingException e) {
492+
propagate(e);
493+
}
485494
}
486495
}
487496
return string;

src/test/java/se/bjurr/prnfb/admin/PrnfbPullRequestEventListenerTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void testThatAUrlWithVariablesFromAndToCanBeInvoked() throws Exception {
289289
.getPullRequest() //
290290
) //
291291
.triggerButton("The Button") //
292-
.invokedUrl(1, "http://bjurr.se/10The%20Button%20Text");
292+
.invokedUrl(1, "http://bjurr.se/10The+Button+Text");
293293
}
294294
}
295295

@@ -333,7 +333,7 @@ public void testThatAUrlWithCommentVariableHasSpacesReplaced() throws Exception
333333
.withCommentText("a text with\nnewline") //
334334
.build() //
335335
) //
336-
.invokedUrl(0, "http://bjurr.se/a%20text%20with%20newline");
336+
.invokedUrl(0, "http://bjurr.se/a+text+with%0Anewline");
337337
}
338338

339339
@Test
@@ -358,7 +358,7 @@ public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsSl
358358
.withPullRequestAction(OPENED) //
359359
.build() //
360360
) //
361-
.invokedUrl(0, "http://bjurr.se/feature/branchmodmerge");
361+
.invokedUrl(0, "http://bjurr.se/feature%2Fbranchmodmerge");
362362
}
363363

364364
@Test
@@ -426,7 +426,7 @@ public void testThatAUrlWithVariablesExceptFromAndToCanBeInvoked() throws Except
426426
) //
427427
.invokedUrl(
428428
0,
429-
"http://bjurr.se/id=10&action=OPENED&displayName=authorDisplayName&authorEmail=authorEmail&authorId=100&authorName=authorName&authorSlug=authorSlug&pullRequestUrl=http://bitbucket.server/projects/theProject/repos/theRepoName/pull-requests/10" //
429+
"http://bjurr.se/id=10&action=OPENED&displayName=authorDisplayName&authorEmail=authorEmail&authorId=100&authorName=authorName&authorSlug=authorSlug&pullRequestUrl=http%3A%2F%2Fbitbucket.server%2Fprojects%2FtheProject%2Frepos%2FtheRepoName%2Fpull-requests%2F10" //
430430
);
431431
}
432432

@@ -889,7 +889,7 @@ public void testThatFilterCanBeUsedWithComments() throws Exception {
889889
.withPullRequestAction(COMMENTED) //
890890
.build() //
891891
) //
892-
.invokedUrl(0, "http://bjurr.se/?comment=keyword%20A%20nice%20comment&version=0"); //
892+
.invokedUrl(0, "http://bjurr.se/?comment=keyword+A+nice+comment&version=0"); //
893893
}
894894

895895
@Test
@@ -922,7 +922,7 @@ public void testThatFilterCanBeUsedWithCommentsAndSpecialEscapedChars() throws E
922922
.withPullRequestAction(COMMENTED) //
923923
.build() //
924924
) //
925-
.invokedUrl(0, "http://bjurr.se/?comment=%20keyword%20&version=0");
925+
.invokedOnlyUrl("http://bjurr.se/?comment=+keyword+&version=0");
926926
}
927927

928928
@Test
@@ -1430,7 +1430,7 @@ public void testThatButtonCanBeUsedForTriggeringEvent() throws Exception {
14301430
) //
14311431
.store() //
14321432
.triggerButton("Button Form") //
1433-
.invokedOnlyUrl("http://bjurr.se/Trigger%20notification") //
1433+
.invokedOnlyUrl("http://bjurr.se/Trigger+notification") //
14341434
.hasButtonsEnabled("Button Form");
14351435
}
14361436

@@ -1512,7 +1512,7 @@ public void testThatEventTriggeredByButtonCanBeFiltered() throws Exception {
15121512
) //
15131513
.store() //
15141514
.triggerButton("Button Form") //
1515-
.invokedOnlyUrl("http://bjurr.se/?123=button%20text%20123");
1515+
.invokedOnlyUrl("http://bjurr.se/?123=button+text+123");
15161516
}
15171517

15181518
@Test
@@ -1547,7 +1547,7 @@ public void testThatEventTriggeredByButtonCanBeIgnoredByFilterWhileAnotherIsNotI
15471547
) //
15481548
.store() //
15491549
.triggerButton("Button Form") //
1550-
.invokedOnlyUrl("http://bjurr.se/?123=button%20text%20123");
1550+
.invokedOnlyUrl("http://bjurr.se/?123=button+text+123");
15511551
}
15521552

15531553
@Test
@@ -1578,8 +1578,8 @@ public void testThatEventTriggeredByButtonCanResultInSeveralNotifications() thro
15781578
.withFieldValue(button_visibility, EVERYONE.name()).build()) //
15791579
.store() //
15801580
.triggerButton("Button Form") //
1581-
.invokedUrl(0, "http://bjurr.se/?123=button%20text%20123") //
1582-
.invokedUrl(1, "http://bjurr.se/?456=button%20text%20123");
1581+
.invokedUrl(0, "http://bjurr.se/?123=button+text+123") //
1582+
.invokedUrl(1, "http://bjurr.se/?456=button+text+123");
15831583
}
15841584

15851585
@Test
@@ -1613,9 +1613,9 @@ public void testThatThereCanBeSeveralButtons() throws Exception {
16131613
) //
16141614
.store() //
16151615
.triggerButton("Button Form 1") //
1616-
.invokedUrl(0, "http://bjurr.se/?button%20text%201") //
1616+
.invokedUrl(0, "http://bjurr.se/?button+text+1") //
16171617
.triggerButton("Button Form 2") //
1618-
.invokedUrl(1, "http://bjurr.se/?button%20text%202");
1618+
.invokedUrl(1, "http://bjurr.se/?button+text+2");
16191619
}
16201620

16211621
@Test
@@ -1637,7 +1637,7 @@ public void testThatValueFromUrlCanBeUsedInInvocation() throws Exception {
16371637
.withPullRequestAction(OPENED) //
16381638
.build() //
16391639
) //
1640-
.invokedOnlyUrl("http://bjurr.se/?some%20content");
1640+
.invokedOnlyUrl("http://bjurr.se/?some+content");
16411641
}
16421642

16431643
@Test

0 commit comments

Comments
 (0)