Skip to content

Commit

Permalink
#704: Add support for Sonarqube 9.8
Browse files Browse the repository at this point in the history
The retrieval of the list of pull requests has been removed from the
scanner engine, as well as the issue metrics on the Pull Request web
service. The scanner has therefore been altered to implement the updated
APIs and drop the classes that implemented the removed interfaces, and
the Pull Request web service has had the gathering and population of the
issue metrics removed.
  • Loading branch information
mc1arke committed Dec 27, 2022
1 parent a1f28e5 commit 4dc494d
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 446 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ repositories {
}
}

def sonarqubeVersion = '9.7.0.61563'
def sonarqubeVersion = '9.8.0.63668'
def sonarqubeLibDir = "${projectDir}/sonarqube-lib"
def sonarLibraries = "${sonarqubeLibDir}/sonarqube-${sonarqubeVersion}/lib"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchConfigurationLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchParamsValidator;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectBranchesLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectPullRequestsLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.ScannerPullRequestPropertySensor;
import com.github.mc1arke.sonarqube.plugin.scanner.autoconfiguration.AzureDevopsAutoConfigurer;
import com.github.mc1arke.sonarqube.plugin.scanner.autoconfiguration.BitbucketPipelinesAutoConfigurer;
Expand Down Expand Up @@ -137,7 +136,7 @@ public void load(CoreExtension.Context context) {
.category(CoreProperties.CATEGORY_HOUSEKEEPING)
.subCategory(CoreProperties.SUBCATEGORY_BRANCHES_AND_PULL_REQUESTS)
.multiValues(true)
.defaultValue("master,develop,trunk")
.defaultValue("main,master,develop,trunk")
.onQualifiers(Qualifiers.PROJECT)
.index(2)
.build()
Expand All @@ -164,7 +163,7 @@ public void load(CoreExtension.Context context) {
@Override
public void define(Plugin.Context context) {
if (SonarQubeSide.SCANNER == context.getRuntime().getSonarQubeSide()) {
context.addExtensions(CommunityProjectBranchesLoader.class, CommunityProjectPullRequestsLoader.class,
context.addExtensions(CommunityProjectBranchesLoader.class,
CommunityBranchConfigurationLoader.class, CommunityBranchParamsValidator.class,
ScannerPullRequestPropertySensor.class, BranchConfigurationFactory.class,
AzureDevopsAutoConfigurer.class, BitbucketPipelinesAutoConfigurer.class,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.sonar.scanner.scan.branch.BranchConfigurationLoader;
import org.sonar.scanner.scan.branch.DefaultBranchConfiguration;
import org.sonar.scanner.scan.branch.ProjectBranches;
import org.sonar.scanner.scan.branch.ProjectPullRequests;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -70,8 +69,7 @@ public CommunityBranchConfigurationLoader(System2 system2, AnalysisWarnings anal
}

@Override
public BranchConfiguration load(Map<String, String> localSettings, ProjectBranches projectBranches,
ProjectPullRequests pullRequests) {
public BranchConfiguration load(Map<String, String> localSettings, ProjectBranches projectBranches) {
List<String> nonEmptyParameters = localSettings.entrySet().stream()
.filter(e -> StringUtils.isNotEmpty(e.getValue()))
.map(Map.Entry::getKey)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
Expand All @@ -47,8 +46,6 @@
import org.sonar.db.project.ProjectDto;
import org.sonar.db.protobuf.DbProjectBranches;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.issue.index.PrStatistics;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsUtils;
import org.sonarqube.ws.ProjectPullRequests;
Expand All @@ -59,18 +56,16 @@
public class ListAction extends ProjectWsAction {

private final UserSession userSession;
private final IssueIndex issueIndex;
private final ProtoBufWriter protoBufWriter;

@Autowired
public ListAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, IssueIndex issueIndex) {
this(dbClient, componentFinder, userSession, issueIndex, WsUtils::writeProtobuf);
public ListAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
this(dbClient, componentFinder, userSession, WsUtils::writeProtobuf);
}

ListAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, IssueIndex issueIndex, ProtoBufWriter protoBufWriter) {
ListAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, ProtoBufWriter protoBufWriter) {
super("list", dbClient, componentFinder);
this.userSession = userSession;
this.issueIndex = issueIndex;
this.protoBufWriter = protoBufWriter;
}

Expand All @@ -96,8 +91,6 @@ public void handleProjectRequest(ProjectDto project, Request request, Response r
.collect(Collectors.toList()))
.stream().collect(MoreCollectors.uniqueIndex(BranchDto::getUuid));

Map<String, PrStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream()
.collect(MoreCollectors.uniqueIndex(PrStatistics::getBranchUuid, Function.identity()));
Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = getDbClient().liveMeasureDao()
.selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, List.of(CoreMetrics.ALERT_STATUS_KEY)).stream()
.collect(MoreCollectors.uniqueIndex(LiveMeasureDto::getComponentUuid));
Expand All @@ -106,7 +99,7 @@ public void handleProjectRequest(ProjectDto project, Request request, Response r

ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
pullRequests
.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()),
.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()),
analysisDateByBranchUuid.get(b.getUuid())));
protoBufWriter.write(protobufResponse.build(), request, response);
}
Expand All @@ -121,7 +114,7 @@ private static void checkPermission(ProjectDto project, UserSession userSession)
}

private static void addPullRequest(ProjectPullRequests.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid,
@Nullable LiveMeasureDto qualityGateMeasure, PrStatistics prStatistics, @Nullable String analysisDate) {
@Nullable LiveMeasureDto qualityGateMeasure, @Nullable String analysisDate) {
Optional<BranchDto> mergeBranch = Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid()));

ProjectPullRequests.PullRequest.Builder builder = ProjectPullRequests.PullRequest.newBuilder();
Expand All @@ -146,18 +139,15 @@ private static void addPullRequest(ProjectPullRequests.ListWsResponse.Builder re
}

Optional.ofNullable(analysisDate).ifPresent(builder::setAnalysisDate);
setQualityGate(builder, qualityGateMeasure, prStatistics);
setQualityGate(builder, qualityGateMeasure);
response.addPullRequests(builder);
}

private static void setQualityGate(ProjectPullRequests.PullRequest.Builder builder, @Nullable LiveMeasureDto qualityGateMeasure, @Nullable PrStatistics prStatistics) {
private static void setQualityGate(ProjectPullRequests.PullRequest.Builder builder, @Nullable LiveMeasureDto qualityGateMeasure) {
ProjectPullRequests.Status.Builder statusBuilder = ProjectPullRequests.Status.newBuilder();
if (qualityGateMeasure != null) {
Optional.ofNullable(qualityGateMeasure.getDataAsString()).ifPresent(statusBuilder::setQualityGateStatus);
}
statusBuilder.setBugs(prStatistics == null ? 0L : prStatistics.getBugs());
statusBuilder.setVulnerabilities(prStatistics == null ? 0L : prStatistics.getVulnerabilities());
statusBuilder.setCodeSmells(prStatistics == null ? 0L : prStatistics.getCodeSmells());
builder.setStatus(statusBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchConfigurationLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchParamsValidator;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectBranchesLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectPullRequestsLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.ScannerPullRequestPropertySensor;
import com.github.mc1arke.sonarqube.plugin.server.CommunityBranchFeatureExtension;
import com.github.mc1arke.sonarqube.plugin.server.CommunityBranchSupportDelegate;

Expand All @@ -63,8 +63,8 @@ void shouldDefineClassesForScannerSide() {
.addExtensions(argumentCaptor.capture(), argumentCaptor.capture(), argumentCaptor.capture());


assertThat(argumentCaptor.getAllValues().subList(0, 4)).isEqualTo(Arrays.asList(CommunityProjectBranchesLoader.class, CommunityProjectPullRequestsLoader.class,
CommunityBranchConfigurationLoader.class, CommunityBranchParamsValidator.class));
assertThat(argumentCaptor.getAllValues().subList(0, 4)).isEqualTo(Arrays.asList(CommunityProjectBranchesLoader.class,
CommunityBranchConfigurationLoader.class, CommunityBranchParamsValidator.class, ScannerPullRequestPropertySensor.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ReportGeneratorTest {
"98, 90.svg?sanitize=true, 1, 3.svg?sanitize=true",
",NoCoverageInfo.svg?sanitize=true,,NoDuplicationInfo.svg?sanitize=true"})
@ParameterizedTest
void shouldProduceCorrectAnlysisSummary(String coverage, String coverageImage, String duplications, String duplicationsImage) {
void shouldProduceCorrectAnalysisSummary(String coverage, String coverageImage, String duplications, String duplicationsImage) {
AnalysisDetails analysisDetails = mock(AnalysisDetails.class);
doReturn("5").when(analysisDetails).getPullRequestId();
doReturn("projectKey").when(analysisDetails).getAnalysisProjectKey();
Expand Down
Loading

0 comments on commit 4dc494d

Please sign in to comment.