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;
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);
}
}
- 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);
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));
}
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) {
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});
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(
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(
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(
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));
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);
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);
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);
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);
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");
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());
return this;
}
- @Override
public String qualifier() {
return qualifier;
}
@CheckForNull
AuthorizedComponentDto selectAuthorizedComponentById(long id);
+ AuthorizedComponentDto selectAuthorizedComponentByKey(String key);
+
void insert(ComponentDto rule);
}
</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>