]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20853 Set visibility from GitHub when creating a project via scanner.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Wed, 1 Nov 2023 14:43:40 +0000 (15:43 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 2 Nov 2023 20:02:42 +0000 (20:02 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedServices.java
server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedProjectService.java
server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedServicesTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectCreator.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorTest.java

index 26d0f0493419adcadba2049eb8b96526eb9c0d5f..35038518b4d4c9ace6401306040cab71d1cd156c 100644 (file)
@@ -130,6 +130,13 @@ public class DelegatingManagedServices implements ManagedInstanceService, Manage
       .ifPresent(managedProjectService -> managedProjectService.queuePermissionSyncTask(submitterUuid, componentUuid, projectUuid));
   }
 
+  @Override
+  public boolean isProjectVisibilitySynchronizationActivated() {
+    return findManagedProjectService()
+      .map(ManagedProjectService::isProjectVisibilitySynchronizationActivated)
+      .orElse(false);
+  }
+
   private Optional<ManagedProjectService> findManagedProjectService() {
     return findManagedInstanceService()
       .filter(ManagedProjectService.class::isInstance)
index 9b4c3b88b80e1d0b95002a3dcbb055205ad1a58b..0b50712facb850c243c7b3153ab79b3bd3ec466d 100644 (file)
@@ -30,4 +30,6 @@ public interface ManagedProjectService {
   boolean isProjectManaged(DbSession dbSession, String projectUuid);
 
   void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid);
+
+  boolean isProjectVisibilitySynchronizationActivated();
 }
index 92e29cb833c74f1d27c61f54c26376e24d2c8803..04bff1e2917c0d51a15a0f6a13953deabaabc64d 100644 (file)
@@ -278,6 +278,18 @@ public class DelegatingManagedServicesTest {
     verify(alwaysManagedInstanceService).queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid");
   }
 
+  @Test
+  public void isProjectVisibilitySynchronizationActivated_whenManagedInstanceServices_shouldDelegatesToRightService() {
+    DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
+
+    assertThat(managedInstanceService.isProjectVisibilitySynchronizationActivated()).isTrue();
+  }
+
+  @Test
+  public void isProjectVisibilitySynchronizationActivated_whenManagedNoInstanceServices_returnsFalse() {
+    assertThat(NO_MANAGED_SERVICES.isProjectVisibilitySynchronizationActivated()).isFalse();
+  }
+
   private static class NeverManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
 
     @Override
@@ -334,6 +346,11 @@ public class DelegatingManagedServicesTest {
     public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
 
     }
+
+    @Override
+    public boolean isProjectVisibilitySynchronizationActivated() {
+      return false;
+    }
   }
 
   private static class AlwaysManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
@@ -392,6 +409,11 @@ public class DelegatingManagedServicesTest {
     public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
 
     }
+
+    @Override
+    public boolean isProjectVisibilitySynchronizationActivated() {
+      return true;
+    }
   }
 
 }
index df7a6b58cbda75fc9a1be66fea669854c53a4353..0aeff1c330e0abd3e3537494e63866212bd34eeb 100644 (file)
@@ -164,7 +164,15 @@ public class GithubProjectCreator implements DevOpsProjectCreator {
   private ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, @Nullable String projectKey, AlmSettingDto almSettingDto,
     GithubApplicationClient.Repository repository, CreationMethod creationMethod) {
     String key = Optional.ofNullable(projectKey).orElse(getUniqueProjectKey(repository));
-    ComponentCreationData componentCreationData = projectCreator.createProject(dbSession, key, repository.getName(), repository.getDefaultBranch(), creationMethod);
+
+    boolean isPrivate;
+    if (managedProjectService.isProjectVisibilitySynchronizationActivated()) {
+      isPrivate = repository.isPrivate();
+    } else {
+      isPrivate = true;
+    }
+
+    ComponentCreationData componentCreationData = projectCreator.createProject(dbSession, key, repository.getName(), repository.getDefaultBranch(), creationMethod, isPrivate);
     ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
     createProjectAlmSettingDto(dbSession, repository, projectDto, almSettingDto);
     addScanPermissionToCurrentUser(dbSession, projectDto);
index 73c913a43ed4d9c20cc6353d8609e416f5052acb..be86eb9204c403b1edb448165818927ad9b1056d 100644 (file)
@@ -50,11 +50,13 @@ public class ProjectCreator {
     this.componentUpdater = componentUpdater;
   }
 
-  public ComponentCreationData createProject(DbSession dbSession, String projectKey, String projectName, @Nullable String mainBranchName, CreationMethod creationMethod) {
+  public ComponentCreationData createProject(DbSession dbSession, String projectKey, String projectName, @Nullable String mainBranchName, CreationMethod creationMethod,
+    @Nullable Boolean isPrivate) {
+    boolean visibility = isPrivate != null ? isPrivate : projectDefaultVisibility.get(dbSession).isPrivate();
     NewComponent projectComponent = newComponentBuilder()
       .setKey(projectKey)
       .setName(projectName)
-      .setPrivate(projectDefaultVisibility.get(dbSession).isPrivate())
+      .setPrivate(visibility)
       .setQualifier(PROJECT)
       .build();
     ComponentCreationParameters componentCreationParameters = ComponentCreationParameters.builder()
@@ -67,4 +69,8 @@ public class ProjectCreator {
       .build();
     return componentUpdater.createWithoutCommit(dbSession, componentCreationParameters);
   }
+
+  public ComponentCreationData createProject(DbSession dbSession, String projectKey, String projectName, @Nullable String mainBranchName, CreationMethod creationMethod) {
+    return createProject(dbSession, projectKey, projectName, mainBranchName, creationMethod, projectDefaultVisibility.get(dbSession).isPrivate());
+  }
 }
index de98ec56e6e5e1ddffbcbb7e81f76b4a6ecd08e7..372c7525f596bda17cd3459bdd3bd4a373885354 100644 (file)
@@ -290,13 +290,55 @@ public class GithubProjectCreatorTest {
     ComponentCreationParameters componentCreationParameters = componentCreationParametersCaptor.getValue();
     assertComponentCreationParametersContainsCorrectInformation(componentCreationParameters, "generated_orga2/repo1", SCANNER_API_DEVOPS_AUTO_CONFIG);
     assertThat(componentCreationParameters.isManaged()).isFalse();
-    assertThat(componentCreationParameters.newComponent().isPrivate()).isFalse();
+    assertThat(componentCreationParameters.newComponent().isPrivate()).isTrue();
 
     verify(projectAlmSettingDao).insertOrUpdate(any(), projectAlmSettingDtoCaptor.capture(), eq(ALM_SETTING_KEY), eq(REPOSITORY_NAME), eq("generated_orga2/repo1"));
     ProjectAlmSettingDto projectAlmSettingDto = projectAlmSettingDtoCaptor.getValue();
     assertAlmSettingsDtoContainsCorrectInformation(almSettingDto, requireNonNull(componentCreationData.projectDto()), projectAlmSettingDto);
   }
 
+  @Test
+  public void createProjectAndBindToDevOpsPlatformFromScanner_whenRepoFoundOnGitHubAndVisibilitySynchronizationEnabled_successfullyCreatesProjectAndSetsVisibility() {
+    // given
+    mockPublicGithubRepository();
+
+    ComponentCreationData componentCreationData = mockProjectCreation("generated_orga2/repo1");
+    ProjectAlmSettingDao projectAlmSettingDao = mock();
+    when(dbClient.projectAlmSettingDao()).thenReturn(projectAlmSettingDao);
+    when(managedProjectService.isProjectVisibilitySynchronizationActivated()).thenReturn(true);
+
+    // when
+    ComponentCreationData actualComponentCreationData = githubProjectCreator.createProjectAndBindToDevOpsPlatform(dbClient.openSession(true),
+      SCANNER_API_DEVOPS_AUTO_CONFIG, null);
+
+    // then
+    assertThat(actualComponentCreationData).isEqualTo(componentCreationData);
+
+    ComponentCreationParameters componentCreationParameters = componentCreationParametersCaptor.getValue();
+    assertThat(componentCreationParameters.newComponent().isPrivate()).isFalse();
+  }
+
+  @Test
+  public void createProjectAndBindToDevOpsPlatformFromScanner_whenRepoFoundOnGitHubAndVisibilitySynchronizationDisabled_successfullyCreatesProjectAndMakesProjectPrivate() {
+    // given
+    mockGitHubRepository();
+
+    ComponentCreationData componentCreationData = mockProjectCreation("generated_orga2/repo1");
+    ProjectAlmSettingDao projectAlmSettingDao = mock();
+    when(dbClient.projectAlmSettingDao()).thenReturn(projectAlmSettingDao);
+    when(managedProjectService.isProjectVisibilitySynchronizationActivated()).thenReturn(false);
+
+    // when
+    ComponentCreationData actualComponentCreationData = githubProjectCreator.createProjectAndBindToDevOpsPlatform(dbClient.openSession(true),
+      SCANNER_API_DEVOPS_AUTO_CONFIG, null);
+
+    // then
+    assertThat(actualComponentCreationData).isEqualTo(componentCreationData);
+
+    ComponentCreationParameters componentCreationParameters = componentCreationParametersCaptor.getValue();
+    assertThat(componentCreationParameters.newComponent().isPrivate()).isTrue();
+  }
+
   @Test
   public void createProjectAndBindToDevOpsPlatformFromApi_whenRepoFoundOnGitHub_successfullyCreatesProject() {
     // given
@@ -316,7 +358,7 @@ public class GithubProjectCreatorTest {
     ComponentCreationParameters componentCreationParameters = componentCreationParametersCaptor.getValue();
     assertComponentCreationParametersContainsCorrectInformation(componentCreationParameters, projectKey, ALM_IMPORT_API);
     assertThat(componentCreationParameters.isManaged()).isFalse();
-    assertThat(componentCreationParameters.newComponent().isPrivate()).isFalse();
+    assertThat(componentCreationParameters.newComponent().isPrivate()).isTrue();
 
     verify(projectAlmSettingDao).insertOrUpdate(any(), projectAlmSettingDtoCaptor.capture(), eq(ALM_SETTING_KEY), eq(REPOSITORY_NAME), eq(projectKey));
     ProjectAlmSettingDto projectAlmSettingDto = projectAlmSettingDtoCaptor.getValue();
@@ -373,16 +415,25 @@ public class GithubProjectCreatorTest {
     assertThat(permissionChange.getProjectUuid()).isEqualTo(actualComponentCreationData.projectDto().getUuid());
   }
 
-  private void mockGitHubRepository() {
+  private void mockPublicGithubRepository() {
+    GithubApplicationClient.Repository repository =mockGitHubRepository();
+    when(repository.isPrivate()).thenReturn(false);
+  }
+
+  private GithubApplicationClient.Repository mockGitHubRepository() {
     GithubApplicationClient.Repository repository = mock();
     when(repository.getDefaultBranch()).thenReturn(MAIN_BRANCH_NAME);
     when(repository.getName()).thenReturn(REPOSITORY_NAME);
     when(repository.getFullName()).thenReturn(DEVOPS_PROJECT_DESCRIPTOR.projectIdentifier());
+    when(repository.isPrivate()).thenReturn(true);
     when(githubApplicationClient.getRepository(DEVOPS_PROJECT_DESCRIPTOR.url(), devOpsAppInstallationToken, DEVOPS_PROJECT_DESCRIPTOR.projectIdentifier())).thenReturn(
       Optional.of(repository));
     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn("generated_" + DEVOPS_PROJECT_DESCRIPTOR.projectIdentifier());
+    return  repository;
   }
 
+
+
   private ComponentCreationData mockProjectCreation(String projectKey) {
     ComponentCreationData componentCreationData = mock();
     ProjectDto projectDto = mockProjectDto(projectKey);