From: Julien Lancelot Date: Fri, 4 Aug 2017 08:46:04 +0000 (+0200) Subject: SONAR-9616 batch/project should fail on unknown branch X-Git-Tag: 6.6-RC1~380^2~126 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=66658a7557d78fd809d71227da15e56a2926d4f7;p=sonarqube.git SONAR-9616 batch/project should fail on unknown branch --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java index b67083fda64..0d82a0af7f8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java @@ -27,7 +27,6 @@ import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; import org.sonar.api.resources.Qualifiers; @@ -70,34 +69,27 @@ public class ProjectDataLoader { public ProjectRepositories load(ProjectDataQuery query) { try (DbSession session = dbClient.openSession(false)) { ProjectRepositories data = new ProjectRepositories(); - ComponentDto module = componentFinder.getByKey(session, query.getModuleKey()); - checkRequest(isProjectOrModule(module), "Key '%s' belongs to a component which is not a Project", query.getModuleKey()); - - boolean hasScanPerm = userSession.hasComponentPermission(SCAN_EXECUTION, module) || - userSession.hasPermission(OrganizationPermission.SCAN, module.getOrganizationUuid()); - boolean hasBrowsePerm = userSession.hasComponentPermission(USER, module); - checkPermission(query.isIssuesMode(), hasScanPerm, hasBrowsePerm); - - ComponentDto moduleToUseForSettings = module; - ComponentDto moduleToUseForFileData = module; + String moduleKey = query.getModuleKey(); String branch = query.getBranch(); - if (branch != null) { - Optional branchDto = dbClient.componentDao().selectByKeyAndBranch(session, query.getModuleKey(), branch); - moduleToUseForSettings = branchDto.orElse(module); - moduleToUseForFileData = branchDto.orElse(null); - } + ComponentDto mainModule = componentFinder.getByKey(session, moduleKey); + checkRequest(isProjectOrModule(mainModule), "Key '%s' belongs to a component which is not a Project", moduleKey); + boolean hasScanPerm = userSession.hasComponentPermission(SCAN_EXECUTION, mainModule) || + userSession.hasPermission(OrganizationPermission.SCAN, mainModule.getOrganizationUuid()); + boolean hasBrowsePerm = userSession.hasComponentPermission(USER, mainModule); + checkPermission(query.isIssuesMode(), hasScanPerm, hasBrowsePerm); + ComponentDto branchOrMainModule = branch == null ? mainModule : componentFinder.getByKeyAndBranch(session, moduleKey, branch); - ComponentDto project = getProject(moduleToUseForSettings, session); - if (!project.getKey().equals(moduleToUseForSettings.getKey())) { - addSettings(data, moduleToUseForSettings.getKey(), getSettingsFromParents(moduleToUseForSettings, hasScanPerm, session)); + ComponentDto project = getProject(branchOrMainModule, session); + if (!project.getKey().equals(branchOrMainModule.getKey())) { + addSettings(data, branchOrMainModule.getKey(), getSettingsFromParents(branchOrMainModule, hasScanPerm, session)); } - List modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, moduleToUseForSettings.uuid()); - List modulesTreeSettings = dbClient.propertiesDao().selectEnabledDescendantModuleProperties(module.uuid(), session); + List modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, branchOrMainModule.uuid()); + List modulesTreeSettings = dbClient.propertiesDao().selectEnabledDescendantModuleProperties(mainModule.uuid(), session); TreeModuleSettings treeModuleSettings = new TreeModuleSettings(session, modulesTree, modulesTreeSettings); - addSettingsToChildrenModules(data, query.getModuleKey(), Maps.newHashMap(), treeModuleSettings, hasScanPerm); - List files = searchFilesWithHashAndRevision(session, moduleToUseForFileData); + addSettingsToChildrenModules(data, moduleKey, Maps.newHashMap(), treeModuleSettings, hasScanPerm); + List files = searchFilesWithHashAndRevision(session, branchOrMainModule); addFileData(data, modulesTree, files); // FIXME need real value but actually only used to know if there is a previous analysis in local issue tracking mode so any value is diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java index 7383857a08c..a6cb622d013 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java @@ -510,31 +510,17 @@ public class ProjectDataLoaderTest { } @Test - public void return_settings_from_project_when_branch_does_not_exist() { + public void throws_NotFoundException_when_branch_does_not_exist() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); - ComponentDto module = db.components().insertComponent(newModuleDto(project)); - // Project properties - db.properties().insertProperties( - new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()), - new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())); - // Module properties - db.properties().insertProperties( - new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR-SERVER").setResourceId(module.getId()), - new PropertyDto().setKey("sonar.coverage.exclusions").setValue("**/*.java").setResourceId(module.getId())); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create() - .setModuleKey(project.getKey()) - .setBranch("my_branch")); + expectedException.expect(NotFoundException.class); + expectedException.expectMessage(format("Component '%s' on branch '%s' not found", project.getKey(), "unknown_branch")); - assertThat(ref.settings(project.getKey())).containsOnly( - entry("sonar.jira.project.key", "SONAR"), - entry("sonar.jira.login.secured", "john")); - assertThat(ref.settings(module.getKey())).containsOnly( - entry("sonar.jira.project.key", "SONAR-SERVER"), - entry("sonar.jira.login.secured", "john"), - entry("sonar.coverage.exclusions", "**/*.java")); + underTest.load(ProjectDataQuery.create() + .setModuleKey(project.getKey()) + .setBranch("unknown_branch")); } @Test @@ -617,23 +603,6 @@ public class ProjectDataLoaderTest { assertThat(ref.fileData(moduleBranch.getKey(), moduleFile.path()).hash()).isEqualTo("789456"); } - @Test - public void return_no_file_data_when_branch_does_not_exist() { - OrganizationDto organizationDto = db.organizations().insert(); - ComponentDto project = db.components().insertPrivateProject(organizationDto); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); - userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); - ComponentDto projectFile = db.components().insertComponent(newFileDto(branch)); - dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456")); - dbSession.commit(); - - ProjectRepositories ref = underTest.load(ProjectDataQuery.create() - .setModuleKey(project.getKey()) - .setBranch("new_branch")); - - assertThat(ref.fileDataByPath(project.getKey())).isEmpty(); - } - @Test public void fails_with_NPE_if_query_is_null() { expectedException.expect(NullPointerException.class);