Skip to content

Commit f00b2b7

Browse files
committed
Not trying to delete PR comments
* Looks like its not supported in API
1 parent 6f60e3b commit f00b2b7

File tree

5 files changed

+52
-76
lines changed

5 files changed

+52
-76
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ repositories {
6363
}
6464

6565
dependencies {
66-
compile 'se.bjurr.violations:violation-comments-lib:1.10'
66+
compile 'se.bjurr.violations:violation-comments-lib:1.11'
6767
compile 'com.jayway.jsonpath:json-path:2.0.0'
6868

6969
testCompile 'junit:junit:4.12'

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/BitbucketServerCommentsProvider.java

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static com.google.common.base.Throwables.propagate;
44
import static com.google.common.collect.Lists.newArrayList;
55

6-
import java.io.IOException;
76
import java.util.ArrayList;
87
import java.util.List;
98

@@ -17,7 +16,7 @@ public class BitbucketServerCommentsProvider implements CommentsProvider {
1716

1817
private final BitbucketServerClient client;
1918

20-
private ViolationCommentsToBitbucketServerApi violationCommentsToGitHubApi;
19+
private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi;
2120

2221
public BitbucketServerCommentsProvider(ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) {
2322
String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl();
@@ -28,74 +27,52 @@ public BitbucketServerCommentsProvider(ViolationCommentsToBitbucketServerApi vio
2827
String bitbucketServerPassword = violationCommentsToBitbucketApi.getPassword();
2928
this.client = new BitbucketServerClient(bitbucketServerBaseUrl, bitbucketServerProject, bitbucketServerRepo,
3029
bitbucketServerPullRequestId, bitbucketServerUser, bitbucketServerPassword);
30+
this.violationCommentsToBitbucketApi = violationCommentsToBitbucketApi;
3131
}
3232

3333
@Override
3434
public void createCommentWithAllSingleFileComments(String comment) {
35-
if (!this.violationCommentsToGitHubApi.getCreateCommentWithAllSingleFileComments()) {
35+
if (!this.violationCommentsToBitbucketApi.getCreateCommentWithAllSingleFileComments()) {
3636
return;
3737
}
3838

39-
try {
40-
this.client.pullRequestComment(comment);
41-
} catch (IOException e) {
42-
throw propagate(e);
43-
}
39+
this.client.pullRequestComment(comment);
4440
}
4541

4642
@Override
4743
public void createSingleFileComment(ChangedFile file, Integer line, String comment) {
48-
if (!this.violationCommentsToGitHubApi.getCreateSingleFileComments()) {
44+
if (!this.violationCommentsToBitbucketApi.getCreateSingleFileComments()) {
4945
return;
5046
}
5147

52-
try {
53-
this.client.pullRequestComment(file.getFilename(), line, comment);
54-
} catch (IOException e) {
55-
propagate(e);
56-
}
48+
this.client.pullRequestComment(file.getFilename(), line, comment);
5749
}
5850

5951
@Override
6052
public List<Comment> getComments() {
6153
List<Comment> comments = newArrayList();
62-
try {
63-
64-
List<BitbucketServerComment> bitbucketServerCommentsOnPR = this.client.pullRequestComments();
65-
for (BitbucketServerComment pullRequestComment : bitbucketServerCommentsOnPR) {
66-
List<String> specifics = newArrayList(pullRequestComment.getVersion() + "");
67-
comments.add(new Comment(pullRequestComment.getId() + "", pullRequestComment.getText(), null, specifics));
68-
}
69-
70-
for (String changedFile : this.client.pullRequestChanges()) {
71-
List<BitbucketServerComment> bitbucketServerCommentsOnFile = this.client.pullRequestComments(changedFile);
72-
for (BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) {
73-
List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile);
74-
comments.add(new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics));
75-
}
54+
for (String changedFile : this.client.pullRequestChanges()) {
55+
List<BitbucketServerComment> bitbucketServerCommentsOnFile = this.client.pullRequestComments(changedFile);
56+
for (BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) {
57+
List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile);
58+
comments.add(new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics));
7659
}
77-
} catch (IOException e) {
78-
throw propagate(e);
7960
}
8061

8162
return comments;
8263
}
8364

8465
@Override
8566
public List<ChangedFile> getFiles() {
86-
try {
87-
List<ChangedFile> changedFiles = newArrayList();
88-
89-
List<String> bitbucketServerChangedFiles = this.client.pullRequestChanges();
67+
List<ChangedFile> changedFiles = newArrayList();
9068

91-
for (String changedFile : bitbucketServerChangedFiles) {
92-
changedFiles.add(new ChangedFile(changedFile, new ArrayList<String>()));
93-
}
69+
List<String> bitbucketServerChangedFiles = this.client.pullRequestChanges();
9470

95-
return changedFiles;
96-
} catch (IOException e) {
97-
throw propagate(e);
71+
for (String changedFile : bitbucketServerChangedFiles) {
72+
changedFiles.add(new ChangedFile(changedFile, new ArrayList<String>()));
9873
}
74+
75+
return changedFiles;
9976
}
10077

10178
@Override

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerClient.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static com.google.common.collect.Lists.newArrayList;
44

5-
import java.io.IOException;
65
import java.util.LinkedHashMap;
76
import java.util.List;
87

@@ -34,36 +33,30 @@ public BitbucketServerClient(String bitbucketServerBaseUrl, String bitbucketServ
3433
this.bitbucketServerUser = bitbucketServerUser;
3534
}
3635

37-
public List<String> pullRequestChanges() throws IOException {
36+
public List<String> pullRequestChanges() {
3837
return invokeAndParse(getBitbucketServerPulLRequestBase() + "/changes?limit=999999", "$..path.toString");
3938
}
4039

41-
public void pullRequestComment(String message) throws IOException {
42-
String postContent = "{ \"text\": \"" + message.replaceAll("\"", "") + "\"}";
40+
public void pullRequestComment(String message) {
41+
String postContent = "{ \"text\": \"" + safeJson(message) + "\"}";
4342
bitbucketServerInvoker.invokeUrl(getBitbucketServerPulLRequestBase() + "/comments",
4443
BitbucketServerInvoker.Method.POST, postContent, this.bitbucketServerUser, this.bitbucketServerPassword);
4544
}
4645

47-
public void pullRequestComment(String changedFile, int line, String message) throws IOException {
48-
String postContent = "{ \"text\": \"" + message.replaceAll("\"", "") + "\", \"anchor\": { \"line\": \"" + line
49-
+ "\", \"lineType\": \"ADDED\", \"fileType\": \"TO\", \"path\": \"" + changedFile + "\" }}";
46+
public void pullRequestComment(String changedFile, int line, String message) {
47+
String postContent = "{ \"text\": \"" + safeJson(message) + "\", \"anchor\": { \"line\": " + line
48+
+ ", \"lineType\": \"ADDED\", \"fileType\": \"TO\", \"path\": \"" + changedFile + "\" }}";
5049
bitbucketServerInvoker.invokeUrl(getBitbucketServerPulLRequestBase() + "/comments",
5150
BitbucketServerInvoker.Method.POST, postContent, this.bitbucketServerUser, this.bitbucketServerPassword);
5251
}
5352

54-
public List<BitbucketServerComment> pullRequestComments() throws IOException {
55-
List<LinkedHashMap<?, ?>> parsed = invokeAndParse(getBitbucketServerPulLRequestBase() + "/comments?limit=999999",
56-
"$.values[*]");
57-
return toBitbucketServerComments(parsed);
58-
}
59-
60-
public List<BitbucketServerComment> pullRequestComments(String changedFile) throws IOException {
53+
public List<BitbucketServerComment> pullRequestComments(String changedFile) {
6154
List<LinkedHashMap<?, ?>> parsed = invokeAndParse(getBitbucketServerPulLRequestBase() + "/comments?path="
6255
+ changedFile + "&limit=999999", "$.values[*]");
6356
return toBitbucketServerComments(parsed);
6457
}
6558

66-
public void pullRequestRemoveComment(Integer commentId, Integer commentVersion) throws IOException {
59+
public void pullRequestRemoveComment(Integer commentId, Integer commentVersion) {
6760
bitbucketServerInvoker.invokeUrl(getBitbucketServerPulLRequestBase() + "/comments/" + commentId + "?version="
6861
+ commentVersion, BitbucketServerInvoker.Method.DELETE, null, this.bitbucketServerUser,
6962
this.bitbucketServerPassword);
@@ -74,7 +67,7 @@ private String getBitbucketServerPulLRequestBase() {
7467
+ this.bitbucketServerRepo + "/pull-requests/" + this.bitbucketServerPullRequestId;
7568
}
7669

77-
private <T> T invokeAndParse(String url, String jsonPath) throws IOException {
70+
private <T> T invokeAndParse(String url, String jsonPath) {
7871
String json = bitbucketServerInvoker.invokeUrl(url, BitbucketServerInvoker.Method.GET, null,
7972
this.bitbucketServerUser, this.bitbucketServerPassword);
8073
try {
@@ -84,6 +77,10 @@ private <T> T invokeAndParse(String url, String jsonPath) throws IOException {
8477
}
8578
}
8679

80+
private String safeJson(String message) {
81+
return message.replaceAll("\"", "").replaceAll("\n", "\\\\n");
82+
}
83+
8784
private List<BitbucketServerComment> toBitbucketServerComments(List<LinkedHashMap<?, ?>> parsed) {
8885
List<BitbucketServerComment> transformed = newArrayList();
8986
for (LinkedHashMap<?, ?> from : parsed) {

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerInvoker.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414

1515
import javax.xml.bind.DatatypeConverter;
1616

17+
import com.google.common.base.Throwables;
18+
1719
public class BitbucketServerInvoker {
1820

1921
public enum Method {
2022
DELETE, GET, POST
2123
}
2224

2325
public String invokeUrl(String url, Method method, String postContent, String bitbucketServerUser,
24-
String bitbucketServerPassword) throws IOException {
26+
String bitbucketServerPassword) {
2527
HttpURLConnection conn = null;
2628
OutputStream output = null;
2729
BufferedReader reader = null;
@@ -50,15 +52,21 @@ public String invokeUrl(String url, Method method, String postContent, String bi
5052
}
5153
String json = stringBuilder.toString();
5254
return json;
55+
} catch (Exception e) {
56+
throw new RuntimeException("Error calling:\n" + url + "\n" + method + "\n" + postContent, e);
5357
} finally {
5458
try {
55-
conn.disconnect();
56-
reader.close();
59+
if (conn != null) {
60+
conn.disconnect();
61+
}
62+
if (reader != null) {
63+
reader.close();
64+
}
5765
if (output != null) {
5866
output.close();
5967
}
6068
} catch (IOException e) {
61-
throw e;
69+
throw Throwables.propagate(e);
6270
}
6371
}
6472
}

src/test/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerClientTest.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,22 @@ public void before() {
2121
BitbucketServerClient.setBitbucketServerInvoker(new BitbucketServerInvoker() {
2222
@Override
2323
public String invokeUrl(String url, Method method, String postContent, String bitbucketServerUser,
24-
String bitbucketServerPassword) throws IOException {
24+
String bitbucketServerPassword) {
2525
return BitbucketServerClientTest.this.mockedJson;
2626
}
2727
});
2828
}
2929

3030
@Test
31-
public void testPullRequestChanges() throws IOException {
31+
public void testPullRequestChanges() {
3232
mockJson("pull-request-changes.json");
3333
List<String> actual = this.sut.pullRequestChanges();
3434
assertThat(actual).isNotEmpty();
3535
assertThat(actual.get(0)).isEqualTo("basic_branching/file.txt");
3636
}
3737

3838
@Test
39-
public void testPullRequestComments() throws IOException {
40-
mockJson("pull-request-comments.json");
41-
List<BitbucketServerComment> actual = this.sut.pullRequestComments();
42-
assertThat(actual).isNotEmpty();
43-
assertThat(actual.get(0).getId()).isEqualTo(2);
44-
assertThat(actual.get(0).getText()).isEqualTo("in diff comment");
45-
assertThat(actual.get(0).getVersion()).isEqualTo(0);
46-
}
47-
48-
@Test
49-
public void testPullRequestCommentsOnFile() throws IOException {
39+
public void testPullRequestCommentsOnFile() {
5040
mockJson("pull-request-comments.json");
5141
List<BitbucketServerComment> actual = this.sut.pullRequestComments("any/file.txt");
5242
assertThat(actual).isNotEmpty();
@@ -55,8 +45,12 @@ public void testPullRequestCommentsOnFile() throws IOException {
5545
assertThat(actual.get(0).getVersion()).isEqualTo(0);
5646
}
5747

58-
private void mockJson(String resourceName) throws IOException {
59-
this.mockedJson = Resources.toString(Resources.getResource(resourceName), Charsets.UTF_8);
48+
private void mockJson(String resourceName) {
49+
try {
50+
this.mockedJson = Resources.toString(Resources.getResource(resourceName), Charsets.UTF_8);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
6054
}
6155

6256
}

0 commit comments

Comments
 (0)