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

Commit 9a58b3f

Browse files
committed
Url encoding evaluated values when they are used in URL invocations #76
1 parent 3b86779 commit 9a58b3f

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 Stash.
44

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

src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public void notify(final PrnfsNotification notification, PrnfsPullRequestAction
170170

171171
Optional<String> postContent = absent();
172172
if (notification.getPostContent().isPresent()) {
173-
postContent = of(renderer.render(notification.getPostContent().get()));
173+
postContent = of(renderer.render(notification.getPostContent().get(), FALSE));
174174
}
175-
String renderedUrl = renderer.render(notification.getUrl());
175+
String renderedUrl = renderer.render(notification.getUrl(), TRUE);
176176
logger.info(notification.getName() + " > " //
177177
+ pullRequest.getFromRef().getId() + "(" + pullRequest.getFromRef().getLatestChangeset() + ") -> " //
178178
+ pullRequest.getToRef().getId() + "(" + pullRequest.getToRef().getLatestChangeset() + ")" + " " //
@@ -185,7 +185,7 @@ public void notify(final PrnfsNotification notification, PrnfsPullRequestAction
185185

186186
for (Header header : notification.getHeaders()) {
187187
urlInvoker//
188-
.withHeader(header.getName(), renderer.render(header.getValue()));
188+
.withHeader(header.getName(), renderer.render(header.getValue(), FALSE));
189189
}
190190
invoker.invoke(urlInvoker//
191191
.withProxyServer(notification.getProxyServer()) //
@@ -201,8 +201,8 @@ public boolean notificationTriggeredByAction(PrnfsNotification notification, Prn
201201
}
202202
if (notification.getFilterRegexp().isPresent()
203203
&& notification.getFilterString().isPresent()
204-
&& !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get()))
205-
.find()) {
204+
&& !compile(notification.getFilterRegexp().get()).matcher(
205+
renderer.render(notification.getFilterString().get(), FALSE)).find()) {
206206
return FALSE;
207207
}
208208

src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package se.bjurr.prnfs.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.prnfs.listener.PrnfsRenderer.REPO_PROTOCOL.http;
811
import static se.bjurr.prnfs.listener.PrnfsRenderer.REPO_PROTOCOL.ssh;
912
import static se.bjurr.prnfs.listener.UrlInvoker.urlInvoker;
1013
import static se.bjurr.prnfs.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;
@@ -434,12 +438,17 @@ public PrnfsRenderer(PullRequest pullRequest, PrnfsPullRequestAction pullRequest
434438
this.variables = variables;
435439
}
436440

437-
public String render(String string) {
441+
public String render(String string, Boolean forUrl) {
438442
for (final PrnfsVariable variable : PrnfsVariable.values()) {
439443
final String regExpStr = "\\$\\{" + variable.name() + "\\}";
440444
if (string.contains(regExpStr.replaceAll("\\\\", ""))) {
441-
string = string.replaceAll(regExpStr, variable.resolve(pullRequest, pullRequestAction, stashUser,
442-
repositoryService, propertiesService, prnfsNotification, variables));
445+
try {
446+
String resolved = variable.resolve(pullRequest, pullRequestAction, stashUser, repositoryService,
447+
propertiesService, prnfsNotification, variables);
448+
string = string.replaceAll(regExpStr, forUrl ? encode(resolved, UTF_8.name()) : resolved);
449+
} catch (UnsupportedEncodingException e) {
450+
propagate(e);
451+
}
443452
}
444453
}
445454
return string;

src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java

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

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

342342
@Test
@@ -361,7 +361,7 @@ public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsSl
361361
.withPullRequestAction(OPENED) //
362362
.build() //
363363
) //
364-
.invokedUrl(0, "http://bjurr.se/feature/branchmodmerge");
364+
.invokedUrl(0, "http://bjurr.se/feature%2Fbranchmodmerge");
365365
}
366366

367367
@Test
@@ -429,7 +429,7 @@ public void testThatAUrlWithVariablesExceptFromAndToCanBeInvoked() throws Except
429429
) //
430430
.invokedUrl(
431431
0,
432-
"http://bjurr.se/id=10&action=OPENED&displayName=authorDisplayName&authorEmail=authorEmail&authorId=100&authorName=authorName&authorSlug=authorSlug&pullRequestUrl=http://stash.server/projects/theProject/repos/theRepoName/pull-requests/10" //
432+
"http://bjurr.se/id=10&action=OPENED&displayName=authorDisplayName&authorEmail=authorEmail&authorId=100&authorName=authorName&authorSlug=authorSlug&pullRequestUrl=http%3A%2F%2Fstash.server%2Fprojects%2FtheProject%2Frepos%2FtheRepoName%2Fpull-requests%2F10" //
433433
);
434434
}
435435

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

898898
@Test
@@ -925,7 +925,7 @@ public void testThatFilterCanBeUsedWithCommentsAndSpecialEscapedChars() throws E
925925
.withPullRequestAction(COMMENTED) //
926926
.build() //
927927
) //
928-
.invokedUrl(0, "http://bjurr.se/?comment=%20keyword%20&version=0");
928+
.invokedUrl(0, "http://bjurr.se/?comment=+keyword+&version=0");
929929
}
930930

931931
@Test
@@ -1433,7 +1433,7 @@ public void testThatButtonCanBeUsedForTriggeringEvent() throws Exception {
14331433
) //
14341434
.store() //
14351435
.triggerButton("Button Form") //
1436-
.invokedOnlyUrl("http://bjurr.se/Trigger%20notification") //
1436+
.invokedOnlyUrl("http://bjurr.se/Trigger+notification") //
14371437
.hasButtonsEnabled("Button Form");
14381438
}
14391439

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

15211521
@Test
@@ -1550,7 +1550,7 @@ public void testThatEventTriggeredByButtonCanBeIgnoredByFilterWhileAnotherIsNotI
15501550
) //
15511551
.store() //
15521552
.triggerButton("Button Form") //
1553-
.invokedOnlyUrl("http://bjurr.se/?123=button%20text%20123");
1553+
.invokedOnlyUrl("http://bjurr.se/?123=button+text+123");
15541554
}
15551555

15561556
@Test
@@ -1581,8 +1581,8 @@ public void testThatEventTriggeredByButtonCanResultInSeveralNotifications() thro
15811581
.withFieldValue(button_visibility, EVERYONE.name()).build()) //
15821582
.store() //
15831583
.triggerButton("Button Form") //
1584-
.invokedUrl(0, "http://bjurr.se/?123=button%20text%20123") //
1585-
.invokedUrl(1, "http://bjurr.se/?456=button%20text%20123");
1584+
.invokedUrl(0, "http://bjurr.se/?123=button+text+123") //
1585+
.invokedUrl(1, "http://bjurr.se/?456=button+text+123");
15861586
}
15871587

15881588
@Test
@@ -1616,9 +1616,9 @@ public void testThatThereCanBeSeveralButtons() throws Exception {
16161616
) //
16171617
.store() //
16181618
.triggerButton("Button Form 1") //
1619-
.invokedUrl(0, "http://bjurr.se/?button%20text%201") //
1619+
.invokedUrl(0, "http://bjurr.se/?button+text+1") //
16201620
.triggerButton("Button Form 2") //
1621-
.invokedUrl(1, "http://bjurr.se/?button%20text%202");
1621+
.invokedUrl(1, "http://bjurr.se/?button+text+2");
16221622
}
16231623

16241624
@Test
@@ -1640,7 +1640,7 @@ public void testThatValueFromUrlCanBeUsedInInvocation() throws Exception {
16401640
.withPullRequestAction(OPENED) //
16411641
.build() //
16421642
) //
1643-
.invokedOnlyUrl("http://bjurr.se/?some%20content");
1643+
.invokedOnlyUrl("http://bjurr.se/?some+content");
16441644
}
16451645

16461646
@Test

0 commit comments

Comments
 (0)