From 30f01907496dbda0b3d177748501dc2f0833813f Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Fri, 13 Oct 2023 10:40:53 +0200 Subject: [PATCH] SONAR-20699 Do not auto-bind if auto-provisionig is on --- .../ce/queue/BranchReportSubmitterIT.java | 4 +- .../server/ce/queue/ReportSubmitterIT.java | 37 +++++++++++++++---- .../server/ce/queue/ReportSubmitter.java | 9 ++++- 3 files changed, 41 insertions(+), 9 deletions(-) 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 d820b52dcb3..06da5572a64 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 @@ -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() { 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 22507bb5a15..3a30c77763d 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 @@ -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 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"); 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 a34ce3c87ac..348c8a16157 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 @@ -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 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 = devOpsPlatformService.getDevOpsProjectDescriptor(characteristics); Optional almSettingDto = devOpsProjectDescriptor.flatMap(descriptor -> devOpsPlatformService.getValidAlmSettingDto(dbSession, descriptor)); if (almSettingDto.isPresent()) { -- 2.39.5