From 7f01689716bb18aab9afd17e95491a12f3dff192 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 3 Aug 2017 14:40:59 +0200 Subject: [PATCH] SONAR-9616 Handle branch in batch/project --- .../org/sonar/server/batch/ProjectAction.java | 14 +- .../sonar/server/batch/ProjectDataLoader.java | 104 +++---- .../sonar/server/batch/ProjectDataQuery.java | 11 + .../sonar/server/batch/ProjectActionTest.java | 10 +- .../server/batch/ProjectDataLoaderTest.java | 272 ++++++++++++++---- 5 files changed, 290 insertions(+), 121 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java index 9e27f6e6854..939b9a46935 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java @@ -31,6 +31,7 @@ import org.sonarqube.ws.WsBatch.WsProjectResponse; import org.sonarqube.ws.WsBatch.WsProjectResponse.FileData.Builder; import static org.sonar.core.util.Protobuf.setNullable; +import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -39,6 +40,7 @@ public class ProjectAction implements BatchWsAction { private static final String PARAM_KEY = "key"; private static final String PARAM_PROFILE = "profile"; private static final String PARAM_ISSUES_MODE = "issues_mode"; + private static final String PARAM_BRANCH = "branch"; private final ProjectDataLoader projectDataLoader; @@ -71,6 +73,12 @@ public class ProjectAction implements BatchWsAction { .setDescription("Issues mode or not") .setDefaultValue(false) .setBooleanPossibleValues(); + + action + .createParam(PARAM_BRANCH) + .setSince("6.6") + .setDescription("Branch key") + .setExampleValue(KEY_BRANCH_EXAMPLE_001); } @Override @@ -78,7 +86,8 @@ public class ProjectAction implements BatchWsAction { ProjectRepositories data = projectDataLoader.load(ProjectDataQuery.create() .setModuleKey(wsRequest.mandatoryParam(PARAM_KEY)) .setProfileName(wsRequest.param(PARAM_PROFILE)) - .setIssuesMode(wsRequest.mandatoryParamAsBoolean(PARAM_ISSUES_MODE))); + .setIssuesMode(wsRequest.mandatoryParamAsBoolean(PARAM_ISSUES_MODE)) + .setBranch(wsRequest.param(PARAM_BRANCH))); WsProjectResponse projectResponse = buildResponse(data); writeProtobuf(projectResponse, wsRequest, wsResponse); @@ -125,8 +134,7 @@ public class ProjectAction implements BatchWsAction { for (Map.Entry> moduleSettingsEntry : data.settings().entrySet()) { settingsByModuleResponse.put( moduleSettingsEntry.getKey(), - toSettingsResponse(moduleSettingsEntry.getValue()) - ); + toSettingsResponse(moduleSettingsEntry.getValue())); } return settingsByModuleResponse; 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 f48bb14fc86..b67083fda64 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 @@ -20,15 +20,20 @@ package org.sonar.server.batch; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; 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; import org.sonar.api.resources.Scopes; import org.sonar.api.server.ServerSide; +import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -37,6 +42,7 @@ import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.property.PropertyDto; import org.sonar.scanner.protocol.input.FileData; import org.sonar.scanner.protocol.input.ProjectRepositories; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.user.UserSession; @@ -44,7 +50,8 @@ import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static org.sonar.api.web.UserRole.USER; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; -import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; +import static org.sonar.core.util.stream.MoreCollectors.index; +import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; import static org.sonar.server.ws.WsUtils.checkRequest; @ServerSide @@ -52,17 +59,18 @@ public class ProjectDataLoader { private final DbClient dbClient; private final UserSession userSession; + private final ComponentFinder componentFinder; - public ProjectDataLoader(DbClient dbClient, UserSession userSession) { + public ProjectDataLoader(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) { this.dbClient = dbClient; this.userSession = userSession; + this.componentFinder = componentFinder; } public ProjectRepositories load(ProjectDataQuery query) { try (DbSession session = dbClient.openSession(false)) { ProjectRepositories data = new ProjectRepositories(); - ComponentDto module = checkFoundWithOptional(dbClient.componentDao().selectByKey(session, query.getModuleKey()), - "Project or module with key '%s' is not found", query.getModuleKey()); + 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) || @@ -70,20 +78,26 @@ public class ProjectDataLoader { boolean hasBrowsePerm = userSession.hasComponentPermission(USER, module); checkPermission(query.isIssuesMode(), hasScanPerm, hasBrowsePerm); - ComponentDto project = getProject(module, session); - if (!project.getDbKey().equals(module.getDbKey())) { - addSettings(data, module.getDbKey(), getSettingsFromParents(module, hasScanPerm, session)); + ComponentDto moduleToUseForSettings = module; + ComponentDto moduleToUseForFileData = module; + String branch = query.getBranch(); + if (branch != null) { + Optional branchDto = dbClient.componentDao().selectByKeyAndBranch(session, query.getModuleKey(), branch); + moduleToUseForSettings = branchDto.orElse(module); + moduleToUseForFileData = branchDto.orElse(null); } - List modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, module.uuid()); - Map moduleUuidsByKey = moduleUuidsByKey(modulesTree); - Map moduleIdsByKey = moduleIdsByKey(modulesTree); + ComponentDto project = getProject(moduleToUseForSettings, session); + if (!project.getKey().equals(moduleToUseForSettings.getKey())) { + addSettings(data, moduleToUseForSettings.getKey(), getSettingsFromParents(moduleToUseForSettings, hasScanPerm, session)); + } + List modulesTree = dbClient.componentDao().selectEnabledDescendantModules(session, moduleToUseForSettings.uuid()); List modulesTreeSettings = dbClient.propertiesDao().selectEnabledDescendantModuleProperties(module.uuid(), session); - TreeModuleSettings treeModuleSettings = new TreeModuleSettings(moduleUuidsByKey, moduleIdsByKey, modulesTree, modulesTreeSettings); + TreeModuleSettings treeModuleSettings = new TreeModuleSettings(session, modulesTree, modulesTreeSettings); - addSettingsToChildrenModules(data, query.getModuleKey(), Maps.newHashMap(), treeModuleSettings, hasScanPerm); - List files = searchFilesWithHashAndRevision(session, module); + addSettingsToChildrenModules(data, query.getModuleKey(), Maps.newHashMap(), treeModuleSettings, hasScanPerm); + List files = searchFilesWithHashAndRevision(session, moduleToUseForFileData); 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 @@ -101,7 +115,10 @@ public class ProjectDataLoader { return Qualifiers.PROJECT.equals(module.qualifier()) || Qualifiers.MODULE.equals(module.qualifier()); } - private List searchFilesWithHashAndRevision(DbSession session, ComponentDto module) { + private List searchFilesWithHashAndRevision(DbSession session, @Nullable ComponentDto module) { + if (module == null) { + return Collections.emptyList(); + } return module.isRootProject() ? dbClient.componentDao().selectEnabledFilesFromProject(session, module.uuid()) : dbClient.componentDao().selectEnabledDescendantFiles(session, module.uuid()); } @@ -121,7 +138,7 @@ public class ProjectDataLoader { Map parentProperties = newHashMap(); for (ComponentDto parent : parents) { - parentProperties.putAll(getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(session, parent.getDbKey()), hasScanPerm)); + parentProperties.putAll(getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(session, parent.getKey()), hasScanPerm)); } return parentProperties; } @@ -145,8 +162,8 @@ public class ProjectDataLoader { addSettings(ref, moduleKey, currentParentProperties); for (ComponentDto childModule : treeModuleSettings.findChildrenModule(moduleKey)) { - addSettings(ref, childModule.getDbKey(), currentParentProperties); - addSettingsToChildrenModules(ref, childModule.getDbKey(), currentParentProperties, treeModuleSettings, hasScanPerm); + addSettings(ref, childModule.getKey(), currentParentProperties); + addSettingsToChildrenModules(ref, childModule.getKey(), currentParentProperties, treeModuleSettings, hasScanPerm); } } @@ -175,7 +192,7 @@ public class ProjectDataLoader { private static void addFileData(ProjectRepositories data, List moduleChildren, List files) { Map moduleKeysByUuid = newHashMap(); for (ComponentDto module : moduleChildren) { - moduleKeysByUuid.put(module.uuid(), module.getDbKey()); + moduleKeysByUuid.put(module.uuid(), module.getKey()); } for (FilePathWithHashDto file : files) { @@ -197,56 +214,29 @@ public class ProjectDataLoader { } } - private static Map moduleUuidsByKey(List moduleChildren) { - Map moduleUuidsByKey = newHashMap(); - for (ComponentDto componentDto : moduleChildren) { - moduleUuidsByKey.put(componentDto.getDbKey(), componentDto.uuid()); - } - return moduleUuidsByKey; - } + private class TreeModuleSettings { - private static Map moduleIdsByKey(List moduleChildren) { - Map moduleIdsByKey = newHashMap(); - for (ComponentDto componentDto : moduleChildren) { - moduleIdsByKey.put(componentDto.getDbKey(), componentDto.getId()); - } - return moduleIdsByKey; - } - - private static class TreeModuleSettings { - - private Map moduleIdsByKey; - private Map moduleUuidsByKey; - private Multimap propertiesByModuleId; + private Map modulesByKey; + private Multimap propertiesByModuleKey; private Multimap moduleChildrenByModuleUuid; - private TreeModuleSettings(Map moduleUuidsByKey, Map moduleIdsByKey, List moduleChildren, - List moduleChildrenSettings) { - this.moduleIdsByKey = moduleIdsByKey; - this.moduleUuidsByKey = moduleUuidsByKey; - propertiesByModuleId = ArrayListMultimap.create(); + private TreeModuleSettings(DbSession session, List moduleChildren, List moduleChildrenSettings) { + modulesByKey = moduleChildren.stream().collect(uniqueIndex(ComponentDto::getKey)); moduleChildrenByModuleUuid = ArrayListMultimap.create(); - for (PropertyDto settings : moduleChildrenSettings) { - propertiesByModuleId.put(settings.getResourceId(), settings); - } - - for (ComponentDto componentDto : moduleChildren) { - String moduleUuid = componentDto.moduleUuid(); - if (moduleUuid != null) { - moduleChildrenByModuleUuid.put(moduleUuid, componentDto); - } - } + Set propertiesByComponentId = moduleChildrenSettings.stream().map(PropertyDto::getResourceId).collect(MoreCollectors.toSet()); + Map componentsById = dbClient.componentDao().selectByIds(session, propertiesByComponentId).stream().collect(uniqueIndex(ComponentDto::getId)); + propertiesByModuleKey = moduleChildrenSettings.stream().collect(index(s -> componentsById.get(s.getResourceId()).getKey())); + moduleChildrenByModuleUuid = moduleChildren.stream().filter(c -> c.moduleUuid() != null).collect(index(ComponentDto::moduleUuid)); } List findModuleSettings(String moduleKey) { - Long moduleId = moduleIdsByKey.get(moduleKey); - return newArrayList(propertiesByModuleId.get(moduleId)); + return ImmutableList.copyOf(propertiesByModuleKey.get(moduleKey)); } List findChildrenModule(String moduleKey) { - String moduleUuid = moduleUuidsByKey.get(moduleKey); - return newArrayList(moduleChildrenByModuleUuid.get(moduleUuid)); + String moduleUuid = modulesByKey.get(moduleKey).uuid(); + return ImmutableList.copyOf(moduleChildrenByModuleUuid.get(moduleUuid)); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataQuery.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataQuery.java index 1581441b068..d25b02215f0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataQuery.java @@ -27,6 +27,7 @@ public class ProjectDataQuery { private String projectOrModuleKey; private String profileName; private boolean issuesMode; + private String branch; private ProjectDataQuery() { // No direct call @@ -60,6 +61,16 @@ public class ProjectDataQuery { return this; } + @CheckForNull + public String getBranch() { + return branch; + } + + public ProjectDataQuery setBranch(@Nullable String branch) { + this.branch = branch; + return this; + } + public static ProjectDataQuery create() { return new ProjectDataQuery(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java index 685ecc062d0..b14f30b70a0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.batch; -import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.sonar.scanner.protocol.input.FileData; @@ -37,12 +36,7 @@ import static org.sonar.test.JsonAssert.assertJson; public class ProjectActionTest { private ProjectDataLoader projectDataLoader = mock(ProjectDataLoader.class); - private WsActionTester ws; - - @Before - public void setUp() { - ws = new WsActionTester(new ProjectAction(projectDataLoader)); - } + private WsActionTester ws = new WsActionTester(new ProjectAction(projectDataLoader)); @Test public void project_referentials() throws Exception { @@ -56,6 +50,7 @@ public class ProjectActionTest { TestResponse response = ws.newRequest() .setParam("key", projectKey) + .setParam("branch", "my_branch") .setParam("profile", "Default") .setParam("preview", "false") .execute(); @@ -64,6 +59,7 @@ public class ProjectActionTest { assertThat(queryArgumentCaptor.getValue().getModuleKey()).isEqualTo(projectKey); assertThat(queryArgumentCaptor.getValue().getProfileName()).isEqualTo("Default"); assertThat(queryArgumentCaptor.getValue().isIssuesMode()).isFalse(); + assertThat(queryArgumentCaptor.getValue().getBranch()).isEqualTo("my_branch"); } /** 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 fef0b41434e..7383857a08c 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 @@ -45,8 +45,10 @@ import org.sonar.server.tester.UserSessionRule; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; import static org.junit.Assert.fail; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; +import static org.sonar.db.component.ComponentDto.generateBranchKey; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.permission.OrganizationPermission.SCAN; @@ -77,9 +79,9 @@ public class ProjectDataLoaderTest { dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())); dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); - Map projectSettings = ref.settings(project.getDbKey()); + Map projectSettings = ref.settings(project.getKey()); assertThat(projectSettings).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); @@ -98,16 +100,16 @@ public class ProjectDataLoaderTest { dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())); dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); - Map projectSettings = ref.settings(project.getDbKey()); + Map projectSettings = ref.settings(project.getKey()); assertThat(projectSettings).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); } @Test - public void not_returned_secured_settings_when_lgged_but_no_scan_permission() { + public void not_returned_secured_settings_when_logged_but_no_scan_permission() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); userSession.logIn("john").addProjectPermission(UserRole.USER, project); @@ -119,8 +121,8 @@ public class ProjectDataLoaderTest { dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())); dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true)); - Map projectSettings = ref.settings(project.getDbKey()); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey()).setIssuesMode(true)); + Map projectSettings = ref.settings(project.getKey()); assertThat(projectSettings).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR")); } @@ -148,11 +150,11 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); + assertThat(ref.settings(project.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); - assertThat(ref.settings(module.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(module.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-SERVER", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); @@ -177,11 +179,11 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); + assertThat(ref.settings(project.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); - assertThat(ref.settings(module.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(module.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); } @@ -216,15 +218,15 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); + assertThat(ref.settings(project.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); - assertThat(ref.settings(module.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(module.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-SERVER", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); - assertThat(ref.settings(subModule.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(subModule.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-SERVER-DAO", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); @@ -259,15 +261,15 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); + assertThat(ref.settings(project.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); - assertThat(ref.settings(module1.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(module1.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-SERVER", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); - assertThat(ref.settings(module2.getDbKey())).isEqualTo(ImmutableMap.of( + assertThat(ref.settings(module2.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-APPLICATION", "sonar.jira.login.secured", "john")); } @@ -284,8 +286,8 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); + assertThat(ref.settings(project.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john")); } @@ -308,10 +310,10 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEmpty(); - assertThat(ref.settings(module.getDbKey())).isEmpty(); - assertThat(ref.settings(subModule.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getKey())); + assertThat(ref.settings(project.getKey())).isEmpty(); + assertThat(ref.settings(module.getKey())).isEmpty(); + assertThat(ref.settings(subModule.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); @@ -341,10 +343,10 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEmpty(); - assertThat(ref.settings(module.getDbKey())).isEmpty(); - assertThat(ref.settings(subModule.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getKey())); + assertThat(ref.settings(project.getKey())).isEmpty(); + assertThat(ref.settings(module.getKey())).isEmpty(); + assertThat(ref.settings(subModule.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); @@ -372,10 +374,10 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEmpty(); - assertThat(ref.settings(module.getDbKey())).isEmpty(); - assertThat(ref.settings(subModule.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getKey())); + assertThat(ref.settings(project.getKey())).isEmpty(); + assertThat(ref.settings(module.getKey())).isEmpty(); + assertThat(ref.settings(subModule.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); @@ -404,15 +406,137 @@ public class ProjectDataLoaderTest { dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getDbKey())); - assertThat(ref.settings(project.getDbKey())).isEmpty(); - assertThat(ref.settings(module.getDbKey())).isEmpty(); - assertThat(ref.settings(subModule.getDbKey())).isEqualTo(ImmutableMap.of( + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.getKey())); + assertThat(ref.settings(project.getKey())).isEmpty(); + assertThat(ref.settings(module.getKey())).isEmpty(); + assertThat(ref.settings(subModule.getKey())).isEqualTo(ImmutableMap.of( "sonar.jira.project.key", "SONAR-SERVER", "sonar.jira.login.secured", "john", "sonar.coverage.exclusions", "**/*.java")); } + @Test + public void return_project_settings_from_project_when_using_branch_parameter() { + OrganizationDto organizationDto = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organizationDto); + userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); + db.properties().insertProperties(new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId())); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); + + ProjectRepositories ref = underTest.load(ProjectDataQuery.create() + .setModuleKey(project.getKey()) + .setBranch("my_branch")); + + assertThat(project.getKey()).isEqualTo(branch.getKey()); + assertThat(ref.settings(project.getKey())).containsExactly(entry("sonar.jira.project.key", "SONAR")); + } + + @Test + public void return_project_with_module_settings_when_using_branch_parameter() { + OrganizationDto organizationDto = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organizationDto); + userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); + ComponentDto module = db.components().insertComponent(newModuleDto(project)); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); + ComponentDto moduleBranch = db.components().insertComponent(newModuleDto(branch).setDbKey(generateBranchKey(module.getKey(), "my_branch"))); + // 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")); + + assertThat(ref.settings(branch.getKey())).containsOnly( + entry("sonar.jira.project.key", "SONAR"), + entry("sonar.jira.login.secured", "john")); + assertThat(ref.settings(moduleBranch.getKey())).containsOnly( + entry("sonar.jira.project.key", "SONAR-SERVER"), + entry("sonar.jira.login.secured", "john"), + entry("sonar.coverage.exclusions", "**/*.java")); + } + + @Test + public void return_settings_from_project_when_module_is_only_in_branch() { + OrganizationDto organizationDto = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organizationDto); + userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); + // This module does not exist on master + ComponentDto moduleBranch = db.components().insertComponent(newModuleDto(branch)); + 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())); + + ProjectRepositories ref = underTest.load(ProjectDataQuery.create() + .setModuleKey(project.getKey()) + .setBranch("my_branch")); + + assertThat(ref.settings(moduleBranch.getKey())).containsOnly( + entry("sonar.jira.project.key", "SONAR"), + entry("sonar.jira.login.secured", "john")); + } + + @Test + public void return_sub_module_settings_when_using_branch_parameter() { + OrganizationDto organizationDto = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organizationDto); + ComponentDto module = db.components().insertComponent(newModuleDto(project)); + ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); + userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); + ComponentDto moduleBranch = db.components().insertComponent(newModuleDto(branch).setDbKey(generateBranchKey(module.getKey(), "my_branch"))); + ComponentDto subModuleBranch = db.components().insertComponent(newModuleDto(moduleBranch).setDbKey(generateBranchKey(subModule.getKey(), "my_branch"))); + // Sub module properties + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(subModule.getId())); + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(subModule.getId())); + dbClient.propertiesDao() + .saveProperty(dbSession, new PropertyDto().setKey("sonar.coverage.exclusions").setValue("**/*.java").setResourceId(subModule.getId())); + dbSession.commit(); + + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModuleBranch.getKey())); + + assertThat(ref.settings(branch.getKey())).isEmpty(); + assertThat(ref.settings(moduleBranch.getKey())).isEmpty(); + assertThat(ref.settings(subModuleBranch.getKey())).containsOnly( + entry("sonar.jira.project.key", "SONAR"), + entry("sonar.jira.login.secured", "john"), + entry("sonar.coverage.exclusions", "**/*.java")); + } + + @Test + public void return_settings_from_project_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")); + + 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")); + } + @Test public void return_file_data_from_single_project() { OrganizationDto organizationDto = db.organizations().insert(); @@ -422,10 +546,10 @@ public class ProjectDataLoaderTest { dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(file).setSrcHash("123456")); db.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); - assertThat(ref.fileDataByPath(project.getDbKey())).hasSize(1); - FileData fileData = ref.fileData(project.getDbKey(), file.path()); + assertThat(ref.fileDataByPath(project.getKey())).hasSize(1); + FileData fileData = ref.fileData(project.getKey(), file.path()); assertThat(fileData.hash()).isEqualTo("123456"); } @@ -443,10 +567,10 @@ public class ProjectDataLoaderTest { dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456")); dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); - assertThat(ref.fileData(project.getDbKey(), projectFile.path()).hash()).isEqualTo("123456"); - assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456"); + assertThat(ref.fileData(project.getKey(), projectFile.path()).hash()).isEqualTo("123456"); + assertThat(ref.fileData(module.getKey(), moduleFile.path()).hash()).isEqualTo("789456"); } @Test @@ -463,11 +587,51 @@ public class ProjectDataLoaderTest { dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456")); dbSession.commit(); - ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.getDbKey())); + ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.getKey())); + + assertThat(ref.fileData(module.getKey(), moduleFile.path()).hash()).isEqualTo("789456"); + assertThat(ref.fileData(module.getKey(), moduleFile.path()).revision()).isEqualTo("123456789"); + assertThat(ref.fileData(project.getKey(), projectFile.path())).isNull(); + } + + @Test + public void return_file_data_from_branch() { + 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 moduleBranch = db.components().insertComponent(newModuleDto(branch)); + // File on branch + ComponentDto projectFile = db.components().insertComponent(newFileDto(branch)); + dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456")); + // File on moduleBranch branch + ComponentDto moduleFile = db.components().insertComponent(newFileDto(moduleBranch)); + dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456")); + dbSession.commit(); + + ProjectRepositories ref = underTest.load(ProjectDataQuery.create() + .setModuleKey(project.getKey()) + .setBranch("my_branch")); + + assertThat(ref.fileData(branch.getKey(), projectFile.path()).hash()).isEqualTo("123456"); + 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.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456"); - assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).revision()).isEqualTo("123456789"); - assertThat(ref.fileData(project.getDbKey(), projectFile.path())).isNull(); + assertThat(ref.fileDataByPath(project.getKey())).isEmpty(); } @Test @@ -542,7 +706,7 @@ public class ProjectDataLoaderTest { expectedException.expect(ForbiddenException.class); expectedException.expectMessage("You're not authorized to execute any SonarQube analysis"); - underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); } @Test @@ -553,7 +717,7 @@ public class ProjectDataLoaderTest { expectedException.expect(ForbiddenException.class); expectedException.expectMessage("You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server"); - underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey())); + underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey())); } @Test @@ -561,7 +725,7 @@ public class ProjectDataLoaderTest { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn().addProjectPermission(UserRole.USER, project); - ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true)); + ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey()).setIssuesMode(true)); assertThat(repositories).isNotNull(); } @@ -574,7 +738,7 @@ public class ProjectDataLoaderTest { expectedException.expect(ForbiddenException.class); expectedException.expectMessage("You don't have the required permissions to access this project"); - underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true)); + underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey()).setIssuesMode(true)); } @Test @@ -583,7 +747,7 @@ public class ProjectDataLoaderTest { userSession.logIn().addPermission(SCAN, project.getOrganizationUuid()); userSession.logIn().addProjectPermission(UserRole.USER, project); - ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true)); + ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getKey()).setIssuesMode(true)); assertThat(repositories).isNotNull(); } -- 2.39.5