String profileName = request.param(PARAM_PROFILE);
ProjectReferentials ref = new ProjectReferentials();
- ComponentDto module = dbClient.componentDao().getByKey(session, projectOrModuleKey);
- 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));
+ String projectKey = null;
+ ComponentDto module = dbClient.componentDao().getNullableByKey(session, projectOrModuleKey);
+ 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));
+ }
+ projectKey = project.key();
+ addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.<String, String>newHashMap(), hasScanPerm, session);
}
- addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.<String, String>newHashMap(), hasScanPerm, session);
- addProfiles(ref, project.key(), profileName, session);
+ addProfiles(ref, projectKey, profileName, session);
addActiveRules(ref);
response.stream().setMediaType(MimeTypes.JSON);
return !key.contains(".secured") || hasScanPerm;
}
- private void addProfiles(ProjectReferentials ref, String projectKey, @Nullable String profileName, DbSession session) {
+ private void addProfiles(ProjectReferentials ref, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
for (Language language : languages.all()) {
String languageKey = language.getKey();
QualityProfileDto qualityProfileDto = getProfile(languageKey, projectKey, profileName, session);
/**
* First try to find a quality profile matching the given name (if provided) and current language
- * If null, try to find the quality profile set on the project
- * If null, try to find the default profile of the language
+ * If no profile found, try to find the quality profile set on the project (if provided)
+ * If still no profile found, try to find the default profile of the language
+ *
+ * Never return null because a default profile should always be set on ech language
*/
- private QualityProfileDto getProfile(String languageKey, String projectKey, @Nullable String profileName, DbSession session) {
+ private QualityProfileDto getProfile(String languageKey, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
QualityProfileDto qualityProfileDto = profileName != null ? qProfileFactory.getByNameAndLanguage(session, profileName, languageKey) : null;
- qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey);
+ if (qualityProfileDto == null && projectKey != null) {
+ qualityProfileDto = qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey);
+ }
qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getDefault(session, languageKey);
if (qualityProfileDto != null) {
return qualityProfileDto;
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.getByKey(session, project.key())).thenReturn(project);
- when(componentDao.getByKey(session, module.key())).thenReturn(module);
- when(componentDao.getByKey(session, subModule.key())).thenReturn(subModule);
+ 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(language.getKey()).thenReturn("java");
when(languages.all()).thenReturn(new Language[] {language});
request.execute().assertJson(getClass(), "return_quality_profile_from_given_profile_name.json");
}
+ @Test
+ public void return_quality_profiles_even_when_project_does_not_exists() throws Exception {
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
+ String projectKey = "org.codehaus.sonar:sonar";
+ when(componentDao.getNullableByKey(session, projectKey)).thenReturn(null);
+
+ when(qProfileFactory.getDefault(session, "java")).thenReturn(
+ QualityProfileDto.createFor("abcd").setName("Default").setLanguage("java").setRulesUpdatedAt("2014-01-14T14:00:00+0200")
+ );
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "project").setParam("key", projectKey);
+ request.execute().assertJson(getClass(), "return_quality_profiles_even_when_project_does_not_exists.json");
+ }
+
@Test
public void return_active_rules() throws Exception {
MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);