public Map<String, String> parameters() {
Map<String, String> params = Maps.newHashMap();
- for (String key : fields.keySet()) {
- if (key.startsWith("param_")) {
- params.put(key.replace("param_", ""), (String) fields.get(key));
+ for (Map.Entry<String, Object> param : fields.entrySet()) {
+ if (param.getKey().startsWith("param_")) {
+ params.put(param.getKey().replace("param_", ""), (String) param.getValue());
}
}
return params;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.rule.index.RuleNormalizer;
-import org.sonar.server.search.*;
-
-import java.util.*;
+import org.sonar.server.search.BaseIndex;
+import org.sonar.server.search.FacetValue;
+import org.sonar.server.search.IndexDefinition;
+import org.sonar.server.search.IndexField;
+import org.sonar.server.search.SearchClient;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, ActiveRuleKey> {
+ public static final String COUNT_ACTIVE_RULES = "countActiveRules";
+
public ActiveRuleIndex(ActiveRuleNormalizer normalizer, SearchClient node) {
super(IndexDefinition.ACTIVE_RULE, normalizer, node);
}
.field(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()))
.subAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())
.field(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field()))
- .subAggregation(AggregationBuilders.count("countActiveRules")))
+ .subAggregation(AggregationBuilders.count(COUNT_ACTIVE_RULES)))
.setSize(0)
.setTypes(this.getIndexType());
SearchResponse response = request.get();
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.sonar.api.resources.Languages;
-import org.sonar.api.server.ws.*;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.Response.Stream;
+import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.db.DbClient;
public class QProfileBackupAction implements BaseQProfileWsAction {
- private static final String PARAM_KEY = "key";
-
private final QProfileBackuper backuper;
private final DbClient dbClient;
String parentKey = request.param(PARAM_PARENT_KEY);
Preconditions.checkArgument(
- (isEmpty(parentName) || isEmpty(parentKey)), "parentKey and parentName cannot be used simultaneously");
+ isEmpty(parentName) || isEmpty(parentKey), "parentKey and parentName cannot be used simultaneously");
if (isEmpty(parentKey)) {
if (!isEmpty(parentName)) {
public class QProfileCompareAction implements BaseQProfileWsAction {
+ private static final String ATTRIBUTE_LEFT = "left";
+ private static final String ATTRIBUTE_RIGHT = "right";
+ private static final String ATTRIBUTE_IN_LEFT = "inLeft";
+ private static final String ATTRIBUTE_IN_RIGHT = "inRight";
+ private static final String ATTRIBUTE_MODIFIED = "modified";
+ private static final String ATTRIBUTE_SAME = "same";
+ private static final String ATTRIBUTE_KEY = "key";
+ private static final String ATTRIBUTE_NAME = "name";
+ private static final String ATTRIBUTE_SEVERITY = "severity";
+ private static final String ATTRIBUTE_PLUGIN_KEY = "pluginKey";
+ private static final String ATTRIBUTE_PLUGIN_NAME = "pluginName";
+ private static final String ATTRIBUTE_LANGUAGE_KEY = "languageKey";
+ private static final String ATTRIBUTE_LANGUAGE_NAME = "languageName";
+ private static final String ATTRIBUTE_PARAMS = "params";
+
private static final String PARAM_LEFT_KEY = "leftKey";
private static final String PARAM_RIGHT_KEY = "rightKey";
private void writeResult(JsonWriter json, QProfileComparisonResult result, Map<RuleKey, Rule> rulesByKey) {
json.beginObject();
- json.name("left").beginObject();
+ json.name(ATTRIBUTE_LEFT).beginObject();
writeProfile(json, result.left());
json.endObject();
- json.name("right").beginObject();
+ json.name(ATTRIBUTE_RIGHT).beginObject();
writeProfile(json, result.right());
json.endObject();
- json.name("inLeft");
+ json.name(ATTRIBUTE_IN_LEFT);
writeRules(json, result.inLeft(), rulesByKey);
- json.name("inRight");
+ json.name(ATTRIBUTE_IN_RIGHT);
writeRules(json, result.inRight(), rulesByKey);
- json.name("modified");
+ json.name(ATTRIBUTE_MODIFIED);
writeDifferences(json, result.modified(), rulesByKey);
- json.name("same");
+ json.name(ATTRIBUTE_SAME);
writeRules(json, result.same(), rulesByKey);
json.endObject().close();
}
private void writeProfile(JsonWriter json, QualityProfileDto profile) {
- json.prop("key", profile.getKey())
- .prop("name", profile.getName());
+ json.prop(ATTRIBUTE_KEY, profile.getKey())
+ .prop(ATTRIBUTE_NAME, profile.getName());
}
private void writeRules(JsonWriter json, Map<RuleKey, ActiveRule> activeRules, Map<RuleKey, Rule> rulesByKey) {
json.beginObject();
writeRule(json, key, rulesByKey);
- json.prop("severity", value.severity());
+ json.prop(ATTRIBUTE_SEVERITY, value.severity());
json.endObject();
}
json.endArray();
private void writeRule(JsonWriter json, RuleKey key, Map<RuleKey, Rule> rulesByKey) {
String repositoryKey = key.repository();
- json.prop("key", key.toString())
- .prop("name", rulesByKey.get(key).name())
- .prop("pluginKey", repositoryKey);
+ json.prop(ATTRIBUTE_KEY, key.toString())
+ .prop(ATTRIBUTE_NAME, rulesByKey.get(key).name())
+ .prop(ATTRIBUTE_PLUGIN_KEY, repositoryKey);
Repository repo = ruleRepositories.repository(repositoryKey);
if (repo != null) {
String languageKey = repo.getLanguage();
Language language = languages.get(languageKey);
- json.prop("pluginName", repo.getName());
- json.prop("languageKey", languageKey);
- json.prop("languageName", language == null ? null : language.getName());
+ json.prop(ATTRIBUTE_PLUGIN_NAME, repo.getName());
+ json.prop(ATTRIBUTE_LANGUAGE_KEY, languageKey);
+ json.prop(ATTRIBUTE_LANGUAGE_NAME, language == null ? null : language.getName());
}
}
json.beginObject();
writeRule(json, key, rulesByKey);
- json.name("left").beginObject();
- json.prop("severity", value.leftSeverity());
- json.name("params").beginObject();
+ json.name(ATTRIBUTE_LEFT).beginObject();
+ json.prop(ATTRIBUTE_SEVERITY, value.leftSeverity());
+ json.name(ATTRIBUTE_PARAMS).beginObject();
for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue().leftValue());
}
// left
json.endObject();
- json.name("right").beginObject();
- json.prop("severity", value.rightSeverity());
- json.name("params").beginObject();
+ json.name(ATTRIBUTE_RIGHT).beginObject();
+ json.prop(ATTRIBUTE_SEVERITY, value.rightSeverity());
+ json.name(ATTRIBUTE_PARAMS).beginObject();
for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue().rightValue());
}
String languageKey = copiedProfile.getLanguage();
Language language = languages.get(copiedProfile.getLanguage());
+ String parentKey = copiedProfile.getParentKee();
response.newJsonWriter()
.beginObject()
.prop("key", copiedProfile.getKey())
.prop("language", languageKey)
.prop("languageName", language == null ? null : language.getName())
.prop("isDefault", copiedProfile.isDefault())
- .prop("isInherited", copiedProfile.getParentKee() != null)
+ .prop("isInherited", parentKey != null)
+ .prop("parentKey", parentKey)
.endObject().close();
}
}
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.search.FacetValue;
+import javax.annotation.Nullable;
+
import java.util.List;
import java.util.Map;
private void writeProfile(JsonWriter json, QualityProfileDto profile, Map<String, Multimap<String, FacetValue>> profileStats) {
String profileKey = profile.getKey();
- json.name("profile").beginObject()
- .prop("key", profileKey)
- .prop("name", profile.getName())
- .prop("parent", profile.getParentKee());
- writeStats(json, profileKey, profileStats);
- json.endObject();
+ json.name("profile");
+ writeProfileAttributes(json, profileKey, profile.getName(), profile.getParentKee(), profileStats);
}
private void writeAncestors(JsonWriter json, List<QProfile> ancestors, Map<String, Multimap<String, FacetValue>> profileStats) {
json.name("ancestors").beginArray();
for (QProfile ancestor : ancestors) {
String ancestorKey = ancestor.key();
- json.beginObject()
- .prop("key", ancestorKey)
- .prop("name", ancestor.name())
- .prop("parent", ancestor.parent());
- writeStats(json, ancestorKey, profileStats);
- json.endObject();
+ writeProfileAttributes(json, ancestorKey, ancestor.name(), ancestor.parent(), profileStats);
}
json.endArray();
}
json.name("children").beginArray();
for (QualityProfileDto child : children) {
String childKey = child.getKey();
- json.beginObject()
- .prop("key", childKey)
- .prop("name", child.getName());
- writeStats(json, childKey, profileStats);
- json.endObject();
+ writeProfileAttributes(json, childKey, child.getName(), null, profileStats);
}
json.endArray();
}
+ private void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Map<String, Multimap<String, FacetValue>> profileStats) {
+ json.beginObject();
+ json.prop("key", key)
+ .prop("name", name)
+ .prop("parent", parentKey);
+ writeStats(json, key, profileStats);
+ json.endObject();
+ }
+
private void writeStats(JsonWriter json, String profileKey, Map<String, Multimap<String, FacetValue>> profileStats) {
if (profileStats.containsKey(profileKey)) {
Multimap<String, FacetValue> ancestorStats = profileStats.get(profileKey);
private Long getActiveRuleCount(Multimap<String, FacetValue> profileStats) {
Long result = null;
- if (profileStats.containsKey("countActiveRules")) {
- result = profileStats.get("countActiveRules").iterator().next().getValue();
+ if (profileStats.containsKey(ActiveRuleIndex.COUNT_ACTIVE_RULES)) {
+ result = profileStats.get(ActiveRuleIndex.COUNT_ACTIVE_RULES).iterator().next().getValue();
}
return result;
}
private Long getOverridingRuleCount(Multimap<String, FacetValue> profileStats) {
Long result = null;
- if (profileStats.containsKey("inheritance")) {
- for (FacetValue value : profileStats.get("inheritance")) {
+ if (profileStats.containsKey(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())) {
+ for (FacetValue value : profileStats.get(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())) {
if ("OVERRIDES".equals(value.getKey())) {
result = value.getValue();
}
verify(qProfileCopier).copyToName(fromProfileKey, toName);
}
+ @Test
+ public void copy_with_parent() throws Exception {
+ MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+ String fromProfileKey = "xoo-sonar-way-23456";
+ String toName = "Other Sonar Way";
+
+ when(qProfileCopier.copyToName(fromProfileKey, toName)).thenReturn(
+ QualityProfileDto.createFor("xoo-other-sonar-way-12345")
+ .setName(toName)
+ .setLanguage("xoo")
+ .setParentKee("xoo-parent-profile-01324"));
+
+ tester.newPostRequest("api/qualityprofiles", "copy")
+ .setParam("fromKey", fromProfileKey)
+ .setParam("toName", toName)
+ .execute().assertJson(getClass(), "copy_with_parent.json");
+
+ verify(qProfileCopier).copyToName(fromProfileKey, toName);
+ }
+
@Test(expected = IllegalArgumentException.class)
public void fail_on_missing_key() throws Exception {
MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
--- /dev/null
+{
+ "key": "xoo-other-sonar-way-12345",
+ "name": "Other Sonar Way",
+ "language": "xoo",
+ "languageName": "Xoo",
+ "isDefault": false,
+ "isInherited": true,
+ "parentKey": "xoo-parent-profile-01324"
+}
\ No newline at end of file
import java.io.IOException;
import java.net.URL;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* Defines a web service. Note that contrary to the deprecated {@link org.sonar.api.web.Webservice}
/**
* Executed once at server startup.
*/
+ @Override
void define(Context context);
}