Skip to content

Commit

Permalink
#801: Add support for Sonarqube 10.2
Browse files Browse the repository at this point in the history
The `checkProjectPermission` on Sonarqube's UserSession has been
replaced with `hasEntityPermission`, and the `mainBranchProjectUuid`
has been dropped from ComponentDTO, which has required a fix to set the
right UUID as the project UUID for the branch. Additionally, the
`MoreCollectors` map/identity collectors have been dropped from
Sonarqube core, so their references have been replaced with equivalent
`toMap` collectors from the JRE.
  • Loading branch information
mc1arke committed Dec 31, 2023
1 parent 851127d commit 531f76a
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The Sonarqube base image. 'latest' if building locally, '8.5-community' if targeting a specific version
SONARQUBE_VERSION=10.1-community
SONARQUBE_VERSION=10.2-community

# The name of the Dockerfile to run. 'Dockerfile' is building locally, 'release.Dockerfile' if building the release image
DOCKERFILE=Dockerfile
Expand Down
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 = '10.1.0.73491'
def sonarqubeVersion = '10.2.1.78527'
def sonarqubeLibDir = "${projectDir}/sonarqube-lib"
def sonarLibraries = "${sonarqubeLibDir}/sonarqube-${sonarqubeVersion}/lib"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Michael Clarke
* Copyright (C) 2020-2023 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 @@ -119,7 +119,7 @@ private static Branch createBranch(DbClient dbClient, String branchName, String

private static Optional<BranchDto> findBranchByUuid(String projectUuid, DbClient dbClient) {
try (DbSession dbSession = dbClient.openSession(false)) {
return dbClient.branchDao().selectByUuid(dbSession, projectUuid);
return dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, projectUuid);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ public ComponentDto createBranchComponent(DbSession dbSession, BranchSupport.Com
.setUuid(branchUuid)
.setBranchUuid(branchUuid)
.setUuidPath(ComponentDto.UUID_PATH_OF_ROOT)
.setMainBranchProjectUuid(mainComponentDto.uuid())
.setCreatedAt(new Date(clock.millis()));
dbClient.componentDao().insert(dbSession, componentDto, false);

BranchDto branchDto = new BranchDto()
.setProjectUuid(mainComponentDto.uuid())
.setProjectUuid(mainComponentBranchDto.getProjectUuid())
.setUuid(branchUuid);
componentKey.getPullRequestKey().ifPresent(pullRequestKey -> branchDto.setBranchType(BranchType.PULL_REQUEST)
.setExcludeFromPurge(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2022 Michael Clarke
* Copyright (C) 2020-2023 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 @@ -69,7 +69,7 @@ public void handle(Request request, Response response) {

try (DbSession dbSession = dbClient.openSession(false)) {
ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
userSession.checkProjectPermission(permission, project);
userSession.hasEntityPermission(permission, project);
handleProjectRequest(project, request, response, dbSession);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2022 SonarSource SA (mailto:info AT sonarsource DOT com), Michael Clarke
* Copyright (C) 2009-2023 SonarSource SA (mailto:info AT sonarsource DOT com), 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 @@ -53,8 +53,7 @@ protected void configureAction(WebService.NewAction action) {

@Override
public void handleProjectRequest(ProjectDto project, Request request, Response response, DbSession dbSession) {
userSession.checkLoggedIn()
.checkProjectPermission(UserRole.ADMIN, project);
userSession.checkLoggedIn().hasEntityPermission(UserRole.ADMIN, project);

String pullRequestId = request.mandatoryParam(PULL_REQUEST_PARAMETER);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2022 SonarSource SA (mailto:info AT sonarsource DOT com), Michael Clarke
* Copyright (C) 2009-2023 SonarSource SA (mailto:info AT sonarsource DOT com), 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 All @@ -23,6 +23,7 @@
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 @@ -34,7 +35,6 @@
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.web.UserRole;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.BranchDao;
Expand Down Expand Up @@ -89,13 +89,13 @@ public void handleProjectRequest(ProjectDto project, Request request, Response r
.map(BranchDto::getMergeBranchUuid)
.filter(Objects::nonNull)
.collect(Collectors.toList()))
.stream().collect(MoreCollectors.uniqueIndex(BranchDto::getUuid));
.stream().collect(Collectors.toMap(BranchDto::getUuid, Function.identity()));

Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = getDbClient().liveMeasureDao()
.selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, List.of(CoreMetrics.ALERT_STATUS_KEY)).stream()
.collect(MoreCollectors.uniqueIndex(LiveMeasureDto::getComponentUuid));
.collect(Collectors.toMap(LiveMeasureDto::getComponentUuid, Function.identity()));
Map<String, String> analysisDateByBranchUuid = getDbClient().snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream()
.collect(MoreCollectors.uniqueIndex(SnapshotDto::getComponentUuid, s -> DateUtils.formatDateTime(s.getCreatedAt())));
.collect(Collectors.toMap(SnapshotDto::getUuid, s -> DateUtils.formatDateTime(s.getCreatedAt())));

ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
pullRequests
Expand All @@ -105,8 +105,8 @@ public void handleProjectRequest(ProjectDto project, Request request, Response r
}

private static void checkPermission(ProjectDto project, UserSession userSession) {
if (userSession.hasProjectPermission(UserRole.USER, project) ||
userSession.hasProjectPermission(UserRole.SCAN, project) ||
if (userSession.hasEntityPermission(UserRole.USER, project) ||
userSession.hasEntityPermission(UserRole.SCAN, project) ||
userSession.hasPermission(GlobalPermission.SCAN)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Michael Clarke
* Copyright (C) 2020-2023 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 @@ -91,7 +91,7 @@ public void testNoBranchDetailsExistingBranchMatch() {
when(branchDto.isMain()).thenReturn(false);

BranchDao branchDao = mock(BranchDao.class);
when(branchDao.selectByUuid(any(), any())).thenReturn(Optional.of(branchDto));
when(branchDao.selectMainBranchByProjectUuid(any(), any())).thenReturn(Optional.of(branchDto));

ScannerReport.Metadata metadata = ScannerReport.Metadata.getDefaultInstance();
when(dbClient.branchDao()).thenReturn(branchDao);
Expand All @@ -118,7 +118,7 @@ public void testNoBranchDetailsExistingBranchMatch() {
verify(dbClient).openSession(anyBoolean());
verifyNoMoreInteractions(dbClient);

verify(branchDao).selectByUuid(any(), any());
verify(branchDao).selectMainBranchByProjectUuid(any(), any());
verifyNoMoreInteractions(branchDao);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2022 Michael Clarke
* Copyright (C) 2020-2023 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 @@ -174,15 +174,13 @@ void shouldOnlyReturnQualityGateConditionsInErrorState() {
QualityGate.Condition condition3 = mock(QualityGate.Condition.class);
when(condition3.getStatus()).thenReturn(QualityGate.EvaluationStatus.NO_VALUE);
QualityGate.Condition condition4 = mock(QualityGate.Condition.class);
when(condition4.getStatus()).thenReturn(QualityGate.EvaluationStatus.WARN);
QualityGate.Condition condition5 = mock(QualityGate.Condition.class);
when(condition5.getStatus()).thenReturn(QualityGate.EvaluationStatus.ERROR);
when(condition4.getStatus()).thenReturn(QualityGate.EvaluationStatus.ERROR);

when(qualityGate.getConditions()).thenReturn(List.of(condition1, condition2, condition3, condition4, condition5));
when(qualityGate.getConditions()).thenReturn(List.of(condition1, condition2, condition3, condition4));

AnalysisDetails underTest = new AnalysisDetails("pullRequest", "commit", List.of(), qualityGate, mock(PostProjectAnalysisTask.ProjectAnalysis.class));

assertThat(underTest.findFailedQualityGateConditions()).isEqualTo(List.of(condition2, condition5));
assertThat(underTest.findFailedQualityGateConditions()).isEqualTo(List.of(condition2, condition4));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ void shouldCreateComponentAndBranchDtoIfValidationPasses(String branchName, Stri
when(copyComponentDto.setKey(any())).thenReturn(copyComponentDto);
when(copyComponentDto.setUuidPath(any())).thenReturn(copyComponentDto);
when(copyComponentDto.setUuid(any())).thenReturn(copyComponentDto);
when(copyComponentDto.setMainBranchProjectUuid(any())).thenReturn(copyComponentDto);
when(copyComponentDto.setCreatedAt(any())).thenReturn(copyComponentDto);

BranchDto branchDto = mock(BranchDto.class);
when(branchDto.getUuid()).thenReturn("componentUuid");
when(branchDto.getProjectUuid()).thenReturn("projectUuid");
when(branchDto.getKey()).thenReturn("nonDummy");

when(clock.millis()).thenReturn(12345678901234L);
Expand All @@ -186,7 +185,6 @@ void shouldCreateComponentAndBranchDtoIfValidationPasses(String branchName, Stri
verify(componentDao).insert(dbSession, copyComponentDto, false);
verify(copyComponentDto).setUuid("uuid0");
verify(copyComponentDto).setUuidPath(".");
verify(copyComponentDto).setMainBranchProjectUuid("componentUuid");
verify(copyComponentDto).setCreatedAt(new Date(12345678901234L));

assertThat(result).isSameAs(copyComponentDto);
Expand All @@ -197,7 +195,7 @@ void shouldCreateComponentAndBranchDtoIfValidationPasses(String branchName, Stri
assertThat(branchDtoArgumentCaptor.getValue()).usingRecursiveComparison().isEqualTo(new BranchDto()
.setBranchType(branchType)
.setExcludeFromPurge(excludedFromPurge)
.setProjectUuid("componentUuid")
.setProjectUuid("projectUuid")
.setKey(branchType == BranchType.BRANCH ? branchName : pullRequestKey)
.setUuid("uuid0")
.setIsMain(false));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2022 Michael Clarke
* Copyright (C) 2020-2023 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 @@ -89,7 +89,7 @@ void shouldHandleEndpointWithValidRequest() {
verify(dbSession).commit();
verify(projectAlmSettingDao).deleteByProject(dbSession, componentDto);
verify(response).noContent();
verify(userSession).checkProjectPermission("admin", componentDto);
verify(userSession).hasEntityPermission("admin", componentDto);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Michael Clarke
* Copyright (C) 2021-2023 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 @@ -203,6 +203,6 @@ void testHandleProjectRequestHappyPath() {
underTest.handle(request, response);

verify(validator).validate(projectAlmSettingDto, almSettingDto);
verify(userSession).checkProjectPermission(UserRole.USER, projectDto);
verify(userSession).hasEntityPermission(UserRole.USER, projectDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Michael Clarke
* Copyright (C) 2022-2023 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 @@ -37,6 +37,7 @@
import org.sonar.db.component.BranchDao;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.entity.EntityDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.component.ComponentFinder;
Expand Down Expand Up @@ -129,7 +130,7 @@ void shouldNotPerformDeleteIfUserNotProjectAdmin() {
when(componentFinder.getProjectByKey(any(), any())).thenReturn(new ProjectDto().setKey("projectKey").setUuid("uuid0"));

when(userSession.checkLoggedIn()).thenReturn(userSession);
when(userSession.checkProjectPermission(any(), any())).thenThrow(new UnauthorizedException("Dummy"));
when(userSession.hasEntityPermission(any(), any(EntityDto.class))).thenThrow(new UnauthorizedException("Dummy"));

Response response = mock(Response.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Michael Clarke
* Copyright (C) 2022-2023 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 @@ -132,7 +132,7 @@ void shouldExecuteRequestWithValidParameter() {

SnapshotDao snapshotDao = mock(SnapshotDao.class);
when(dbClient.snapshotDao()).thenReturn(snapshotDao);
when(snapshotDao.selectLastAnalysesByRootComponentUuids(any(), any())).thenReturn(List.of(new SnapshotDto().setComponentUuid("componentUuid").setCreatedAt(1234L)));
when(snapshotDao.selectLastAnalysesByRootComponentUuids(any(), any())).thenReturn(List.of(new SnapshotDto().setUuid("componentUuid").setCreatedAt(1234L)));

Response response = mock(Response.class);

Expand Down

0 comments on commit 531f76a

Please sign in to comment.