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;
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<ComponentDto> 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<ComponentDto> modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, moduleToUseForSettings.uuid());
- List<PropertyDto> modulesTreeSettings = dbClient.propertiesDao().selectEnabledDescendantModuleProperties(module.uuid(), session);
+ List<ComponentDto> modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, branchOrMainModule.uuid());
+ List<PropertyDto> modulesTreeSettings = dbClient.propertiesDao().selectEnabledDescendantModuleProperties(mainModule.uuid(), session);
TreeModuleSettings treeModuleSettings = new TreeModuleSettings(session, modulesTree, modulesTreeSettings);
- addSettingsToChildrenModules(data, query.getModuleKey(), Maps.newHashMap(), treeModuleSettings, hasScanPerm);
- List<FilePathWithHashDto> files = searchFilesWithHashAndRevision(session, moduleToUseForFileData);
+ addSettingsToChildrenModules(data, moduleKey, Maps.newHashMap(), treeModuleSettings, hasScanPerm);
+ List<FilePathWithHashDto> 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
}
@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
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);