-
Notifications
You must be signed in to change notification settings - Fork 81
implement resetting of overview comments #162
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,39 +398,41 @@ public void resetComments(PullRequestRef pr, | |
StashUser sonarUser, | ||
StashClient stashClient) { | ||
try { | ||
// Let's call this "diffRep_loop" | ||
for (StashComment comment : diffReport.getComments()) { | ||
|
||
// delete comment only if published by the current SQ user | ||
if (sonarUser.getId() != comment.getAuthor().getId()) { | ||
continue; | ||
// Next element in "diffRep_loop" | ||
|
||
// comment contains tasks which cannot be deleted => do nothing | ||
} else if (comment.containsPermanentTasks()) { | ||
LOGGER.debug("Comment \"{}\" (path:\"{}\", line:\"{}\")" | ||
+ "CANNOT be deleted because one of its tasks is not deletable.", comment.getId(), | ||
comment.getPath(), | ||
comment.getLine()); | ||
continue; // Next element in "diffRep_loop" | ||
} | ||
|
||
// delete tasks linked to the current comment | ||
for (StashTask task : comment.getTasks()) { | ||
stashClient.deleteTaskOnComment(task); | ||
} | ||
|
||
stashClient.deletePullRequestComment(pr, comment); | ||
} | ||
// FIXME delete tasks on file-wide comments | ||
// resetComments(diffReport.getComments(), pr, sonarUser, stashClient); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
resetComments(stashClient.getPullRequestOverviewComments(pr), pr, sonarUser, stashClient); | ||
|
||
LOGGER.info("SonarQube issues reported to Stash by user \"{}\" have been reset", | ||
sonarUser.getName()); | ||
|
||
} catch (StashClientException e) { | ||
LOGGER.error("Unable to reset comment list", e); | ||
} | ||
} | ||
|
||
private void resetComments(Collection<StashComment> comments, PullRequestRef pr, StashUser sonarUser, StashClient stashClient) | ||
throws StashClientException { | ||
for (StashComment comment : comments) { | ||
if (sonarUser.getId() != comment.getAuthor().getId()) { | ||
continue; | ||
} | ||
|
||
if (comment.containsPermanentTasks()) { | ||
LOGGER.debug("Comment \"{}\" (path:\"{}\", line:\"{}\")" | ||
+ "CANNOT be deleted because one of its tasks is not deletable.", comment.getId(), | ||
comment.getPath(), | ||
comment.getLine()); | ||
continue; // Next element in "diffRep_loop" | ||
} | ||
|
||
// delete tasks linked to the current comment | ||
for (StashTask task : comment.getTasks()) { | ||
stashClient.deleteTaskOnComment(task); | ||
} | ||
|
||
stashClient.deletePullRequestComment(pr, comment); | ||
} | ||
} | ||
|
||
@Override | ||
public String getIssuePath(PostJobIssue issue) { | ||
InputComponent ip = issue.inputComponent(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package org.sonar.plugins.stash.client; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.asynchttpclient.AsyncHttpClient; | ||
import org.asynchttpclient.BoundRequestBuilder; | ||
import org.asynchttpclient.DefaultAsyncHttpClient; | ||
|
@@ -19,7 +21,6 @@ | |
import org.sonar.plugins.stash.PeekableInputStream; | ||
import org.sonar.plugins.stash.PluginInfo; | ||
import org.sonar.plugins.stash.PullRequestRef; | ||
import org.sonar.plugins.stash.StashPlugin; | ||
import org.sonar.plugins.stash.StashPlugin.IssueType; | ||
import org.sonar.plugins.stash.StashPluginUtils; | ||
import org.sonar.plugins.stash.exceptions.StashClientException; | ||
|
@@ -64,7 +65,7 @@ public class StashClient implements AutoCloseable { | |
private static final String API_ONE_PR_ALL_COMMENTS = API_ONE_PR + "/comments"; | ||
private static final String API_ONE_PR_DIFF = API_ONE_PR + "/diff?withComments=true"; | ||
private static final String API_ONE_PR_APPROVAL = API_ONE_PR + "/approve"; | ||
private static final String API_ONE_PR_COMMENT_PATH = API_ONE_PR + "/comments?path={4}&start={5,number,#}"; | ||
private static final String API_ONE_PR_COMMENT_PATH = API_ONE_PR + "/comments?path={4}"; | ||
|
||
private static final String API_ONE_PR_ONE_COMMENT = API_ONE_PR_ALL_COMMENTS + "/{4}?version={5}"; | ||
|
||
|
@@ -111,31 +112,29 @@ public void postCommentOnPullRequest(PullRequestRef pr, String report) | |
postCreate(request, json, MessageFormat.format(COMMENT_POST_ERROR_MESSAGE, pr.repository(), pr.pullRequestId())); | ||
} | ||
|
||
public Collection<StashComment> getPullRequestOverviewComments(PullRequestRef pr) throws StashClientException { | ||
return getPaged( | ||
MessageFormat.format(API_ONE_PR + "/activities", baseUrl, pr.project(), pr.repository(), pr.pullRequestId()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"Error!" | ||
).stream().map(StashCollector::extractCommentFromActivity).flatMap(StashPluginUtils::removeEmpty).collect(Collectors.toList()); | ||
} | ||
|
||
public StashCommentReport getPullRequestComments(PullRequestRef pr, String path) | ||
throws StashClientException { | ||
StashCommentReport result = new StashCommentReport(); | ||
|
||
long start = 0; | ||
boolean isLastPage = false; | ||
String url = MessageFormat.format(API_ONE_PR_COMMENT_PATH, | ||
baseUrl, | ||
pr.project(), | ||
pr.repository(), | ||
pr.pullRequestId(), | ||
path | ||
); | ||
|
||
while (!isLastPage) { | ||
for (JsonObject jsonComment: getPaged(url, | ||
MessageFormat.format(COMMENT_GET_ERROR_MESSAGE, pr.repository(), pr.pullRequestId()))) { | ||
try { | ||
String request = MessageFormat.format(API_ONE_PR_COMMENT_PATH, | ||
baseUrl, | ||
pr.project(), | ||
pr.repository(), | ||
pr.pullRequestId(), | ||
path, | ||
start); | ||
JsonObject jsonComments = get(request, | ||
MessageFormat.format(COMMENT_GET_ERROR_MESSAGE, | ||
pr.repository(), | ||
pr.pullRequestId())); | ||
result.add(StashCollector.extractComments(jsonComments)); | ||
|
||
// Stash pagination: check if you get all comments linked to the pull-request | ||
isLastPage = StashCollector.isLastPage(jsonComments); | ||
start = StashCollector.getNextPageStart(jsonComments); | ||
result.add(StashCollector.extractComment(jsonComment)); | ||
} catch (StashReportExtractionException e) { | ||
throw new StashClientException(e); | ||
} | ||
|
@@ -310,6 +309,10 @@ private JsonObject get(String url, String errorMessage) throws StashClientExcept | |
return performRequest(httpClient.prepareGet(url), null, HttpURLConnection.HTTP_OK, errorMessage); | ||
} | ||
|
||
private Collection<JsonObject> getPaged(String url, String errorMessage) throws StashClientException { | ||
return performPagedRequest(httpClient.prepareGet(url), null, HttpURLConnection.HTTP_OK, errorMessage); | ||
} | ||
|
||
private JsonObject post(String url, JsonObject body, String errorMessage) throws StashClientException { | ||
return performRequest(httpClient.preparePost(url), body, HttpURLConnection.HTTP_OK, errorMessage); | ||
} | ||
|
@@ -456,4 +459,33 @@ AsyncHttpClient createHttpClient(String sonarQubeVersion) { | |
.build() | ||
); | ||
} | ||
|
||
private Collection<JsonObject> performPagedRequest(BoundRequestBuilder requestBuilder, | ||
JsonObject body, | ||
int expectedStatusCode, | ||
String errorMessage) throws StashClientException { | ||
|
||
Collection<JsonObject> result = new ArrayList<>(); | ||
|
||
boolean doneLastPage = false; | ||
int nextPageStart = 0; | ||
// FIXME size/limit support | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
while (!doneLastPage) { | ||
if (nextPageStart > 0) { | ||
requestBuilder.addQueryParam("start", String.valueOf(nextPageStart)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will add an additional parameter "start" and not replace the previous one. With more activities you will end up with link params: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marekjagielski Good point. thanks! |
||
} | ||
|
||
JsonObject res = performRequest(requestBuilder, body, expectedStatusCode, errorMessage); | ||
JsonArray values = ((JsonArray) res.get("values")); | ||
if (values == null) { | ||
throw new StashClientException("Paged response did not include values"); | ||
} | ||
values.asCollection(result); | ||
doneLastPage = res.getBooleanOrDefault("isLastPage", true); | ||
nextPageStart = res.getIntegerOrDefault("nextPageStart", -1); | ||
} | ||
|
||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package org.sonar.plugins.stash.issue.collector; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import org.json.simple.JsonArray; | ||
import org.json.simple.JsonObject; | ||
import org.sonar.plugins.stash.PullRequestRef; | ||
|
@@ -20,9 +22,7 @@ public final class StashCollector { | |
private static final String AUTHOR = "author"; | ||
private static final String VERSION = "version"; | ||
|
||
private StashCollector() { | ||
// Hiding implicit public constructor with an explicit private one (squid:S1118) | ||
} | ||
private StashCollector() {} | ||
|
||
public static StashCommentReport extractComments(JsonObject jsonComments) throws StashReportExtractionException { | ||
StashCommentReport result = new StashCommentReport(); | ||
|
@@ -41,8 +41,11 @@ public static StashCommentReport extractComments(JsonObject jsonComments) throws | |
return result; | ||
} | ||
|
||
public static StashComment extractComment(JsonObject jsonComment, String path, Long line) { | ||
public static Optional<StashComment> extractCommentFromActivity(JsonObject json) { | ||
return Optional.ofNullable((JsonObject) json.get("comment")).filter(Objects::nonNull).map(j -> StashCollector.extractComment(j, null, null)); | ||
} | ||
|
||
public static StashComment extractComment(JsonObject jsonComment, String path, Long line) { | ||
long id = getLong(jsonComment, "id"); | ||
String message = (String)jsonComment.get("text"); | ||
|
||
|
@@ -51,7 +54,10 @@ public static StashComment extractComment(JsonObject jsonComment, String path, L | |
JsonObject jsonAuthor = (JsonObject)jsonComment.get(AUTHOR); | ||
StashUser stashUser = extractUser(jsonAuthor); | ||
|
||
return new StashComment(id, message, path, line, stashUser, version); | ||
StashComment result = new StashComment(id, message, path, line, stashUser, version); | ||
// FIXME do this at some central place | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
updateCommentTasks(result, (JsonArray) jsonComment.get("tasks")); | ||
return result; | ||
} | ||
|
||
public static StashComment extractComment(JsonObject jsonComment) throws StashReportExtractionException { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take the required action to fix the issue indicated by this comment.