]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Get batch project referentials using WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Aug 2014 08:34:49 +0000 (10:34 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Aug 2014 08:34:49 +0000 (10:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java
server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java
sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java
sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java
sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml

index be4f94f5381d5c0ab8cd8f4c05c920ea08b1a701..76b04cf40d64c7cde9589de4a6afccb920fbd827 100644 (file)
@@ -24,13 +24,13 @@ import com.google.common.collect.Maps;
 import org.apache.commons.io.IOUtils;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
-import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.core.UtcDateUtils;
+import org.sonar.core.component.AuthorizedComponentDto;
 import org.sonar.core.component.ComponentDto;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
@@ -109,14 +109,21 @@ public class ProjectReferentialsAction implements RequestHandler {
       ProjectReferentials ref = new ProjectReferentials();
 
       String projectKey = null;
-      ComponentDto module = dbClient.componentDao().getNullableByKey(session, projectOrModuleKey);
+      AuthorizedComponentDto module = dbClient.componentDao().getNullableAuthorizedComponentByKey(projectOrModuleKey, session);
+      // Current project can be null when analysing a new project
       if (module != null) {
-        ComponentDto project = !module.qualifier().equals(Qualifiers.PROJECT) ? dbClient.componentDao().getRootProjectByKey(projectOrModuleKey, session) : module;
-        if (!project.key().equals(module.key())) {
-          addSettings(ref, module.getKey(), getSettingsFromParentModules(module.key(), hasScanPerm, session));
+        ComponentDto project = dbClient.componentDao().getNullableRootProjectByKey(projectOrModuleKey, session);
+        // Can be null if the given project is a provisioned one
+        if (project != null) {
+          if (!project.key().equals(module.key())) {
+            addSettings(ref, module.getKey(), getSettingsFromParents(module.key(), hasScanPerm, session));
+          }
+          projectKey = project.key();
+          addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.<String, String>newHashMap(), hasScanPerm, session);
+        } else {
+          // Add settings of the provisioned project
+          addSettings(ref, projectOrModuleKey, getPropertiesMap(propertiesDao.selectProjectProperties(projectOrModuleKey, session), hasScanPerm));
         }
-        projectKey = project.key();
-        addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.<String, String>newHashMap(), hasScanPerm, session);
       }
 
       addProfiles(ref, projectKey, profileName, session);
@@ -129,7 +136,7 @@ public class ProjectReferentialsAction implements RequestHandler {
     }
   }
 
-  private Map<String, String> getSettingsFromParentModules(String moduleKey, boolean hasScanPerm, DbSession session) {
+  private Map<String, String> getSettingsFromParents(String moduleKey, boolean hasScanPerm, DbSession session) {
     List<ComponentDto> parents = newArrayList();
     aggregateParentModules(moduleKey, parents, session);
     Collections.reverse(parents);
index 5b6ff6d7ec8539e11c71f406501726b742a0ffc6..376e78ec3bfdd9a2f10fccf409528f50ff5a05fb 100644 (file)
@@ -60,8 +60,17 @@ public class ComponentDao extends BaseDao<ComponentMapper, ComponentDto, String>
     return mapper(session).countById(id) > 0;
   }
 
+  /**
+   * Return null only if the component does not exists.
+   * If the component if a root project, it will return itself.
+   */
+  @CheckForNull
+  public ComponentDto getNullableRootProjectByKey(String componentKey, DbSession session) {
+    return mapper(session).selectRootProjectByKey(componentKey);
+  }
+
   public ComponentDto getRootProjectByKey(String componentKey, DbSession session) {
-    ComponentDto componentDto = mapper(session).selectRootProjectByKey(componentKey);
+    ComponentDto componentDto = getNullableRootProjectByKey(componentKey, session);
     if (componentDto == null) {
       throw new NotFoundException(String.format("Root project for project '%s' not found", componentKey));
     }
@@ -90,6 +99,19 @@ public class ComponentDao extends BaseDao<ComponentMapper, ComponentDto, String>
     return componentDto;
   }
 
+  @CheckForNull
+  public AuthorizedComponentDto getNullableAuthorizedComponentByKey(String key, DbSession session) {
+    return mapper(session).selectAuthorizedComponentByKey(key);
+  }
+
+  public AuthorizedComponentDto getAuthorizedComponentByKey(String key, DbSession session) {
+    AuthorizedComponentDto componentDto = getNullableAuthorizedComponentByKey(key, session);
+    if (componentDto == null) {
+      throw new NotFoundException(String.format("Project with key '%s' not found", key));
+    }
+    return componentDto;
+  }
+
   @Override
   @CheckForNull
   protected ComponentDto doGetNullableByKey(DbSession session, String key) {
index 65e1995054fa1e355eeb1f8ff749e2a383c31bc0..75ea4a1fb6f9fa7d19c16f02c7cc8bdea2306d0e 100644 (file)
@@ -98,9 +98,9 @@ public class ProjectReferentialsActionTest {
     module = new ComponentDto().setKey("org.codehaus.sonar:sonar-server").setQualifier(Qualifiers.MODULE);
     subModule = new ComponentDto().setKey("org.codehaus.sonar:sonar-server-dao").setQualifier(Qualifiers.MODULE);
 
-    when(componentDao.getNullableByKey(session, project.key())).thenReturn(project);
-    when(componentDao.getNullableByKey(session, module.key())).thenReturn(module);
-    when(componentDao.getNullableByKey(session, subModule.key())).thenReturn(subModule);
+    when(componentDao.getNullableAuthorizedComponentByKey(project.key(), session)).thenReturn(project);
+    when(componentDao.getNullableAuthorizedComponentByKey(module.key(), session)).thenReturn(module);
+    when(componentDao.getNullableAuthorizedComponentByKey(subModule.key(), session)).thenReturn(subModule);
 
     when(language.getKey()).thenReturn("java");
     when(languages.all()).thenReturn(new Language[] {language});
@@ -118,6 +118,7 @@ public class ProjectReferentialsActionTest {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
     // Project without modules
+    when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project);
     when(componentDao.findModulesByProject(project.key(), session)).thenReturn(Collections.<ComponentDto>emptyList());
 
     when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList(
@@ -133,6 +134,7 @@ public class ProjectReferentialsActionTest {
   public void return_project_with_module_settings() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
+    when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project);
     when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module));
 
     when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList(
@@ -153,6 +155,7 @@ public class ProjectReferentialsActionTest {
   public void return_project_with_module_settings_inherited_from_project() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
+    when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project);
     when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module));
 
     when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList(
@@ -169,6 +172,7 @@ public class ProjectReferentialsActionTest {
   public void return_project_with_module_with_sub_module() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
+    when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project);
     when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module));
     when(componentDao.findModulesByProject(module.key(), session)).thenReturn(newArrayList(subModule));
 
@@ -190,11 +194,27 @@ public class ProjectReferentialsActionTest {
     request.execute().assertJson(getClass(), "return_project_with_module_with_sub_module.json");
   }
 
+  @Test
+  public void return_provisioned_project_settings() throws Exception {
+    MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
+
+    // No root project will be found on provisioned project
+    when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(null);
+
+    when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList(
+      new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR"),
+      new PropertyDto().setKey("sonar.jira.login.secured").setValue("john")
+    ));
+
+    WsTester.TestRequest request = tester.newGetRequest("batch", "project").setParam("key", project.key());
+    request.execute().assertJson(getClass(), "return_project_settings.json");
+  }
+
   @Test
   public void return_sub_module_settings() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
-    when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project);
+    when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module);
 
@@ -212,7 +232,7 @@ public class ProjectReferentialsActionTest {
   public void return_sub_module_settings_including_settings_from_parent_modules() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
-    when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project);
+    when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module);
 
@@ -236,7 +256,7 @@ public class ProjectReferentialsActionTest {
   public void return_sub_module_settings_only_inherited_from_project() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
-    when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project);
+    when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module);
 
@@ -255,7 +275,7 @@ public class ProjectReferentialsActionTest {
   public void return_sub_module_settings_inherited_from_project_and_module() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
 
-    when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project);
+    when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project);
     when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module);
 
index 4ff1d97a5568ae00d994045332b594c676a33422..5296d905c942780895eb6848450d214b2778fa83 100644 (file)
@@ -140,6 +140,19 @@ public class ComponentDaoTest extends AbstractDaoTestCase {
     assertThat(dao.findModulesByProject("unknown", session)).isEmpty();
   }
 
+  @Test
+  public void get_nullable_root_project_by_key() throws Exception {
+    setupData("multi-modules");
+
+    assertThat(dao.getNullableRootProjectByKey("org.struts:struts-data", session).getKey()).isEqualTo("org.struts:struts");
+    assertThat(dao.getNullableRootProjectByKey("org.struts:struts-core", session).getKey()).isEqualTo("org.struts:struts");
+
+    // Root project of a project is itself
+    assertThat(dao.getNullableRootProjectByKey("org.struts:struts", session).getKey()).isEqualTo("org.struts:struts");
+
+    assertThat(dao.getNullableRootProjectByKey("unknown", session)).isNull();
+  }
+
   @Test
   public void get_root_project_by_key() throws Exception {
     setupData("multi-modules");
@@ -192,6 +205,31 @@ public class ComponentDaoTest extends AbstractDaoTestCase {
     dao.getAuthorizedComponentById(111L, session);
   }
 
+  @Test
+  public void get_nullable_authorized_component_by_key() {
+    setupData("shared");
+
+    AuthorizedComponentDto result = dao.getNullableAuthorizedComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java", session);
+    assertThat(result).isNotNull();
+    assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
+
+    assertThat(dao.getNullableAuthorizedComponentByKey("unknown", session)).isNull();
+  }
+
+  @Test
+  public void get_authorized_component_by_key() {
+    setupData("shared");
+
+    assertThat(dao.getAuthorizedComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java", session)).isNotNull();
+  }
+
+  @Test(expected = NotFoundException.class)
+  public void fail_to_get_authorized_component_by_key_when_project_not_found() {
+    setupData("shared");
+
+    dao.getAuthorizedComponentByKey("unknown", session);
+  }
+
   @Test
   public void insert() {
     when(system2.now()).thenReturn(DateUtils.parseDate("2014-06-18").getTime());
index 569e8646269c0fde912d3243dad637c214048eab..6691124af8a8711c092df0be0f264dbfe1594873 100644 (file)
@@ -77,7 +77,6 @@ public class ComponentDto extends AuthorizedComponentDto implements Component {
     return this;
   }
 
-  @Override
   public String qualifier() {
     return qualifier;
   }
index 5561948dc8ca65cb824127503dea20e56db4f512..58cfdba2b5da5f5ac4a06a8a62a7c3694caa7241 100644 (file)
@@ -54,5 +54,7 @@ public interface ComponentMapper {
   @CheckForNull
   AuthorizedComponentDto selectAuthorizedComponentById(long id);
 
+  AuthorizedComponentDto selectAuthorizedComponentByKey(String key);
+
   void insert(ComponentDto rule);
 }
index 77a9ad0a35d646174c510f505e58489780f09ad6..452bebb9fcf7c646d6f563c5017cdec5987d1794 100644 (file)
     </where>
   </select>
 
+  <select id="selectAuthorizedComponentByKey" parameterType="String" resultType="AuthorizedComponent">
+    SELECT <include refid="authorizedComponentColumns"/>
+    FROM projects p
+    <where>
+      AND p.enabled=${_true}
+      AND p.kee=#{key}
+    </where>
+  </select>
+
   <sql id="insertColumns">
     (kee, name, long_name, qualifier, scope, language, root_id, path, created_at)
   </sql>