]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 batch/project should fail on unknown branch
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Aug 2017 08:46:04 +0000 (10:46 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 08:55:10 +0000 (10:55 +0200)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java

index b67083fda64adc98e67b0a305157a1697413c0ee..0d82a0af7f8f684880db07513b79257ef454f49b 100644 (file)
@@ -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<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
index 7383857a08c321104a398b0a646cc06e9452c203..a6cb622d013975e21be6afcf6192c055d71d4508 100644 (file)
@@ -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);