From 7fb5a568b97ed05545713517003c9f35fae9a03b Mon Sep 17 00:00:00 2001 From: Michael Clarke Date: Sun, 18 Aug 2024 14:38:12 +0100 Subject: [PATCH] #934: Use static report key for non-monorepo Bitbucket decoration The Bitbucket decoration is currently using the project key to create the analysis report key, but the Sonarqube documentation states this should be a static value across all projects. To ensure that the Bitbucket `Required report` configuration can be created as per the guidance in Sonarqube documentation, the static key is being used where the repository has not been set as a monorepo. --- .../BitbucketPullRequestDecorator.java | 21 +++++++++++-------- .../BitbucketPullRequestDecoratorTest.java | 20 +++++++++++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecorator.java index 47311b9c6..a221eafeb 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecorator.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2023 Mathias Åhsberg, Michael Clarke + * Copyright (C) 2020-2024 Mathias Åhsberg, Michael Clarke * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -64,6 +64,7 @@ public class BitbucketPullRequestDecorator implements PullRequestBuildStatusDeco private static final Logger LOGGER = LoggerFactory.getLogger(BitbucketPullRequestDecorator.class); private static final DecorationResult DEFAULT_DECORATION_RESULT = DecorationResult.builder().build(); + private static final String REPORT_KEY = "com.sonarsource.sonarqube"; private final BitbucketClientFactory bitbucketClientFactory; private final ReportGenerator reportGenerator; @@ -93,9 +94,11 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysisDetail analysisDetails.getQualityGateStatus() == QualityGate.Status.OK ? ReportStatus.PASSED : ReportStatus.FAILED ); - client.uploadReport(analysisDetails.getCommitSha(), codeInsightsReport, analysisDetails.getAnalysisProjectKey()); + String reportKey = Boolean.TRUE.equals(projectAlmSettingDto.getMonorepo()) ? analysisDetails.getAnalysisProjectKey() : REPORT_KEY; - updateAnnotations(client, analysisDetails); + client.uploadReport(analysisDetails.getCommitSha(), codeInsightsReport, reportKey); + + updateAnnotations(client, analysisDetails, reportKey); } catch (IOException e) { LOGGER.error("Could not decorate pull request for project {}", analysisDetails.getAnalysisProjectKey(), e); } @@ -120,10 +123,10 @@ private static List toReport(BitbucketClient client, AnalysisSummary return reportData; } - private void updateAnnotations(BitbucketClient client, AnalysisDetails analysisDetails) throws IOException { + private void updateAnnotations(BitbucketClient client, AnalysisDetails analysisDetails, String reportKey) throws IOException { final AtomicInteger chunkCounter = new AtomicInteger(0); - client.deleteAnnotations(analysisDetails.getCommitSha(), analysisDetails.getAnalysisProjectKey()); + client.deleteAnnotations(analysisDetails.getCommitSha(), reportKey); AnnotationUploadLimit uploadLimit = client.getAnnotationUploadLimit(); @@ -152,7 +155,7 @@ private void updateAnnotations(BitbucketClient client, AnalysisDetails analysisD break; } - client.uploadAnnotations(analysisDetails.getCommitSha(), annotations, analysisDetails.getAnalysisProjectKey()); + client.uploadAnnotations(analysisDetails.getCommitSha(), annotations, reportKey); } 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."); @@ -197,19 +200,19 @@ private static String toBitbucketType(RuleType sonarqubeType) { } } - private static ReportData securityReport(Long vulnerabilities, Long hotspots) { + private static 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 static ReportData reliabilityReport(Long bugs) { + private static ReportData reliabilityReport(long bugs) { String description = bugs == 1 ? "Bug" : "Bugs"; return new ReportData("Reliability", new DataValue.Text(format("%d %s", bugs, description))); } - private static ReportData maintainabilityReport(Long codeSmells) { + private static ReportData maintainabilityReport(long codeSmells) { String description = codeSmells == 1 ? "Code Smell" : "Code Smells"; return new ReportData("Maintainability", new DataValue.Text(format("%d %s", codeSmells, description))); } diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java index 194bd2ef3..e3ef29a57 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/bitbucket/BitbucketPullRequestDecoratorTest.java @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020-2024 Mathias Åhsberg, Michael Clarke + * + * 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; import com.github.mc1arke.sonarqube.plugin.almclient.bitbucket.BitbucketClient; @@ -43,7 +61,7 @@ class BitbucketPullRequestDecoratorTest { private static final String COMMIT = "commit"; - private static final String REPORT_KEY = "report-key"; + private static final String REPORT_KEY = "com.sonarsource.sonarqube"; private static final String ISSUE_KEY = "issue-key"; private static final int ISSUE_LINE = 1;