Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.InstantiationStrategy;
Expand Down Expand Up @@ -136,6 +137,10 @@ public boolean tryReportIssuesInline() {
return !settings.getBoolean(GitHubPlugin.GITHUB_DISABLE_INLINE_COMMENTS);
}

public @Nullable String projectKey() {
return settings.getString("sonar.projectKey");
}

/**
* Checks if a proxy was passed with command line parameters or configured in the system.
* If only an HTTP proxy was configured then it's properties are copied to the HTTPS proxy (like SonarQube configuration)
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/org/sonar/plugins/github/GlobalReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.Locale;
import javax.annotation.Nullable;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.github.GHCommitState;
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.batch.rule.Severity;
Expand All @@ -30,15 +32,17 @@ public class GlobalReport {
private int[] newIssuesBySeverity = new int[Severity.values().length];
private int extraIssueCount = 0;
private int maxGlobalReportedIssues;
private String projectKey;
private final ReportBuilder builder;

public GlobalReport(MarkDownUtils markDownUtils, boolean tryReportIssuesInline) {
this(markDownUtils, tryReportIssuesInline, GitHubPluginConfiguration.MAX_GLOBAL_ISSUES);
public GlobalReport(MarkDownUtils markDownUtils, boolean tryReportIssuesInline, @Nullable String projectKey) {
this(markDownUtils, tryReportIssuesInline, GitHubPluginConfiguration.MAX_GLOBAL_ISSUES, projectKey);
}

public GlobalReport(MarkDownUtils markDownUtils, boolean tryReportIssuesInline, int maxGlobalReportedIssues) {
public GlobalReport(MarkDownUtils markDownUtils, boolean tryReportIssuesInline, int maxGlobalReportedIssues, @Nullable String projectKey) {
this.tryReportIssuesInline = tryReportIssuesInline;
this.maxGlobalReportedIssues = maxGlobalReportedIssues;
this.projectKey = projectKey;
this.builder = new MarkDownReportBuilder(markDownUtils);
}

Expand All @@ -48,13 +52,17 @@ private void increment(Severity severity) {

public String formatForMarkdown() {
int newIssues = newIssues(Severity.BLOCKER) + newIssues(Severity.CRITICAL) + newIssues(Severity.MAJOR) + newIssues(Severity.MINOR) + newIssues(Severity.INFO);
if (newIssues == 0) {
return "SonarQube analysis reported no issues.";
}

boolean hasInlineIssues = newIssues > extraIssueCount;
boolean extraIssuesTruncated = extraIssueCount > maxGlobalReportedIssues;

if (newIssues == 0) {
builder.append("SonarQube analysis reported no issues.").appendProjectId(projectKey);
return builder.toString();
}

builder.append("SonarQube analysis reported ").append(newIssues).append(" issue").append(newIssues > 1 ? "s" : "").append("\n");

if (hasInlineIssues || extraIssuesTruncated) {
appendSummaryBySeverity(builder);
}
Expand All @@ -66,7 +74,7 @@ public String formatForMarkdown() {
appendExtraIssues(builder, hasInlineIssues, extraIssuesTruncated);
}

return builder.toString();
return builder.appendProjectId(projectKey).toString();
}

private void appendExtraIssues(ReportBuilder builder, boolean hasInlineIssues, boolean extraIssuesTruncated) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/sonar/plugins/github/MarkDownReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.batch.rule.Severity;

Expand Down Expand Up @@ -50,6 +51,15 @@ private IssueHolder(PostJobIssue issue, String gitHubUrl) {
this.markDownUtils = markDownUtils;
}

@Override
public ReportBuilder appendProjectId(String projectId) {
if (!StringUtils.isEmpty(projectId)) {
sb.append(MarkDownUtils.projectId(projectId));
}

return this;
}

@Override
public ReportBuilder append(Object o) {
sb.append(o);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/sonar/plugins/github/MarkDownUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public MarkDownUtils(Settings settings) {
this.ruleUrlPrefix = baseUrl;
}

public static String projectId(String projectId) {
StringBuilder sb = new StringBuilder();
sb.append("\n[project-id]: ").append(projectId);
return sb.toString();
}

public String inlineIssue(Severity severity, String message, String ruleKey) {
String ruleLink = getRuleLink(ruleKey);
StringBuilder sb = new StringBuilder();
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/sonar/plugins/github/PullRequestFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHCommitStatus;
import org.kohsuke.github.GHIssueComment;
Expand Down Expand Up @@ -72,6 +74,7 @@ public class PullRequestFacade {

public PullRequestFacade(GitHubPluginConfiguration config) {
this.config = config;

}

public void init(int pullRequestNumber, File projectBaseDir) {
Expand Down Expand Up @@ -136,8 +139,12 @@ private void loadExistingReviewComments() throws IOException {
// Ignore comments from other users
continue;
}
if (!StringUtils.isEmpty(config.projectKey()) && !comment.getBody().contains(MarkDownUtils.projectId(config.projectKey()))) {
// Ignore comments that don't contain projectId
continue;
}
if (!existingReviewCommentsByLocationByFile.containsKey(comment.getPath())) {
existingReviewCommentsByLocationByFile.put(comment.getPath(), new HashMap<Integer, GHPullRequestReviewComment>());
existingReviewCommentsByLocationByFile.put(comment.getPath(), new HashMap<>());
}
// By default all previous comments will be marked for deletion
reviewCommentToBeDeletedById.put(comment.getId(), comment);
Expand Down Expand Up @@ -255,6 +262,10 @@ private boolean findAndDeleteOthers(@Nullable String markup) throws IOException
boolean found = false;
for (GHIssueComment comment : pr.listComments()) {
if (myself.equals(comment.getUser().getLogin())) {
if (!StringUtils.isEmpty(config.projectKey()) && !comment.getBody().contains(MarkDownUtils.projectId(config.projectKey()))) {
// Ignore comments that don't contain projectId
continue;
}
if (markup == null || found || !markup.equals(comment.getBody())) {
comment.delete();
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;

import org.kohsuke.github.GHCommitState;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
Expand Down Expand Up @@ -61,9 +63,11 @@ public void describe(PostJobDescriptor descriptor) {

@Override
public void execute(PostJobContext context) {
GlobalReport report = new GlobalReport(markDownUtils, gitHubPluginConfiguration.tryReportIssuesInline());
String projectKey = gitHubPluginConfiguration.projectKey();

GlobalReport report = new GlobalReport(markDownUtils, gitHubPluginConfiguration.tryReportIssuesInline(), projectKey);
try {
Map<InputFile, Map<Integer, StringBuilder>> commentsToBeAddedByLine = processIssues(report, context.issues());
Map<InputFile, Map<Integer, StringBuilder>> commentsToBeAddedByLine = processIssues(report, context.issues(), projectKey);

updateReviewComments(commentsToBeAddedByLine);

Expand All @@ -78,7 +82,8 @@ public void execute(PostJobContext context) {
}
}

private Map<InputFile, Map<Integer, StringBuilder>> processIssues(GlobalReport report, Iterable<PostJobIssue> issues) {
private Map<InputFile, Map<Integer, StringBuilder>> processIssues(
GlobalReport report, Iterable<PostJobIssue> issues, @Nullable String projectKey) {
Map<InputFile, Map<Integer, StringBuilder>> commentToBeAddedByFileAndByLine = new HashMap<>();

StreamSupport.stream(issues.spliterator(), false)
Expand All @@ -91,35 +96,43 @@ private Map<InputFile, Map<Integer, StringBuilder>> processIssues(GlobalReport r
pullRequestFacade.hasFile((InputFile) inputComponent);
})
.sorted(ISSUE_COMPARATOR)
.forEach(i -> processIssue(report, commentToBeAddedByFileAndByLine, i));
.forEach(i -> processIssue(report, commentToBeAddedByFileAndByLine, i, projectKey));
return commentToBeAddedByFileAndByLine;

}

private void processIssue(GlobalReport report, Map<InputFile, Map<Integer, StringBuilder>> commentToBeAddedByFileAndByLine, PostJobIssue issue) {
private void processIssue(GlobalReport report, Map<InputFile, Map<Integer, StringBuilder>> commentToBeAddedByFileAndByLine, PostJobIssue issue, @Nullable String projectKey) {
boolean reportedInline = false;
InputComponent inputComponent = issue.inputComponent();
if (gitHubPluginConfiguration.tryReportIssuesInline() && inputComponent != null && inputComponent.isFile()) {
reportedInline = tryReportInline(commentToBeAddedByFileAndByLine, issue, (InputFile) inputComponent);
reportedInline = tryReportInline(commentToBeAddedByFileAndByLine, issue, (InputFile) inputComponent, projectKey);
}
report.process(issue, pullRequestFacade.getGithubUrl(inputComponent, issue.line()), reportedInline);
}

private boolean tryReportInline(Map<InputFile, Map<Integer, StringBuilder>> commentToBeAddedByFileAndByLine, PostJobIssue issue, InputFile inputFile) {
private boolean tryReportInline(Map<InputFile, Map<Integer, StringBuilder>> commentToBeAddedByFileAndByLine, PostJobIssue issue, InputFile inputFile, @Nullable String projectKey) {
Integer lineOrNull = issue.line();
if (lineOrNull != null) {
int line = lineOrNull.intValue();
if (pullRequestFacade.hasFileLine(inputFile, line)) {
String message = issue.message();
String ruleKey = issue.ruleKey().toString();
if (!commentToBeAddedByFileAndByLine.containsKey(inputFile)) {
commentToBeAddedByFileAndByLine.put(inputFile, new HashMap<Integer, StringBuilder>());
commentToBeAddedByFileAndByLine.put(inputFile, new HashMap<>());
}
Map<Integer, StringBuilder> commentsByLine = commentToBeAddedByFileAndByLine.get(inputFile);

if (!commentsByLine.containsKey(line)) {
commentsByLine.put(line, new StringBuilder());
StringBuilder stringBuilder = new StringBuilder();
commentsByLine.put(line, stringBuilder);
}

commentsByLine.get(line).append(markDownUtils.inlineIssue(issue.severity(), message, ruleKey)).append("\n");

if (!StringUtils.isEmpty(projectKey)) {
commentsByLine.get(line).append(MarkDownUtils.projectId(projectKey)).append("\n");
}

return true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/sonar/plugins/github/ReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
import org.sonar.api.batch.rule.Severity;

public interface ReportBuilder {
/**
* Append project id to the report.
*
* @param projectId Project id to append
* @return a reference to this object
*/
ReportBuilder appendProjectId(String projectId);

/**
* Append an object to the report, using its toString() method.
*
Expand Down
Loading