aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-10-12 15:33:38 +0200
committerStas Vilchik <stas.vilchik@sonarsource.com>2017-10-17 10:03:56 +0200
commita344a9b3647f3227ebe50fb5620d51a1eba36ec4 (patch)
tree0d7a17013a2e5282de95e292a6651c346c281db6 /server
parent3e15ddb6e8b8d8195c3d2334bda43a53ffb593a7 (diff)
downloadsonarqube-a344a9b3647f3227ebe50fb5620d51a1eba36ec4.tar.gz
sonarqube-a344a9b3647f3227ebe50fb5620d51a1eba36ec4.zip
SONAR-8303 Add type parameter to WS api/rules/create
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java12
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java12
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java6
4 files changed, 27 insertions, 5 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java b/server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java
index 2d82b22cc32..be9eb8f8127 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/rule/NewCustomRule.java
@@ -27,6 +27,7 @@ import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rules.RuleType;
public class NewCustomRule {
@@ -37,6 +38,7 @@ public class NewCustomRule {
private String markdownDescription;
private String severity;
private RuleStatus status;
+ private RuleType type;
private final Map<String, String> parameters = Maps.newHashMap();
private boolean preventReactivation = false;
@@ -105,6 +107,16 @@ public class NewCustomRule {
}
@CheckForNull
+ public RuleType type() {
+ return type;
+ }
+
+ public NewCustomRule setType(@Nullable RuleType type) {
+ this.type = type;
+ return this;
+ }
+
+ @CheckForNull
public String parameter(final String paramKey) {
return parameters.get(paramKey);
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
index f9ad9c6ce6a..725550f1eb7 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
@@ -166,13 +166,13 @@ public class RuleCreator {
.setDescriptionFormat(Format.MARKDOWN)
.setSeverity(newRule.severity())
.setStatus(newRule.status())
+ .setType(newRule.type() == null ? templateRuleDto.getType() : newRule.type().getDbConstant())
.setLanguage(templateRuleDto.getLanguage())
.setDefRemediationFunction(templateRuleDto.getDefRemediationFunction())
.setDefRemediationGapMultiplier(templateRuleDto.getDefRemediationGapMultiplier())
.setDefRemediationBaseEffort(templateRuleDto.getDefRemediationBaseEffort())
.setGapDescription(templateRuleDto.getGapDescription())
.setSystemTags(templateRuleDto.getSystemTags())
- .setType(templateRuleDto.getType())
.setCreatedAt(system2.now())
.setUpdatedAt(system2.now());
dbClient.ruleDao().insert(dbSession, ruleDefinition);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java
index d0625002a03..9b0a465b258 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java
@@ -26,6 +26,7 @@ import java.util.Optional;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
+import org.sonar.api.rules.RuleType;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
@@ -43,11 +44,9 @@ import org.sonarqube.ws.Rules;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.net.HttpURLConnection.HTTP_CONFLICT;
import static java.util.Collections.singletonList;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
-/**
- * @since 4.4
- */
public class CreateAction implements RulesWsAction {
public static final String PARAM_CUSTOM_KEY = "custom_key";
@@ -56,6 +55,7 @@ public class CreateAction implements RulesWsAction {
public static final String PARAM_SEVERITY = "severity";
public static final String PARAM_STATUS = "status";
public static final String PARAM_TEMPLATE_KEY = "template_key";
+ public static final String PARAM_TYPE = "type";
public static final String PARAMS = "params";
public static final String PARAM_PREVENT_REACTIVATION = "prevent_reactivation";
@@ -132,6 +132,11 @@ public class CreateAction implements RulesWsAction {
.setDescription("If set to true and if the rule has been deactivated (status 'REMOVED'), a status 409 will be returned")
.setDefaultValue(false)
.setBooleanPossibleValues();
+
+ action.createParam(PARAM_TYPE)
+ .setDescription("Rule type")
+ .setSince("6.7")
+ .setPossibleValues(RuleType.names());
}
@Override
@@ -150,6 +155,7 @@ public class CreateAction implements RulesWsAction {
if (!isNullOrEmpty(params)) {
newRule.setParameters(KeyValueFormat.parse(params));
}
+ setNullable(request.param(PARAM_TYPE), t -> newRule.setType(RuleType.valueOf(t)));
writeResponse(dbSession, request, response, ruleCreator.create(dbSession, newRule));
} catch (ReactivationException e) {
response.stream().setStatus(HTTP_CONFLICT);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java
index 366d1cbdb4c..16d7eb73674 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java
@@ -50,6 +50,8 @@ import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
+import static org.sonar.api.rules.RuleType.BUG;
+import static org.sonar.api.rules.RuleType.CODE_SMELL;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.db.rule.RuleTesting.newCustomRule;
import static org.sonar.db.rule.RuleTesting.newTemplateRule;
@@ -84,7 +86,7 @@ public class CreateActionTest {
public void create_custom_rule() {
logInAsQProfileAdministrator();
// Template rule
- RuleDto templateRule = newTemplateRule(RuleKey.of("java", "S001"), db.getDefaultOrganization());
+ RuleDto templateRule = newTemplateRule(RuleKey.of("java", "S001"), db.getDefaultOrganization()).setType(CODE_SMELL);
db.rules().insert(templateRule.getDefinition());
db.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleId(templateRule.getId()));
db.rules().insertRuleParam(templateRule.getDefinition(), param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
@@ -96,6 +98,7 @@ public class CreateActionTest {
.setParam("markdown_description", "Description")
.setParam("severity", "MAJOR")
.setParam("status", "BETA")
+ .setParam("type", BUG.name())
.setParam("params", "regex=a.*")
.execute().getInput();
@@ -107,6 +110,7 @@ public class CreateActionTest {
" \"htmlDesc\": \"Description\",\n" +
" \"severity\": \"MAJOR\",\n" +
" \"status\": \"BETA\",\n" +
+ " \"type\": \"BUG\",\n" +
" \"internalKey\": \"InternalKeyS001\",\n" +
" \"isTemplate\": false,\n" +
" \"templateKey\": \"java:S001\",\n" +