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
public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
}
+
+ @Override
+ public boolean isProjectVisibilitySynchronizationActivated() {
+ return false;
+ }
}
private static class AlwaysManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
}
+
+ @Override
+ public boolean isProjectVisibilitySynchronizationActivated() {
+ return true;
+ }
}
}
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);
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()
.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());
+ }
}
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
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();
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);