This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
implement resetting of overview comments #162
Open
t-8ch
wants to merge
3
commits into
master
Choose a base branch
from
reset_overview_comments
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -394,43 +394,45 @@ public StashDiffReport getPullRequestDiffReport(PullRequestRef pr, StashClient s | |
* Reset all comments linked to a pull-request. | ||
*/ | ||
public void resetComments(PullRequestRef pr, | ||
StashDiffReport diffReport, | ||
Collection<StashComment> comments, | ||
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(comments, 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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -16,10 +18,10 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.sonar.api.batch.rule.Severity; | ||
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 +66,8 @@ 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_ACTIVITIES = API_ONE_PR + "/activities"; | ||
|
||
private static final String API_ONE_PR_ONE_COMMENT = API_ONE_PR_ALL_COMMENTS + "/{4}?version={5}"; | ||
|
||
|
@@ -111,31 +114,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()), | ||
"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 +311,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 +461,34 @@ 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) { | ||
requestBuilder.resetQuery(); | ||
if (nextPageStart > 0) { | ||
requestBuilder.addQueryParam("start", String.valueOf(nextPageStart)); | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.