*/
package org.sonar.server.computation.queue;
+import com.google.common.base.Optional;
import java.io.InputStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
private final CeQueue queue;
private final UserSession userSession;
private final ComponentService componentService;
- private final PermissionService permissionService;
+ private final PermissionService permissionTemplateService;
private final DbClient dbClient;
public ReportSubmitter(CeQueue queue, UserSession userSession,
- ComponentService componentService, PermissionService permissionService, DbClient dbClient) {
+ ComponentService componentService, PermissionService permissionTemplateService, DbClient dbClient) {
this.queue = queue;
this.userSession = userSession;
this.componentService = componentService;
- this.permissionService = permissionService;
+ this.permissionTemplateService = permissionTemplateService;
this.dbClient = dbClient;
}
public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
- String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
- ComponentDto project = componentService.getNullableByKey(effectiveProjectKey);
- if (project == null) {
- project = createProject(projectKey, projectBranch, projectName);
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
+ Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, effectiveProjectKey);
+ ComponentDto project = opt.or(() -> createProject(dbSession, projectKey, projectBranch, projectName));
+ userSession.checkComponentUuidPermission(SCAN_EXECUTION, project.uuid());
+ return submitReport(dbSession, reportInput, project);
}
-
- userSession.checkComponentPermission(SCAN_EXECUTION, projectKey);
-
- return submitReport(reportInput, project);
}
@CheckForNull
- private ComponentDto createProject(String projectKey, @Nullable String projectBranch, @Nullable String projectName) {
- DbSession dbSession = dbClient.openSession(false);
- try {
- boolean wouldCurrentUserHaveScanPermission = permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(dbSession, SCAN_EXECUTION, projectBranch, projectKey,
- Qualifiers.PROJECT);
- if (!wouldCurrentUserHaveScanPermission) {
- throw insufficientPrivilegesException();
- }
-
- NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
- newProject.setBranch(projectBranch);
- newProject.setQualifier(Qualifiers.PROJECT);
- // "provisioning" permission is check in ComponentService
- ComponentDto project = componentService.create(dbSession, newProject);
- permissionService.applyDefaultPermissionTemplate(dbSession, project.getKey());
- return project;
- } finally {
- dbClient.closeSession(dbSession);
+ private ComponentDto createProject(DbSession dbSession, String projectKey, @Nullable String projectBranch, @Nullable String projectName) {
+ boolean wouldCurrentUserHaveScanPermission = permissionTemplateService.wouldCurrentUserHavePermissionWithDefaultTemplate(dbSession, SCAN_EXECUTION, projectBranch, projectKey,
+ Qualifiers.PROJECT);
+ if (!wouldCurrentUserHaveScanPermission) {
+ throw insufficientPrivilegesException();
}
+
+ NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
+ newProject.setBranch(projectBranch);
+ newProject.setQualifier(Qualifiers.PROJECT);
+ // "provisioning" permission is check in ComponentService
+ ComponentDto project = componentService.create(dbSession, newProject);
+ permissionTemplateService.applyDefaultPermissionTemplate(project.getKey());
+ return project;
}
- private CeTask submitReport(InputStream reportInput, ComponentDto project) {
+ private CeTask submitReport(DbSession dbSession, InputStream reportInput, ComponentDto project) {
// the report file must be saved before submitting the task
CeTaskSubmit.Builder submit = queue.prepareSubmit();
- try (DbSession dbSession = dbClient.openSession(false)) {
- dbClient.ceTaskInputDao().insert(dbSession, submit.getUuid(), reportInput);
- dbSession.commit();
- }
+ dbClient.ceTaskInputDao().insert(dbSession, submit.getUuid(), reportInput);
+ dbSession.commit();
submit.setType(CeTaskTypes.REPORT);
submit.setComponentUuid(project.uuid());
public class ReportSubmitterTest {
- static final String PROJECT_KEY = "MY_PROJECT";
- static final String PROJECT_UUID = "P1";
- static final String PROJECT_NAME = "My Project";
- static final String TASK_UUID = "TASK_1";
+ private static final String PROJECT_KEY = "MY_PROJECT";
+ private static final String PROJECT_UUID = "P1";
+ private static final String PROJECT_NAME = "My Project";
+ private static final String TASK_UUID = "TASK_1";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void submit_a_report_on_existing_project() {
userSession.setGlobalPermissions(SCAN_EXECUTION);
-
+ ComponentDto project = dbTester.components().insertProject();
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
- underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+ underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
verifyReportIsPersisted(TASK_UUID);
verifyZeroInteractions(permissionService);
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
- return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
+ return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(project.uuid()) &&
submit.getUuid().equals(TASK_UUID);
}
userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
when(permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
.thenReturn(true);
userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
when(permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
.thenReturn(true);
@Test
public void submit_a_report_on_existing_project_with_global_scan_permission() {
+ ComponentDto project = dbTester.components().insertProject();
userSession.setGlobalPermissions(SCAN_EXECUTION);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
- underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+ underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
verify(queue).submit(any(CeTaskSubmit.class));
}
@Test
public void submit_a_report_on_existing_project_with_project_scan_permission() {
- userSession.addProjectPermissions(SCAN_EXECUTION, PROJECT_KEY);
+ ComponentDto project = dbTester.components().insertProject();
+ userSession.addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
- underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+ underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
verify(queue).submit(any(CeTaskSubmit.class));
}
@Test
public void fail_with_forbidden_exception_on_new_project_when_only_project_scan_permission() {
- userSession.addProjectPermissions(SCAN_EXECUTION, PROJECT_KEY);
+ userSession.addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
thrown.expect(ForbiddenException.class);