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 {
private String markdownDescription;
private String severity;
private RuleStatus status;
+ private RuleType type;
private final Map<String, String> parameters = Maps.newHashMap();
private boolean preventReactivation = false;
return this;
}
+ @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);
.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);
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;
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";
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";
.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
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);
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;
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(".*"));
.setParam("markdown_description", "Description")
.setParam("severity", "MAJOR")
.setParam("status", "BETA")
+ .setParam("type", BUG.name())
.setParam("params", "regex=a.*")
.execute().getInput();
" \"htmlDesc\": \"Description\",\n" +
" \"severity\": \"MAJOR\",\n" +
" \"status\": \"BETA\",\n" +
+ " \"type\": \"BUG\",\n" +
" \"internalKey\": \"InternalKeyS001\",\n" +
" \"isTemplate\": false,\n" +
" \"templateKey\": \"java:S001\",\n" +