]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19558 Fix import/export of project_links to not depend on component
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Thu, 22 Jun 2023 12:50:17 +0000 (14:50 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 26 Jun 2023 20:03:55 +0000 (20:03 +0000)
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportLinksStepIT.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportLinksStep.java
server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto
server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectExportMapper.xml
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ProjectLinkDbTester.java

index 2d54cea746b1e4bcdc241f32be0747e98e6e568d..117e24e288b998b4bda6efa32f49bdff88ebc70c 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.ce.task.step.TestComputationStepContext;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ProjectLinkDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.project.ProjectExportMapper;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -65,16 +66,15 @@ public class ExportLinksStepIT {
   public LogTester logTester = new LogTester();
 
   private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
-  private final ComponentRepository componentRepository = mock(ComponentRepository.class);
   private final ProjectHolder projectHolder = mock(ProjectHolder.class);
-  private final ExportLinksStep underTest = new ExportLinksStep(db.getDbClient(), componentRepository, projectHolder, dumpWriter);
+  private final ExportLinksStep underTest = new ExportLinksStep(db.getDbClient(), projectHolder, dumpWriter);
+  private ProjectDto project;
 
   @Before
   public void setUp() {
     logTester.setLevel(Level.DEBUG);
-    ComponentDto project = db.components().insertPublicProject(PROJECT).getMainBranchComponent();
-    when(projectHolder.projectDto()).thenReturn(db.components().getProjectDtoByMainBranch(project));
-    when(componentRepository.getRef(PROJECT_UUID)).thenReturn(1L);
+    project = db.components().insertPublicProject(PROJECT).getProjectDto();
+    when(projectHolder.projectDto()).thenReturn(project);
   }
 
   @Test
@@ -87,9 +87,9 @@ public class ExportLinksStepIT {
 
   @Test
   public void export_links() {
-    ProjectLinkDto link1 = db.componentLinks().insertCustomLink(PROJECT);
-    ProjectLinkDto link2 = db.componentLinks().insertProvidedLink(PROJECT);
-    db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getMainBranchComponent());
+    ProjectLinkDto link1 = db.componentLinks().insertCustomLink(project);
+    ProjectLinkDto link2 = db.componentLinks().insertProvidedLink(project);
+    db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getProjectDto());
 
     underTest.execute(new TestComputationStepContext());
 
@@ -103,10 +103,10 @@ public class ExportLinksStepIT {
 
   @Test
   public void throws_ISE_if_error() {
-    db.componentLinks().insertCustomLink(PROJECT);
-    db.componentLinks().insertProvidedLink(PROJECT);
-    db.componentLinks().insertProvidedLink(PROJECT);
-    db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getMainBranchComponent());
+    db.componentLinks().insertCustomLink(project);
+    db.componentLinks().insertProvidedLink(project);
+    db.componentLinks().insertProvidedLink(project);
+    db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getProjectDto());
 
     dumpWriter.failIfMoreThan(2, DumpElement.LINKS);
 
@@ -117,7 +117,7 @@ public class ExportLinksStepIT {
 
   @Test
   public void test_all_fields() {
-    ProjectLinkDto link = db.componentLinks().insertCustomLink(PROJECT, l -> l.setName("name").setHref("href").setType("type"));
+    ProjectLinkDto link = db.componentLinks().insertCustomLink(project, l -> l.setName("name").setHref("href").setType("type"));
 
     underTest.execute(new TestComputationStepContext());
 
index d0b96486d882ba68707783fc2faad5e706c8cbe3..6bee8e5070ef584aee21d584ef1d5a91aa60a0c6 100644 (file)
@@ -35,13 +35,11 @@ import static org.apache.commons.lang.StringUtils.defaultString;
 public class ExportLinksStep implements ComputationStep {
 
   private final DbClient dbClient;
-  private final ComponentRepository componentRepository;
   private final ProjectHolder projectHolder;
   private final DumpWriter dumpWriter;
 
-  public ExportLinksStep(DbClient dbClient, ComponentRepository componentRepository, ProjectHolder projectHolder, DumpWriter dumpWriter) {
+  public ExportLinksStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
     this.dbClient = dbClient;
-    this.componentRepository = componentRepository;
     this.projectHolder = projectHolder;
     this.dumpWriter = dumpWriter;
   }
@@ -61,8 +59,7 @@ public class ExportLinksStep implements ComputationStep {
             .setUuid(link.getUuid())
             .setName(defaultString(link.getName()))
             .setHref(defaultString(link.getHref()))
-            .setType(defaultString(link.getType()))
-            .setComponentRef(componentRepository.getRef(link.getProjectUuid()));
+            .setType(defaultString(link.getType()));
           linksWriter.write(builder.build());
           ++count;
         }
index 7e4e29da8fcdc7595e04836f6bb3401521251c3e..854edbbeeb3d85f9245b5e783cc7483352e139ec 100644 (file)
@@ -185,7 +185,7 @@ message Link {
   string name = 2;
   string href = 3;
   string uuid = 4;
-  int64 component_ref = 5;
+  reserved 5; //component_ref
 }
 
 message Event {
index 64ced52a1b9f39b0b6bace40163928bd4c61a546..6d6a9b969bc0469b6578f739cfca547129cc6963 100644 (file)
@@ -36,9 +36,7 @@
         p.created_at as "createdAt",
         p.updated_at as "updatedAt"
         from project_links p
-        join components proj on proj.uuid = p.project_uuid
-        join project_branches pb on pb.uuid = proj.uuid
-        where pb.project_uuid=#{projectUuid,jdbcType=VARCHAR} and pb.branch_type='BRANCH' and pb.exclude_from_purge = ${_true}
+        where p.project_uuid=#{projectUuid,jdbcType=VARCHAR}
         order by p.uuid
     </select>
 
index 001be344fcef475718aea5f3b8aa7942f19c7749..1e1542aeb8e435386cf3702c358cf4b3132277e0 100644 (file)
@@ -24,6 +24,7 @@ import java.util.function.Consumer;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.project.ProjectDto;
 
 import static org.sonar.db.component.ProjectLinkTesting.newCustomLinkDto;
 import static org.sonar.db.component.ProjectLinkTesting.newProvidedLinkDto;
@@ -56,4 +57,23 @@ public class ProjectLinkDbTester {
     db.commit();
     return componentLink;
   }
+
+  @SafeVarargs
+  public final ProjectLinkDto insertProvidedLink(ProjectDto project, Consumer<ProjectLinkDto>... dtoPopulators) {
+    return insertLink(project, newProvidedLinkDto(), dtoPopulators);
+  }
+
+  @SafeVarargs
+  public final ProjectLinkDto insertCustomLink(ProjectDto project, Consumer<ProjectLinkDto>... dtoPopulators) {
+    return insertLink(project, newCustomLinkDto(), dtoPopulators);
+  }
+
+  @SafeVarargs
+  private final ProjectLinkDto insertLink(ProjectDto project, ProjectLinkDto componentLink, Consumer<ProjectLinkDto>... dtoPopulators) {
+    Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(componentLink));
+    dbClient.projectLinkDao().insert(dbSession, componentLink.setProjectUuid(project.getUuid()));
+    db.commit();
+    return componentLink;
+  }
+
 }