import org.sonar.server.debt.DebtModelPluginRepository;
import org.sonar.server.debt.DebtModelXMLExporter;
import org.sonar.server.debt.DebtRulesXMLImporter;
-import org.sonar.server.duplication.ws.ShowResponseBuilder;
import org.sonar.server.duplication.ws.DuplicationsParser;
import org.sonar.server.duplication.ws.DuplicationsWs;
+import org.sonar.server.duplication.ws.ShowResponseBuilder;
import org.sonar.server.email.ws.EmailsWsModule;
import org.sonar.server.es.IndexCreator;
import org.sonar.server.es.IndexDefinitions;
import org.sonar.server.qualityprofile.QProfileCopier;
import org.sonar.server.qualityprofile.QProfileExporters;
import org.sonar.server.qualityprofile.QProfileFactoryImpl;
-import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileResetImpl;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.qualityprofile.RuleActivatorContextFactory;
import org.sonar.server.qualityprofile.ws.OldRestoreAction;
import org.sonar.server.qualityprofile.ws.ProfilesWs;
import org.sonar.server.qualityprofile.ws.QProfilesWsModule;
-import org.sonar.server.qualityprofile.ws.SearchDataLoader;
import org.sonar.server.root.ws.RootWsModule;
import org.sonar.server.rule.CommonRuleDefinitionsImpl;
import org.sonar.server.rule.DeprecatedRulesDefinitionLoader;
XMLProfileParser.class,
XMLProfileSerializer.class,
AnnotationProfileParser.class,
- QProfileLookup.class,
QProfileComparison.class,
- SearchDataLoader.class,
ProfilesWs.class,
OldRestoreAction.class,
RuleActivator.class,
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.qualityprofile;
-
-import java.util.Collection;
-import java.util.List;
-import org.sonar.api.server.ServerSide;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.QProfileDto;
-
-import static com.google.common.collect.Lists.newArrayList;
-
-@ServerSide
-public class QProfileLookup {
-
- private final DbClient dbClient;
-
- public QProfileLookup(DbClient dbClient) {
- this.dbClient = dbClient;
- }
-
- public List<QProfileDto> allProfiles(DbSession dbSession, OrganizationDto organization) {
- return dbClient.qualityProfileDao().selectOrderedByOrganizationUuid(dbSession, organization);
- }
-
- public Collection<QProfileDto> profiles(DbSession dbSession, String language, OrganizationDto organization) {
- return dbClient.qualityProfileDao().selectByLanguage(dbSession, organization, language);
- }
-
- public List<QProfileDto> ancestors(QProfileDto profile, DbSession dbSession) {
- List<QProfileDto> ancestors = newArrayList();
- collectAncestors(profile, ancestors, dbSession);
- return ancestors;
- }
-
- private void collectAncestors(QProfileDto profile, List<QProfileDto> ancestors, DbSession session) {
- if (profile.getParentKee() == null) {
- return;
- }
-
- QProfileDto parent = getParent(session, profile);
- ancestors.add(parent);
- collectAncestors(parent, ancestors, session);
- }
-
- private QProfileDto getParent(DbSession dbSession, QProfileDto profile) {
- QProfileDto parent = dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getParentKee());
- if (parent == null) {
- throw new IllegalStateException("Cannot find parent of profile: " + profile.getKee());
- }
- return parent;
- }
-}
import org.sonar.db.qualityprofile.ActiveRuleDao;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QProfileDto;
-import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse;
import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse.QualityProfile;
+import static com.google.common.collect.Lists.newArrayList;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.qualityprofile.ws.QProfileWsSupport.createOrganizationParam;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class InheritanceAction implements QProfileWsAction {
private final DbClient dbClient;
- private final QProfileLookup profileLookup;
private final QProfileWsSupport wsSupport;
private final Languages languages;
- public InheritanceAction(DbClient dbClient, QProfileLookup profileLookup, QProfileWsSupport wsSupport, Languages languages) {
+ public InheritanceAction(DbClient dbClient, QProfileWsSupport wsSupport, Languages languages) {
this.dbClient = dbClient;
- this.profileLookup = profileLookup;
this.wsSupport = wsSupport;
this.languages = languages;
}
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, reference);
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
- List<QProfileDto> ancestors = profileLookup.ancestors(profile, dbSession);
+ List<QProfileDto> ancestors = ancestors(profile, dbSession);
List<QProfileDto> children = dbClient.qualityProfileDao().selectChildren(dbSession, profile);
Statistics statistics = new Statistics(dbSession, organization);
}
}
+ public List<QProfileDto> ancestors(QProfileDto profile, DbSession dbSession) {
+ List<QProfileDto> ancestors = newArrayList();
+ collectAncestors(profile, ancestors, dbSession);
+ return ancestors;
+ }
+
+ private void collectAncestors(QProfileDto profile, List<QProfileDto> ancestors, DbSession session) {
+ if (profile.getParentKee() == null) {
+ return;
+ }
+
+ QProfileDto parent = getParent(session, profile);
+ ancestors.add(parent);
+ collectAncestors(parent, ancestors, session);
+ }
+
+ private QProfileDto getParent(DbSession dbSession, QProfileDto profile) {
+ QProfileDto parent = dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getParentKee());
+ if (parent == null) {
+ throw new IllegalStateException("Cannot find parent of profile: " + profile.getKee());
+ }
+ return parent;
+ }
+
private static InheritanceWsResponse buildResponse(QProfileDto profile, List<QProfileDto> ancestors, List<QProfileDto> children, Statistics statistics) {
return InheritanceWsResponse.newBuilder()
.setProfile(buildProfile(profile, statistics))
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
return Arrays.stream(languages.all()).map(Language::getKey).collect(MoreCollectors.toSet());
}
- private ComponentDto getProject(String moduleKey, DbSession dbSession) {
- ComponentDto module = checkFoundWithOptional(dbClient.componentDao().selectByKey(dbSession, moduleKey), "Component key '%s' not found", moduleKey);
- if (module.isRootProject()) {
- return module;
- }
- return dbClient.componentDao().selectOrFailByUuid(dbSession, module.projectUuid());
- }
-
private SearchWsResponse buildResponse(SearchData data) {
List<QProfileDto> profiles = data.getProfiles();
Map<String, QProfileDto> profilesByKey = profiles.stream().collect(Collectors.toMap(QProfileDto::getKee, identity()));
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.qualityprofile.ws;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.Sets;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-import javax.annotation.Nullable;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.QProfileDto;
-import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
-
-import static java.lang.String.format;
-
-public class SearchDataLoader {
-
- private static final Comparator<QProfileDto> Q_PROFILE_COMPARATOR = Comparator
- .comparing(QProfileDto::getLanguage)
- .thenComparing(QProfileDto::getName);
-
- private final Languages languages;
- private final QProfileLookup profileLookup;
- private final DbClient dbClient;
-
- public SearchDataLoader(Languages languages, QProfileLookup profileLookup, DbClient dbClient) {
- this.languages = languages;
- this.profileLookup = profileLookup;
- this.dbClient = dbClient;
- }
-
- @VisibleForTesting
- List<QProfileDto> findProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization, @Nullable ComponentDto project) {
- Collection<QProfileDto> profiles;
- if (askDefaultProfiles(request)) {
- profiles = findDefaultProfiles(dbSession, request, organization);
- } else if (project != null) {
- profiles = findProjectProfiles(dbSession, request, organization, project);
- } else {
- profiles = findAllProfiles(dbSession, request, organization);
- }
-
- return profiles.stream().sorted(Q_PROFILE_COMPARATOR).collect(Collectors.toList());
- }
-
- private Collection<QProfileDto> findDefaultProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
- String profileName = request.getProfileName();
-
- Set<String> languageKeys = getLanguageKeys();
- Map<String, QProfileDto> qualityProfiles = new HashMap<>(languageKeys.size());
-
- Set<String> missingLanguageKeys = lookupByProfileName(dbSession, organization, qualityProfiles, languageKeys, profileName);
- Set<String> noDefaultProfileLanguageKeys = lookupDefaults(dbSession, organization, qualityProfiles, missingLanguageKeys);
-
- if (!noDefaultProfileLanguageKeys.isEmpty()) {
- throw new IllegalStateException(format("No quality profile can been found on language(s) '%s'", noDefaultProfileLanguageKeys));
- }
-
- return qualityProfiles.values();
- }
-
- private Collection<QProfileDto> findProjectProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization, ComponentDto project) {
- String profileName = request.getProfileName();
-
- Set<String> languageKeys = getLanguageKeys();
- Map<String, QProfileDto> qualityProfiles = new HashMap<>(languageKeys.size());
-
- // look up profiles by profileName (if any) for each language
- Set<String> unresolvedLanguages = lookupByProfileName(dbSession, organization, qualityProfiles, languageKeys, profileName);
- // look up profile by componentKey for each language for which we don't have one yet
- Set<String> stillUnresolvedLanguages = lookupByModule(dbSession, qualityProfiles, unresolvedLanguages, project);
- // look up profile by default for each language for which we don't have one yet
- Set<String> noDefaultProfileLanguages = lookupDefaults(dbSession, organization, qualityProfiles, stillUnresolvedLanguages);
-
- if (!noDefaultProfileLanguages.isEmpty()) {
- throw new IllegalStateException(format("No quality profile can been found on language(s) '%s' for project '%s'", noDefaultProfileLanguages, project.getKey()));
- }
-
- return qualityProfiles.values();
- }
-
- private Collection<QProfileDto> findAllProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
- String language = request.getLanguage();
-
- if (language == null) {
- return profileLookup.allProfiles(dbSession, organization).stream().filter(qProfile -> languages.get(qProfile.getLanguage()) != null).collect(Collectors.toList());
- }
- return profileLookup.profiles(dbSession, language, organization);
- }
-
- private Set<String> lookupByProfileName(DbSession dbSession, OrganizationDto organization, Map<String, QProfileDto> qualityProfiles, Set<String> languageKeys,
- @Nullable String profileName) {
- if (languageKeys.isEmpty() || profileName == null) {
- return languageKeys;
- }
-
- dbClient.qualityProfileDao().selectByNameAndLanguages(dbSession, organization, profileName, languageKeys)
- .forEach(qualityProfile -> qualityProfiles
- .put(qualityProfile.getLanguage(), qualityProfile));
- return difference(languageKeys, qualityProfiles.keySet());
- }
-
- private Set<String> lookupByModule(DbSession dbSession, Map<String, QProfileDto> qualityProfiles, Set<String> languageKeys,
- ComponentDto project) {
- if (languageKeys.isEmpty()) {
- return languageKeys;
- }
-
- dbClient.qualityProfileDao().selectAssociatedToProjectUuidAndLanguages(dbSession, project, languageKeys)
- .forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), qualityProfile));
- return difference(languageKeys, qualityProfiles.keySet());
- }
-
- private Set<String> lookupDefaults(DbSession dbSession, OrganizationDto organization, Map<String, QProfileDto> qualityProfiles, Set<String> languageKeys) {
- if (languageKeys.isEmpty()) {
- return languageKeys;
- }
-
- addAll(qualityProfiles, findDefaultProfiles(dbSession, organization, languageKeys));
- return difference(languageKeys, qualityProfiles.keySet());
- }
-
- private static <T> Set<T> difference(Set<T> languageKeys, Set<T> set2) {
- return Sets.newHashSet(Sets.difference(languageKeys, set2));
- }
-
- private static void addAll(Map<String, QProfileDto> qualityProfiles, Collection<QProfileDto> list) {
- list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), qualityProfile));
- }
-
- private Set<String> getLanguageKeys() {
- return Arrays.stream(languages.all()).map(Language::getKey).collect(Collectors.toSet());
- }
-
- private List<QProfileDto> findDefaultProfiles(final DbSession dbSession, OrganizationDto organization, Set<String> languageKeys) {
- return dbClient.qualityProfileDao().selectDefaultProfiles(dbSession, organization, languageKeys);
- }
-
- private static boolean askDefaultProfiles(SearchWsRequest request) {
- return request.getDefaults();
- }
-}
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.RuleActivator;
TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
underTest = new InheritanceAction(
dbClient,
- new QProfileLookup(dbClient),
new QProfileWsSupport(dbClient, userSession, defaultOrganizationProvider),
new Languages());
ws = new WsActionTester(underTest);
new CompareAction(null, null, languages),
new DeleteAction(languages, null, null, userSessionRule, wsSupport),
new ExportersAction(),
- new InheritanceAction(null, null, null, languages),
+ new InheritanceAction(null, null, languages),
new RenameAction(dbClient, userSessionRule, wsSupport))).controller(QProfilesWs.API_ENDPOINT);
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.qualityprofile.ws;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.qualityprofile.QProfileLookup;
-
-import static org.mockito.Mockito.mock;
-
-public class SearchDataLoaderTest {
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private Languages languages;
- private QProfileLookup profileLookup;
- private ComponentFinder componentFinder;
- private OrganizationDto organization;
-
- @Before
- public void before() {
- organization = dbTester.organizations().insert();
- languages = new Languages();
- DbClient dbClient = dbTester.getDbClient();
- profileLookup = new QProfileLookup(dbClient);
- componentFinder = mock(ComponentFinder.class);
- }
-
-// @Test
-// public void find_no_profiles_if_database_is_empty() throws Exception {
-// assertThat(findProfiles(
-// new SearchWsRequest(),
-// null)).isEmpty();
-// }
-//
-// @Test
-// public void findAll() throws Exception {
-// insertQualityProfile(organization);
-// assertThat(findProfiles(
-// new SearchWsRequest()
-// .setOrganizationKey(organization.getKey()),
-// null)).hasSize(1);
-// }
-//
-// @Test
-// public void findDefaults() throws Exception {
-// QProfileDto profile = insertQualityProfile(organization);
-// dbTester.qualityProfiles().setAsDefault(profile);
-// assertThat(findProfiles(
-// new SearchWsRequest()
-// .setOrganizationKey(organization.getKey())
-// .setDefaults(true),
-// null)).hasSize(1);
-// }
-//
-// @Test
-// public void findForProject() throws Exception {
-// QProfileDto profile = insertQualityProfile(organization);
-// dbTester.qualityProfiles().setAsDefault(profile);
-// ComponentDto project1 = insertProject();
-// assertThat(findProfiles(
-// new SearchWsRequest()
-// .setOrganizationKey(organization.getKey()),
-// project1)).hasSize(1);
-// }
-//
-// @Test
-// public void findAllForLanguage() throws Exception {
-// QProfileDto profile = insertQualityProfile(organization);
-// dbTester.qualityProfiles().setAsDefault(profile);
-// assertThat(findProfiles(
-// new SearchWsRequest()
-// .setOrganizationKey(organization.getKey())
-// .setLanguage(profile.getLanguage()),
-// null)).hasSize(1);
-// assertThat(findProfiles(
-// new SearchWsRequest()
-// .setOrganizationKey(organization.getKey())
-// .setLanguage("other language"),
-// null)).hasSize(0);
-// }
-//
-// private List<QProfileDto> findProfiles(SearchWsRequest request, @Nullable ComponentDto project) {
-// return new SearchDataLoader(languages, profileLookup, dbTester.getDbClient())
-// .findProfiles(dbTester.getSession(), request, organization, project);
-// }
-//
-// private QProfileDto insertQualityProfile(OrganizationDto organization) {
-// Language language = insertLanguage();
-// return dbTester.qualityProfiles().insert(organization, p -> p.setLanguage(language.getKey()));
-// }
-//
-// private Language insertLanguage() {
-// Language language = LanguageTesting.newLanguage(randomAlphanumeric(20));
-// languages.add(language);
-// return language;
-// }
-//
-// private ComponentDto insertProject() {
-// ComponentDto project = dbTester.components().insertPrivateProject(organization);
-// doReturn(project).when(componentFinder).getByKey(any(DbSession.class), eq(project.getKey()));
-// return project;
-// }
-}