diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-03-01 14:16:11 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-03-02 14:19:36 +0100 |
commit | 84bc220fc84aaa1bbf88fc5c29ff7db113aef711 (patch) | |
tree | fe31a7462fea1f2114d5ccb477fd64768e5719ee | |
parent | 6e79f62bfb40e11fabbf35ffd468119eb0456396 (diff) | |
download | sonarqube-84bc220fc84aaa1bbf88fc5c29ff7db113aef711.tar.gz sonarqube-84bc220fc84aaa1bbf88fc5c29ff7db113aef711.zip |
SONAR-7329 Add type in Rules WS
18 files changed, 104 insertions, 35 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java index 97cd80d2d9d..b2fd487880c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java @@ -146,7 +146,7 @@ public class SearchResponseFormat { private void formatIssue(Issues.Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data) { issueBuilder.setKey(dto.getKey()); - Issues.IssueType type = Issues.IssueType.valueOf(dto.getType()); + Common.RuleType type = Common.RuleType.valueOf(dto.getType()); if (type != null) { issueBuilder.setType(type); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 0afad0b7870..5da852ecf80 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -78,7 +78,12 @@ public class RuleMapper { public Rules.Rule toWsRule(RuleDto ruleDto, SearchResult result, Set<String> fieldsToReturn) { Rules.Rule.Builder ruleResponse = Rules.Rule.newBuilder(); + // Mandatory fields ruleResponse.setKey(ruleDto.getKey().toString()); + Common.RuleType type = Common.RuleType.valueOf(ruleDto.getType()); + ruleResponse.setType(type); + + // Optional fields setRepository(ruleResponse, ruleDto, fieldsToReturn); setName(ruleResponse, ruleDto, fieldsToReturn); setStatus(ruleResponse, ruleDto, fieldsToReturn); diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java index 91251371d10..7ed6347411d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -47,6 +47,7 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; +import org.sonar.core.rule.RuleType; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QualityProfileDto; @@ -55,7 +56,6 @@ import org.sonar.db.rule.RuleParamDto; import org.sonar.server.es.Facets; import org.sonar.server.es.SearchIdResult; import org.sonar.server.qualityprofile.ActiveRule; -import org.sonar.server.rule.Rule; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.rule.index.RuleIndexDefinition; import org.sonar.server.rule.index.RuleQuery; @@ -71,6 +71,7 @@ import static org.sonar.server.rule.index.RuleIndex.FACET_REPOSITORIES; import static org.sonar.server.rule.index.RuleIndex.FACET_SEVERITIES; import static org.sonar.server.rule.index.RuleIndex.FACET_STATUSES; import static org.sonar.server.rule.index.RuleIndex.FACET_TAGS; +import static org.sonar.server.rule.index.RuleIndex.FACET_TYPES; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.rule.RulesWsParameters.OPTIONAL_FIELDS; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVATION; @@ -88,6 +89,7 @@ import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_SEVERITIES; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_STATUSES; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TAGS; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TEMPLATE_KEY; +import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TYPES; /** * @since 4.4 @@ -182,6 +184,7 @@ public class SearchAction implements RulesWsAction { FACET_SEVERITIES, FACET_ACTIVE_SEVERITIES, FACET_STATUSES, + FACET_TYPES, FACET_OLD_DEFAULT); } @@ -242,6 +245,13 @@ public class SearchAction implements RulesWsAction { .setExampleValue("security,java8"); action + .createParam(PARAM_TYPES) + .setSince("5.5") + .setDescription("Comma-separated list of types. Returned rules match any of the tags (OR operator)") + .setPossibleValues(RuleType.values()) + .setExampleValue(RuleType.BUG); + + action .createParam(PARAM_ACTIVATION) .setDescription("Filter rules that are activated or deactivated on the selected Quality profile. Ignored if " + "the parameter '" + PARAM_QPROFILE + "' is not set.") @@ -306,6 +316,7 @@ public class SearchAction implements RulesWsAction { query.setActiveSeverities(request.paramAsStrings(PARAM_ACTIVE_SEVERITIES)); query.setIsTemplate(request.paramAsBoolean(PARAM_IS_TEMPLATE)); query.setTemplateKey(request.param(PARAM_TEMPLATE_KEY)); + query.setTypes(request.paramAsEnums(PARAM_TYPES, RuleType.class)); query.setKey(request.param(PARAM_KEY)); String sortParam = request.param(Param.SORT); @@ -415,6 +426,7 @@ public class SearchAction implements RulesWsAction { addMandatoryFacetValues(results, FACET_SEVERITIES, Severity.ALL); addMandatoryFacetValues(results, FACET_ACTIVE_SEVERITIES, Severity.ALL); addMandatoryFacetValues(results, FACET_TAGS, request.paramAsStrings(PARAM_TAGS)); + addMandatoryFacetValues(results, FACET_TYPES, RuleType.ALL_NAMES); Common.Facet.Builder facet = Common.Facet.newBuilder(); Common.FacetValue.Builder value = Common.FacetValue.newBuilder(); @@ -547,12 +559,13 @@ public class SearchAction implements RulesWsAction { } } - private enum RuleToRuleKey implements Function<Rule, RuleKey> { + private enum TypeToString implements Function<RuleType, String> { INSTANCE; @Override - public RuleKey apply(@Nonnull Rule input) { - return input.key(); + public String apply(@Nonnull RuleType input) { + return input.name(); } + } } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-search.json b/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-search.json index 7b639d78419..66e05d1a969 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-search.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-search.json @@ -16,6 +16,7 @@ "sysTags": ["brain-overload"], "lang": "java", "langName": "Java", + "type": "CODE_SMELL", "params": [ { "key": "max", @@ -37,6 +38,7 @@ "sysTags": ["brain-overload"], "lang": "java", "langName": "Java", + "type": "BUG", "params": [ { "key": "max", @@ -58,6 +60,7 @@ "sysTags": ["brain-overload"], "lang": "java", "langName": "Java", + "type": "VULNERABILITY", "params": [ { "key": "max", @@ -83,6 +86,7 @@ "noteLogin": "eric.hartmann", "lang": "java", "langName": "Java", + "type": "CODE_SMELL", "params": [ { "key": "xpathQuery", @@ -111,6 +115,7 @@ "sysTags": [ ], "lang": "java", "langName": "Java", + "type": "CODE_SMELL", "params": [ { "key": "xpathQuery", diff --git a/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-show.json b/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-show.json index f96c98b3ab4..3a4041a2fcf 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-show.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/rule/ws/example-show.json @@ -14,6 +14,7 @@ "debtRemFnOffset": "10min", "lang": "java", "langName": "Java", + "type": "CODE_SMELL", "params": [ { "key": "max", @@ -44,4 +45,4 @@ } ] } -]}
\ No newline at end of file +]} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index f38f97333a8..52ab1060cf9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -202,7 +202,7 @@ public class QProfilesWsTest { WebService.Action restoreProfiles = controller.action(BulkRuleActivationActions.BULK_DEACTIVATE_ACTION); assertThat(restoreProfiles).isNotNull(); assertThat(restoreProfiles.isPost()).isTrue(); - assertThat(restoreProfiles.params()).hasSize(19); + assertThat(restoreProfiles.params()).hasSize(20); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java index 02fba900faf..92562d64d0a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java @@ -34,6 +34,7 @@ import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.DateUtils; +import org.sonar.core.rule.RuleType; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleDao; @@ -129,8 +130,10 @@ public class SearchActionMediumTest { @Test public void search_2_rules() throws Exception { - ruleDao.insert(dbSession, RuleTesting.newXooX1()); - ruleDao.insert(dbSession, RuleTesting.newXooX2()); + ruleDao.insert(dbSession, RuleTesting.newXooX1() + .setType(RuleType.BUG)); + ruleDao.insert(dbSession, RuleTesting.newXooX2() + .setType(RuleType.VULNERABILITY)); dbSession.commit(); ruleIndexer.index(); @@ -141,9 +144,15 @@ public class SearchActionMediumTest { } @Test - public void search_2_rules_with_field_selection() throws Exception { - ruleDao.insert(dbSession, RuleTesting.newXooX1()); - ruleDao.insert(dbSession, RuleTesting.newXooX2().setDescription("A *Xoo* rule").setDescriptionFormat(RuleDto.Format.MARKDOWN)); + public void search_2_rules_with_fields_selection() throws Exception { + ruleDao.insert(dbSession, RuleTesting.newXooX1() + .setType(RuleType.CODE_SMELL)) + ; + ruleDao.insert(dbSession, RuleTesting.newXooX2() + .setType(RuleType.BUG) + .setDescription("A *Xoo* rule") + .setDescriptionFormat(RuleDto.Format.MARKDOWN)) + ; dbSession.commit(); ruleIndexer.index(); @@ -154,6 +163,21 @@ public class SearchActionMediumTest { } @Test + public void return_mandatory_fields_even_when_setting_f_param() throws Exception { + ruleDao.insert(dbSession, RuleTesting.newXooX1() + .setName("Rule x1") + .setType(RuleType.CODE_SMELL)) + ; + dbSession.commit(); + ruleIndexer.index(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "name"); + WsTester.Result result = request.execute(); + + result.assertJson(getClass(), "return_mandatory_fields_even_when_setting_f_param.json"); + } + + @Test public void return_lang_field() throws Exception { ruleDao.insert(dbSession, RuleTesting.newXooX1()); dbSession.commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java index 371c7b172fb..02d3f0145c9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java @@ -28,6 +28,7 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.rule.RuleType; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.rule.RuleDao; @@ -83,7 +84,8 @@ public class ShowActionMediumTest { .setConfigKey("InternalKeyS001") .setLanguage("xoo") .setTags(newHashSet("tag1", "tag2")) - .setSystemTags(newHashSet("systag1", "systag2")); + .setSystemTags(newHashSet("systag1", "systag2")) + .setType(RuleType.BUG); ruleDao.insert(session, ruleDto); RuleParamDto param = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*"); ruleDao.insertRuleParam(session, ruleDto, param); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_active.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_active.json index 78fa263c0b5..2e936cacb6d 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_active.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_active.json @@ -8,11 +8,16 @@ "status": "READY", "internalKey": "InternalKeyx1", "isTemplate": false, - "tags": ["tag1", "tag2"], - "sysTags": ["systag1", "systag2"], + "tags": [ + "tag1", + "tag2" + ], + "sysTags": [ + "systag1", + "systag2" + ], "debtRemFnType": "LINEAR", "debtRemFnCoeff": "1h", - "debtRemFnOffset": "5min", "lang": "xoo", "params": [] }, @@ -23,4 +28,5 @@ "severity": "BLOCKER", "params": [] } - ]} + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_no_active.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_no_active.json index f43b0339098..d511ffae2ff 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_no_active.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWsMediumTest/show_rule_no_active.json @@ -11,7 +11,6 @@ "sysTags": ["systag1", "systag2"], "debtRemFnType": "LINEAR", "debtRemFnCoeff": "1h", - "debtRemFnOffset": "5min", "lang": "xoo", "params": [] }} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/return_mandatory_fields_even_when_setting_f_param.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/return_mandatory_fields_even_when_setting_f_param.json new file mode 100644 index 00000000000..f3a9823c7ba --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/return_mandatory_fields_even_when_setting_f_param.json @@ -0,0 +1,9 @@ +{ + "rules": [ + { + "key": "xoo:x1", + "name": "Rule x1", + "type": "CODE_SMELL" + } + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules.json index a3da4d33ff2..315359d66f9 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules.json @@ -22,8 +22,8 @@ ], "debtRemFnType": "LINEAR", "debtRemFnCoeff": "1h", - "debtRemFnOffset": "5min", "lang": "xoo", + "type": "VULNERABILITY", "params": [] }, { @@ -45,8 +45,8 @@ ], "debtRemFnType": "LINEAR", "debtRemFnCoeff": "1h", - "debtRemFnOffset": "5min", "lang": "xoo", + "type": "BUG", "params": [] } ] diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules_fields.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules_fields.json index 6ae36b21898..30c6468a86b 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules_fields.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_2_rules_fields.json @@ -7,12 +7,14 @@ "key": "xoo:x2", "name": "Rule x2", "mdDesc": "A *Xoo* rule", - "htmlDesc": "A <strong>Xoo</strong> rule" + "htmlDesc": "A <strong>Xoo</strong> rule", + "type": "BUG" }, { "key": "xoo:x1", "name": "Rule x1", - "htmlDesc": "Description x1" + "htmlDesc": "Description x1", + "type": "CODE_SMELL" } ] } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json index 8712b74c2b7..ebc65b24748 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json @@ -9,6 +9,7 @@ "isTemplate": false, "tags": ["tag1", "tag2"], "sysTags": ["systag1", "systag2"], + "type": "BUG", "lang": "xoo", "params": [ { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java index 384423b5beb..d636e7966e5 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java @@ -35,6 +35,7 @@ public class RulesWsParameters { public static final String PARAM_DEBT_CHARACTERISTICS = "debt_characteristics"; public static final String PARAM_HAS_DEBT_CHARACTERISTIC = "has_debt_characteristic"; public static final String PARAM_TAGS = "tags"; + public static final String PARAM_TYPES = "types"; public static final String PARAM_INHERITANCE = "inheritance"; public static final String PARAM_ACTIVE_SEVERITIES = "active_severities"; public static final String PARAM_IS_TEMPLATE = "is_template"; diff --git a/sonar-ws/src/main/protobuf/ws-commons.proto b/sonar-ws/src/main/protobuf/ws-commons.proto index efd2fee3ca4..9a2b9ce836f 100644 --- a/sonar-ws/src/main/protobuf/ws-commons.proto +++ b/sonar-ws/src/main/protobuf/ws-commons.proto @@ -112,3 +112,15 @@ message Metric { optional string bestValue = 11; optional string worstValue = 12; } + +enum RuleType { + // Zero is required in order to not get MAINTAINABILITY as default value + // See http://androiddevblog.com/protocol-buffers-pitfall-adding-enum-values/ + UNKNOWN = 0; + + // same name as in Java enum IssueType, + // same index values as in database (see column ISSUES.ISSUE_TYPE) + CODE_SMELL = 1; + BUG = 2; + VULNERABILITY = 3; +} diff --git a/sonar-ws/src/main/protobuf/ws-issues.proto b/sonar-ws/src/main/protobuf/ws-issues.proto index 077871bebe1..5459f84b684 100644 --- a/sonar-ws/src/main/protobuf/ws-issues.proto +++ b/sonar-ws/src/main/protobuf/ws-issues.proto @@ -54,18 +54,6 @@ message Operation { repeated ActionPlan actionPlans = 5; } -enum IssueType { - // Zero is required in order to not get MAINTAINABILITY as default value - // See http://androiddevblog.com/protocol-buffers-pitfall-adding-enum-values/ - UNKNOWN = 0; - - // same name as in Java enum IssueType, - // same index values as in database (see column ISSUES.ISSUE_TYPE) - CODE_SMELL = 1; - BUG = 2; - VULNERABILITY = 3; -} - message Issue { optional string key = 1; optional string rule = 2; @@ -102,7 +90,7 @@ message Issue { optional string fUpdateAge = 25; optional string closeDate = 26; - optional IssueType type = 27; + optional sonarqube.ws.commons.RuleType type = 27; } message Transitions { diff --git a/sonar-ws/src/main/protobuf/ws-rules.proto b/sonar-ws/src/main/protobuf/ws-rules.proto index e377cf7b3c2..ce049c220bc 100644 --- a/sonar-ws/src/main/protobuf/ws-rules.proto +++ b/sonar-ws/src/main/protobuf/ws-rules.proto @@ -102,6 +102,7 @@ message Rule { optional string debtRemFnType = 34; optional string debtRemFnCoeff = 35; optional string debtRemFnOffset = 36; + optional sonarqube.ws.commons.RuleType type = 37; message Params { repeated Param params = 1; |