]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20683 fixed the issue where adding project to portfolio would later add an...
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>
Thu, 19 Oct 2023 08:11:22 +0000 (10:11 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 20 Oct 2023 20:02:39 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepIT.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java

index 1972a83bb760b8eef4fc88fe6d4d50adaa89fbbf..d0ad21e6cd7bc4f9f8fb6f4a3b4b0be90353ac3d 100644 (file)
@@ -46,13 +46,17 @@ import org.sonar.ce.task.step.ComputationStep;
 import org.sonar.ce.task.step.TestComputationStepContext;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
+import org.sonar.db.audit.AuditPersister;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION;
 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO;
@@ -78,8 +82,10 @@ public class ViewsPersistComponentsStepIT extends BaseStepTest {
   private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
   private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
 
+  private final AuditPersister auditPersister = mock();
+
   @Rule
-  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+  public DbTester dbTester = DbTester.create(System2.INSTANCE, auditPersister);
 
   @Rule
   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
@@ -313,6 +319,23 @@ public class ViewsPersistComponentsStepIT extends BaseStepTest {
     assertDtoIsProjectView1(pv1Dto, view, view, project);
   }
 
+  @Test
+  public void execute_whenPortfolioProjectAndTechnicalProjectIsCreated_shouldStoreTwoAuditLogs() {
+    ComponentDto view = newViewDto();
+    ComponentDto project = ComponentTesting.newPrivateProjectDto();
+    persistComponents(view, project);
+
+    treeRootHolder.setRoot(
+      createViewBuilder(PORTFOLIO)
+        .addChildren(createProjectView1Builder(project, null).build())
+        .build());
+
+    underTest.execute(new TestComputationStepContext());
+
+    //We create audits for the portfolio and the project, not for the technical project
+    verify(auditPersister, times(2)).addComponent(any(), any());
+  }
+
   @Test
   public void update_copy_component_uuid_of_project_view() {
     ComponentDto view = newViewDto();
index 21b4d1dfbf582c8c78ac9fdbc6654d6539be7dd7..e1070725c210ca376a4676c4257873fc84294571 100644 (file)
@@ -200,16 +200,20 @@ public class PersistComponentsStep implements ComputationStep {
     @Override
     public void visitProjectView(Component projectView, Path<ComponentDtoHolder> path) {
       ComponentDto dto = createForProjectView(projectView, path);
-      persistComponent(dto);
+      persistComponent(dto, false);
     }
 
     private ComponentDto persistComponent(ComponentDto componentDto) {
+      return persistComponent(componentDto, true);
+    }
+
+    private ComponentDto persistComponent(ComponentDto componentDto, boolean shouldPersistAudit) {
       ComponentDto existingComponent = existingComponentDtosByUuids.remove(componentDto.uuid());
       if (existingComponent == null) {
         if (componentDto.qualifier().equals("APP") && componentDto.scope().equals("PRJ")) {
           throw new IllegalStateException("Application should already exists: " + componentDto);
         }
-        dbClient.componentDao().insert(dbSession, componentDto, analysisMetadataHolder.getBranch().isMain());
+        dbClient.componentDao().insert(dbSession, componentDto, shouldPersistAudit);
         return componentDto;
       }
       Optional<ComponentUpdateDto> update = compareForUpdate(existingComponent, componentDto);