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;
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() {
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;
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() {
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
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);
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");
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;
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;
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) {
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()) {