Skip to content

Commit

Permalink
#934: Use static report key for non-monorepo Bitbucket decoration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mc1arke committed Sep 6, 2024
1 parent 405c894 commit 7fb5a56
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -120,10 +123,10 @@ private static List<ReportData> 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();

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7fb5a56

Please sign in to comment.