Skip to content

Commit fb65dc1

Browse files
committed
Not searching comments on files when not commenting files
1 parent 00d3ffb commit fb65dc1

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class BitbucketServerCommentsProvider implements CommentsProvider {
4141
.expireAfterWrite(2, MINUTES)
4242
.build(
4343
new CacheLoader<String, BitbucketServerDiffResponse>() {
44-
public BitbucketServerDiffResponse load(String path) {
44+
@Override
45+
public BitbucketServerDiffResponse load(final String path) {
4546
return client.pullRequestDiff(path);
4647
}
4748
});
@@ -108,15 +109,26 @@ public void createSingleFileComment(
108109
@Override
109110
public List<Comment> getComments() {
110111
final List<Comment> comments = newArrayList();
111-
for (final String changedFile : client.pullRequestChanges()) {
112-
final List<BitbucketServerComment> bitbucketServerCommentsOnFile =
113-
client.pullRequestComments(changedFile);
114-
for (final BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) {
115-
final List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile);
116-
comments.add(new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics));
112+
if (shouldCreateSingleFileComment()) {
113+
/**
114+
* This is time consuming to do and is only needed if we are creating comments on each file.
115+
*/
116+
for (final String changedFile : client.pullRequestChanges()) {
117+
final List<BitbucketServerComment> bitbucketServerCommentsOnFile =
118+
client.pullRequestComments(changedFile);
119+
for (final BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) {
120+
final List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile);
121+
comments.add(
122+
new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics));
123+
}
117124
}
118125
}
119126

127+
for (final BitbucketServerComment comment : client.pullRequestComments()) {
128+
final List<String> specifics = newArrayList(comment.getVersion() + "", "");
129+
comments.add(new Comment(comment.getId() + "", comment.getText(), null, specifics));
130+
}
131+
120132
return comments;
121133
}
122134

@@ -162,7 +174,7 @@ public boolean shouldComment(final ChangedFile changedFile, final Integer change
162174
final List<BitbucketServerDiff> diffs =
163175
diffResponse.get(changedFile.getFilename()).getDiffs();
164176
return shouldComment(changedFile, changedLine, context, diffs);
165-
} catch (Exception e) {
177+
} catch (final Exception e) {
166178
violationsLogger.log(SEVERE, "Was unable to get diff from " + changedFile.getFilename(), e);
167179
return false;
168180
}
@@ -246,9 +258,9 @@ private void removeComment(final BitbucketServerComment comment) {
246258
}
247259

248260
private void removeTasks(final BitbucketServerComment comment) {
249-
List<BitbucketServerTask> bitbucketServerTasks = comment.getTasks();
261+
final List<BitbucketServerTask> bitbucketServerTasks = comment.getTasks();
250262

251-
for (BitbucketServerTask bitbucketServerTask : bitbucketServerTasks) {
263+
for (final BitbucketServerTask bitbucketServerTask : bitbucketServerTasks) {
252264
client.removeTask(bitbucketServerTask);
253265
}
254266
}

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

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ private <T> T invokeAndParse(
9999
}
100100

101101
public List<String> pullRequestChanges() {
102-
String url = getBitbucketServerPullRequestBase() + "/changes?limit=999999";
102+
final String url = getBitbucketServerPullRequestBase() + "/changes?limit=999999";
103103
final String json = doInvokeUrl(url, Method.GET, null);
104104

105-
String jsonPath = "$..path.toString";
105+
final String jsonPath = "$..path.toString";
106106
try {
107-
List<String> response = JsonPath.read(json, jsonPath);
107+
final List<String> response = JsonPath.read(json, jsonPath);
108108
if (response.isEmpty()) {
109109
violationsLogger.log(
110110
INFO,
@@ -176,45 +176,55 @@ public BitbucketServerComment pullRequestComment(
176176
}
177177

178178
public BitbucketServerComment pullRequestComment(final Long commentId) {
179-
String url = getBitbucketServerPullRequestBase() + "/comments/" + commentId;
179+
final String url = getBitbucketServerPullRequestBase() + "/comments/" + commentId;
180180

181181
final LinkedHashMap<?, ?> parsed =
182182
invokeAndParse(url, BitbucketServerInvoker.Method.GET, null, "$");
183183

184184
return toBitbucketServerComment(parsed);
185185
}
186186

187+
public List<BitbucketServerComment> pullRequestComments() {
188+
final String url =
189+
getBitbucketServerPullRequestBase() + "/comments?limit=999999&anchorState=ALL";
190+
return getComments(url);
191+
}
192+
187193
public List<BitbucketServerComment> pullRequestComments(final String changedFile) {
188194
try {
189195
final String encodedChangedFile = encode(changedFile, UTF_8.name());
190-
String url =
196+
final String url =
191197
getBitbucketServerPullRequestBase()
192198
+ "/comments?path="
193199
+ encodedChangedFile
194200
+ "&limit=999999&anchorState=ALL";
195-
String jsonPath = "$.values[*]";
196-
197-
final String json = doInvokeUrl(url, Method.GET, null);
198-
List<LinkedHashMap<?, ?>> parsed = null;
199-
try {
200-
parsed = JsonPath.read(json, jsonPath);
201-
} catch (final Exception e) {
202-
throw new RuntimeException(
203-
"Unable to parse diff response from " + url + " using " + jsonPath + "\n\n" + json, e);
204-
}
205-
206-
if (parsed.isEmpty()) {
207-
violationsLogger.log(
208-
INFO,
209-
"Found no comments from " + url + " with JSONPath " + jsonPath + " in JSON:\n" + json);
210-
}
211-
212-
return toBitbucketServerComments(parsed);
201+
return getComments(url);
213202
} catch (final UnsupportedEncodingException e) {
214203
throw new RuntimeException(e.getMessage(), e);
215204
}
216205
}
217206

207+
private List<BitbucketServerComment> getComments(final String url) {
208+
final String jsonPath = "$.values[*]";
209+
210+
final String json = doInvokeUrl(url, Method.GET, null);
211+
List<LinkedHashMap<?, ?>> parsed = null;
212+
try {
213+
parsed = JsonPath.read(json, jsonPath);
214+
} catch (final Exception e) {
215+
throw new RuntimeException(
216+
"Unable to parse diff response from " + url + " using " + jsonPath + "\n\n" + json, e);
217+
}
218+
219+
if (parsed.isEmpty()) {
220+
violationsLogger.log(
221+
INFO,
222+
"Found no comments from " + url + " with JSONPath " + jsonPath + " in JSON:\n" + json);
223+
}
224+
225+
return toBitbucketServerComments(parsed);
226+
}
227+
218228
public BitbucketServerDiffResponse pullRequestDiff(final String path) {
219229
final String url = getBitbucketServerPullRequestBase() + "/diff/" + path;
220230
final String json = doInvokeUrl(url, BitbucketServerInvoker.Method.GET, null);
@@ -249,7 +259,7 @@ public void removeTask(final BitbucketServerTask task) {
249259
}
250260

251261
public void commentCreateTask(
252-
final BitbucketServerComment comment, String changedFile, int line) {
262+
final BitbucketServerComment comment, final String changedFile, final int line) {
253263
final String changedFileName = new File(changedFile).getName();
254264

255265
final String taskPostContent =
@@ -278,7 +288,7 @@ private List<BitbucketServerComment> toBitbucketServerComments(
278288
return transformed;
279289
}
280290

281-
private BitbucketServerComment toBitbucketServerComment(LinkedHashMap<?, ?> parsed) {
291+
private BitbucketServerComment toBitbucketServerComment(final LinkedHashMap<?, ?> parsed) {
282292
final Integer version = (Integer) parsed.get("version");
283293
final String text = (String) parsed.get("text");
284294
final Integer id = (Integer) parsed.get("id");
@@ -303,17 +313,17 @@ private BitbucketServerComment toBitbucketServerComment(LinkedHashMap<?, ?> pars
303313
return comment;
304314
}
305315

306-
private List<BitbucketServerTask> toBitbucketServerTasks(List<LinkedHashMap<?, ?>> parsed) {
307-
List<BitbucketServerTask> bitbucketServerTasks = new ArrayList<>();
316+
private List<BitbucketServerTask> toBitbucketServerTasks(final List<LinkedHashMap<?, ?>> parsed) {
317+
final List<BitbucketServerTask> bitbucketServerTasks = new ArrayList<>();
308318

309-
for (LinkedHashMap<?, ?> parsedTask : parsed) {
319+
for (final LinkedHashMap<?, ?> parsedTask : parsed) {
310320
bitbucketServerTasks.add(toBitbucketServerTask(parsedTask));
311321
}
312322

313323
return bitbucketServerTasks;
314324
}
315325

316-
private BitbucketServerTask toBitbucketServerTask(LinkedHashMap<?, ?> parsed) {
326+
private BitbucketServerTask toBitbucketServerTask(final LinkedHashMap<?, ?> parsed) {
317327
final Integer id = (Integer) parsed.get("id");
318328
final String text = (String) parsed.get("text");
319329
return new BitbucketServerTask(id, text);

0 commit comments

Comments
 (0)