Skip to content

Commit a0c2b0f

Browse files
author
Frederik Boster
committed
Add optional creation of Bitbucket tasks for violation comments.
1 parent a85c76e commit a0c2b0f

File tree

3 files changed

+77
-24
lines changed

3 files changed

+77
-24
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ public void createCommentWithAllSingleFileComments(final String comment) {
8888
@Override
8989
public void createSingleFileComment(
9090
final ChangedFile file, final Integer line, final String comment) {
91-
client.pullRequestComment(file.getFilename(), line, comment);
91+
final BitbucketServerComment bitbucketComment =
92+
client.pullRequestComment(file.getFilename(), line, comment);
93+
94+
if (violationCommentsToBitbucketApi.getCreateSingleFileCommentsTasks()) {
95+
client.commentCreateTask(bitbucketComment, file.getFilename(), line);
96+
}
9297
}
9398

9499
@Override

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static ViolationCommentsToBitbucketServerApi violationCommentsToBitbucket
2424
private String bitbucketServerUrl = null;
2525
private boolean createCommentWithAllSingleFileComments = false;
2626
private boolean createSingleFileComments = true;
27+
private boolean createSingleFileCommentsTasks = false;
2728
private String password;
2829
private String projectKey;
2930
private String propPassword = DEFAULT_PROP_VIOLATIONS_PASSWORD;
@@ -92,6 +93,10 @@ public boolean getCreateSingleFileComments() {
9293
return createSingleFileComments;
9394
}
9495

96+
public boolean getCreateSingleFileCommentsTasks() {
97+
return createSingleFileCommentsTasks;
98+
}
99+
95100
public String getPassword() {
96101
return password;
97102
}
@@ -194,6 +199,12 @@ public ViolationCommentsToBitbucketServerApi withCreateSingleFileComments(
194199
return this;
195200
}
196201

202+
public ViolationCommentsToBitbucketServerApi withCreateSingleFileCommentsTasks(
203+
final boolean createSingleFileCommentsTasks) {
204+
this.createSingleFileCommentsTasks = createSingleFileCommentsTasks;
205+
return this;
206+
}
207+
197208
public ViolationCommentsToBitbucketServerApi withPassword(final String password) {
198209
this.password = password;
199210
return this;

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

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.common.annotations.VisibleForTesting;
99
import com.google.gson.Gson;
1010
import com.jayway.jsonpath.JsonPath;
11+
import java.io.File;
1112
import java.io.UnsupportedEncodingException;
1213
import java.util.LinkedHashMap;
1314
import java.util.List;
@@ -62,18 +63,23 @@ public BitbucketServerClient(
6263
new ProxyConfig(proxyHostNameOrIp, proxyHostPort, proxyUser, proxyPassword);
6364
}
6465

65-
private String getBitbucketServerPulLRequestBase() {
66-
return bitbucketServerBaseUrl
67-
+ "/rest/api/1.0/projects/"
66+
private String getBitbucketServerApiBase() {
67+
return bitbucketServerBaseUrl + "/rest/api/1.0";
68+
}
69+
70+
private String getBitbucketServerPullRequestBase() {
71+
return getBitbucketServerApiBase()
72+
+ "/projects/"
6873
+ bitbucketServerProject
6974
+ "/repos/"
7075
+ bitbucketServerRepo
7176
+ "/pull-requests/"
7277
+ bitbucketServerPullRequestId;
7378
}
7479

75-
private <T> T invokeAndParse(final String url, final String jsonPath) {
76-
final String json = doInvokeUrl(url, BitbucketServerInvoker.Method.GET, null);
80+
private <T> T invokeAndParse(
81+
final String url, final Method method, final String postContent, final String jsonPath) {
82+
final String json = doInvokeUrl(url, method, postContent);
7783

7884
try {
7985
return JsonPath.read(json, jsonPath);
@@ -85,24 +91,27 @@ private <T> T invokeAndParse(final String url, final String jsonPath) {
8591

8692
public List<String> pullRequestChanges() {
8793
return invokeAndParse(
88-
getBitbucketServerPulLRequestBase() + "/changes?limit=999999", "$..path.toString");
94+
getBitbucketServerPullRequestBase() + "/changes?limit=999999",
95+
Method.GET,
96+
null,
97+
"$..path.toString");
8998
}
9099

91100
public void pullRequestComment(final String message) {
92101
final String postContent = "{ \"text\": \"" + safeJson(message) + "\"}";
93102
doInvokeUrl(
94-
getBitbucketServerPulLRequestBase() + "/comments",
103+
getBitbucketServerPullRequestBase() + "/comments",
95104
BitbucketServerInvoker.Method.POST,
96105
postContent);
97106
}
98107

99-
private String doInvokeUrl(final String string, final Method method, final String postContent) {
108+
private String doInvokeUrl(final String url, final Method method, final String postContent) {
100109
if (isNullOrEmpty(bitbucketServerUser) || isNullOrEmpty(bitbucketServerPassword)) {
101110
return bitbucketServerInvoker.invokeUrl(
102-
string, method, postContent, bitbucketPersonalAccessToken, proxyInformation);
111+
url, method, postContent, bitbucketPersonalAccessToken, proxyInformation);
103112
} else {
104113
return bitbucketServerInvoker.invokeUrl(
105-
string,
114+
url,
106115
method,
107116
postContent,
108117
bitbucketServerUser,
@@ -111,33 +120,40 @@ private String doInvokeUrl(final String string, final Method method, final Strin
111120
}
112121
}
113122

114-
public void pullRequestComment(final String changedFile, int line, final String message) {
123+
public BitbucketServerComment pullRequestComment(final String changedFile, int line, final String message) {
115124
if (line == 0) {
116125
line = 1;
117126
}
118-
final String postContent =
127+
final String commentPostContent =
119128
"{ \"text\": \""
120129
+ safeJson(message)
121130
+ "\", \"anchor\": { \"line\": "
122131
+ line
123132
+ ", \"lineType\": \"ADDED\", \"fileType\": \"TO\", \"path\": \""
124133
+ changedFile
125134
+ "\" }}";
126-
doInvokeUrl(
127-
getBitbucketServerPulLRequestBase() + "/comments",
128-
BitbucketServerInvoker.Method.POST,
129-
postContent);
135+
136+
final LinkedHashMap<?, ?> parsed =
137+
invokeAndParse(
138+
getBitbucketServerPullRequestBase() + "/comments",
139+
BitbucketServerInvoker.Method.POST,
140+
commentPostContent,
141+
"$");
142+
143+
return toBitbucketServerComment(parsed);
130144
}
131145

132146
public List<BitbucketServerComment> pullRequestComments(final String changedFile) {
133147
try {
134148
final String encodedChangedFile = encode(changedFile, UTF_8.name());
135149
final List<LinkedHashMap<?, ?>> parsed =
136150
invokeAndParse(
137-
getBitbucketServerPulLRequestBase()
151+
getBitbucketServerPullRequestBase()
138152
+ "/comments?path="
139153
+ encodedChangedFile
140154
+ "&limit=999999",
155+
Method.GET,
156+
null,
141157
"$.values[*]");
142158
return toBitbucketServerComments(parsed);
143159
} catch (final UnsupportedEncodingException e) {
@@ -146,7 +162,7 @@ public List<BitbucketServerComment> pullRequestComments(final String changedFile
146162
}
147163

148164
public BitbucketServerDiffResponse pullRequestDiff() {
149-
final String url = getBitbucketServerPulLRequestBase() + "/diff?limit=999999";
165+
final String url = getBitbucketServerPullRequestBase() + "/diff?limit=999999";
150166
final String json = doInvokeUrl(url, BitbucketServerInvoker.Method.GET, null);
151167
try {
152168
return new Gson().fromJson(json, BitbucketServerDiffResponse.class);
@@ -157,7 +173,7 @@ public BitbucketServerDiffResponse pullRequestDiff() {
157173

158174
public void pullRequestRemoveComment(final Integer commentId, final Integer commentVersion) {
159175
doInvokeUrl(
160-
getBitbucketServerPulLRequestBase()
176+
getBitbucketServerPullRequestBase()
161177
+ "/comments/"
162178
+ commentId
163179
+ "?version="
@@ -166,6 +182,22 @@ public void pullRequestRemoveComment(final Integer commentId, final Integer comm
166182
null);
167183
}
168184

185+
public void commentCreateTask(
186+
final BitbucketServerComment comment, String changedFile, int line) {
187+
final String changedFileName = new File(changedFile).getName();
188+
189+
final String taskPostContent =
190+
"{ \"anchor\": { \"id\": "
191+
+ comment.getId()
192+
+ ", \"type\": \"COMMENT\" }, \"text\": \"[Violation] "
193+
+ changedFileName
194+
+ " L"
195+
+ line
196+
+ "\" }}";
197+
198+
doInvokeUrl(getBitbucketServerApiBase() + "/tasks", Method.POST, taskPostContent);
199+
}
200+
169201
@VisibleForTesting
170202
String safeJson(final String message) {
171203
return message.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "").replaceAll("\n", "\\\\n");
@@ -175,11 +207,16 @@ private List<BitbucketServerComment> toBitbucketServerComments(
175207
final List<LinkedHashMap<?, ?>> parsed) {
176208
final List<BitbucketServerComment> transformed = newArrayList();
177209
for (final LinkedHashMap<?, ?> from : parsed) {
178-
final Integer version = (Integer) from.get("version");
179-
final String text = (String) from.get("text");
180-
final Integer id = (Integer) from.get("id");
181-
transformed.add(new BitbucketServerComment(version, text, id));
210+
transformed.add(toBitbucketServerComment(from));
182211
}
183212
return transformed;
184213
}
214+
215+
private BitbucketServerComment toBitbucketServerComment(LinkedHashMap<?, ?> parsed) {
216+
final Integer version = (Integer) parsed.get("version");
217+
final String text = (String) parsed.get("text");
218+
final Integer id = (Integer) parsed.get("id");
219+
220+
return new BitbucketServerComment(version, text, id);
221+
}
185222
}

0 commit comments

Comments
 (0)