Skip to content

Commit

Permalink
Remove Bitbucket comments implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
goober committed Apr 27, 2020
1 parent d25e49c commit aae2954
Show file tree
Hide file tree
Showing 30 changed files with 205 additions and 1,591 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.github.mc1arke.sonarqube.plugin.ce.CommunityReportAnalysisComponentProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PullRequestBuildStatusDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.BitbucketServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.server.BitbucketServerCommentsPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v4.GraphqlCheckRunProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab.GitlabServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchConfigurationLoader;
Expand Down Expand Up @@ -137,10 +136,6 @@ public void load(CoreExtension.Context context) {
.onQualifiers(Qualifiers.PROJECT).name("The token for the user to comment to the PR on Bitbucket (Server or Cloud) instance")
.description("Token used for authentication and commenting to your Bitbucket instance").type(PropertyType.STRING).build(),

PropertyDefinition.builder(BitbucketServerCommentsPullRequestDecorator.PULL_REQUEST_BITBUCKET_COMMENT_USER_SLUG)
.category(PULL_REQUEST_CATEGORY_LABEL).subCategory(BITBUCKET_INTEGRATION_SUBCATEGORY_LABEL).onQualifiers(Qualifiers.PROJECT).name("Comment User Slug")
.description("User slug for the comment user. Needed only for comment deletion.").type(PropertyType.STRING).build(),

PropertyDefinition.builder(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_REPOSITORY_SLUG)
.category(PULL_REQUEST_CATEGORY_LABEL).subCategory(BITBUCKET_INTEGRATION_SUBCATEGORY_LABEL).onlyOnQualifiers(Qualifiers.PROJECT).name("Repository Slug").description(
"Repository Slug see for example https://docs.atlassian.com/bitbucket-server/rest/latest/bitbucket-rest.html")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,35 @@
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.AnalysisDetails;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PullRequestBuildStatusDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.UnifyConfiguration;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.server.BitbucketServerCommentsPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.BitbucketServerCodeInsightsPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.BitbucketClient;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.BitbucketException;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.Annotation;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.CreateAnnotationsRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.CreateReportRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.DataValue;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.ReportData;
import org.sonar.api.ce.posttask.QualityGate;
import org.sonar.api.issue.Issue;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.ce.task.projectanalysis.component.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static java.util.stream.Collectors.toSet;

public class BitbucketServerPullRequestDecorator implements PullRequestBuildStatusDecorator {

Expand All @@ -36,26 +63,170 @@ public class BitbucketServerPullRequestDecorator implements PullRequestBuildStat

public static final String PULL_REQUEST_BITBUCKET_USER_SLUG = "sonar.pullrequest.bitbucket.userSlug";

private final BitbucketServerCodeInsightsPullRequestDecorator codeInsightsDecorator;
private final BitbucketServerCommentsPullRequestDecorator commentsDecorator;
private static final Logger LOGGER = Loggers.get(BitbucketServerPullRequestDecorator.class);

private static final int DEFAULT_MAX_ANNOTATIONS = 1000;

private static final List<String> OPEN_ISSUE_STATUSES =
Issue.STATUSES.stream().filter(s -> !Issue.STATUS_CLOSED.equals(s) && !Issue.STATUS_RESOLVED.equals(s))
.collect(Collectors.toList());

private final BitbucketClient client;

public BitbucketServerPullRequestDecorator(BitbucketServerCodeInsightsPullRequestDecorator codeInsightDecorator,
BitbucketServerCommentsPullRequestDecorator commentsDecorator) {
this.codeInsightsDecorator = codeInsightDecorator;
this.commentsDecorator = commentsDecorator;
public BitbucketServerPullRequestDecorator(BitbucketClient client) {
this.client = client;
}

@Override
public String name() {
return "BitbucketServer";
}

public boolean isEnabled() {
return client.isConfigured() && client.supportsCodeInsights();
}

@Override
public void decorateQualityGateStatus(AnalysisDetails analysisDetails, UnifyConfiguration configuration) {
if(codeInsightsDecorator.isEnabled()) {
codeInsightsDecorator.decorateQualityGateStatus(analysisDetails, configuration);
} else {
commentsDecorator.decorateQualityGateStatus(analysisDetails, configuration);
try {
String project = configuration.getRequiredProperty(PULL_REQUEST_BITBUCKET_PROJECT_KEY);

String repo = configuration.getRequiredProperty(PULL_REQUEST_BITBUCKET_REPOSITORY_SLUG);
client.createReport(project, repo,
analysisDetails.getCommitSha(),
toReport(analysisDetails)
);
updateAnnotations(project, repo, analysisDetails);
} catch (IOException e) {
LOGGER.error("Could not decorate pull request for project {}", analysisDetails.getAnalysisProjectKey(), e);
}
}

private CreateReportRequest toReport(AnalysisDetails analysisDetails) {
Map<RuleType, Long> rules = analysisDetails.getRulesCount();

List<ReportData> reportData = new ArrayList<>();
reportData.add(reliabilityReport(rules.get(RuleType.BUG)));
reportData.add(new ReportData("Code coverage", new DataValue.Percentage(newCoverage(analysisDetails))));
reportData.add(securityReport(rules.get(RuleType.VULNERABILITY), rules.get(RuleType.SECURITY_HOTSPOT)));
reportData.add(new ReportData("Duplication", new DataValue.Percentage(newDuplication(analysisDetails))));
reportData.add(maintainabilityReport(rules.get(RuleType.CODE_SMELL)));
reportData.add(new ReportData("Analysis details", new DataValue.Link("Go to SonarQube", analysisDetails.getDashboardUrl())));

return new CreateReportRequest(reportData,
reportDescription(analysisDetails),
"SonarQube",
"SonarQube",
analysisDetails.getAnalysisDate().toInstant(),
analysisDetails.getDashboardUrl(),
format("%s/common/icon.png", analysisDetails.getBaseImageUrl()),
asInsightStatus(analysisDetails.getQualityGateStatus()));
}

private void updateAnnotations(String project, String repo, AnalysisDetails analysisDetails) throws IOException {
final AtomicInteger chunkCounter = new AtomicInteger(0);

client.deleteAnnotations(project, repo, analysisDetails.getCommitSha());

Map<Object, Set<Annotation>> annotationChunks = analysisDetails.getPostAnalysisIssueVisitor().getIssues().stream()
.filter(i -> i.getComponent().getReportAttributes().getScmPath().isPresent())
.filter(i -> i.getComponent().getType() == Component.Type.FILE)
.filter(i -> OPEN_ISSUE_STATUSES.contains(i.getIssue().status()))
.sorted(Comparator.comparing(a -> Severity.ALL.indexOf(a.getIssue().severity())))
.map(componentIssue -> {
String path = componentIssue.getComponent().getReportAttributes().getScmPath().get();
return new Annotation(componentIssue.getIssue().key(),
Optional.ofNullable(componentIssue.getIssue().getLine()).orElse(0),
analysisDetails.getIssueUrl(componentIssue.getIssue().key()),
componentIssue.getIssue().getMessage(),
path,
toBitbucketSeverity(componentIssue.getIssue().severity()),
toBitbucketType(componentIssue.getIssue().type()));
}).collect(Collectors.groupingBy(s -> chunkCounter.getAndIncrement() / DEFAULT_MAX_ANNOTATIONS, toSet()));

for (Set<Annotation> annotations : annotationChunks.values()) {
try {
client.createAnnotations(project, repo, analysisDetails.getCommitSha(), new CreateAnnotationsRequest(annotations));
} catch (BitbucketException e) {
if (e.isError(BitbucketException.PAYLOAD_TOO_LARGE)) {
LOGGER.warn("The annotations will be truncated since the maximum number of annotations for this report has been reached.");
} else {
throw e;
}

}
}
}

private String asInsightStatus(QualityGate.Status status) {
return QualityGate.Status.ERROR.equals(status) ? "FAIL" : "PASS";
}

private String toBitbucketSeverity(String severity) {
if (severity == null) {
return "LOW";
}
switch (severity) {
case Severity.BLOCKER:
case Severity.CRITICAL:
return "HIGH";
case Severity.MAJOR:
return "MEDIUM";
default:
return "LOW";
}
}

private String toBitbucketType(RuleType sonarqubeType) {
switch (sonarqubeType) {
case SECURITY_HOTSPOT:
case VULNERABILITY:
return "VULNERABILITY";
case CODE_SMELL:
return "CODE_SMELL";
case BUG:
return "BUG";
default:
return "";
}
}

private ReportData securityReport(Long vulnerabilities, Long hotspots) {
String vulnerabilityDescription = vulnerabilities == 1 ? "Vulnerability" : "Vulnerabilities";
String hotspotDescription = hotspots == 1 ? "Hotspot" : "Hotspots";
String security = format("%d %s (and %d %s)", vulnerabilities, vulnerabilityDescription, hotspots, hotspotDescription);
return new ReportData("Security", new DataValue.Text(security));
}

private ReportData reliabilityReport(Long bugs) {
String description = bugs == 1 ? "Bug" : "Bugs";
return new ReportData("Reliability", new DataValue.Text(format("%d %s", bugs, description)));
}

private ReportData maintainabilityReport(Long codeSmells) {
String description = codeSmells == 1 ? "Code Smell" : "Code Smells";
return new ReportData("Maintainability", new DataValue.Text(format("%d %s", codeSmells, description)));
}

private String reportDescription(AnalysisDetails details) {
String header = details.getQualityGateStatus() == QualityGate.Status.OK ? "Quality Gate passed" : "Quality Gate failed";
String body = details.getFailedConditions().stream().map(AnalysisDetails::format).map(s -> format("- %s", s)).collect(Collectors.joining("\n"));
return format("%s%n%s", header, body);
}

private BigDecimal newCoverage(AnalysisDetails details) {
return details.findQualityGateCondition(CoreMetrics.NEW_COVERAGE_KEY)
.filter(condition -> condition.getStatus() != QualityGate.EvaluationStatus.NO_VALUE)
.map(QualityGate.Condition::getValue)
.map(BigDecimal::new)
.orElse(BigDecimal.ZERO);
}

private BigDecimal newDuplication(AnalysisDetails details) {
return details.findQualityGateCondition(CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY)
.filter(condition -> condition.getStatus() != QualityGate.EvaluationStatus.NO_VALUE)
.map(QualityGate.Condition::getValue)
.map(BigDecimal::new)
.orElse(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.CreateAnnotationsRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.CreateReportRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.ErrorResponse;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.ServerProperties;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.BitbucketServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.CreateAnnotationsRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.CreateReportRequest;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.ErrorResponse;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.ServerProperties;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client;

import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.ErrorResponse;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model.ErrorResponse;

import java.util.Optional;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import java.io.Serializable;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model;
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.client.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down

This file was deleted.

Loading

0 comments on commit aae2954

Please sign in to comment.