return executeLargeInputs(languageKeys, input -> mapper(session).selectByNameAndLanguages(organization.getUuid(), name, input));
}
+ /**
+ * @deprecated provide organization
+ */
+ @Deprecated
public List<ComponentDto> selectProjects(String profileName, String language, DbSession session) {
return mapper(session).selectProjects(profileName, language);
}
+ /**
+ * @deprecated provide organization
+ */
+ @Deprecated
public Map<String, Long> countProjectsByProfileKey(DbSession dbSession) {
Map<String, Long> countByKey = new HashMap<>();
QualityProfileMapper mapper = mapper(dbSession);
return mapper(session).selectProjectAssociations(profileKey, nameQuery);
}
- private String sqlQueryString(@Nullable String query) {
- return query == null ? "%" : "%" + query.toUpperCase(Locale.ENGLISH) + "%";
+ private static String sqlQueryString(@Nullable String query) {
+ if (query == null) {
+ return "%";
+ }
+ return "%" + query.toUpperCase(Locale.ENGLISH) + "%";
}
private static QualityProfileMapper mapper(DbSession session) {
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
-import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.rule.RuleDto;
public class CachingRuleActivatorContextFactory extends RuleActivatorContextFactory implements Startable {
return dbClient.activeRuleDao().selectByProfileKey(session, profileKey).stream()
.collect(Collectors.uniqueIndex(dto -> dto.getKey().ruleKey()));
}
-
- @Override
- List<ActiveRuleParamDto> getActiveRuleParams(DbSession session, ActiveRuleDto activeRuleDto) {
- return super.getActiveRuleParams(session, activeRuleDto);
- }
}
private static final Joiner RULE_KEY_JOINER = Joiner.on(", ").skipNulls();
+ private static final String ATTRIBUTE_PROFILE = "profile";
+ private static final String ATTRIBUTE_NAME = "name";
+ private static final String ATTRIBUTE_LANGUAGE = "language";
+
+ private static final String ATTRIBUTE_RULES = "rules";
+ private static final String ATTRIBUTE_RULE = "rule";
+ private static final String ATTRIBUTE_REPOSITORY_KEY = "repositoryKey";
+ private static final String ATTRIBUTE_KEY = "key";
+ private static final String ATTRIBUTE_PRIORITY = "priority";
+
+ private static final String ATTRIBUTE_PARAMETERS = "parameters";
+ private static final String ATTRIBUTE_PARAMETER = "parameter";
+ private static final String ATTRIBUTE_PARAMETER_KEY = "key";
+ private static final String ATTRIBUTE_PARAMETER_VALUE = "value";
+
private final DbClient db;
private final QProfileReset profileReset;
private final QProfileFactory profileFactory;
private void writeXml(DbSession dbSession, Writer writer, QualityProfileDto profile, Iterator<ActiveRuleDto> activeRules) {
XmlWriter xml = XmlWriter.of(writer).declaration();
- xml.begin("profile");
- xml.prop("name", profile.getName());
- xml.prop("language", profile.getLanguage());
- xml.begin("rules");
+ xml.begin(ATTRIBUTE_PROFILE);
+ xml.prop(ATTRIBUTE_NAME, profile.getName());
+ xml.prop(ATTRIBUTE_LANGUAGE, profile.getLanguage());
+ xml.begin(ATTRIBUTE_RULES);
while (activeRules.hasNext()) {
ActiveRuleDto activeRule = activeRules.next();
- xml.begin("rule");
- xml.prop("repositoryKey", activeRule.getKey().ruleKey().repository());
- xml.prop("key", activeRule.getKey().ruleKey().rule());
- xml.prop("priority", activeRule.getSeverityString());
- xml.begin("parameters");
+ xml.begin(ATTRIBUTE_RULE);
+ xml.prop(ATTRIBUTE_REPOSITORY_KEY, activeRule.getKey().ruleKey().repository());
+ xml.prop(ATTRIBUTE_KEY, activeRule.getKey().ruleKey().rule());
+ xml.prop(ATTRIBUTE_PRIORITY, activeRule.getSeverityString());
+ xml.begin(ATTRIBUTE_PARAMETERS);
for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId())) {
xml
- .begin("parameter")
- .prop("key", param.getKey())
- .prop("value", param.getValue())
+ .begin(ATTRIBUTE_PARAMETER)
+ .prop(ATTRIBUTE_PARAMETER_KEY, param.getKey())
+ .prop(ATTRIBUTE_PARAMETER_VALUE, param.getValue())
.end();
}
- xml.end("parameters");
- xml.end("rule");
+ xml.end(ATTRIBUTE_PARAMETERS);
+ xml.end(ATTRIBUTE_RULE);
}
- xml.end("rules").end("profile").close();
+ xml.end(ATTRIBUTE_RULES).end(ATTRIBUTE_PROFILE).close();
}
@Override
SMInputCursor cursor = rootC.childElementCursor();
while (cursor.getNext() != null) {
String nodeName = cursor.getLocalName();
- if (StringUtils.equals("name", nodeName)) {
+ if (StringUtils.equals(ATTRIBUTE_NAME, nodeName)) {
profileName = StringUtils.trim(cursor.collectDescendantText(false));
- } else if (StringUtils.equals("language", nodeName)) {
+ } else if (StringUtils.equals(ATTRIBUTE_LANGUAGE, nodeName)) {
profileLang = StringUtils.trim(cursor.collectDescendantText(false));
- } else if (StringUtils.equals("rules", nodeName)) {
+ } else if (StringUtils.equals(ATTRIBUTE_RULES, nodeName)) {
SMInputCursor rulesCursor = cursor.childElementCursor("rule");
ruleActivations = parseRuleActivations(rulesCursor);
}
Map<String, String> parameters = Maps.newHashMap();
while (ruleCursor.getNext() != null) {
String nodeName = ruleCursor.getLocalName();
- if (StringUtils.equals("repositoryKey", nodeName)) {
+ if (StringUtils.equals(ATTRIBUTE_REPOSITORY_KEY, nodeName)) {
repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
- } else if (StringUtils.equals("key", nodeName)) {
+ } else if (StringUtils.equals(ATTRIBUTE_KEY, nodeName)) {
key = StringUtils.trim(ruleCursor.collectDescendantText(false));
- } else if (StringUtils.equals("priority", nodeName)) {
+ } else if (StringUtils.equals(ATTRIBUTE_PRIORITY, nodeName)) {
severity = StringUtils.trim(ruleCursor.collectDescendantText(false));
- } else if (StringUtils.equals("parameters", nodeName)) {
- SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
+ } else if (StringUtils.equals(ATTRIBUTE_PARAMETERS, nodeName)) {
+ SMInputCursor propsCursor = ruleCursor.childElementCursor(ATTRIBUTE_PARAMETER);
readParameters(propsCursor, parameters);
}
}
String value = null;
while (propCursor.getNext() != null) {
String nodeName = propCursor.getLocalName();
- if (StringUtils.equals("key", nodeName)) {
+ if (StringUtils.equals(ATTRIBUTE_PARAMETER_KEY, nodeName)) {
key = StringUtils.trim(propCursor.collectDescendantText(false));
- } else if (StringUtils.equals("value", nodeName)) {
+ } else if (StringUtils.equals(ATTRIBUTE_PARAMETER_VALUE, nodeName)) {
value = StringUtils.trim(propCursor.collectDescendantText(false));
}
}
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.qualityprofile.ws.QProfileReference;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
-import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkRequest;
/**
// ------------- DEFAULT PROFILE
- /**
- * @deprecated replaced by {@link org.sonar.server.qualityprofile.ws.QProfileWsSupport#getProfile(DbSession, QProfileReference)}
- */
- @Deprecated
- public QualityProfileDto find(DbSession dbSession, QProfileRef ref) {
- if (ref.hasKey()) {
- return findByKey(dbSession, ref.getKey());
- }
- return findByName(dbSession, ref.getLanguage(), ref.getName());
- }
-
- private QualityProfileDto findByKey(DbSession dbSession, String profileKey) {
- QualityProfileDto profile;
- profile = db.qualityProfileDao().selectByKey(dbSession, profileKey);
- return checkFound(profile, "Unable to find a profile for with key '%s'", profileKey);
- }
-
- private QualityProfileDto findByName(DbSession dbSession, String language, String profileName) {
- QualityProfileDto profile;
- profile = db.qualityProfileDao().selectByNameAndLanguage(profileName, language, dbSession);
- return checkFound(profile, "Unable to find a profile for language '%s' with name '%s'", language, profileName);
- }
-
private static void checkNotDefault(QualityProfileDto p) {
if (p.isDefault()) {
throw BadRequestException.create("The profile marked as default can not be deleted: " + p.getKey());
List<RuleActivation> activations = Lists.newArrayList();
for (RulesProfile def : entry.getValue()) {
for (ActiveRule activeRule : def.getActiveRules()) {
- RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
- activation.setSeverity(activeRule.getSeverity().name());
- if (!activeRule.getActiveRuleParams().isEmpty()) {
- for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
- activation.setParameter(param.getParamKey(), param.getValue());
- }
- } else {
- for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
- activation.setParameter(param.getName(), param.getDefaultValue());
- }
- }
- activations.add(activation);
+ activations.add(getRuleActivation(dbSession, activeRule));
}
}
reset(dbSession, profile, activations);
}
}
+ private RuleActivation getRuleActivation(DbSession dbSession, ActiveRule activeRule) {
+ RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
+ activation.setSeverity(activeRule.getSeverity().name());
+ if (!activeRule.getActiveRuleParams().isEmpty()) {
+ for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
+ activation.setParameter(param.getParamKey(), param.getValue());
+ }
+ } else {
+ for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
+ activation.setParameter(param.getName(), param.getDefaultValue());
+ }
+ }
+ return activation;
+ }
+
@Override
public BulkChangeResult reset(DbSession dbSession, QualityProfileDto profile, Collection<RuleActivation> activations) {
requireNonNull(profile.getId(), "Quality profile must be persisted");
.setResponseExample(getClass().getResource("example-create.json"))
.setHandler(this);
- qProfileWsSupport
+ QProfileWsSupport
.createOrganizationParam(create)
.setSince("6.4");
.setPost(true)
.setHandler(this);
- qProfileWsSupport.createOrganizationParam(action)
+ QProfileWsSupport.createOrganizationParam(action)
.setSince("6.4");
QProfileReference.defineParams(action, languages);
checkArgument(backup != null, "A backup file must be provided");
reader = new InputStreamReader(backup, StandardCharsets.UTF_8);
OrganizationDto defaultOrg = qProfileWsSupport.getOrganizationByKey(dbSession, null);
- QProfileRestoreSummary result = backuper.restore(dbSession, reader, defaultOrg,null);
+ QProfileRestoreSummary result = backuper.restore(dbSession, reader, defaultOrg, null);
writeResponse(response.newJsonWriter(), result);
} finally {
IOUtils.closeQuietly(reader);
private void writeResponse(JsonWriter json, QProfileRestoreSummary result) {
QualityProfileDto profile = result.getProfile();
- String languageKey = profile.getLanguage();
- Language language = languages.get(languageKey);
+ String languageKey = profile.getLanguage();
+ Language language = languages.get(languageKey);
- JsonWriter jsonProfile = json.beginObject().name("profile").beginObject();
- jsonProfile
- .prop("key", profile.getKey())
- .prop("name", profile.getName())
- .prop("language", languageKey)
- .prop("isDefault", false)
- .prop("isInherited", false);
- if (language != null) {
- jsonProfile.prop("languageName", language.getName());
- }
- jsonProfile.endObject();
+ JsonWriter jsonProfile = json.beginObject().name("profile").beginObject();
+ jsonProfile
+ .prop("key", profile.getKey())
+ .prop("name", profile.getName())
+ .prop("language", languageKey)
+ .prop("isDefault", false)
+ .prop("isInherited", false);
+ if (language != null) {
+ jsonProfile.prop("languageName", language.getName());
+ }
+ jsonProfile.endObject();
BulkChangeResult ruleChanges = result.getRuleChanges();
json.prop("ruleSuccesses", ruleChanges.countSucceeded());
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.user.UserSession;
import static java.lang.String.format;
private static final String PARAM_PROFILE_KEY = "key";
private static final int MAXIMUM_NAME_LENGTH = 100;
- private final QProfileFactory profileFactory;
- private final QProfileWsSupport qProfileWsSupport;
private final DbClient dbClient;
private final UserSession userSession;
- public RenameAction(QProfileFactory profileFactory, QProfileWsSupport qProfileWsSupport, DbClient dbClient, UserSession userSession) {
- this.profileFactory = profileFactory;
- this.qProfileWsSupport = qProfileWsSupport;
+ public RenameAction(DbClient dbClient, UserSession userSession) {
this.dbClient = dbClient;
this.userSession = userSession;
}
+++ /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 org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryImpl;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.qualityprofile.QualityProfileTesting;
-import org.sonar.server.exceptions.NotFoundException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class QProfileFactoryTest {
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private DbSession dbSession = dbTester.getSession();
- private QProfileFactory underTest = new QProfileFactory(dbTester.getDbClient(), UuidFactoryImpl.INSTANCE, System2.INSTANCE);
-
- @Before
- public void setUp() throws Exception {
- QualityProfileDto dto = QualityProfileTesting.newQualityProfileDto().setKey("sw").setName("Sonar way").setLanguage("js");
- dbTester.getDbClient().qualityProfileDao().insert(dbSession, dto);
- dbTester.commit();
- }
-
- @Test
- public void find_profile_by_key() {
- QualityProfileDto profile = underTest.find(dbSession, QProfileRef.fromKey("sw"));
- assertThat(profile.getKey()).isEqualTo("sw");
- assertThat(profile.getLanguage()).isEqualTo("js");
- assertThat(profile.getName()).isEqualTo("Sonar way");
- }
-
- @Test
- public void find_profile_by_name() {
- QualityProfileDto profile = underTest.find(dbSession, QProfileRef.fromName("js", "Sonar way"));
- assertThat(profile.getKey()).isEqualTo("sw");
- assertThat(profile.getLanguage()).isEqualTo("js");
- assertThat(profile.getName()).isEqualTo("Sonar way");
- }
-
- @Test
- public void throw_NFE_if_profile_key_does_not_exist() {
- expectedException.expect(NotFoundException.class);
-
- underTest.find(dbSession, QProfileRef.fromKey("missing"));
- }
-
- @Test
- public void throw_NFE_if_profile_name_does_not_exist() {
- expectedException.expect(NotFoundException.class);
-
- underTest.find(dbSession, QProfileRef.fromName("js", "Missing"));
- }
-
-}
+++ /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.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.qualityprofile.QualityProfileTesting;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.QProfileFactory;
-import org.sonar.server.qualityprofile.QProfileRef;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class QProfileFactoryTest {
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
- private DbSession dbSession = dbTester.getSession();
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private QProfileFactory underTest = new QProfileFactory(dbTester.getDbClient(), UuidFactoryFast.getInstance(), System2.INSTANCE);
-
- @Before
- public void setUp() throws Exception {
- QualityProfileDto dto = QualityProfileTesting.newQualityProfileDto().setKey("sw").setName("Sonar way").setLanguage("js");
- dbTester.getDbClient().qualityProfileDao().insert(dbSession, dto);
- dbTester.commit();
- }
-
- @Test
- public void find_profile_by_key() {
- QualityProfileDto profile = underTest.find(dbSession, QProfileRef.fromKey("sw"));
- assertThat(profile.getKey()).isEqualTo("sw");
- assertThat(profile.getLanguage()).isEqualTo("js");
- assertThat(profile.getName()).isEqualTo("Sonar way");
- }
-
- @Test
- public void find_profile_by_name() {
- QualityProfileDto profile = underTest.find(dbSession, QProfileRef.fromName("js", "Sonar way"));
- assertThat(profile.getKey()).isEqualTo("sw");
- assertThat(profile.getLanguage()).isEqualTo("js");
- assertThat(profile.getName()).isEqualTo("Sonar way");
- }
-
- @Test
- public void throw_NFE_if_profile_key_does_not_exist() {
- expectedException.expect(NotFoundException.class);
-
- underTest.find(dbSession, QProfileRef.fromKey("missing"));
- }
-
- @Test
- public void throw_NFE_if_profile_name_does_not_exist() {
- expectedException.expect(NotFoundException.class);
-
- underTest.find(dbSession, QProfileRef.fromName("js", "Missing"));
- }
-}
new DeleteAction(languages, null, null, userSessionRule, wsSupport),
new ExportersAction(),
new InheritanceAction(null, null, null, null, languages),
- new RenameAction(null, wsSupport, dbClient, userSessionRule))).controller(QProfilesWs.API_ENDPOINT);
+ new RenameAction(dbClient, userSessionRule))).controller(QProfilesWs.API_ENDPOINT);
}
private ProfileImporter[] createImporters(Languages languages) {
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSessionRule, defaultOrganizationProvider);
underTest = new RenameAction(
- new QProfileFactory(dbClient, UuidFactoryFast.getInstance(), System2.INSTANCE),
- wsSupport,
dbClient,
userSessionRule);
tester = new WsTester(new QProfilesWs(