From aae295446af08b15b254a435189fb8daebd69aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20=C3=85hsberg?= Date: Mon, 27 Apr 2020 11:15:32 +0000 Subject: [PATCH] Remove Bitbucket comments implementation --- .../plugin/CommunityBranchPlugin.java | 5 - .../BitbucketServerPullRequestDecorator.java | 195 ++++++++++- .../client/BitbucketClient.java | 10 +- .../client/BitbucketException.java | 4 +- .../client/model/Annotation.java | 2 +- .../model/CreateAnnotationsRequest.java | 2 +- .../client/model/CreateReportRequest.java | 2 +- .../client/model/DataValue.java | 2 +- .../client/model/ErrorResponse.java | 2 +- .../client/model/ReportData.java | 2 +- .../client/model/ServerProperties.java | 2 +- .../bitbucket/comments/Anchor.java | 58 ---- .../bitbucket/comments/FileComment.java | 45 --- .../bitbucket/comments/SummaryComment.java | 38 --- .../comments/response/activity/Activity.java | 51 --- .../response/activity/ActivityPage.java | 72 ---- .../comments/response/activity/Comment.java | 58 ---- .../comments/response/activity/User.java | 44 --- .../comments/response/diff/Diff.java | 66 ---- .../comments/response/diff/DiffLine.java | 66 ---- .../comments/response/diff/DiffPage.java | 59 ---- .../comments/response/diff/File.java | 66 ---- .../comments/response/diff/Hunk.java | 73 ----- .../comments/response/diff/Segment.java | 52 --- ...ketServerCommentsPullRequestDecorator.java | 310 ------------------ ...erverCodeInsightsPullRequestDecorator.java | 223 ------------- .../plugin/CommunityBranchPluginTest.java | 2 +- ...=> BitbucketPullRequestDecoratorTest.java} | 15 +- ...tbucketServerPullRequestDecoratorTest.java | 39 --- ...erverCommentsPullRequestDecoratorTest.java | 231 ------------- 30 files changed, 205 insertions(+), 1591 deletions(-) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/BitbucketClient.java (97%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/BitbucketException.java (96%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/Annotation.java (98%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/CreateAnnotationsRequest.java (97%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/CreateReportRequest.java (98%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/DataValue.java (98%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/ErrorResponse.java (98%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/ReportData.java (98%) rename src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights => }/client/model/ServerProperties.java (98%) delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/Anchor.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/FileComment.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/SummaryComment.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Activity.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/ActivityPage.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Comment.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/User.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Diff.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffLine.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffPage.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/File.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Hunk.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Segment.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecorator.java delete mode 100644 src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketServerCodeInsightsPullRequestDecorator.java rename src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/{insights/BitbucketCodeInsightsPullRequestDecoratorTest.java => BitbucketPullRequestDecoratorTest.java} (93%) delete mode 100644 src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecoratorTest.java delete mode 100644 src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecoratorTest.java diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPlugin.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPlugin.java index 40527d1b9..344daec4d 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPlugin.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPlugin.java @@ -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; @@ -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") diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecorator.java index 1a5f9ec27..4b2dd8be5 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecorator.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecorator.java @@ -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 { @@ -36,13 +63,18 @@ 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 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 @@ -50,12 +82,151 @@ 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 rules = analysisDetails.getRulesCount(); + + List 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> 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 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); + } } diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketClient.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketClient.java similarity index 97% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketClient.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketClient.java index d392b436c..209768a38 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketClient.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketClient.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketException.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketException.java similarity index 96% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketException.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketException.java index 7312b7c01..392a480d8 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/BitbucketException.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/BitbucketException.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/Annotation.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/Annotation.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/Annotation.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/Annotation.java index 28cba15ea..255fe16ca 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/Annotation.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/Annotation.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateAnnotationsRequest.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateAnnotationsRequest.java similarity index 97% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateAnnotationsRequest.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateAnnotationsRequest.java index 1cba7fdff..d115bea5d 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateAnnotationsRequest.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateAnnotationsRequest.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateReportRequest.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateReportRequest.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateReportRequest.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateReportRequest.java index 74fdc1de1..c39e0259c 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/CreateReportRequest.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/CreateReportRequest.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/DataValue.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/DataValue.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/DataValue.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/DataValue.java index b6ced2b8d..e7ebd1edc 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/DataValue.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/DataValue.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ErrorResponse.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ErrorResponse.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ErrorResponse.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ErrorResponse.java index 69644f771..bbe230b45 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ErrorResponse.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ErrorResponse.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ReportData.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ReportData.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ReportData.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ReportData.java index 59ac72ec9..3039f029f 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ReportData.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ReportData.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ServerProperties.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ServerProperties.java similarity index 98% rename from src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ServerProperties.java rename to src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ServerProperties.java index 06d27ded7..1d5ef8b48 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/client/model/ServerProperties.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/client/model/ServerProperties.java @@ -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; diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/Anchor.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/Anchor.java deleted file mode 100644 index ae155e8fc..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/Anchor.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.io.Serializable; - -public class Anchor implements Serializable { - private final int line; - - private final String lineType; - - private final String path; - - private final String fileType; - - @JsonCreator - public Anchor(@JsonProperty("line") int line, @JsonProperty("lineType") String lineType, @JsonProperty("path") String path, @JsonProperty("fileType") String fileType) { - this.line = line; - this.lineType = lineType; - this.path = path; - this.fileType = fileType; - } - - public int getLine() { - return line; - } - - public String getLineType() { - return lineType; - } - - public String getPath() { - return path; - } - - public String getFileType() { - return fileType; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/FileComment.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/FileComment.java deleted file mode 100644 index b6388a6d9..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/FileComment.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.Anchor; - -public class FileComment implements Serializable { - private final String text; - - private final Anchor anchor; - - @JsonCreator - public FileComment(@JsonProperty("text") String text, @JsonProperty("anchor") Anchor anchor) { - this.text = text; - this.anchor = anchor; - } - - public String getText() { - return text; - } - - public Anchor getAnchor() { - return anchor; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/SummaryComment.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/SummaryComment.java deleted file mode 100644 index 3f2f965eb..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/SummaryComment.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class SummaryComment implements Serializable { - private final String text; - - @JsonCreator - public SummaryComment(@JsonProperty("text") final String text) { - super(); - this.text = text; - } - - public String getText() { - return text; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Activity.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Activity.java deleted file mode 100644 index 4fb0fed61..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Activity.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class Activity implements Serializable { - private final int id; - - private final User user; - - private final Comment comment; - - @JsonCreator - public Activity(@JsonProperty("id") final int id, @JsonProperty("user") final User user, @JsonProperty("comment") final Comment comment) { - this.id = id; - this.user = user; - this.comment = comment; - } - - public int getId() { - return id; - } - - public User getUser() { - return user; - } - - public Comment getComment() { - return comment; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/ActivityPage.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/ActivityPage.java deleted file mode 100644 index 07b05289f..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/ActivityPage.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class ActivityPage implements Serializable { - private final int size; - - private final int limit; - - private final boolean isLastPage; - - private final int start; - - private final int nextPageStart; - - private final Activity[] values; - - @JsonCreator - public ActivityPage(@JsonProperty("size") final int size, @JsonProperty("limit") final int limit, @JsonProperty("isLastPage") final boolean isLastPage, @JsonProperty("start") final int start, @JsonProperty("nextPageStart") final int nextPageStart, @JsonProperty("values") final Activity[] values) { - this.size = size; - this.limit = limit; - this.isLastPage = isLastPage; - this.start = start; - this.nextPageStart = nextPageStart; - this.values = values; - } - - public int getSize() { - return size; - } - - public int getLimit() { - return limit; - } - - public boolean isLastPage() { - return isLastPage; - } - - public int getStart() { - return start; - } - - public int getNextPageStart() { - return nextPageStart; - } - - public Activity[] getValues() { - return values; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Comment.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Comment.java deleted file mode 100644 index 105aaafaf..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/Comment.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class Comment implements Serializable { - private final int id; - - private final int version; - - private final String text; - - private final User author; - - @JsonCreator - public Comment(@JsonProperty("id") final int id, @JsonProperty("version") final int version, @JsonProperty("text") final String text, @JsonProperty("author") final User author) { - this.id = id; - this.version = version; - this.text = text; - this.author = author; - } - - public int getId() { - return id; - } - - public int getVersion() { - return version; - } - - public String getText() { - return text; - } - - public User getAuthor() { - return author; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/User.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/User.java deleted file mode 100644 index 5e4c1136a..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/activity/User.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity; - -import java.io.Serializable; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class User implements Serializable { - private final String name; - - private final String slug; - - @JsonCreator - public User(@JsonProperty("name") final String name, @JsonProperty("slug") final String slug) { - this.name = name; - this.slug = slug; - } - - public String getName() { - return name; - } - - public String getSlug() { - return slug; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Diff.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Diff.java deleted file mode 100644 index ddb2909a2..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Diff.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class Diff implements Serializable { - private final String fromHash; - - private final String toHash; - - private final List hunks; - - private final File source; - - private final File destination; - - @JsonCreator - public Diff(@JsonProperty("fromHash") String fromHash, @JsonProperty("toHash") String toHash, @JsonProperty("hunks") List hunks, @JsonProperty("source") File source, @JsonProperty("destination") File destination) { - this.fromHash = fromHash; - this.toHash = toHash; - this.hunks = hunks; - this.source = source; - this.destination = destination; - } - - public String getFromHash() { - return fromHash; - } - - public String getToHash() { - return toHash; - } - - public List getHunks() { - return hunks; - } - - public File getSource() { - return source; - } - - public File getDestination() { - return destination; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffLine.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffLine.java deleted file mode 100644 index 8983897fe..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffLine.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class DiffLine implements Serializable { - private final int source; - - private final int destination; - - private final String line; - - private final boolean truncated; - - private final List commentIds; - - @JsonCreator - public DiffLine(@JsonProperty("source") int source, @JsonProperty("destination") int destination, @JsonProperty("line") String line, @JsonProperty("truncated") boolean truncated, @JsonProperty("commentIds") List commentIds) { - this.source = source; - this.destination = destination; - this.line = line; - this.truncated = truncated; - this.commentIds = commentIds; - } - - public int getSource() { - return source; - } - - public int getDestination() { - return destination; - } - - public String getLine() { - return line; - } - - public boolean isTruncated() { - return truncated; - } - - public List getCommentIds() { - return commentIds; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffPage.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffPage.java deleted file mode 100644 index cba0b1702..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/DiffPage.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class DiffPage implements Serializable { - private final String fromHash; - - private final String toHash; - - private final boolean truncated; - - private final List diffs; - - @JsonCreator - public DiffPage(@JsonProperty("fromHash") final String fromHash, @JsonProperty("toHash") final String toHash, @JsonProperty("truncated") final boolean truncated, @JsonProperty("diffs") final List diffs) { - this.fromHash = fromHash; - this.toHash = toHash; - this.truncated = truncated; - this.diffs = diffs; - } - - public String getFromHash() { - return fromHash; - } - - public String getToHash() { - return toHash; - } - - public boolean isTruncated() { - return truncated; - } - - public List getDiffs() { - return diffs; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/File.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/File.java deleted file mode 100644 index 4a6d2c634..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/File.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class File implements Serializable { - private final String parent; - - private final String name; - - private final String extension; - - private final String toString; - - private final List components; - - @JsonCreator - public File(@JsonProperty("parent") final String parent, @JsonProperty("name") final String name, @JsonProperty("extension") final String extension, @JsonProperty("toString") final String toString, @JsonProperty("components") final List components) { - this.parent = parent; - this.name = name; - this.extension = extension; - this.toString = toString; - this.components = components; - } - - public String getParent() { - return parent; - } - - public String getName() { - return name; - } - - public String getExtension() { - return extension; - } - - public String getToString() { - return toString; - } - - public List getComponents() { - return components; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Hunk.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Hunk.java deleted file mode 100644 index ebef295ca..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Hunk.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class Hunk implements Serializable { - private final String context; - - private final int sourceLine; - - private final int sourceSpan; - - private final int destinationLine; - - private final int destinationSpan; - - private final List segments; - - @JsonCreator - public Hunk(@JsonProperty("context") final String context, @JsonProperty("sourceLine") final int sourceLine, @JsonProperty("sourceSpan") final int sourceSpan, @JsonProperty("destinationLine") final int destinationLine, @JsonProperty("destinationSpan") final int destinationSpan, @JsonProperty("segments") final List segments) { - this.context = context; - this.sourceLine = sourceLine; - this.sourceSpan = sourceSpan; - this.destinationLine = destinationLine; - this.destinationSpan = destinationSpan; - this.segments = segments; - } - - public String getContext() { - return context; - } - - public int getSourceLine() { - return sourceLine; - } - - public int getSourceSpan() { - return sourceSpan; - } - - public int getDestinationLine() { - return destinationLine; - } - - public int getDestinationSpan() { - return destinationSpan; - } - - public List getSegments() { - return segments; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Segment.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Segment.java deleted file mode 100644 index d13c18b7a..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/response/diff/Segment.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff; - -import java.io.Serializable; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class Segment implements Serializable { - private final String type; - - private final List lines; - - private final boolean truncated; - - @JsonCreator - public Segment(@JsonProperty("type") final String type, @JsonProperty("lines") final List lines, @JsonProperty("truncated") final boolean truncated) { - this.type = type; - this.lines = lines; - this.truncated = truncated; - } - - public String getType() { - return type; - } - - public List getLines() { - return lines; - } - - public boolean isTruncated() { - return truncated; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecorator.java deleted file mode 100644 index 939bdd70a..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecorator.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * Copyright (C) 2019 Oliver Jedinger - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.server; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.UnifyConfiguration; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.AnalysisDetails; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PostAnalysisIssueVisitor; -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.response.activity.Activity; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity.ActivityPage; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.Anchor; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.FileComment; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.SummaryComment; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity.Comment; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.Diff; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.DiffLine; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.DiffPage; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.Hunk; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.Segment; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.MarkdownFormatterFactory; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; -import org.sonar.api.issue.Issue; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.core.issue.DefaultIssue; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -public class BitbucketServerCommentsPullRequestDecorator implements PullRequestBuildStatusDecorator { - - public static final String PULL_REQUEST_BITBUCKET_COMMENT_USER_SLUG = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.bitbucket.comment.userSlug"; - - private static final Logger LOGGER = Loggers.get(BitbucketServerCommentsPullRequestDecorator.class); - private static final List OPEN_ISSUE_STATUSES = - Issue.STATUSES.stream().filter(s -> !Issue.STATUS_CLOSED.equals(s) && !Issue.STATUS_RESOLVED.equals(s)) - .collect(Collectors.toList()); - - private static final String REST_API = "/rest/api/1.0/"; - private static final String USER_PR_API = "users/%s/repos/%s/pull-requests/%s/"; - private static final String PROJECT_PR_API = "projects/%s/repos/%s/pull-requests/%s/"; - private static final String COMMENTS_API = "comments"; - private static final String DIFF_API = "diff"; - private static final String ACTIVITIES = "activities?limit=%s"; - - private static final String FULL_PR_COMMENT_API = "%s" + REST_API + PROJECT_PR_API + COMMENTS_API; - private static final String FULL_PR_COMMENT_USER_API = "%s" + REST_API + USER_PR_API + COMMENTS_API; - - private static final String FULL_PR_ACTIVITIES_API = "%s" + REST_API + PROJECT_PR_API + ACTIVITIES; - private static final String FULL_PR_ACTIVITIES_USER_API = "%s" + REST_API + USER_PR_API + ACTIVITIES; - - private static final String FULL_PR_DIFF_API = "%s" + REST_API + PROJECT_PR_API + DIFF_API; - private static final String FULL_PR_DIFF_USER_API = "%s" + REST_API + USER_PR_API + DIFF_API; - - @Override - public void decorateQualityGateStatus(AnalysisDetails analysisDetails, UnifyConfiguration configuration) { - LOGGER.info("starting to analyze with " + analysisDetails.toString()); - - try { - final String hostURL = configuration.getRequiredServerProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_URL); - final String apiToken = configuration.getRequiredServerProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_TOKEN); - final String repositorySlug = configuration.getRequiredProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_REPOSITORY_SLUG); - final String pullRequestId = analysisDetails.getBranchName(); - final String userSlug = configuration.getProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_USER_SLUG).orElse(StringUtils.EMPTY); - final String projectKey = configuration.getProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_PROJECT_KEY).orElse(StringUtils.EMPTY); - final String commentUserSlug = configuration.getServerProperty(PULL_REQUEST_BITBUCKET_COMMENT_USER_SLUG).orElse(StringUtils.EMPTY); - - final boolean summaryCommentEnabled = Boolean.parseBoolean(configuration.getRequiredServerProperty(PULL_REQUEST_COMMENT_SUMMARY_ENABLED)); - final boolean fileCommentEnabled = Boolean.parseBoolean(configuration.getRequiredServerProperty(PULL_REQUEST_FILE_COMMENT_ENABLED)); - final boolean deleteCommentsEnabled = Boolean.parseBoolean(configuration.getRequiredServerProperty(PULL_REQUEST_DELETE_COMMENTS_ENABLED)); - - final String commentUrl; - final String activityUrl; - final String diffUrl; - if (StringUtils.isNotBlank(userSlug)) { - commentUrl = String.format(FULL_PR_COMMENT_USER_API, hostURL, userSlug, repositorySlug, pullRequestId); - diffUrl = String.format(FULL_PR_DIFF_USER_API, hostURL, userSlug, repositorySlug, pullRequestId); - activityUrl = String.format(FULL_PR_ACTIVITIES_USER_API, hostURL, userSlug, repositorySlug, pullRequestId, 250); - } else if (StringUtils.isNotBlank(projectKey)) { - commentUrl = String.format(FULL_PR_COMMENT_API, hostURL, projectKey, repositorySlug, pullRequestId); - diffUrl = String.format(FULL_PR_DIFF_API, hostURL, projectKey, repositorySlug, pullRequestId); - activityUrl = String.format(FULL_PR_ACTIVITIES_API, hostURL, projectKey, repositorySlug, pullRequestId, 250); - } else { - throw new IllegalStateException(String.format("Property userSlug (%s) for /user repo or projectKey (%s) for /projects repo needs to be set.", BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_USER_SLUG, BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_PROJECT_KEY)); - } - LOGGER.debug(String.format("Comment URL is: %s ", commentUrl)); - LOGGER.debug(String.format("Activity URL is: %s ", activityUrl)); - LOGGER.debug(String.format("Diff URL is: %s ", diffUrl)); - - Map headers = new HashMap<>(); - headers.put("Authorization", String.format("Bearer %s", apiToken)); - headers.put("Accept", "application/json"); - - deleteComments(activityUrl, commentUrl, commentUserSlug, headers, deleteCommentsEnabled); - String analysisSummary = analysisDetails.createAnalysisSummary(new MarkdownFormatterFactory()); - StringEntity summaryCommentEntity = new StringEntity(new ObjectMapper().writeValueAsString(new SummaryComment(analysisSummary)), ContentType.APPLICATION_JSON); - postComment(commentUrl, headers, summaryCommentEntity, summaryCommentEnabled); - - DiffPage diffPage = getPage(diffUrl, headers, DiffPage.class); - List 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 issuePath = analysisDetails.getSCMPathForIssue(componentIssue).orElse(StringUtils.EMPTY); - int issueLine = issue.getLine() != null ? issue.getLine() : 0; - String issueType = getIssueType(diffPage, issuePath, issueLine); - String fileType = "TO"; - if (issueType.equals("CONTEXT")) { - fileType = "FROM"; - } - StringEntity fileCommentEntity = new StringEntity( - new ObjectMapper().writeValueAsString(new FileComment(analysisIssueSummary, new Anchor(issueLine, issueType, issuePath, fileType))), ContentType.APPLICATION_JSON - ); - postComment(commentUrl, headers, fileCommentEntity, fileCommentEnabled); - } - } catch (IOException ex) { - throw new IllegalStateException("Could not decorate Pull Request on Bitbucket Server", ex); - } - - } - - protected String getIssueType(DiffPage diffPage, String issuePath, int issueLine) { - String issueType = "CONTEXT"; - List diffs = diffPage.getDiffs().stream() - .filter(diff -> diff.getDestination() != null) - .filter(diff -> issuePath.equals(diff.getDestination().getToString())) - .collect(Collectors.toList()); - - if (!diffs.isEmpty()) { - for (Diff diff : diffs) { - List hunks = diff.getHunks(); - if (!hunks.isEmpty()) { - issueType = getExtractIssueType(issueLine, issueType, hunks); - } - } - } - return issueType; - } - - private String getExtractIssueType(int issueLine, String issueType, List hunks) { - for (Hunk hunk : hunks) { - List segments = hunk.getSegments(); - for (Segment segment : segments) { - Optional optionalLine = segment.getLines().stream().filter(diffLine -> diffLine.getDestination() == issueLine).findFirst(); - if (optionalLine.isPresent()) { - issueType = segment.getType(); - break; - } - } - } - return issueType; - } - - protected boolean deleteComments(String activityUrl, String commentUrl, String userSlug, Map headers, boolean deleteCommentsEnabled) { - if (!deleteCommentsEnabled) { - return false; - } - if (StringUtils.isEmpty(userSlug)) { - LOGGER.info("No comments deleted cause property comment.userSlug is not set."); - return false; - } - boolean commentsRemoved = false; - final ActivityPage activityPage = getPage(activityUrl, headers, ActivityPage.class); - if (activityPage != null) { - final List commentsToDelete = getCommentsToDelete(userSlug, activityPage); - LOGGER.debug(String.format("Deleting %s comments", commentsToDelete)); - for (Comment comment : commentsToDelete) { - try { - boolean commentDeleted = deleteComment(commentUrl, headers, comment); - if (commentDeleted) { - commentsRemoved = true; - } - } catch (IOException ex) { - LOGGER.error("Could not delete comment from Bitbucket Server", ex); - } - } - - } - return commentsRemoved; - } - - private boolean deleteComment(String commentUrl, Map headers, Comment comment) throws IOException { - boolean commentDeleted = false; - String deleteCommentUrl = commentUrl + "/%s?version=%s"; - HttpDelete httpDelete = new HttpDelete(String.format(deleteCommentUrl, comment.getId(), comment.getVersion())); - LOGGER.debug("delete " + comment.getId() + " " + comment.getVersion()); - for (Map.Entry entry : headers.entrySet()) { - httpDelete.addHeader(entry.getKey(), entry.getValue()); - } - try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) { - HttpResponse deleteResponse = closeableHttpClient.execute(httpDelete); - if (null == deleteResponse) { - LOGGER.error("HttpResponse for deleting comment was null"); - } else if (deleteResponse.getStatusLine().getStatusCode() != 204) { - LOGGER.error(IOUtils.toString(deleteResponse.getEntity().getContent(), StandardCharsets.UTF_8.name())); - LOGGER.error("An error was returned in the response from the Bitbucket API. See the previous log messages for details"); - } else { - LOGGER.debug(String.format("Comment %s version %s deleted", comment.getId(), comment.getVersion())); - commentDeleted = true; - } - } - return commentDeleted; - } - - protected List getCommentsToDelete(String userSlug, ActivityPage activityPage) { - return Arrays.stream(activityPage.getValues()) - .filter(a -> a.getComment() != null) - .filter(a -> a.getComment().getAuthor() != null) - .filter(a -> userSlug.equals(a.getComment().getAuthor().getSlug())) - .map(Activity::getComment) - .collect(Collectors.toList()); - } - - protected T getPage(String diffUrl, Map headers, Class type) { - T page = null; - try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) { - LOGGER.debug(String.format("Getting page %s", type)); - HttpGet httpGet = new HttpGet(diffUrl); - for (Map.Entry entry : headers.entrySet()) { - httpGet.addHeader(entry.getKey(), entry.getValue()); - } - HttpResponse httpResponse = closeableHttpClient.execute(httpGet); - if (null == httpResponse) { - LOGGER.error(String.format("HttpResponse for getting page %s was null", type)); - } else if (httpResponse.getStatusLine().getStatusCode() != 200) { - HttpEntity entity = httpResponse.getEntity(); - LOGGER.error("Error response from Bitbucket: " + IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8.name())); - throw new IllegalStateException(String.format("Error response returned from Bitbucket Server. Expected HTTP Status 200 but got %s", httpResponse.getStatusLine().getStatusCode()) ); - } else { - HttpEntity entity = httpResponse.getEntity(); - page = new ObjectMapper() - .configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true) - .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - .readValue(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8.name()), type); - LOGGER.debug(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(page)); - } - } catch (IOException ex) { - LOGGER.error(String.format("Could not get %s from Bitbucket Server", type.getName()), ex); - } - return type.cast(page); - } - - protected boolean postComment(String commentUrl, Map headers, StringEntity requestEntity, boolean sendRequest) throws IOException { - boolean commentPosted = false; - HttpPost httpPost = new HttpPost(commentUrl); - for (Map.Entry entry : headers.entrySet()) { - httpPost.addHeader(entry.getKey(), entry.getValue()); - } - httpPost.setEntity(requestEntity); - LOGGER.debug(EntityUtils.toString(requestEntity)); - if (sendRequest) { - try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) { - HttpResponse httpResponse = closeableHttpClient.execute(httpPost); - if (null == httpResponse) { - LOGGER.error("HttpResponse for posting comment was null"); - } else if (httpResponse.getStatusLine().getStatusCode() != 201) { - HttpEntity entity = httpResponse.getEntity(); - LOGGER.error(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8.name())); - } else { - HttpEntity entity = httpResponse.getEntity(); - LOGGER.debug(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8.name())); - commentPosted = true; - } - } - } - return commentPosted; - } - - @Override - public String name() { - return "BitbucketServer Comments"; - } -} diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketServerCodeInsightsPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketServerCodeInsightsPullRequestDecorator.java deleted file mode 100644 index da6ed7376..000000000 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketServerCodeInsightsPullRequestDecorator.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * Copyright (C) 2020 Mathias Ã…hsberg - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights; - -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.BitbucketServerPullRequestDecorator; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.BitbucketClient; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.BitbucketException; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.Annotation; -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.DataValue; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.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 BitbucketServerCodeInsightsPullRequestDecorator implements PullRequestBuildStatusDecorator { - - private static final Logger LOGGER = Loggers.get(BitbucketServerCodeInsightsPullRequestDecorator.class); - - private static final int DEFAULT_MAX_ANNOTATIONS = 1000; - - private static final List 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 BitbucketServerCodeInsightsPullRequestDecorator(BitbucketClient client) { - this.client = client; - } - - @Override - public String name() { - return "BitbucketServer Code Insights"; - } - - public boolean isEnabled() { - return client.isConfigured() && client.supportsCodeInsights(); - } - - @Override - public void decorateQualityGateStatus(AnalysisDetails analysisDetails, UnifyConfiguration configuration) { - try { - String project = configuration.getRequiredProperty(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_PROJECT_KEY); - - String repo = configuration.getRequiredProperty(BitbucketServerPullRequestDecorator.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 rules = analysisDetails.getRulesCount(); - - List 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> 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 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); - } -} diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPluginTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPluginTest.java index b77039317..aaf46631e 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPluginTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPluginTest.java @@ -119,7 +119,7 @@ public void testServerSideLoad() { final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Object.class); verify(context, times(2)).addExtensions(argumentCaptor.capture(), argumentCaptor.capture()); - assertEquals(22, argumentCaptor.getAllValues().size()); + assertEquals(21, argumentCaptor.getAllValues().size()); assertEquals(Arrays.asList(CommunityBranchFeatureExtension.class, CommunityBranchSupportDelegate.class), argumentCaptor.getAllValues().subList(0, 2)); diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketCodeInsightsPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java similarity index 93% rename from src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketCodeInsightsPullRequestDecoratorTest.java rename to src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java index 4c4639045..736ef8e10 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/insights/BitbucketCodeInsightsPullRequestDecoratorTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java @@ -1,13 +1,12 @@ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights; +package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket; import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.AnalysisDetails; import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PostAnalysisIssueVisitor; import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.UnifyConfiguration; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.BitbucketServerPullRequestDecorator; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.BitbucketClient; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.insights.client.model.Annotation; -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.client.BitbucketClient; +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 org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,7 +38,7 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) -public class BitbucketCodeInsightsPullRequestDecoratorTest { +public class BitbucketPullRequestDecoratorTest { @Captor ArgumentCaptor reportCaptor; @@ -64,7 +63,7 @@ public class BitbucketCodeInsightsPullRequestDecoratorTest { private BitbucketClient client = mock(BitbucketClient.class); - private BitbucketServerCodeInsightsPullRequestDecorator underTest = new BitbucketServerCodeInsightsPullRequestDecorator(client); + private BitbucketServerPullRequestDecorator underTest = new BitbucketServerPullRequestDecorator(client); @Before public void setUp() { diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecoratorTest.java deleted file mode 100644 index 7916b07d6..000000000 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketServerPullRequestDecoratorTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket; - -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.AnalysisDetails; -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 org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; - -public class BitbucketServerPullRequestDecoratorTest { - - private AnalysisDetails analysisDetails = mock(AnalysisDetails.class); - private UnifyConfiguration unifyConfiguration = mock(UnifyConfiguration.class); - - BitbucketServerCommentsPullRequestDecorator comments = mock(BitbucketServerCommentsPullRequestDecorator.class); - BitbucketServerCodeInsightsPullRequestDecorator insights = mock(BitbucketServerCodeInsightsPullRequestDecorator.class); - - BitbucketServerPullRequestDecorator underTest = new BitbucketServerPullRequestDecorator(insights, comments); - - @Test - public void testName() { - assertThat(underTest.name()).isEqualTo("BitbucketServer"); - } - - @Test - public void testInsightsDecoratorWhenSupported() { - when(insights.isEnabled()).thenReturn(true); - - underTest.decorateQualityGateStatus(analysisDetails, unifyConfiguration); - - verify(insights).decorateQualityGateStatus(analysisDetails,unifyConfiguration); - verifyNoInteractions(comments); - } -} diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecoratorTest.java deleted file mode 100644 index 0c5e4e775..000000000 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/comments/server/BitbucketServerCommentsPullRequestDecoratorTest.java +++ /dev/null @@ -1,231 +0,0 @@ -package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.server; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.SummaryComment; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.activity.ActivityPage; -import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.comments.response.diff.DiffPage; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.apache.commons.io.FileUtils; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.mockito.InjectMocks; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsNull.notNullValue; -import static org.hamcrest.core.IsNull.nullValue; -import static org.hamcrest.core.Is.is; - -public class BitbucketServerCommentsPullRequestDecoratorTest { - - @Rule - public final WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8089)); - - @InjectMocks - private BitbucketServerCommentsPullRequestDecorator bitbucketServerCommentsPullRequestDecorator; - - private Map headers; - - /** - * configure these settings if you want to trigger your server instead of the test - * APITOKEN: use a real api token - * ACTIVITYURL: use for your bitbucket url (http://localhost:7990/rest/api/1.0/users/repo.owner/repos/testrepo/pull-requests/1/activities) - */ - private static final String APITOKEN = "APITOKEN"; - - private static final String ACTIVITYURL = "http://localhost:8089/activities"; - - private static final String DIFFURL = "http://localhost:8089/diff"; - - private static final String COMMENTURL = "http://localhost:8089/comments"; - - @Before - public void setUp() { - bitbucketServerCommentsPullRequestDecorator = new BitbucketServerCommentsPullRequestDecorator(); - - headers = new HashMap<>(); - headers.put("Authorization", String.format("Bearer %s", APITOKEN)); - headers.put("Accept", "application/json"); - } - - @Test - public void getPageActivityClass() throws Exception { - stubFor( - get(urlEqualTo("/activities")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn(aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody("") - ) - ); - assertThat(bitbucketServerCommentsPullRequestDecorator.getPage(ACTIVITYURL, headers, ActivityPage.class), nullValue()); - - stubFor( - get(urlEqualTo("/activities")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/activity.json"))) - ) - ); - ActivityPage activityPage = bitbucketServerCommentsPullRequestDecorator.getPage(ACTIVITYURL, headers, ActivityPage.class); - assertThat(activityPage, notNullValue()); - assertThat(activityPage.getSize(), is(3)); - } - - @Test(expected = IllegalStateException.class) - public void getPageActivityClassError() { - - stubFor( - get(urlEqualTo("/activities")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(400) - .withHeader("Content-Type", "application/json") - .withBody("{}") - ) - ); - bitbucketServerCommentsPullRequestDecorator.getPage(ACTIVITYURL, headers, ActivityPage.class); - } - - @Test - public void getPageDiffClass() throws Exception { - stubFor( - get(urlEqualTo("/diff")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/diff.json"))) - ) - ); - DiffPage page = bitbucketServerCommentsPullRequestDecorator.getPage(DIFFURL, headers, DiffPage.class); - assertThat(page, notNullValue()); - assertThat(page.getDiffs().size(), is(1)); - } - - @Test - public void getCommentsToDelete() throws Exception { - ActivityPage activityPage = new ObjectMapper().readValue(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/delete/activityPageCase1.json")), ActivityPage.class); - assertThat(bitbucketServerCommentsPullRequestDecorator.getCommentsToDelete("susi.sonar", activityPage).size() , is(0)); - - activityPage = new ObjectMapper().readValue(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/delete/activityPageCase2.json")), ActivityPage.class); - assertThat(bitbucketServerCommentsPullRequestDecorator.getCommentsToDelete("susi.sonar", activityPage).size() , is(0)); - - activityPage = new ObjectMapper().readValue(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/delete/activityPageCase3.json")), ActivityPage.class); - assertThat(bitbucketServerCommentsPullRequestDecorator.getCommentsToDelete("susi.sonar", activityPage).size() , is(0)); - - activityPage = new ObjectMapper().readValue(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/delete/activityPageCase4.json")), ActivityPage.class); - assertThat(bitbucketServerCommentsPullRequestDecorator.getCommentsToDelete("susi.sonar", activityPage).size() , is(1)); - } - - @Test - public void deleteComments() throws Exception { - assertThat(bitbucketServerCommentsPullRequestDecorator.deleteComments(ACTIVITYURL, COMMENTURL, "susi.sonar", headers, false), is(false)); - - stubFor( - get(urlEqualTo("/activities")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/activity.json"))) - ) - ); - - stubFor( - delete(urlMatching("/comments/([0-9]*)\\?version=([0-9]*)")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(404) - .withHeader("Content-Type", "application/json") - .withBody("{}") - ) - ); - - assertThat(bitbucketServerCommentsPullRequestDecorator.deleteComments(ACTIVITYURL, COMMENTURL, "susi.sonar", headers, true), is(false)); - - stubFor( - delete(urlMatching("/comments/([0-9]*)\\?version=([0-9]*)")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(204) - .withHeader("Content-Type", "application/json") - .withBody("{}") - ) - ); - assertThat(bitbucketServerCommentsPullRequestDecorator.deleteComments(ACTIVITYURL, COMMENTURL, "susi.sonar", headers, true), is(true)); - } - - @Test - public void getIssueType() throws Exception{ - stubFor( - get(urlEqualTo("/diff")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(FileUtils.readFileToByteArray(new File("src/test/resources/bitbucket/diff.json"))) - ) - ); - DiffPage diffPage = bitbucketServerCommentsPullRequestDecorator.getPage(DIFFURL, headers, DiffPage.class); - - // wrong file - String issueType = bitbucketServerCommentsPullRequestDecorator.getIssueType(diffPage, "src/DoesNotExist.java", 15); - assertThat(issueType, is("CONTEXT")); - - // line not within diff - issueType = bitbucketServerCommentsPullRequestDecorator.getIssueType(diffPage, "src/com/sonar/sample/classes/ClassWithInvalidMethodName.java", 0); - assertThat(issueType, is("CONTEXT")); - - issueType = bitbucketServerCommentsPullRequestDecorator.getIssueType(diffPage, "src/com/sonar/sample/classes/ClassWithInvalidMethodName.java", 15); - assertThat(issueType, is("ADDED")); - } - - @Test - public void postComment() throws Exception{ - StringEntity summaryComment = new StringEntity(new ObjectMapper().writeValueAsString(new SummaryComment("summaryComment")), ContentType.APPLICATION_JSON); - assertThat(bitbucketServerCommentsPullRequestDecorator.postComment(COMMENTURL, headers, summaryComment, false), is(false)); - - stubFor( - post(urlEqualTo("/comments")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(400) - .withHeader("Content-Type", "application/json") - .withBody("{}") - ) - ); - assertThat(bitbucketServerCommentsPullRequestDecorator.postComment(COMMENTURL, headers, summaryComment, true), is(false)); - - stubFor( - post(urlEqualTo("/comments")) - .withHeader("Accept" , equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(201) - .withHeader("Content-Type", "application/json") - .withBody("{}") - ) - ); - assertThat(bitbucketServerCommentsPullRequestDecorator.postComment(COMMENTURL, headers, summaryComment, true), is(true)); - } -}