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

Commit eddca4a

Browse files
committed
Adding option to encode variables for JSON #303
1 parent d8a31bd commit eddca4a

File tree

10 files changed

+115
-7
lines changed

10 files changed

+115
-7
lines changed

CHANGELOG.md

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

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### GitHub [#303](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/303) Post content encoding issue for single quote
7+
Adding option to encode variables for JSON
8+
9+
[1f22177444553b7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/1f22177444553b7) Tomas Bjerre *2018-09-28 14:49:20*
10+
11+
### No issue
12+
Updating fmt-plugin
13+
14+
[d8a31bd9402b106](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d8a31bd9402b106) Tomas Bjerre *2018-08-31 07:02:13*
15+
516
## 3.22
617
### GitHub [#229](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/229) PULL_REQUEST_{TO,FROM}_{HTTP,SSH}_CLONE_URL variables contains username
718
Only stripping username from HTTP clone URL

build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
22
npm install
33
grunt
4+
#atlas-mvn versions:update-properties
45
atlas-mvn package verify

export_and_run.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export MAVEN_OPTS=-Dplugin.resource.directories=`pwd`/src/main/resources
2+
#atlas-mvn versions:update-properties
23
atlas-run || mvn bitbucket:run

pom.xml

+7-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<plugin>
142142
<groupId>se.bjurr.gitchangelog</groupId>
143143
<artifactId>git-changelog-maven-plugin</artifactId>
144-
<version>1.52</version>
144+
<version>${changelog.version}</version>
145145
<executions>
146146
<execution>
147147
<id>GenerateGitChangelog</id>
@@ -243,7 +243,7 @@ Changelog of Pull Request Notifier for Bitbucket.
243243
<plugin>
244244
<groupId>com.coveo</groupId>
245245
<artifactId>fmt-maven-plugin</artifactId>
246-
<version>2.5.1</version>
246+
<version>${fmt.version}</version>
247247
<executions>
248248
<execution>
249249
<goals>
@@ -273,7 +273,7 @@ Changelog of Pull Request Notifier for Bitbucket.
273273
<plugin>
274274
<groupId>se.bjurr.violations</groupId>
275275
<artifactId>violations-maven-plugin</artifactId>
276-
<version>1.3</version>
276+
<version>${violations.version}</version>
277277
<configuration>
278278
<maxViolations>99999999</maxViolations>
279279
<minSeverity>INFO</minSeverity>
@@ -327,9 +327,12 @@ Changelog of Pull Request Notifier for Bitbucket.
327327
</profiles>
328328

329329
<properties>
330+
<changelog.version>1.54</changelog.version>
331+
<fmt.version>2.5.1</fmt.version>
332+
<violations.version>1.13</violations.version>
330333
<bitbucket.version>5.13.1</bitbucket.version>
331334
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>
332335
<quick.reload.version>2.0.0</quick.reload.version>
333336
<amps.version>6.3.6</amps.version>
334337
</properties>
335-
</project>
338+
</project>

release.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
#atlas-mvn versions:update-properties
3+
atlas-mvn release:prepare release:perform -B || exit 1
4+
./build.sh
5+
git commit -a --amend --no-edit
6+
git push -f
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package se.bjurr.prnfb.service;
2+
3+
import java.io.StringWriter;
4+
5+
/** Copied from GSON JsonWriter */
6+
public class JsonEscaper {
7+
8+
private static final String[] REPLACEMENT_CHARS;
9+
private static final String[] HTML_SAFE_REPLACEMENT_CHARS;
10+
11+
static {
12+
REPLACEMENT_CHARS = new String[128];
13+
for (int i = 0; i <= 0x1f; i++) {
14+
REPLACEMENT_CHARS[i] = String.format("\\u%04x", (int) i);
15+
}
16+
REPLACEMENT_CHARS['"'] = "\\\"";
17+
REPLACEMENT_CHARS['\\'] = "\\\\";
18+
REPLACEMENT_CHARS['\t'] = "\\t";
19+
REPLACEMENT_CHARS['\b'] = "\\b";
20+
REPLACEMENT_CHARS['\n'] = "\\n";
21+
REPLACEMENT_CHARS['\r'] = "\\r";
22+
REPLACEMENT_CHARS['\f'] = "\\f";
23+
HTML_SAFE_REPLACEMENT_CHARS = REPLACEMENT_CHARS.clone();
24+
HTML_SAFE_REPLACEMENT_CHARS['<'] = "\\u003c";
25+
HTML_SAFE_REPLACEMENT_CHARS['>'] = "\\u003e";
26+
HTML_SAFE_REPLACEMENT_CHARS['&'] = "\\u0026";
27+
HTML_SAFE_REPLACEMENT_CHARS['='] = "\\u003d";
28+
HTML_SAFE_REPLACEMENT_CHARS['\''] = "\\u0027";
29+
}
30+
31+
public static String jsonEscape(String value) {
32+
final StringWriter out = new StringWriter();
33+
String[] replacements = HTML_SAFE_REPLACEMENT_CHARS;
34+
int last = 0;
35+
int length = value.length();
36+
for (int i = 0; i < length; i++) {
37+
char c = value.charAt(i);
38+
String replacement;
39+
if (c < 128) {
40+
replacement = replacements[c];
41+
if (replacement == null) {
42+
continue;
43+
}
44+
} else if (c == '\u2028') {
45+
replacement = "\\u2028";
46+
} else if (c == '\u2029') {
47+
replacement = "\\u2029";
48+
} else {
49+
continue;
50+
}
51+
if (last < i) {
52+
out.write(value, last, i - last);
53+
}
54+
out.write(replacement);
55+
last = i + 1;
56+
}
57+
if (last < length) {
58+
out.write(value, last, length - last);
59+
}
60+
return out.toString();
61+
}
62+
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import static com.google.common.base.Throwables.propagate;
66
import static java.net.URLEncoder.encode;
77
import static org.slf4j.LoggerFactory.getLogger;
8+
import static se.bjurr.prnfb.service.JsonEscaper.jsonEscape;
89
import static se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR.HTML;
10+
import static se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR.JSON;
911
import static se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR.URL;
1012
import static se.bjurr.prnfb.service.PrnfbVariable.EVERYTHING_URL;
1113

@@ -29,7 +31,8 @@ public class PrnfbRenderer {
2931
public enum ENCODE_FOR {
3032
NONE,
3133
URL,
32-
HTML
34+
HTML,
35+
JSON
3336
}
3437

3538
private static final Logger LOG = getLogger(PrnfbRenderer.class);
@@ -85,6 +88,8 @@ String getRenderedStringResolved(
8588
}
8689
} else if (encodeFor == HTML) {
8790
replaceWith = StringEscapeUtils.escapeHtml4(resolved).replaceAll("(\r\n|\n)", "<br />");
91+
} else if (encodeFor == JSON) {
92+
replaceWith = jsonEscape(resolved);
8893
} else {
8994
replaceWith = resolved;
9095
}

src/main/resources/admin.vm

+5
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,11 @@
619619
<input class="radio" type="radio" name="postContentEncoding" value="HTML"> Encode as HTML
620620
</label>
621621
</div>
622+
<div class="radio">
623+
<label>
624+
<input class="radio" type="radio" name="postContentEncoding" value="JSON"> Encode as JSON
625+
</label>
626+
</div>
622627
</fieldset>
623628

624629
<h4>Headers</h4>

src/main/resources/pr-triggerbutton.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ define('plugin/prnfb/pr-triggerbutton', [
295295
});
296296
});
297297

298-
require(['bitbucket/util/events'], function(events) {
298+
require(['bitbucket/util/events'], function(events) {
299299
events.on('bitbucket.internal.feature.repositories.recent.loaded', function() {
300300
require('plugin/prnfb/pr-triggerbutton');
301-
})
301+
});
302302
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package se.bjurr.prnfb.service;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class JsonEscaperTest {
8+
9+
@Test
10+
public void testThatStringCanBeEscaped() {
11+
assertThat(JsonEscaper.jsonEscape("some'string")) //
12+
.isEqualTo("some\\u0027string");
13+
}
14+
}

0 commit comments

Comments
 (0)