From 18876fc2f0b87fab0abf45e0be0ee1a446ee50e8 Mon Sep 17 00:00:00 2001 From: lukasz-jarocki-sonarsource Date: Tue, 11 Jul 2023 07:26:49 +0200 Subject: [PATCH] SONAR-19850 Use different uuids for project and branch --- .../component/ProjectPersister.java | 7 +- .../ProjectExportTaskProcessor.java | 8 +- .../src/main/protobuf/project_dump.proto | 1 + .../sonar/db/component/ComponentMapper.java | 2 +- .../java/org/sonar/db/issue/IssueDto.java | 9 +- .../java/org/sonar/db/purge/PurgeDao.java | 2 +- .../sonar/db/component/ComponentMapper.xml | 2 +- .../ce/projectdump/ExportSubmitterImplIT.java | 4 +- .../ce/queue/BranchReportSubmitterIT.java | 10 +- .../server/ce/queue/ReportSubmitterIT.java | 13 +- .../sonar/server/ce/ws/ComponentActionIT.java | 133 +++++++++--------- .../ce/projectdump/ExportSubmitterImpl.java | 5 +- .../server/ce/queue/ReportSubmitter.java | 2 +- .../sonar/server/ce/ws/ComponentAction.java | 16 +-- .../server/component/ComponentUpdater.java | 2 +- 15 files changed, 109 insertions(+), 107 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java index cc6f1e76bc9..c733fe9007b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java @@ -47,7 +47,7 @@ public class ProjectPersister { ProjectDto dbProjectDto = dbClient.projectDao().selectProjectByKey(dbSession, treeRootHolder.getRoot().getKey()) .orElseThrow(() -> new IllegalStateException("Project has been deleted by end-user during analysis")); - ProjectDto projectDto = toProjectDto(treeRootHolder.getRoot()); + ProjectDto projectDto = toProjectDto(treeRootHolder.getRoot(), dbProjectDto); if (hasChanged(dbProjectDto, projectDto)) { // insert or update in projects table @@ -64,9 +64,10 @@ public class ProjectPersister { !StringUtils.equals(dbProject.getDescription(), newProject.getDescription()); } - private ProjectDto toProjectDto(Component root) { + private ProjectDto toProjectDto(Component root, ProjectDto projectDtoFromDatabase) { ProjectDto projectDto = new ProjectDto(); - projectDto.setUuid(root.getUuid()); + // Component has different uuid from project + projectDto.setUuid(projectDtoFromDatabase.getUuid()); projectDto.setName(root.getName()); projectDto.setDescription(root.getDescription()); projectDto.setUpdatedAt(system2.now()); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java index 3383d7258e3..e18d97a589b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java @@ -52,8 +52,7 @@ public class ProjectExportTaskProcessor implements CeTaskProcessor { private void processProjectExport(CeTask task) { CeTask.Component exportComponent = mandatoryComponent(task, PROJECT_EXPORT); - failIfNotMain(exportComponent, task); - ProjectDescriptor projectExportDescriptor = new ProjectDescriptor(exportComponent.getUuid(), + ProjectDescriptor projectExportDescriptor = new ProjectDescriptor(task.getEntity().get().getUuid(), mandatoryKey(exportComponent), mandatoryName(exportComponent)); try (TaskContainer taskContainer = new TaskContainerImpl(componentContainer, @@ -64,11 +63,6 @@ public class ProjectExportTaskProcessor implements CeTaskProcessor { } - private static void failIfNotMain(CeTask.Component exportComponent, CeTask task) { - task.getEntity().filter(entity -> entity.equals(exportComponent)) - .orElseThrow(() -> new IllegalStateException("Component of task must be the same as entity")); - } - private static CeTask.Component mandatoryComponent(CeTask task, String type) { return task.getComponent().orElseThrow(() -> new IllegalStateException(String.format("Task with type %s must have a component", type))); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto index 854edbbeeb3..f495c6df4d0 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto +++ b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto @@ -210,6 +210,7 @@ message Plugin { message LineHashes { int64 component_ref = 1; string hashes = 2; + // this is branch uuid string project_uuid = 3; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 68c0d16bf74..25424e75349 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -85,7 +85,7 @@ public interface ComponentMapper { List selectUuidsByKeyFromProjectKeyAndBranchOrPr(@Param("projectKey") String projectKey, @Nullable @Param("branch") String branch, @Nullable @Param("pullRequest") String pullRequest); - Set selectViewKeysWithEnabledCopyOfProject(@Param("projectUuids") Collection projectUuids); + Set selectViewKeysWithEnabledCopyOfProject(@Param("branchUuids") Collection branchUuids); /** * Return technical projects from a view or a sub-view diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java index 88b351b7126..06334fd6de6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java @@ -218,9 +218,9 @@ public final class IssueDto implements Serializable { * The project branch where the issue is located. * Note that the name is misleading - it should be branch. */ - public IssueDto setProject(ComponentDto project) { - this.projectKey = project.getKey(); - this.projectUuid = project.uuid(); + public IssueDto setProject(ComponentDto branch) { + this.projectKey = branch.getKey(); + this.projectUuid = branch.uuid(); return this; } @@ -585,8 +585,7 @@ public final class IssueDto implements Serializable { } /** - * Should only be used to persist in E/S - *

+ * This is branch uuid, not a project uuid. The naming is wrong, the javadoc is right. * Please use {@link #setProject(ComponentDto)} instead */ public IssueDto setProjectUuid(String s) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java index daf1ddd183d..0e35b7cd53f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -118,7 +118,7 @@ public class PurgeDao implements Dao { String rootUuid = conf.rootUuid(); List issueKeys = mapper.selectOldClosedIssueKeys(rootUuid, dateToLong(toDate)); deleteIssues(mapper, issueKeys); - listener.onIssuesRemoval(rootUuid, issueKeys); + listener.onIssuesRemoval(conf.projectUuid(), issueKeys); } private static void deleteIssues(PurgeMapper mapper, Collection issueKeys) { diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 9e499a062df..e55f64f3fde 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -422,7 +422,7 @@ and leaf.scope = 'FIL' and leaf.enabled = ${_true} and leaf.copy_component_uuid in - #{uuid,jdbcType=VARCHAR} + #{uuid,jdbcType=VARCHAR} where p.enabled = ${_true} and p.uuid = leaf.branch_uuid diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/projectdump/ExportSubmitterImplIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/projectdump/ExportSubmitterImplIT.java index a6796644141..a9af4585f5b 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/projectdump/ExportSubmitterImplIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/projectdump/ExportSubmitterImplIT.java @@ -70,7 +70,7 @@ public class ExportSubmitterImplIT { underTest.submitProjectExport(projectDto.getKey(), SOME_SUBMITTER_UUID); assertThat(dbClient.ceQueueDao().selectAllInAscOrder(db.getSession())) - .extracting(CeQueueDto::getComponentUuid, CeQueueDto::getTaskType, CeQueueDto::getSubmitterUuid) + .extracting(CeQueueDto::getEntityUuid, CeQueueDto::getTaskType, CeQueueDto::getSubmitterUuid) .containsExactlyInAnyOrder(tuple(projectDto.getUuid(), "PROJECT_EXPORT", SOME_SUBMITTER_UUID)); } @@ -81,7 +81,7 @@ public class ExportSubmitterImplIT { underTest.submitProjectExport(projectDto.getKey(), null); assertThat(dbClient.ceQueueDao().selectAllInAscOrder(db.getSession())) - .extracting(CeQueueDto::getComponentUuid, CeQueueDto::getTaskType, CeQueueDto::getSubmitterUuid) + .extracting(CeQueueDto::getEntityUuid, CeQueueDto::getTaskType, CeQueueDto::getSubmitterUuid) .containsExactlyInAnyOrder(tuple(projectDto.getUuid(), "PROJECT_EXPORT", null)); } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java index 0086013dc79..7a485268bac 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java @@ -200,10 +200,10 @@ public class BranchReportSubmitterIT { underTest.submit(nonExistingBranch.getKey(), nonExistingBranch.name(), randomCharacteristics, reportInput); - BranchDto exitingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), nonExistingBranch.uuid()).get(); - verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingBranch), eq(exitingProjectMainBranch)); + BranchDto existingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), nonExistingBranch.uuid()).get(); + verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingBranch), eq(existingProjectMainBranch)); verify(branchSupportDelegate).createComponentKey(nonExistingBranch.getKey(), randomCharacteristics); - verify(branchSupportDelegate).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingBranch), eq(exitingProjectMainBranch)); + verify(branchSupportDelegate).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingBranch), eq(existingProjectMainBranch)); verifyNoMoreInteractions(branchSupportDelegate); verifyQueueSubmit(nonExistingBranch, createdBranch, user, randomCharacteristics, taskUuid); verify(componentUpdater).commitAndIndex(any(DbSession.class), eq(componentCreationData)); @@ -261,7 +261,6 @@ public class BranchReportSubmitterIT { private void verifyQueueSubmit(ComponentDto project, ComponentDto branch, UserDto user, Map characteristics, String taskUuid) { ArgumentCaptor captor = ArgumentCaptor.forClass(CeTaskSubmit.class); - verify(queue).submit(captor.capture()); CeTaskSubmit ceTask = captor.getValue(); assertThat(ceTask.getUuid()).isEqualTo(taskUuid); @@ -270,7 +269,8 @@ public class BranchReportSubmitterIT { assertThat(ceTask.getType()).isEqualTo(CeTaskTypes.REPORT); assertThat(ceTask.getComponent()).isPresent(); assertThat(ceTask.getComponent().get().getUuid()).isEqualTo(branch.uuid()); - assertThat(ceTask.getComponent().get().getEntityUuid()).isEqualTo(project.uuid()); + BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid()).get(); + assertThat(ceTask.getComponent().get().getEntityUuid()).isEqualTo(branchDto.getProjectUuid()); } private static BranchSupport.ComponentKey createComponentKeyOfBranch(String projectKey) { diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java index edd8ac74eec..748ed942153 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java @@ -38,6 +38,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.component.ProjectData; import org.sonar.db.permission.GlobalPermission; import org.sonar.db.project.ProjectDto; @@ -138,17 +139,17 @@ public class ReportSubmitterIT { @Test public void submit_a_report_on_existing_project() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectData project = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); - userSession.logIn(user).addProjectPermission(SCAN.getKey(), project); + userSession.logIn(user).addProjectPermission(SCAN.getKey(), project.getMainBranchComponent()); mockSuccessfulPrepareSubmitCall(); - underTest.submit(project.getKey(), project.name(), emptyMap(), IOUtils.toInputStream("{binary}", UTF_8)); + underTest.submit(project.projectKey(), project.getProjectDto().getName(), emptyMap(), IOUtils.toInputStream("{binary}", UTF_8)); verifyReportIsPersisted(TASK_UUID); verifyNoInteractions(permissionTemplateService); verify(queue).submit(argThat(submit -> submit.getType().equals(CeTaskTypes.REPORT) - && submit.getComponent().filter(cpt -> cpt.getUuid().equals(project.uuid()) && cpt.getEntityUuid().equals(project.uuid())).isPresent() + && submit.getComponent().filter(cpt -> cpt.getUuid().equals(project.getMainBranchComponent().uuid()) && cpt.getEntityUuid().equals(project.projectUuid())).isPresent() && submit.getSubmitterUuid().equals(user.getUuid()) && submit.getUuid().equals(TASK_UUID))); } @@ -165,9 +166,11 @@ public class ReportSubmitterIT { underTest.submit(PROJECT_KEY, PROJECT_NAME, emptyMap(), IOUtils.toInputStream("{binary}", UTF_8)); ComponentDto createdProject = db.getDbClient().componentDao().selectByKey(db.getSession(), PROJECT_KEY).get(); + EntityDto entityDto = db.getDbClient().entityDao().selectByKey(db.getSession(), PROJECT_KEY).get(); + verifyReportIsPersisted(TASK_UUID); verify(queue).submit(argThat(submit -> submit.getType().equals(CeTaskTypes.REPORT) - && submit.getComponent().filter(cpt -> cpt.getUuid().equals(createdProject.uuid()) && cpt.getEntityUuid().equals(createdProject.uuid())).isPresent() + && submit.getComponent().filter(cpt -> cpt.getUuid().equals(createdProject.uuid()) && cpt.getEntityUuid().equals(entityDto.getUuid())).isPresent() && submit.getUuid().equals(TASK_UUID))); } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/ComponentActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/ComponentActionIT.java index 219ad666f72..2018a1b80ba 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/ComponentActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/ComponentActionIT.java @@ -36,7 +36,10 @@ import org.sonar.db.ce.CeTaskMessageDto; import org.sonar.db.ce.CeTaskMessageType; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ProjectData; import org.sonar.db.component.SnapshotDto; +import org.sonar.db.entity.EntityDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -73,7 +76,7 @@ public class ComponentActionIT { @Test public void empty_queue_and_empty_activity() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); userSession.addProjectPermission(UserRole.USER, project); Ce.ComponentResponse response = ws.newRequest() @@ -86,18 +89,18 @@ public class ComponentActionIT { @Test public void project_tasks() { - ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent(); - SnapshotDto analysisProject1 = db.components().insertSnapshot(project1); - ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, project1); - insertActivity("T1", project1, CeActivityDto.Status.SUCCESS, analysisProject1); - insertActivity("T2", project2, CeActivityDto.Status.FAILED, null); - insertActivity("T3", project1, CeActivityDto.Status.FAILED, null); - insertQueue("T4", project1, IN_PROGRESS); - insertQueue("T5", project1, PENDING); + ProjectData project1 = db.components().insertPrivateProject(); + SnapshotDto analysisProject1 = db.components().insertSnapshot(project1.getMainBranchComponent()); + ProjectData project2 = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, project1.getProjectDto()); + insertActivity("T1", project1.getMainBranchComponent(), project1.getProjectDto(), CeActivityDto.Status.SUCCESS, analysisProject1); + insertActivity("T2", project2.getMainBranchComponent(), project2.getProjectDto(), CeActivityDto.Status.FAILED, null); + insertActivity("T3", project1.getMainBranchComponent(), project1.getProjectDto(), CeActivityDto.Status.FAILED, null); + insertQueue("T4", project1.getMainBranchComponent(), project1.getProjectDto(), IN_PROGRESS); + insertQueue("T5", project1.getMainBranchComponent(), project1.getProjectDto(), PENDING); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, project1.getKey()) + .setParam(PARAM_COMPONENT, project1.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.getQueueCount()).isEqualTo(2); assertThat(response.getQueue(0).getId()).isEqualTo("T4"); @@ -113,13 +116,13 @@ public class ComponentActionIT { @Test public void search_tasks_by_component_key() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - logInWithBrowsePermission(project); - SnapshotDto analysis = db.components().insertSnapshot(project); - insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis); + ProjectData project = db.components().insertPrivateProject(); + logInWithBrowsePermission(project.getProjectDto()); + SnapshotDto analysis = db.components().insertSnapshot(project.getMainBranchComponent()); + insertActivity("T1", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.SUCCESS, analysis); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, project.getKey()) + .setParam(PARAM_COMPONENT, project.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.hasCurrent()).isTrue(); Ce.Task current = response.getCurrent(); @@ -131,13 +134,13 @@ public class ComponentActionIT { @Test public void search_tasks_by_component() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - logInWithBrowsePermission(project); - SnapshotDto analysis = db.components().insertSnapshot(project); - insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis); + ProjectData project = db.components().insertPrivateProject(); + logInWithBrowsePermission(project.getProjectDto()); + SnapshotDto analysis = db.components().insertSnapshot(project.getMainBranchComponent()); + insertActivity("T1", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.SUCCESS, analysis); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, project.getKey()) + .setParam(PARAM_COMPONENT, project.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.hasCurrent()).isTrue(); Ce.Task current = response.getCurrent(); @@ -149,16 +152,16 @@ public class ComponentActionIT { @Test public void canceled_tasks_must_not_be_picked_as_current_analysis() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, project); - insertActivity("T1", project, CeActivityDto.Status.SUCCESS); - insertActivity("T2", project, CeActivityDto.Status.FAILED); - insertActivity("T3", project, CeActivityDto.Status.SUCCESS); - insertActivity("T4", project, CeActivityDto.Status.CANCELED); - insertActivity("T5", project, CeActivityDto.Status.CANCELED); + ProjectData project = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, project.getProjectDto()); + insertActivity("T1", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.SUCCESS); + insertActivity("T2", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.FAILED); + insertActivity("T3", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.SUCCESS); + insertActivity("T4", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.CANCELED); + insertActivity("T5", project.getMainBranchComponent(), project.getProjectDto(), CeActivityDto.Status.CANCELED); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, project.getKey()) + .setParam(PARAM_COMPONENT, project.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.getQueueCount()).isZero(); // T3 is the latest task executed on PROJECT_1 ignoring Canceled ones @@ -171,35 +174,35 @@ public class ComponentActionIT { @Test public void branch_in_activity() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, project); + ProjectData project = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, project.getProjectDto()); String branchName = randomAlphanumeric(248); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); + ComponentDto branch = db.components().insertProjectBranch(project.getMainBranchComponent(), b -> b.setBranchType(BRANCH).setKey(branchName)); SnapshotDto analysis = db.components().insertSnapshot(branch); - CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); + CeActivityDto activity = insertActivity("T1", project.getMainBranchComponent(), project.getProjectDto(), SUCCESS, analysis); insertCharacteristic(activity, BRANCH_KEY, branchName); insertCharacteristic(activity, BRANCH_TYPE_KEY, BRANCH.name()); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, project.getKey()) + .setParam(PARAM_COMPONENT, project.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.getCurrent()) .extracting(Ce.Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getStatus, Ce.Task::getComponentKey, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( - "T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, project.getKey(), 0, emptyList()); + "T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, project.projectKey(), 0, emptyList()); } @Test public void branch_in_queue_analysis() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, project); + ProjectData project = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, project.getProjectDto()); String branchName = randomAlphanumeric(248); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); - CeQueueDto queue1 = insertQueue("T1", project, IN_PROGRESS); + ComponentDto branch = db.components().insertProjectBranch(project.getMainBranchComponent(), b -> b.setBranchType(BRANCH).setKey(branchName)); + CeQueueDto queue1 = insertQueue("T1", project.getMainBranchComponent(), project.getProjectDto(), IN_PROGRESS); insertCharacteristic(queue1, BRANCH_KEY, branchName); insertCharacteristic(queue1, BRANCH_TYPE_KEY, BRANCH.name()); - CeQueueDto queue2 = insertQueue("T2", project, PENDING); + CeQueueDto queue2 = insertQueue("T2", project.getMainBranchComponent(), project.getProjectDto(), PENDING); insertCharacteristic(queue2, BRANCH_KEY, branchName); insertCharacteristic(queue2, BRANCH_TYPE_KEY, BRANCH.name()); @@ -210,44 +213,46 @@ public class ComponentActionIT { assertThat(response.getQueueList()) .extracting(Ce.Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getStatus, Ce.Task::getComponentKey, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( - tuple("T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.IN_PROGRESS, project.getKey(), 0, emptyList()), - tuple("T2", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.PENDING, project.getKey(), 0, emptyList())); + tuple("T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.IN_PROGRESS, project.projectKey(), 0, emptyList()), + tuple("T2", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.PENDING, project.projectKey(), 0, emptyList())); } @Test public void return_many_tasks_from_same_project() { - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, project); - insertQueue("Main", project, IN_PROGRESS); + ProjectData project = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, project.getProjectDto()); + insertQueue("Main", project.getMainBranchComponent(), project.getProjectDto(), IN_PROGRESS); + String branchName1 = "Branch1"; - ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("branch1")); - CeQueueDto branchQueue1 = insertQueue("Branch1", project, IN_PROGRESS); + ComponentDto branch1 = db.components().insertProjectBranch(project.getMainBranchComponent(), b -> b.setBranchType(BRANCH).setKey("branch1")); + CeQueueDto branchQueue1 = insertQueue("Branch1", project.getMainBranchComponent(), project.getProjectDto(), IN_PROGRESS); insertCharacteristic(branchQueue1, BRANCH_KEY, branchName1); insertCharacteristic(branchQueue1, BRANCH_TYPE_KEY, BRANCH.name()); - ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("branch2")); + + ComponentDto branch2 = db.components().insertProjectBranch(project.getMainBranchComponent(), b -> b.setBranchType(BRANCH).setKey("branch2")); String branchName2 = "Branch2"; - CeQueueDto branchQueue2 = insertQueue("Branch2", project, PENDING); + CeQueueDto branchQueue2 = insertQueue("Branch2", project.getMainBranchComponent(), project.getProjectDto(), PENDING); insertCharacteristic(branchQueue2, BRANCH_KEY, branchName2); insertCharacteristic(branchQueue2, BRANCH_TYPE_KEY, BRANCH.name()); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, branch1.getKey()) + .setParam(PARAM_COMPONENT, project.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.getQueueList()) .extracting(Ce.Task::getId, Ce.Task::getComponentKey, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( - tuple("Main", project.getKey(), "", Common.BranchType.UNKNOWN_BRANCH_TYPE, 0, emptyList()), + tuple("Main", project.projectKey(), "", Common.BranchType.UNKNOWN_BRANCH_TYPE, 0, emptyList()), tuple("Branch1", branch1.getKey(), branchName1, Common.BranchType.BRANCH, 0, emptyList()), tuple("Branch2", branch2.getKey(), branchName2, Common.BranchType.BRANCH, 0, emptyList())); } @Test public void populates_warning_count_and_warnings_of_activities() { - ComponentDto privateProject = db.components().insertPrivateProject().getMainBranchComponent(); - userSession.addProjectPermission(UserRole.USER, privateProject); - SnapshotDto analysis = db.components().insertSnapshot(privateProject); - CeActivityDto activity = insertActivity("Branch", privateProject, SUCCESS, analysis); + ProjectData privateProject = db.components().insertPrivateProject(); + userSession.addProjectPermission(UserRole.USER, privateProject.getProjectDto()); + SnapshotDto analysis = db.components().insertSnapshot(privateProject.getMainBranchComponent()); + CeActivityDto activity = insertActivity("Branch", privateProject.getMainBranchComponent(), privateProject.getProjectDto(), SUCCESS, analysis); int messageCount = 5; List ceTaskMessageDtos = IntStream.range(0, messageCount) .mapToObj(i -> new CeTaskMessageDto() @@ -262,7 +267,7 @@ public class ComponentActionIT { db.commit(); Ce.ComponentResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT, privateProject.getKey()) + .setParam(PARAM_COMPONENT, privateProject.projectKey()) .executeProtobuf(Ce.ComponentResponse.class); assertThat(response.hasCurrent()).isTrue(); assertThat(response.getCurrent()) @@ -294,22 +299,22 @@ public class ComponentActionIT { @Test public void fail_when_no_component_parameter() { - logInWithBrowsePermission(db.components().insertPrivateProject().getMainBranchComponent()); + logInWithBrowsePermission(db.components().insertPrivateProject().getProjectDto()); TestRequest request = ws.newRequest(); assertThatThrownBy(request::execute) .isInstanceOf(IllegalArgumentException.class); } - private void logInWithBrowsePermission(ComponentDto project) { + private void logInWithBrowsePermission(ProjectDto project) { userSession.logIn().addProjectPermission(UserRole.USER, project); } - private CeQueueDto insertQueue(String taskUuid, ComponentDto component, CeQueueDto.Status status) { + private CeQueueDto insertQueue(String taskUuid, ComponentDto component, EntityDto entityDto, CeQueueDto.Status status) { CeQueueDto queueDto = new CeQueueDto(); queueDto.setTaskType(CeTaskTypes.REPORT); queueDto.setComponentUuid(component.uuid()); - queueDto.setEntityUuid(component.uuid()); + queueDto.setEntityUuid(entityDto.getUuid()); queueDto.setUuid(taskUuid); queueDto.setStatus(status); db.getDbClient().ceQueueDao().insert(db.getSession(), queueDto); @@ -317,15 +322,15 @@ public class ComponentActionIT { return queueDto; } - private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status) { - return insertActivity(taskUuid, component, status, db.components().insertSnapshot(component)); + private CeActivityDto insertActivity(String taskUuid, ComponentDto component, EntityDto entityDto, CeActivityDto.Status status) { + return insertActivity(taskUuid, component, entityDto, status, db.components().insertSnapshot(component)); } - private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status, @Nullable SnapshotDto analysis) { + private CeActivityDto insertActivity(String taskUuid, ComponentDto component, EntityDto entityDto, CeActivityDto.Status status, @Nullable SnapshotDto analysis) { CeQueueDto queueDto = new CeQueueDto(); queueDto.setTaskType(CeTaskTypes.REPORT); queueDto.setComponentUuid(component.uuid()); - queueDto.setEntityUuid(component.uuid()); + queueDto.setEntityUuid(entityDto.getUuid()); queueDto.setUuid(taskUuid); CeActivityDto activityDto = new CeActivityDto(queueDto); activityDto.setStatus(status); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/projectdump/ExportSubmitterImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/projectdump/ExportSubmitterImpl.java index 45223f39d80..96f4b5721db 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/projectdump/ExportSubmitterImpl.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/projectdump/ExportSubmitterImpl.java @@ -27,6 +27,7 @@ import org.sonar.ce.task.CeTask; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.ce.CeTaskTypes; +import org.sonar.db.component.ComponentDto; import org.sonar.db.project.ProjectDto; import static com.google.common.base.Preconditions.checkArgument; @@ -49,10 +50,12 @@ public class ExportSubmitterImpl implements ExportSubmitter { try (DbSession dbSession = dbClient.openSession(false)) { Optional project = dbClient.projectDao().selectProjectByKey(dbSession, projectKey); + Optional componentDto = dbClient.componentDao().selectByKey(dbSession, projectKey); checkArgument(project.isPresent(), "Project with key [%s] does not exist", projectKey); + checkArgument(componentDto.isPresent(), "Component with key [%s] does not exist", projectKey); CeTaskSubmit submit = ceQueue.prepareSubmit() - .setComponent(fromDto(project.get().getUuid(), project.get().getUuid())) + .setComponent(fromDto(componentDto.get().uuid(), project.get().getUuid())) .setType(CeTaskTypes.PROJECT_EXPORT) .setSubmitterUuid(submitterUuid) .setCharacteristics(emptyMap()) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java index 81427f7d77d..9a332ccbd7d 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java @@ -182,7 +182,7 @@ public class ReportSubmitter { dbClient.ceTaskInputDao().insert(dbSession, submit.getUuid(), reportInput); dbSession.commit(); submit.setType(CeTaskTypes.REPORT); - submit.setComponent(CeTaskSubmit.Component.fromDto(branch.uuid(), mainBranch.getUuid())); + submit.setComponent(CeTaskSubmit.Component.fromDto(branch.uuid(), mainBranch.getProjectUuid())); submit.setSubmitterUuid(userSession.getUuid()); submit.setCharacteristics(characteristics); return queue.submit(submit.build()); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ComponentAction.java index 7ea20e7da03..fb3d38625b7 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ComponentAction.java @@ -30,7 +30,7 @@ import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskQuery; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; import org.sonar.server.ws.KeyExamples; @@ -80,11 +80,12 @@ public class ComponentAction implements CeWsAction { @Override public void handle(Request wsRequest, Response wsResponse) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto component = loadComponent(dbSession, wsRequest); - userSession.checkComponentPermission(UserRole.USER, component); - List queueDtos = dbClient.ceQueueDao().selectByEntityUuid(dbSession, component.uuid()); + String entityKey = wsRequest.mandatoryParam(PARAM_COMPONENT); + EntityDto entityDto = componentFinder.getEntityByKey(dbSession, entityKey); + userSession.checkEntityPermission(UserRole.USER, entityDto); + List queueDtos = dbClient.ceQueueDao().selectByEntityUuid(dbSession, entityDto.getUuid()); CeTaskQuery activityQuery = new CeTaskQuery() - .setEntityUuid(component.uuid()) + .setEntityUuid(entityDto.getUuid()) .setOnlyCurrents(true); List activityDtos = dbClient.ceActivityDao().selectByQuery(dbSession, activityQuery, forPage(1).andSize(1)); @@ -96,9 +97,4 @@ public class ComponentAction implements CeWsAction { writeProtobuf(wsResponseBuilder.build(), wsRequest, wsResponse); } } - - private ComponentDto loadComponent(DbSession dbSession, Request wsRequest) { - String componentKey = wsRequest.mandatoryParam(PARAM_COMPONENT); - return componentFinder.getByKey(dbSession, componentKey); - } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index 56713dd745a..843183bd137 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -204,7 +204,7 @@ public class ComponentUpdater { private ProjectDto toProjectDto(ComponentDto component, long now) { return new ProjectDto() - .setUuid(useDifferentUuids ? uuidFactory.create() : component.uuid()) + .setUuid(uuidFactory.create()) .setKey(component.getKey()) .setQualifier(component.qualifier()) .setName(component.name()) -- 2.39.5