]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20699 Do not auto-bind if auto-provisionig is on
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Fri, 13 Oct 2023 08:40:53 +0000 (10:40 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 20 Oct 2023 20:02:40 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java

index d820b52dcb393dd019b1222fe4774dbfa244c962..06da5572a64deb88cd542274718d05ba66d778de 100644 (file)
@@ -54,6 +54,7 @@ import org.sonar.server.component.ComponentCreationParameters;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.favorite.FavoriteUpdater;
+import org.sonar.server.management.ManagedInstanceService;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.project.ProjectDefaultVisibility;
 import org.sonar.server.project.Visibility;
@@ -109,8 +110,9 @@ public class BranchReportSubmitterIT {
   private final DevOpsPlatformService devOpsPlatformService = new GitHubDevOpsPlatformService(db.getDbClient(), null,
     null, projectDefaultVisibility, null, userSession, componentUpdater, null);
 
+  private final ManagedInstanceService managedInstanceService = mock();
   private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), branchSupport,
-    projectDefaultVisibility, devOpsPlatformService);
+    projectDefaultVisibility, devOpsPlatformService, managedInstanceService);
 
   @Before
   public void before() {
index 22507bb5a15d9eacdcfd03ea2ae3ecd608b34d57..3a30c77763d286897bdbe477d880a13c0b87ad4c 100644 (file)
@@ -62,6 +62,7 @@ import org.sonar.server.es.TestIndexers;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.favorite.FavoriteUpdater;
+import org.sonar.server.management.ManagedInstanceService;
 import org.sonar.server.permission.PermissionService;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.permission.PermissionUpdater;
@@ -126,8 +127,10 @@ public class ReportSubmitterIT {
 
   private final DevOpsPlatformService devOpsPlatformServiceSpy = spy(devOpsPlatformService);
 
+  private final ManagedInstanceService managedInstanceService = mock();
+
   private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), ossEditionBranchSupport,
-    projectDefaultVisibility, devOpsPlatformServiceSpy);
+    projectDefaultVisibility, devOpsPlatformServiceSpy, managedInstanceService);
 
   @Before
   public void before() {
@@ -268,12 +271,7 @@ public class ReportSubmitterIT {
 
     underTest.submit(PROJECT_KEY, PROJECT_NAME, emptyMap(), IOUtils.toInputStream("{binary}", UTF_8));
 
-    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), PROJECT_KEY).orElseThrow();
-    assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.SCANNER_API);
-    assertThat(projectDto.getName()).isEqualTo(PROJECT_NAME);
-
-    BranchDto branchDto = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), projectDto.getUuid(), "main").orElseThrow();
-    assertThat(branchDto.isMain()).isTrue();
+    assertLocalProjectWasCreated();
   }
 
   @Test
@@ -288,6 +286,27 @@ public class ReportSubmitterIT {
 
     underTest.submit(PROJECT_KEY, PROJECT_NAME, characteristics, IOUtils.toInputStream("{binary}", UTF_8));
 
+    assertLocalProjectWasCreated();
+  }
+
+  @Test
+  public void submit_whenReportIsForANewProjectWithValidAlmSettingsButAutoProvisioningOn_createsLocalProject() {
+    userSession.addPermission(GlobalPermission.SCAN).addPermission(PROVISION_PROJECTS);
+    when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
+    when(permissionTemplateService.wouldUserHaveScanPermissionWithDefaultTemplate(any(DbSession.class), any(), eq(PROJECT_KEY))).thenReturn(true);
+    mockSuccessfulPrepareSubmitCall();
+
+    Map<String, String> characteristics = Map.of("random", "data");
+    DevOpsProjectDescriptor projectDescriptor = new DevOpsProjectDescriptor(ALM.GITHUB, "apiUrl", "orga/repo");
+
+    mockInteractionsWithDevOpsPlatformServiceSpyBeforeProjectCreation(characteristics, projectDescriptor);
+
+    underTest.submit(PROJECT_KEY, PROJECT_NAME, characteristics, IOUtils.toInputStream("{binary}", UTF_8));
+
+    assertLocalProjectWasCreated();
+  }
+
+  private void assertLocalProjectWasCreated() {
     ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), PROJECT_KEY).orElseThrow();
     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.SCANNER_API);
     assertThat(projectDto.getName()).isEqualTo(PROJECT_NAME);
@@ -311,6 +330,10 @@ public class ReportSubmitterIT {
 
     underTest.submit(PROJECT_KEY, PROJECT_NAME, characteristics, IOUtils.toInputStream("{binary}", UTF_8));
 
+    assertProjectWasCreatedWithBinding();
+  }
+
+  private void assertProjectWasCreatedWithBinding() {
     ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), PROJECT_KEY).orElseThrow();
     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.SCANNER_API_DEVOPS_AUTO_CONFIG);
     assertThat(projectDto.getName()).isEqualTo("repoName");
index a34ce3c87ac4392c8f3e0dd2e11ca18efd6fcebe..348c8a1615729443202e98fa5facf57997f173b9 100644 (file)
@@ -46,6 +46,7 @@ import org.sonar.server.component.ComponentCreationParameters;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.component.NewComponent;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.management.ManagedInstanceService;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.project.ProjectDefaultVisibility;
 import org.sonar.server.user.UserSession;
@@ -67,10 +68,11 @@ public class ReportSubmitter {
   private final BranchSupport branchSupport;
   private final ProjectDefaultVisibility projectDefaultVisibility;
   private final DevOpsPlatformService devOpsPlatformService;
+  private final ManagedInstanceService managedInstanceService;
 
   public ReportSubmitter(CeQueue queue, UserSession userSession, ComponentUpdater componentUpdater,
     PermissionTemplateService permissionTemplateService, DbClient dbClient, BranchSupport branchSupport, ProjectDefaultVisibility projectDefaultVisibility,
-    DevOpsPlatformService devOpsPlatformService) {
+    DevOpsPlatformService devOpsPlatformService, ManagedInstanceService managedInstanceService) {
     this.queue = queue;
     this.userSession = userSession;
     this.componentUpdater = componentUpdater;
@@ -79,6 +81,7 @@ public class ReportSubmitter {
     this.branchSupport = branchSupport;
     this.projectDefaultVisibility = projectDefaultVisibility;
     this.devOpsPlatformService = devOpsPlatformService;
+    this.managedInstanceService = managedInstanceService;
   }
 
   public CeTask submit(String projectKey, @Nullable String projectName, Map<String, String> characteristics, InputStream reportInput) {
@@ -168,6 +171,10 @@ public class ReportSubmitter {
       throw insufficientPrivilegesException();
     }
 
+    if (managedInstanceService.isInstanceExternallyManaged()) {
+      return createProject(dbSession, componentKey.getKey(), defaultIfBlank(projectName, projectKey));
+    }
+
     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = devOpsPlatformService.getDevOpsProjectDescriptor(characteristics);
     Optional<AlmSettingDto> almSettingDto = devOpsProjectDescriptor.flatMap(descriptor -> devOpsPlatformService.getValidAlmSettingDto(dbSession, descriptor));
     if (almSettingDto.isPresent()) {