Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#141 Implement updating comment instead of deleting and recreating #142

Closed
wants to merge 13 commits into from
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
*.iml

#Project libs
/sonarqube-lib/
/sonarqube-lib/
*.classpath
*.project
.settings/
bin/
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectPullRequestsLoader;
import com.github.mc1arke.sonarqube.plugin.server.CommunityBranchFeatureExtension;
import com.github.mc1arke.sonarqube.plugin.server.CommunityBranchSupportDelegate;

import java.util.Arrays;
import java.util.stream.Collectors;

import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.PropertyType;
Expand All @@ -37,6 +41,7 @@
import org.sonar.api.resources.Qualifiers;
import org.sonar.core.config.PurgeConstants;
import org.sonar.core.extension.CoreExtension;
import org.sonarqube.ws.Common.Severity;

/**
* @author Michael Clarke
Expand Down Expand Up @@ -121,10 +126,18 @@ public void load(CoreExtension.Context context) {
PropertyDefinition.builder(PullRequestBuildStatusDecorator.PULL_REQUEST_FILE_COMMENT_ENABLED).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT).name("Enable file comment").description("This enables commenting (if implemented).").type(PropertyType.BOOLEAN)
.defaultValue("true").build(),

PropertyDefinition.builder(PullRequestBuildStatusDecorator.PULL_REQUEST_COMMENTS_MIN_SEVERITY).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT).name("Min Comment Severity").description("Issues below this level are not attached as file comments.")
.type(PropertyType.SINGLE_SELECT_LIST).options(Arrays.stream(Severity.values()).map(Severity::name).collect(Collectors.toList())).build(),

PropertyDefinition.builder(PullRequestBuildStatusDecorator.PULL_REQUEST_DELETE_COMMENTS_ENABLED).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT).name("Enable deleting comments").description("This cleans up the comments from previous runs (if implemented).")
.type(PropertyType.BOOLEAN).defaultValue("false").build(),

PropertyDefinition.builder(PullRequestBuildStatusDecorator.PULL_REQUEST_COMPACT_COMMENTS_ENABLED).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT).name("Use compact file comments").description("Uses a compact form of the file comments.").type(PropertyType.BOOLEAN)
.defaultValue("true").build(),

PropertyDefinition.builder(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_URL).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(BITBUCKET_INTEGRATION_SUBCATEGORY_LABEL)
.onQualifiers(Qualifiers.PROJECT).name("URL for Bitbucket (Server or Cloud) instance").description("Example: http://bitbucket.local").type(PropertyType.STRING).build(),
Expand Down Expand Up @@ -176,7 +189,12 @@ public void load(CoreExtension.Context context) {
.name("Repository Slug for the Gitlab (Server or Cloud) instance")
.description("The repository slug can be either in the form of user/repo or it can be the Project ID")
.type(PropertyType.STRING)
.build()
.build(),

PropertyDefinition.builder(GitlabServerPullRequestDecorator.PULLREQUEST_CAN_FAIL_PIPELINE_ENABLED).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GITLAB_INTEGRATION_SUBCATEGORY_LABEL)
.onQualifiers(Qualifiers.PROJECT).name("Fail pipeline if gate not reached").description("Fail the pipeline if the Qualitiy Gate passed succesfully.").type(PropertyType.BOOLEAN)
.defaultValue("true").build()

);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,35 @@ public String createAnalysisSummary(FormatterFactory formatterFactory) {
return formatterFactory.documentFormatter().format(document, formatterFactory);
}

public String createAnalysisIssueSummary(PostAnalysisIssueVisitor.ComponentIssue componentIssue, FormatterFactory formatterFactory) {
public String createAnalysisIssueSummary(PostAnalysisIssueVisitor.ComponentIssue componentIssue, FormatterFactory formatterFactory, boolean compact) {
final DefaultIssue issue = componentIssue.getIssue();

String baseImageUrl = getBaseImageUrl();

Long effort = issue.effortInMinutes();
Node effortNode = (null == effort ? new Text("") : new Paragraph(new Text(String.format("**Duration (min):** %s", effort))));

String resolution = issue.resolution();
Node resolutionNode = (StringUtils.isBlank(resolution) ? new Text("") : new Paragraph(new Text(String.format("**Resolution:** %s ", resolution))));

Document document = new Document(
new Paragraph(new Text(String.format("**Type:** %s ", issue.type().name())), new Image(issue.type().name(), String.format("%s/checks/IssueType/%s.svg?sanitize=true", baseImageUrl, issue.type().name().toLowerCase()))),
new Paragraph(new Text(String.format("**Severity:** %s ", issue.severity())), new Image(issue.severity(), String.format("%s/checks/Severity/%s.svg?sanitize=true", baseImageUrl, issue.severity().toLowerCase()))),
new Paragraph(new Text(String.format("**Message:** %s", issue.getMessage()))),
effortNode,
resolutionNode,
new Link(publicRootURL + "/project/issues?id=" + URLEncoder.encode(project.getKey()) + "&pullRequest=" + branchDetails.getBranchName() + "&issues=" + issue.key() + "&open=" + issue.key(), new Text("View in SonarQube"))
);
Document document;
if (compact) {
document = new Document(
new Image(issue.type().name(), String.format("%s/checks/IssueType/%s.svg?sanitize=true", baseImageUrl, issue.type().name().toLowerCase())),
new Image(issue.severity(), String.format("%s/checks/Severity/%s.svg?sanitize=true", baseImageUrl, issue.severity().toLowerCase())),
new Link(publicRootURL + "/project/issues?id=" + URLEncoder.encode(project.getKey()) + "&pullRequest=" + branchDetails.getBranchName() + "&issues=" + issue.key() + "&open=" + issue.key(), new Text(issue.getRuleKey().rule())),
new Text(String.format(": %s", issue.getMessage()))
);

} else {
Long effort = issue.effortInMinutes();
Node effortNode = (null == effort ? new Text("") : new Paragraph(new Text(String.format("**Duration (min):** %s", effort))));

String resolution = issue.resolution();
Node resolutionNode = (StringUtils.isBlank(resolution) ? new Text("") : new Paragraph(new Text(String.format("**Resolution:** %s ", resolution))));
document = new Document(
new Paragraph(new Text(String.format("**Type:** %s ", issue.type().name())), new Image(issue.type().name(), String.format("%s/checks/IssueType/%s.svg?sanitize=true", baseImageUrl, issue.type().name().toLowerCase()))),
new Paragraph(new Text(String.format("**Severity:** %s ", issue.severity())), new Image(issue.severity(), String.format("%s/checks/Severity/%s.svg?sanitize=true", baseImageUrl, issue.severity().toLowerCase()))),
new Text(String.format("**Message:** %s %s", issue.getRuleKey(), issue.getMessage())),
effortNode,
resolutionNode,
new Link(publicRootURL + "/project/issues?id=" + URLEncoder.encode(project.getKey()) + "&pullRequest=" + branchDetails.getBranchName() + "&issues=" + issue.key() + "&open=" + issue.key(), new Text("View in SonarQube"))
);
}
return formatterFactory.documentFormatter().format(document, formatterFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface PullRequestBuildStatusDecorator {

String PULL_REQUEST_DELETE_COMMENTS_ENABLED = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.delete.comments.enabled";

String PULL_REQUEST_COMMENTS_MIN_SEVERITY = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.comment.minSeverity";

String PULL_REQUEST_COMPACT_COMMENTS_ENABLED = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.comments.compact";

String name();

void decorateQualityGateStatus(AnalysisDetails analysisDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void decorateQualityGateStatus(AnalysisDetails analysisDetails) {
final boolean summaryCommentEnabled = Boolean.parseBoolean(getMandatoryProperty(PULL_REQUEST_COMMENT_SUMMARY_ENABLED, configuration));
final boolean fileCommentEnabled = Boolean.parseBoolean(getMandatoryProperty(PULL_REQUEST_FILE_COMMENT_ENABLED, configuration));
final boolean deleteCommentsEnabled = Boolean.parseBoolean(getMandatoryProperty(PULL_REQUEST_DELETE_COMMENTS_ENABLED, configuration));
final boolean compactCommentsEnabled = Boolean.parseBoolean(getMandatoryProperty(PULL_REQUEST_COMPACT_COMMENTS_ENABLED, configuration));

final String commentUrl;
final String activityUrl;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void decorateQualityGateStatus(AnalysisDetails analysisDetails) {
List<PostAnalysisIssueVisitor.ComponentIssue> componentIssues = analysisDetails.getPostAnalysisIssueVisitor().getIssues().stream().filter(i -> OPEN_ISSUE_STATUSES.contains(i.getIssue().status())).collect(Collectors.toList());
for (PostAnalysisIssueVisitor.ComponentIssue componentIssue : componentIssues) {
final DefaultIssue issue = componentIssue.getIssue();
String analysisIssueSummary = analysisDetails.createAnalysisIssueSummary(componentIssue, new MarkdownFormatterFactory());
String analysisIssueSummary = analysisDetails.createAnalysisIssueSummary(componentIssue, new MarkdownFormatterFactory(), compactCommentsEnabled);
String issuePath = analysisDetails.getSCMPathForIssue(componentIssue).orElse(StringUtils.EMPTY);
int issueLine = issue.getLine() != null ? issue.getLine() : 0;
String issueType = getIssueType(diffPage, issuePath, issueLine);
Expand Down
Loading