From 2b6c82353a2e1e6823572a398b17c0b711ad9829 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 20 Oct 2016 18:40:16 +0200 Subject: [PATCH] SONAR-8134 use a single DbSession in ReportSubmitter --- .../computation/queue/ReportSubmitter.java | 60 ++++++++----------- .../queue/ReportSubmitterTest.java | 30 ++++------ 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java index 92141581e99..5780fedd6a0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java @@ -19,6 +19,7 @@ */ package org.sonar.server.computation.queue; +import com.google.common.base.Optional; import java.io.InputStream; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -47,59 +48,50 @@ public class ReportSubmitter { 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 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()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java index 1de8f34a4bc..69265ea1655 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java @@ -55,10 +55,10 @@ import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; 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(); @@ -77,18 +77,17 @@ public class ReportSubmitterTest { @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() { @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); } @@ -104,7 +103,6 @@ public class ReportSubmitterTest { 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); @@ -132,7 +130,6 @@ public class ReportSubmitterTest { 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); @@ -144,24 +141,24 @@ public class ReportSubmitterTest { @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)); } @@ -176,10 +173,9 @@ public class ReportSubmitterTest { @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); -- 2.39.5