*/
package org.sonar.server.common.rule.service;
-import java.util.ArrayList;
+import java.util.List;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.common.rule.RuleCreator;
+import static java.util.Collections.singletonList;
+
public class RuleService {
private final DbClient dbClient;
}
public RuleInformation createCustomRule(NewCustomRule newCustomRule, DbSession dbSession) {
- return new RuleInformation(ruleCreator.create(dbSession, newCustomRule), new ArrayList<>());
+ RuleDto ruleDto = ruleCreator.create(dbSession, newCustomRule);
+ List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleUuids(dbSession, singletonList(ruleDto.getUuid()));
+ return new RuleInformation(ruleDto, ruleParameters);
}
}
import org.sonar.server.common.rule.service.RuleService;
import org.sonar.server.user.UserSession;
import org.sonar.server.v2.api.rule.converter.RuleRestResponseGenerator;
-import org.sonar.server.v2.api.rule.request.Impact;
+import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
import org.sonar.server.v2.api.rule.request.RuleCreateRestRequest;
+import org.sonar.server.v2.api.rule.resource.Impact;
import org.sonar.server.v2.api.rule.response.RuleRestResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
+import static java.util.Optional.ofNullable;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
public class DefaultRuleController implements RuleController {
NewCustomRule newCustomRule = NewCustomRule.createForCustomRule(RuleKey.parse(request.key()), RuleKey.parse(request.templateKey()))
.setName(request.name())
.setMarkdownDescription(request.markdownDescription())
- .setStatus(request.status())
- .setCleanCodeAttribute(request.cleanCodeAttribute())
+ .setStatus(ofNullable(request.status()).map(RuleStatusRestEnum::getRuleStatus).orElse(null))
+ .setCleanCodeAttribute(request.cleanCodeAttribute().getCleanCodeAttribute())
.setImpacts(request.impacts().stream().map(DefaultRuleController::toNewCustomRuleImpact).toList())
.setPreventReactivation(true);
if (request.parameters() != null) {
}
private static NewCustomRule.Impact toNewCustomRuleImpact(Impact impact) {
- return new NewCustomRule.Impact(impact.softwareQuality(), impact.severity());
+ return new NewCustomRule.Impact(impact.softwareQuality().getSoftwareQuality(), impact.severity().getSeverity());
}
}
import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
import org.sonar.server.v2.api.rule.enums.RuleTypeRestEnum;
import org.sonar.server.v2.api.rule.enums.SoftwareQualityRestEnum;
+import org.sonar.server.v2.api.rule.resource.Impact;
+import org.sonar.server.v2.api.rule.resource.Parameter;
import org.sonar.server.v2.api.rule.response.RuleDescriptionSectionContextRestResponse;
import org.sonar.server.v2.api.rule.response.RuleDescriptionSectionRestResponse;
-import org.sonar.server.v2.api.rule.response.RuleImpactRestResponse;
-import org.sonar.server.v2.api.rule.ressource.Parameter;
import org.sonar.server.v2.api.rule.response.RuleRestResponse;
import static java.util.Optional.ofNullable;
return new RuleDescriptionSectionRestResponse(RuleDescriptionSectionDto.DEFAULT_KEY, macroInterpreter.interpret(description), null);
}
- private static List<RuleImpactRestResponse> toImpactRestResponse(Set<ImpactDto> defaultImpacts) {
+ private static List<Impact> toImpactRestResponse(Set<ImpactDto> defaultImpacts) {
return defaultImpacts.stream()
- .map(i -> new RuleImpactRestResponse(SoftwareQualityRestEnum.from(i.getSoftwareQuality()), ImpactSeverityRestEnum.from(i.getSeverity())))
+ .map(i -> new Impact(SoftwareQualityRestEnum.from(i.getSoftwareQuality()), ImpactSeverityRestEnum.from(i.getSeverity())))
.toList();
}
this.cleanCodeAttributeCategory = cleanCodeAttributeCategory;
}
+ public CleanCodeAttributeCategory getCleanCodeAttributeCategory() {
+ return cleanCodeAttributeCategory;
+ }
+
@CheckForNull
public static CleanCodeAttributeCategoryRestEnum from(@Nullable CleanCodeAttributeCategory cleanCodeAttributeCategory) {
if (cleanCodeAttributeCategory == null) {
this.cleanCodeAttribute = cleanCodeAttribute;
}
+ public CleanCodeAttribute getCleanCodeAttribute() {
+ return cleanCodeAttribute;
+ }
+
@CheckForNull
public static CleanCodeAttributeRestEnum from(@Nullable CleanCodeAttribute cleanCodeAttribute) {
if (cleanCodeAttribute == null) {
this.severity = severity;
}
+ public Severity getSeverity() {
+ return severity;
+ }
+
public static ImpactSeverityRestEnum from(Severity severity) {
return Arrays.stream(ImpactSeverityRestEnum.values())
.filter(severityRestResponse -> severityRestResponse.severity.equals(severity))
this.ruleStatus = ruleStatus;
}
+ public RuleStatus getRuleStatus() {
+ return ruleStatus;
+ }
+
public static RuleStatusRestEnum from(RuleStatus ruleStatus) {
return Arrays.stream(RuleStatusRestEnum.values())
.filter(ruleStatusRestEnum -> ruleStatusRestEnum.ruleStatus.equals(ruleStatus))
this.ruleType = ruleType;
}
+ public RuleType getRuleType() {
+ return ruleType;
+ }
+
public static RuleTypeRestEnum from(RuleType ruleType) {
return Arrays.stream(RuleTypeRestEnum.values())
.filter(ruleTypeRestEnum -> ruleTypeRestEnum.ruleType.equals(ruleType))
this.softwareQuality = softwareQuality;
}
+ public SoftwareQuality getSoftwareQuality() {
+ return softwareQuality;
+ }
+
public static SoftwareQualityRestEnum from(SoftwareQuality softwareQuality) {
return Arrays.stream(SoftwareQualityRestEnum.values())
.filter(softwareQualityRest -> softwareQualityRest.softwareQuality.equals(softwareQuality))
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.rules.CleanCodeAttribute;
-import org.sonar.server.v2.api.rule.ressource.Parameter;
+import org.sonar.server.v2.api.rule.enums.CleanCodeAttributeRestEnum;
+import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
+import org.sonar.server.v2.api.rule.resource.Impact;
+import org.sonar.server.v2.api.rule.resource.Parameter;
public record RuleCreateRestRequest(
@Nullable
@Schema(description = "Rule status", defaultValue = "READY")
- RuleStatus status,
+ RuleStatusRestEnum status,
@Nullable
@Schema(description = "Custom rule parameters")
@NotNull
@Schema(description = "Clean code attribute")
- CleanCodeAttribute cleanCodeAttribute,
+ CleanCodeAttributeRestEnum cleanCodeAttribute,
@Valid
@NotEmpty
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v2.api.rule.resource;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import org.sonar.server.v2.api.rule.enums.ImpactSeverityRestEnum;
+import org.sonar.server.v2.api.rule.enums.SoftwareQualityRestEnum;
+
+@Schema(accessMode = Schema.AccessMode.READ_WRITE)
+public record Impact(
+ SoftwareQualityRestEnum softwareQuality,
+ ImpactSeverityRestEnum severity
+) {
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v2.api.rule.resource;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import javax.annotation.Nullable;
+
+import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
+import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;
+
+public record Parameter(
+
+ @Schema(accessMode = READ_WRITE)
+ String key,
+ @Schema(accessMode = READ_ONLY)
+ String htmlDescription,
+ @Nullable
+ @Schema(accessMode = READ_WRITE)
+ String defaultValue,
+ @Schema(allowableValues = {
+ "STRING",
+ "TEXT",
+ "BOOLEAN",
+ "INTEGER",
+ "FLOAT"
+ }, accessMode = READ_ONLY)
+ String type
+) {
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.v2.api.rule.resource;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 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.v2.api.rule.response;
-
-import io.swagger.v3.oas.annotations.media.Schema;
-import org.sonar.server.v2.api.rule.enums.ImpactSeverityRestEnum;
-import org.sonar.server.v2.api.rule.enums.SoftwareQualityRestEnum;
-
-@Schema(accessMode = Schema.AccessMode.READ_ONLY)
-public record RuleImpactRestResponse(
- SoftwareQualityRestEnum softwareQuality,
- ImpactSeverityRestEnum severity
-) {
-}
import org.sonar.server.v2.api.rule.enums.CleanCodeAttributeRestEnum;
import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
import org.sonar.server.v2.api.rule.enums.RuleTypeRestEnum;
-import org.sonar.server.v2.api.rule.ressource.Parameter;
+import org.sonar.server.v2.api.rule.resource.Impact;
+import org.sonar.server.v2.api.rule.resource.Parameter;
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
public record RuleRestResponse(
@Nullable
String severity,
RuleTypeRestEnum type,
- List<RuleImpactRestResponse> impacts,
+ List<Impact> impacts,
@Nullable
CleanCodeAttributeRestEnum cleanCodeAttribute,
@Nullable
private String name;
private String severity;
private RuleTypeRestEnum type;
- private List<RuleImpactRestResponse> impacts;
+ private List<Impact> impacts;
private CleanCodeAttributeRestEnum cleanCodeAttribute;
private CleanCodeAttributeCategoryRestEnum cleanCodeAttributeCategory;
private RuleStatusRestEnum status;
return this;
}
- public Builder setImpacts(List<RuleImpactRestResponse> impacts) {
+ public Builder setImpacts(List<Impact> impacts) {
this.impacts = impacts;
return this;
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 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.v2.api.rule.ressource;
-
-import io.swagger.v3.oas.annotations.media.Schema;
-import javax.annotation.Nullable;
-
-import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.*;
-
-public record Parameter(
-
- @Schema(accessMode = READ_WRITE)
- String key,
- @Schema(accessMode = READ_ONLY)
- String htmlDescription,
- @Nullable
- @Schema(accessMode = READ_WRITE)
- String defaultValue,
- @Schema(allowableValues = {
- "STRING",
- "TEXT",
- "BOOLEAN",
- "INTEGER",
- "FLOAT"
- }, accessMode = READ_ONLY)
- String type
-) {
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 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.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.server.v2.api.rule.ressource;
-
-import javax.annotation.ParametersAreNonnullByDefault;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.common.rule.ReactivationException;
import org.sonar.server.common.rule.service.NewCustomRule;
+import org.sonar.server.common.rule.service.RuleInformation;
import org.sonar.server.common.rule.service.RuleService;
import org.sonarqube.ws.Rules;
try (DbSession dbSession = dbClient.openSession(false)) {
try {
NewCustomRule newCustomRule = toNewCustomRule(request);
- writeResponse(dbSession, request, response, ruleService.createCustomRule(newCustomRule, dbSession).ruleDto());
+ RuleInformation customRule = ruleService.createCustomRule(newCustomRule, dbSession);
+ writeResponse(dbSession, request, response, customRule.ruleDto(), customRule.params());
} catch (ReactivationException e) {
response.stream().setStatus(HTTP_CONFLICT);
writeResponse(dbSession, request, response, e.ruleKey());
return newRule;
}
- private void writeResponse(DbSession dbSession, Request request, Response response, RuleDto rule) {
- writeProtobuf(createResponse(dbSession, rule), request, response);
+ private void writeResponse(DbSession dbSession, Request request, Response response, RuleDto rule, List<RuleParamDto> params) {
+ writeProtobuf(createResponse(dbSession, rule, params), request, response);
}
private void writeResponse(DbSession dbSession, Request request, Response response, RuleKey ruleKey) {
RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, ruleKey)
.orElseThrow(() -> new IllegalStateException(String.format("Cannot load rule, that has just been created '%s'", ruleKey)));
- writeProtobuf(createResponse(dbSession, rule), request, response);
+ List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleUuids(dbSession, singletonList(rule.getUuid()));
+ writeProtobuf(createResponse(dbSession, rule, ruleParameters), request, response);
}
- private Rules.CreateResponse createResponse(DbSession dbSession, RuleDto rule) {
+ private Rules.CreateResponse createResponse(DbSession dbSession, RuleDto rule, List<RuleParamDto> params) {
List<RuleDto> templateRules = new ArrayList<>();
if (rule.isCustomRule()) {
Optional<RuleDto> templateRule = dbClient.ruleDao().selectByUuid(rule.getTemplateUuid(), dbSession);
templateRule.ifPresent(templateRules::add);
}
- List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleUuids(dbSession, singletonList(rule.getUuid()));
+
RulesResponseFormatter.SearchResult searchResult = new RulesResponseFormatter.SearchResult()
- .setRuleParameters(ruleParameters)
+ .setRuleParameters(params)
.setTemplateRules(templateRules)
.setTotal(1L);
return Rules.CreateResponse.newBuilder()