From 288eceadc2c213031d5d456b8e1634f81625ff1a Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 16 Jun 2023 16:08:19 -0500 Subject: [PATCH] SONAR-19558 Fix WS action ITs --- .../step/BuildComponentTreeStepIT.java | 8 ++-- .../step/QualityProfileEventsStep.java | 8 ++-- .../taskprocessor/ProjectDescriptor.java | 5 ++- .../org/sonar/db/component/SnapshotDao.java | 26 ++++++------- .../sonar/db/component/SnapshotMapper.java | 4 +- .../org/sonar/db/component/SnapshotMapper.xml | 2 +- .../sonar/db/component/ComponentDbTester.java | 6 ++- .../sonar/db/component/ComponentTesting.java | 28 +++++++------- .../org/sonar/db/issue/IssueDbTester.java | 4 +- .../sonar/server/badge/ws/TokenActionIT.java | 8 ++-- .../sonar/server/ce/ws/CancelActionIT.java | 11 ++++-- .../sonar/server/hotspot/ws/PullActionIT.java | 38 +++++++++++++------ .../server/ui/ws/MarketplaceActionIT.java | 5 ++- .../developers/ws/SearchEventsAction.java | 9 ++--- .../server/developers/ws/UuidFromPair.java | 10 ++--- .../server/developers/ws/UuidFromPairs.java | 4 +- 16 files changed, 99 insertions(+), 77 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepIT.java index 4850fe1d361..90dae4147fc 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepIT.java @@ -233,10 +233,10 @@ public class BuildComponentTreeStepIT { @Test public void return_existing_uuids() { setAnalysisMetadataHolder(); - ComponentDto project = dbTester.components().insertPrivateProject("ABCD", p -> p.setKey(REPORT_PROJECT_KEY)).getMainBranchComponent(); - ComponentDto directory = newDirectory(project, "CDEF", REPORT_DIR_PATH_1); + ComponentDto mainBranch = dbTester.components().insertPrivateProject("ABCD", p -> p.setKey(REPORT_PROJECT_KEY)).getMainBranchComponent(); + ComponentDto directory = newDirectory(mainBranch, "CDEF", REPORT_DIR_PATH_1); insertComponent(directory.setKey(REPORT_PROJECT_KEY + ":" + REPORT_DIR_PATH_1)); - insertComponent(newFileDto(project, directory, "DEFG") + insertComponent(newFileDto(mainBranch, directory, "DEFG") .setKey(REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1) .setPath(REPORT_FILE_PATH_1)); @@ -246,7 +246,7 @@ public class BuildComponentTreeStepIT { underTest.execute(new TestComputationStepContext()); - verifyComponentByRef(ROOT_REF, REPORT_PROJECT_KEY, analysisMetadataHolder.getProject().getName(), "ABCD"); + verifyComponentByRef(ROOT_REF, REPORT_PROJECT_KEY, analysisMetadataHolder.getProject().getName(), mainBranch.uuid()); verifyComponentByKey(REPORT_PROJECT_KEY + ":" + REPORT_DIR_PATH_1, REPORT_DIR_PATH_1, "CDEF"); verifyComponentByRef(FILE_1_REF, REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1, REPORT_FILE_NAME_1, "DEFG"); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityProfileEventsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityProfileEventsStep.java index 8d9f13c2bae..d80ab7addc5 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityProfileEventsStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityProfileEventsStep.java @@ -72,18 +72,18 @@ public class QualityProfileEventsStep implements ComputationStep { @Override public void execute(ComputationStep.Context context) { - executeForProject(treeRootHolder.getRoot()); + executeForBranch(treeRootHolder.getRoot()); } - private void executeForProject(Component projectComponent) { - Optional baseMeasure = measureRepository.getBaseMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY)); + private void executeForBranch(Component branchComponent) { + Optional baseMeasure = measureRepository.getBaseMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY)); if (!baseMeasure.isPresent()) { // first analysis -> do not generate events return; } // Load profiles used in current analysis for which at least one file of the corresponding language exists - Optional rawMeasure = measureRepository.getRawMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY)); + Optional rawMeasure = measureRepository.getRawMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY)); if (!rawMeasure.isPresent()) { // No qualify profile computed on the project return; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectDescriptor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectDescriptor.java index d1f1bb12fd2..282bc315bc8 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectDescriptor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectDescriptor.java @@ -23,6 +23,7 @@ import java.util.Objects; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import static java.util.Objects.requireNonNull; @@ -41,8 +42,8 @@ public class ProjectDescriptor { /** * Build a {@link ProjectDescriptor} without checking qualifier of ComponentDto. */ - public static ProjectDescriptor of(ComponentDto project) { - return new ProjectDescriptor(project.uuid(), project.getKey(), project.name()); + public static ProjectDescriptor of(ProjectDto project) { + return new ProjectDescriptor(project.getUuid(), project.getKey(), project.getName()); } public final String getUuid() { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java index 1d8b0ef5b6a..9859d2370fa 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -97,18 +97,17 @@ public class SnapshotDao implements Dao { /** * Returned finished analysis from a list of projects and dates. * "Finished" analysis means that the status in the CE_ACTIVITY table is SUCCESS => the goal is to be sure that the CE task is completely finished. - * * Note that branches analysis of projects are also returned. */ public List selectFinishedByProjectUuidsAndFromDates(DbSession dbSession, List projectUuids, List fromDates) { checkArgument(projectUuids.size() == fromDates.size(), "The number of components (%s) and from dates (%s) must be the same.", String.valueOf(projectUuids.size()), String.valueOf(fromDates.size())); - List componentUuidFromDatePairs = IntStream.range(0, projectUuids.size()) - .mapToObj(i -> new ComponentUuidFromDatePair(projectUuids.get(i), fromDates.get(i))) + List projectUuidFromDatePairs = IntStream.range(0, projectUuids.size()) + .mapToObj(i -> new ProjectUuidFromDatePair(projectUuids.get(i), fromDates.get(i))) .collect(MoreCollectors.toList(projectUuids.size())); - return executeLargeInputs(componentUuidFromDatePairs, partition -> mapper(dbSession).selectFinishedByProjectUuidsAndFromDates(partition), i -> i / 2); + return executeLargeInputs(projectUuidFromDatePairs, partition -> mapper(dbSession).selectFinishedByProjectUuidsAndFromDates(partition), i -> i / 2); } public void switchIsLastFlagAndSetProcessedStatus(DbSession dbSession, String componentUuid, String analysisUuid) { @@ -148,22 +147,22 @@ public class SnapshotDao implements Dao { return session.getMapper(SnapshotMapper.class); } - static class ComponentUuidFromDatePair implements Comparable { - private final String componentUuid; + static class ProjectUuidFromDatePair implements Comparable { + private final String projectUuid; private final long from; - ComponentUuidFromDatePair(String componentUuid, long from) { - this.componentUuid = requireNonNull(componentUuid); + ProjectUuidFromDatePair(String projectUuid, long from) { + this.projectUuid = requireNonNull(projectUuid); this.from = from; } @Override - public int compareTo(ComponentUuidFromDatePair other) { + public int compareTo(ProjectUuidFromDatePair other) { if (this == other) { return 0; } - int c = componentUuid.compareTo(other.componentUuid); + int c = projectUuid.compareTo(other.projectUuid); if (c == 0) { c = Long.compare(from, other.from); } @@ -180,14 +179,13 @@ public class SnapshotDao implements Dao { return false; } - ComponentUuidFromDatePair other = (ComponentUuidFromDatePair) o; - return componentUuid.equals(other.componentUuid) - && from == other.from; + ProjectUuidFromDatePair other = (ProjectUuidFromDatePair) o; + return projectUuid.equals(other.projectUuid) && from == other.from; } @Override public int hashCode() { - return Objects.hash(componentUuid, from); + return Objects.hash(projectUuid, from); } } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java index a28063c7b78..acf6105d11a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -24,7 +24,7 @@ import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.RowBounds; -import org.sonar.db.component.SnapshotDao.ComponentUuidFromDatePair; +import org.sonar.db.component.SnapshotDao.ProjectUuidFromDatePair; public interface SnapshotMapper { @@ -52,7 +52,7 @@ public interface SnapshotMapper { void update(SnapshotDto analysis); - List selectFinishedByProjectUuidsAndFromDates(@Param("projectUuidFromDatePairs") List pairs); + List selectFinishedByProjectUuidsAndFromDates(@Param("projectUuidFromDatePairs") List pairs); @CheckForNull Long selectLastAnalysisDateByProject(String projectUuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index 7d2adfa6b13..da7a40f453d 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -153,7 +153,7 @@ inner join project_branches pb on pb.uuid=p.uuid where - (pb.project_uuid=#{projectUuidFromDatePair.componentUuid, jdbcType=VARCHAR} and s.created_at >= #{projectUuidFromDatePair.from, jdbcType=BIGINT}) + (pb.project_uuid=#{projectUuidFromDatePair.projectUuid, jdbcType=VARCHAR} and s.created_at >= #{projectUuidFromDatePair.from, jdbcType=BIGINT}) and s.status = 'P' order by diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index 4a8d78515a6..cf752455f0a 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -169,7 +169,11 @@ public class ComponentDbTester { } public final ProjectData insertPrivateProject(String uuid, Consumer dtoPopulator) { - return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true, defaults(), dtoPopulator); + if (useDifferentUuids) { + return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, defaults(), dtoPopulator, p -> p.setUuid(uuid)); + } else { + return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true, defaults(), dtoPopulator); + } } public final ProjectData insertPrivateProjectWithCustomBranch(String branchKey) { diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java index 5d301036a6f..cf638ea7a4d 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java @@ -37,12 +37,12 @@ import static org.sonar.db.portfolio.PortfolioDto.SelectionMode.NONE; public class ComponentTesting { - public static ComponentDto newFileDto(ComponentDto project) { - return newFileDto(project, (ComponentDto) null); + public static ComponentDto newFileDto(ComponentDto branch) { + return newFileDto(branch, (ComponentDto) null); } - public static ComponentDto newFileDto(ComponentDto project, @Nullable ComponentDto directory) { - return newFileDto(project, directory, Uuids.createFast()); + public static ComponentDto newFileDto(ComponentDto branch, @Nullable ComponentDto directory) { + return newFileDto(branch, directory, Uuids.createFast()); } public static ComponentDto newFileDto(ComponentDto branch, String mainBranchUuid) { @@ -72,28 +72,28 @@ public class ComponentTesting { .setLanguage("xoo"); } - public static ComponentDto newDirectory(ComponentDto project, String path) { - return newDirectory(project, Uuids.createFast(), path); + public static ComponentDto newDirectory(ComponentDto branch, String path) { + return newDirectory(branch, Uuids.createFast(), path); } - public static ComponentDto newDirectoryOnBranch(ComponentDto project, String path, String mainBranchUuid) { - return newDirectory(project, Uuids.createFast(), path, mainBranchUuid); + public static ComponentDto newDirectoryOnBranch(ComponentDto branch, String path, String mainBranchUuid) { + return newDirectory(branch, Uuids.createFast(), path, mainBranchUuid); } - private static ComponentDto newDirectory(ComponentDto project, String uuid, String path, String mainBranchUuid) { - String key = !path.equals("/") ? project.getKey() + ":" + path : project.getKey() + ":/"; - return newChildComponent(uuid, project, project) + private static ComponentDto newDirectory(ComponentDto branch, String uuid, String path, String mainBranchUuid) { + String key = !path.equals("/") ? branch.getKey() + ":" + path : branch.getKey() + ":/"; + return newChildComponent(uuid, branch, branch) .setKey(key) .setName(path) .setLongName(path) - .setBranchUuid(project.branchUuid()) + .setBranchUuid(branch.branchUuid()) .setPath(path) .setScope(Scopes.DIRECTORY) .setQualifier(Qualifiers.DIRECTORY); } - public static ComponentDto newDirectory(ComponentDto project, String uuid, String path) { - return newDirectory(project, uuid, path, null); + public static ComponentDto newDirectory(ComponentDto branch, String uuid, String path) { + return newDirectory(branch, uuid, path, null); } public static ComponentDto newSubPortfolio(ComponentDto portfolioOrSubPortfolio, String uuid, String key) { diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/issue/IssueDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/issue/IssueDbTester.java index ae4da9f4a9f..96fdfcb5df9 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/issue/IssueDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/issue/IssueDbTester.java @@ -90,11 +90,11 @@ public class IssueDbTester { * @throws AssertionError if rule is a Security Hotspot */ @SafeVarargs - public final IssueDto insertIssue(RuleDto rule, ComponentDto project, ComponentDto file, Consumer... populators) { + public final IssueDto insertIssue(RuleDto rule, ComponentDto branch, ComponentDto file, Consumer... populators) { assertThat(rule.getType()) .describedAs("rule must not be a Security Hotspot type") .isNotEqualTo(SECURITY_HOTSPOT.getDbConstant()); - IssueDto issue = newIssue(rule, project, file) + IssueDto issue = newIssue(rule, branch, file) .setType(RULE_TYPES_EXCEPT_HOTSPOTS[new Random().nextInt(RULE_TYPES_EXCEPT_HOTSPOTS.length)]); stream(populators).forEach(p -> p.accept(issue)); return insertIssue(issue); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/badge/ws/TokenActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/badge/ws/TokenActionIT.java index 4256242dba1..74e4fcfdb1e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/badge/ws/TokenActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/badge/ws/TokenActionIT.java @@ -26,6 +26,7 @@ import org.mockito.Mockito; import org.sonar.api.web.UserRole; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.user.TokenType; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; @@ -72,7 +73,7 @@ public class TokenActionIT { @Test public void should_generate_token() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); userSession.logIn().addProjectPermission(UserRole.USER, project); when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token"); @@ -83,7 +84,7 @@ public class TokenActionIT { @Test public void handle_whenApplicationKeyPassed_shouldReturnToken() { - ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent(); + ProjectDto application = db.components().insertPrivateApplication().getProjectDto(); userSession.logIn().addProjectPermission(UserRole.USER, application); when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token"); @@ -92,10 +93,9 @@ public class TokenActionIT { response.assertJson("{\"token\":\"generated_token\"}"); } - @Test public void should_reuse_generated_token() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); userSession.logIn().addProjectPermission(UserRole.USER, project); when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token"); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/CancelActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/CancelActionIT.java index 381323b96f1..d148bc5dfa3 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/CancelActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/CancelActionIT.java @@ -35,6 +35,8 @@ import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ProjectData; +import org.sonar.db.project.ProjectDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.platform.NodeInformation; import org.sonar.server.tester.UserSessionRule; @@ -98,9 +100,12 @@ public class CancelActionIT { @Test public void cancel_pending_task_when_project_administer() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.ADMIN, project); - CeQueueDto queue = createTaskSubmit(project); + ProjectData project = db.components().insertPrivateProject(); + ProjectDto projectDto = project.getProjectDto(); + ComponentDto mainBranchComponent = project.getMainBranchComponent(); + userSession.addProjectPermission(UserRole.ADMIN, projectDto); + userSession.addProjectBranchMapping(projectDto.getUuid(), mainBranchComponent); + CeQueueDto queue = createTaskSubmit(mainBranchComponent); tester.newRequest() .setParam("id", queue.getUuid()) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/hotspot/ws/PullActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/hotspot/ws/PullActionIT.java index e845bfe0a6d..a03151cac61 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/hotspot/ws/PullActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/hotspot/ws/PullActionIT.java @@ -33,11 +33,14 @@ import org.sonar.api.issue.Issue; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ProjectData; import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.issue.IssueDbTester; import org.sonar.db.issue.IssueDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.protobuf.DbCommons; import org.sonar.db.protobuf.DbIssues; import org.sonar.db.rule.RuleDto; @@ -89,16 +92,21 @@ public class PullActionIT { pullActionProtobufObjectGenerator); private final WsActionTester tester = new WsActionTester(underTest); + private ProjectDto project; private ComponentDto correctProject, incorrectProject; private ComponentDto correctFile, incorrectFile; @Before public void before() { when(system2.now()).thenReturn(NOW); - correctProject = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectData projectData = db.components().insertPrivateProject(); + correctProject = projectData.getMainBranchComponent(); + project = projectData.getProjectDto(); + correctFile = db.components().insertComponent(newFileDto(correctProject)); - incorrectProject = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectData incorrectProjectData = db.components().insertPrivateProject(); + incorrectProject = incorrectProjectData.getMainBranchComponent(); incorrectFile = db.components().insertComponent(newFileDto(incorrectProject)); } @@ -220,10 +228,12 @@ public class PullActionIT { @Test public void wsExecution_whenHotspotOnAnotherBranchThanMain_shouldReturnOneIssue() throws IOException { - ComponentDto developBranch = componentDbTester.insertPrivateProjectWithCustomBranch("develop").getMainBranchComponent(); + ProjectData projectData = componentDbTester.insertPrivateProjectWithCustomBranch("develop"); + ProjectDto project = projectData.getProjectDto(); + ComponentDto developBranch = projectData.getMainBranchComponent(); ComponentDto developFile = db.components().insertComponent(newFileDto(developBranch)); List hotspotKeys = generateHotspots(developBranch, developFile, 1); - loginWithBrowsePermission(developBranch.uuid(), developFile.uuid()); + loginWithBrowsePermission(project, developFile.uuid()); TestRequest request = tester.newRequest() .setParam("projectKey", developBranch.getKey()) @@ -268,7 +278,7 @@ public class PullActionIT { @Test public void wsExecution_whenDifferentHotspotsInTheTable_shouldReturnOnlyThatBelongToSelectedProject() throws IOException { - loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid()); + loginWithBrowsePermission(project, correctFile.uuid()); List correctIssueKeys = generateHotspots(correctProject, correctFile, 10); List incorrectIssueKeys = generateHotspots(incorrectProject, incorrectFile, 5); @@ -288,7 +298,7 @@ public class PullActionIT { @Test public void wsExecution_whenNoIssuesBelongToTheProject_shouldReturnZeroIssues() throws IOException { - loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid()); + loginWithBrowsePermission(project, correctFile.uuid()); generateHotspots(incorrectProject, incorrectFile, 5); TestRequest request = tester.newRequest() @@ -303,7 +313,7 @@ public class PullActionIT { @Test public void wsExecution_whenLanguagesParam_shouldReturnOneIssue() throws IOException { - loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid()); + loginWithBrowsePermission(project, correctFile.uuid()); RuleDto javaRule = db.rules().insert(r -> r.setLanguage("java")); IssueDto javaIssue = issueDbTester.insertHotspot(p -> p.setSeverity("MINOR") @@ -331,7 +341,7 @@ public class PullActionIT { @Test public void wsExecution_whenChangedSinceParam_shouldReturnMatchingIssue() throws IOException { - loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid()); + loginWithBrowsePermission(project, correctFile.uuid()); RuleDto javaRule = db.rules().insert(r -> r.setLanguage("java")); IssueDto issueBefore = issueDbTester.insertHotspot(p -> p.setSeverity("MINOR") @@ -371,7 +381,7 @@ public class PullActionIT { @Test public void wsExecution_whenWrongLanguageSet_shouldReturnZeroIssues() throws IOException { - loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid()); + loginWithBrowsePermission(project, correctFile.uuid()); RuleDto javascriptRule = db.rules().insert(r -> r.setLanguage("javascript")); issueDbTester.insertHotspot(p -> p.setSeverity("MINOR") @@ -418,12 +428,16 @@ public class PullActionIT { } private void loginWithBrowsePermission(IssueDto issueDto) { - loginWithBrowsePermission(issueDto.getProjectUuid(), issueDto.getComponentUuid()); + BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get(); + ProjectDto projectDto = db.getDbClient().projectDao().selectByUuid(db.getSession(), branchDto.getProjectUuid()).get(); + loginWithBrowsePermission(projectDto, issueDto.getComponentUuid()); } - private void loginWithBrowsePermission(String projectUuid, String componentUuid) { + private void loginWithBrowsePermission(ProjectDto project, String componentUuid) { UserDto user = db.users().insertUser("john"); - userSession.logIn(user).addProjectPermission(USER, getComponentOrFail(projectUuid, "project not found"), getComponentOrFail(componentUuid, "component not found")); + userSession.logIn(user) + .addProjectPermission(USER, project) + .addProjectPermission(USER, getComponentOrFail(componentUuid, "component not found")); } private ComponentDto getComponentOrFail(String componentUuid, String failMessage) { diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ui/ws/MarketplaceActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ui/ws/MarketplaceActionIT.java index aeeb6fedb9e..244c45af479 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ui/ws/MarketplaceActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ui/ws/MarketplaceActionIT.java @@ -28,6 +28,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.tester.UserSessionRule; @@ -116,8 +117,8 @@ public class MarketplaceActionIT { } private void setNcloc(double ncloc) { - ComponentDto project = db.components().insertPublicProject().getMainBranchComponent(); - db.getDbClient().projectDao().updateNcloc(db.getSession(), project.uuid(), (long) ncloc); + ProjectDto project = db.components().insertPublicProject().getProjectDto(); + db.getDbClient().projectDao().updateNcloc(db.getSession(), project.getUuid(), (long) ncloc); db.commit(); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/SearchEventsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/SearchEventsAction.java index 1f965dc9986..598c7616760 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/SearchEventsAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/SearchEventsAction.java @@ -24,7 +24,6 @@ import java.net.URLEncoder; import java.util.Date; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.function.Predicate; @@ -63,8 +62,8 @@ import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; import static org.sonar.db.component.BranchType.BRANCH; import static org.sonar.db.component.BranchType.PULL_REQUEST; -import static org.sonar.server.developers.ws.UuidFromPairs.componentUuids; import static org.sonar.server.developers.ws.UuidFromPairs.fromDates; +import static org.sonar.server.developers.ws.UuidFromPairs.projectUuids; import static org.sonar.server.exceptions.BadRequestException.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -136,7 +135,7 @@ public class SearchEventsAction implements DevelopersWsAction { List authorizedProjects = searchProjects(dbSession, projectKeys); Map projectsByUuid = authorizedProjects.stream().collect(uniqueIndex(ProjectDto::getUuid)); List uuidFromPairs = buildUuidFromPairs(fromDates, projectKeys, authorizedProjects); - List analyses = dbClient.snapshotDao().selectFinishedByProjectUuidsAndFromDates(dbSession, componentUuids(uuidFromPairs), fromDates(uuidFromPairs)); + List analyses = dbClient.snapshotDao().selectFinishedByProjectUuidsAndFromDates(dbSession, projectUuids(uuidFromPairs), fromDates(uuidFromPairs)); if (analyses.isEmpty()) { return Stream.empty(); @@ -184,9 +183,9 @@ public class SearchEventsAction implements DevelopersWsAction { private Stream computeNewIssuesEvents(Map projectsByUuid, Map branchesByUuids, List uuidFromPairs) { Map fromsByProjectUuid = uuidFromPairs.stream().collect(Collectors.toMap( - UuidFromPair::getComponentUuid, + UuidFromPair::getProjectUuid, UuidFromPair::getFrom)); - List projectStatistics = issueIndex.searchProjectStatistics(componentUuids(uuidFromPairs), fromDates(uuidFromPairs), userSession.getUuid()); + List projectStatistics = issueIndex.searchProjectStatistics(projectUuids(uuidFromPairs), fromDates(uuidFromPairs), userSession.getUuid()); return projectStatistics .stream() .map(e -> { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPair.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPair.java index 0a025c70dfe..2575545fb98 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPair.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPair.java @@ -20,16 +20,16 @@ package org.sonar.server.developers.ws; class UuidFromPair { - private final String componentUuid; + private final String projectUuid; private final long from; - public UuidFromPair(String componentUuid, long from) { - this.componentUuid = componentUuid; + public UuidFromPair(String projectUuid, long from) { + this.projectUuid = projectUuid; this.from = from; } - public String getComponentUuid() { - return componentUuid; + public String getProjectUuid() { + return projectUuid; } public long getFrom() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPairs.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPairs.java index 93383b711fc..f074c8b0ea9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPairs.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/UuidFromPairs.java @@ -27,8 +27,8 @@ public class UuidFromPairs { // prevent instantiation } - public static List componentUuids(List pairs) { - return pairs.stream().map(UuidFromPair::getComponentUuid).collect(MoreCollectors.toList(pairs.size())); + public static List projectUuids(List pairs) { + return pairs.stream().map(UuidFromPair::getProjectUuid).collect(MoreCollectors.toList(pairs.size())); } public static List fromDates(List pairs) { -- 2.39.5