]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11463 Don't consider disabled components during migration
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 12 Dec 2018 10:41:20 +0000 (11:41 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 16 Jan 2019 08:43:09 +0000 (09:43 +0100)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ComponentUuidFactoryWithMigration.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ComponentUuidFactoryWithMigrationTest.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index 5ffc8b1c360add7a292e4ed73ec392e25e1110ea..c42465eb668f4e1f4aa7fd2ed21b281413bbde34 100644 (file)
@@ -99,7 +99,7 @@ public class ComponentUuidFactoryWithMigration implements ComponentUuidFactory {
   }
 
   private static List<ComponentWithModuleUuidDto> loadComponentsWithModuleUuid(DbClient dbClient, DbSession dbSession, String rootKey) {
-    return dbClient.componentDao().selectComponentsWithModuleUuidFromProjectKey(dbSession, rootKey);
+    return dbClient.componentDao().selectEnabledComponentsWithModuleUuidFromProjectKey(dbSession, rootKey);
   }
 
   private static Map<String, String> loadModulePathsByUuid(DbClient dbClient, DbSession dbSession, String rootKey, Map<String, String> pathByModuleKey) {
index 05b6dd760a201e8c6d101c3f1031ba7810f58fc6..b394b9c600ad2decc5d1c3ebc00789130aabcd1d 100644 (file)
@@ -85,6 +85,54 @@ public class ComponentUuidFactoryWithMigrationTest {
     assertThat(underTest.getOrCreateForKey(file2.getDbKey())).isNotIn(project.uuid(), module1.uuid(), module2.uuid(), file1.uuid(), file2.uuid());
   }
 
+  @Test
+  public void migrate_project_with_disabled_components_no_path() {
+    ComponentDto project = db.components().insertPrivateProject(dto -> dto.setDbKey("project"));
+    ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
+      .setDbKey("project:module1"));
+    ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
+      .setDbKey("project:file1")
+      .setPath("file1"));
+    ComponentDto disabledFileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project)
+      .setDbKey("project:file2")
+      .setPath(null)
+      .setEnabled(false));
+
+    Map<String, String> modulesRelativePaths = new HashMap<>();
+    modulesRelativePaths.put("project:module1", "module1_path");
+    ComponentUuidFactoryWithMigration underTest = new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getDbKey(), pathToKey, modulesRelativePaths);
+
+    // migrated files
+    assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
+
+    // project remains the same
+    assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid());
+  }
+
+  @Test
+  public void migrate_project_with_disabled_components_same_path() {
+    ComponentDto project = db.components().insertPrivateProject(dto -> dto.setDbKey("project"));
+    ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
+      .setDbKey("project:module1"));
+    ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
+      .setDbKey("project:file1")
+      .setPath("file1"));
+    ComponentDto disabledFileSamePath = db.components().insertComponent(ComponentTesting.newFileDto(project)
+      .setDbKey("project:file2")
+      .setPath("file1")
+      .setEnabled(false));
+
+    Map<String, String> modulesRelativePaths = new HashMap<>();
+    modulesRelativePaths.put("project:module1", "module1_path");
+    ComponentUuidFactoryWithMigration underTest = new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getDbKey(), pathToKey, modulesRelativePaths);
+
+    // migrated files
+    assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
+
+    // project remains the same
+    assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid());
+  }
+
   @Test
   public void migrate_branch_with_modules() {
     pathToKey = path -> path != null ? "project:" + path + ":BRANCH:branch1" : "project:BRANCH:branch1";
@@ -143,24 +191,6 @@ public class ComponentUuidFactoryWithMigrationTest {
     assertThat(underTest.getOrCreateForKey("project:module1")).isNotIn(project.uuid(), module1.uuid(), dir1.uuid());
   }
 
-  @Test
-  public void migrate_project_with_disabled_modules() {
-    ComponentDto project = db.components().insertPrivateProject(dto -> dto.setDbKey("project"));
-    ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
-      .setDbKey("project:module1")
-      .setEnabled(false));
-    ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(module1)
-      .setDbKey("project:file1")
-      .setEnabled(false)
-      .setPath("file1_path"));
-    Map<String, String> modulesRelativePaths = Collections.singletonMap("project:module1", "module1_path");
-
-    ComponentUuidFactoryWithMigration underTest = new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getDbKey(), pathToKey, modulesRelativePaths);
-
-    // migrated file
-    assertThat(underTest.getOrCreateForKey("project:module1_path/file1_path")).isEqualTo(file1.uuid());
-  }
-
   @Test
   public void dont_override_root_uuid_if_module_path_is_not_sent() {
     ComponentDto project = db.components().insertPrivateProject(dto -> dto.setDbKey("project"));
index 56d4d24a09b933f2121bfd94548a9c1268dd0dae..263cd660c5e08b3e796579694b96bc0ac864fe20 100644 (file)
@@ -433,37 +433,6 @@ public class BuildComponentTreeStepTest {
     verifyComponentByRef(FILE_1_REF, REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1);
   }
 
-  @Test
-  public void return_existing_uuids_when_components_were_removed() {
-    setAnalysisMetadataHolder();
-    OrganizationDto organizationDto = dbTester.organizations().insert();
-    ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD")
-      .setDbKey(REPORT_PROJECT_KEY));
-    ComponentDto removedModule = insertComponent(newModuleDto("BCDE", project)
-      .setDbKey(REPORT_MODULE_KEY).setEnabled(false));
-    ComponentDto removedDirectory = insertComponent(newDirectory(removedModule, "CDEF", REPORT_DIR_PATH_1)
-      .setDbKey(REPORT_MODULE_KEY + ":" + REPORT_DIR_PATH_1).setEnabled(false));
-    insertComponent(newFileDto(removedModule, removedDirectory, "DEFG")
-      .setDbKey(REPORT_MODULE_KEY + ":" + REPORT_FILE_PATH_1).setPath(REPORT_FILE_PATH_1).setEnabled(false));
-
-    reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY, FILE_1_REF));
-    reportReader.putComponent(componentWithPath(FILE_1_REF, FILE, "module/" + REPORT_FILE_PATH_1));
-
-    reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
-      .putModulesProjectRelativePathByKey(REPORT_PROJECT_KEY, "")
-      .putModulesProjectRelativePathByKey(REPORT_MODULE_KEY, "module")
-      .build());
-
-    underTest.execute(new TestComputationStepContext());
-
-    verifyComponentByRef(ROOT_REF, REPORT_PROJECT_KEY, "ABCD");
-
-    // No new UUID is generated on removed components
-    verifyComponentMissingByRef(MODULE_REF);
-    verifyComponentByKey(REPORT_PROJECT_KEY + ":module/" + REPORT_DIR_PATH_1, REPORT_PROJECT_KEY + ":module/" + REPORT_DIR_PATH_1, "CDEF");
-    verifyComponentByRef(FILE_1_REF, REPORT_PROJECT_KEY + ":module/" + REPORT_FILE_PATH_1, "DEFG");
-  }
-
   @Test
   public void set_no_base_project_snapshot_when_no_snapshot() {
     setAnalysisMetadataHolder();
index aed5fc1138a800198bafefcff6c012a5f97e87bd..9ff0aa9e17447c5892520265f790c579c5a466d6 100644 (file)
@@ -355,8 +355,8 @@ public class ComponentDao implements Dao {
     return new HashSet<>(mapper(dbSession).selectComponentsByQualifiers(qualifiers));
   }
 
-  public List<ComponentWithModuleUuidDto> selectComponentsWithModuleUuidFromProjectKey(DbSession dbSession, String projectKey) {
-    return mapper(dbSession).selectComponentsWithModuleUuidFromProjectKey(projectKey);
+  public List<ComponentWithModuleUuidDto> selectEnabledComponentsWithModuleUuidFromProjectKey(DbSession dbSession, String projectKey) {
+    return mapper(dbSession).selectEnabledComponentsWithModuleUuidFromProjectKey(projectKey);
   }
 
   public List<ComponentDto> selectProjectsByNameQuery(DbSession dbSession, @Nullable String nameQuery, boolean includeModules) {
index 7be4d074673256e7af294e98184c797f9e363a4b..86cac939a71d0af103575261476642a5e6afdb39 100644 (file)
@@ -167,5 +167,5 @@ public interface ComponentMapper {
 
   List<ProjectNclocDistributionDto> selectPrivateProjectsWithNcloc(@Param("organizationUuid") String organizationUuid);
 
-  List<ComponentWithModuleUuidDto> selectComponentsWithModuleUuidFromProjectKey(String projectKey);
+  List<ComponentWithModuleUuidDto> selectEnabledComponentsWithModuleUuidFromProjectKey(String projectKey);
 }
index e5c276400432c42b6fd62e74941f64eac4181d26..b0aaf491f63d2eaf17250a2d5b95797c48342164 100644 (file)
     </where>
   </select>
 
-  <select id="selectComponentsWithModuleUuidFromProjectKey" resultType="ComponentWithModuleUuid">
+  <select id="selectEnabledComponentsWithModuleUuidFromProjectKey" resultType="ComponentWithModuleUuid">
     SELECT
       p.uuid as uuid, p.module_uuid as moduleUuid, p.path as path, p.scope as scope
     FROM
       projects p
     INNER JOIN
-      projects root ON root.uuid=p.project_uuid AND root.kee=#{projectKey,jdbcType=VARCHAR}
+      projects root ON root.uuid=p.project_uuid AND p.enabled = ${_true} AND root.kee=#{projectKey,jdbcType=VARCHAR}
   </select>
   
   <select id="selectUuidsByKeyFromProjectKey" parameterType="string" resultType="KeyWithUuid">
index a2a8964e82e957f0275445725d42f6c3aa49fef1..ae64c7f0d11c47a5a07c0d64440175a1fcea9874 100644 (file)
@@ -528,7 +528,7 @@ public class ComponentDaoTest {
   }
 
   @Test
-  public void select_components_with_module_dto() {
+  public void select_enabled_components_with_module_dto() {
     ComponentDto project = db.components().insertPrivateProject();
     ComponentDto module = db.components().insertComponent(newModuleDto(project));
     ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
@@ -540,18 +540,14 @@ public class ComponentDaoTest {
     ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
 
     // From root project
-    assertThat(underTest.selectComponentsWithModuleUuidFromProjectKey(dbSession, project.getDbKey()))
+    assertThat(underTest.selectEnabledComponentsWithModuleUuidFromProjectKey(dbSession, project.getDbKey()))
       .extracting(ComponentWithModuleUuidDto::uuid)
       .containsExactlyInAnyOrder(
         project.uuid(),
         module.uuid(),
-        removedModule.uuid(),
         subModule.uuid(),
-        removedSubModule.uuid(),
         directory.uuid(),
-        removedDirectory.uuid(),
-        file.uuid(),
-        removedFile.uuid()
+        file.uuid()
       );
   }