]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21131 Use same enums between request and response and map parameters
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Wed, 6 Dec 2023 09:57:10 +0000 (10:57 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 8 Dec 2023 20:03:05 +0000 (20:03 +0000)
18 files changed:
server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/service/RuleService.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/converter/RuleRestResponseGenerator.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/CleanCodeAttributeCategoryRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/CleanCodeAttributeRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/ImpactSeverityRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/RuleStatusRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/RuleTypeRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/enums/SoftwareQualityRestEnum.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Impact.java [new file with mode: 0644]
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Parameter.java [new file with mode: 0644]
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/package-info.java [new file with mode: 0644]
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/response/RuleImpactRestResponse.java [deleted file]
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/response/RuleRestResponse.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/Parameter.java [deleted file]
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/package-info.java [deleted file]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/CreateAction.java

index 9a55ea1965e75731348676376965f770ca7c3b52..ebdf2021b747f93abfbe7487934058cd842cbb86 100644 (file)
  */
 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;
@@ -41,6 +45,8 @@ public class RuleService {
   }
 
   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);
   }
 }
index 34fad2f218e8f06cc6165ceee1b5637eb58eacaf..c5fe2dcb1238f1680812a89d380b7403edebe7d0 100644 (file)
@@ -28,12 +28,14 @@ import org.sonar.server.common.rule.service.RuleInformation;
 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 {
@@ -64,8 +66,8 @@ 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) {
@@ -77,6 +79,6 @@ public class DefaultRuleController implements RuleController {
   }
 
   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());
   }
 }
index 2e06e4ff9d863aa1121eab3f9f4e574af61605eb..ebb87d72f65ceb6132e4d21221a75411605b08cc 100644 (file)
@@ -47,10 +47,10 @@ import org.sonar.server.v2.api.rule.enums.ImpactSeverityRestEnum;
 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;
@@ -167,9 +167,9 @@ public class RuleRestResponseGenerator {
     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();
   }
 
index bd07a53efb151131939a415ff90dc6114f8aacfc..72bed69456faf2a2f0632364bbeb9b64e411b22f 100644 (file)
@@ -36,6 +36,10 @@ public enum CleanCodeAttributeCategoryRestEnum {
     this.cleanCodeAttributeCategory = cleanCodeAttributeCategory;
   }
 
+  public CleanCodeAttributeCategory getCleanCodeAttributeCategory() {
+    return cleanCodeAttributeCategory;
+  }
+
   @CheckForNull
   public static CleanCodeAttributeCategoryRestEnum from(@Nullable CleanCodeAttributeCategory cleanCodeAttributeCategory) {
     if (cleanCodeAttributeCategory == null) {
index 6bfa406ec025fb7ab5a0fbb192f83ba3544a82ba..34bb49a553e3fc77f54aa010adebc03c3d7571b9 100644 (file)
@@ -49,6 +49,10 @@ public enum CleanCodeAttributeRestEnum {
     this.cleanCodeAttribute = cleanCodeAttribute;
   }
 
+  public CleanCodeAttribute getCleanCodeAttribute() {
+    return cleanCodeAttribute;
+  }
+
   @CheckForNull
   public static CleanCodeAttributeRestEnum from(@Nullable CleanCodeAttribute cleanCodeAttribute) {
     if (cleanCodeAttribute == null) {
index 469797cd31c573101b791764985250c0df103843..84a1f6bb6ca00042b7d19c324051ac8128679029 100644 (file)
@@ -34,6 +34,10 @@ public enum ImpactSeverityRestEnum {
     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))
index 071b6ad53d437c4e89681f8de1c02b6483b053fe..5333892237a29ecb0ddf10ff211bd0a80583dfdf 100644 (file)
@@ -34,6 +34,10 @@ public enum RuleStatusRestEnum {
     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))
index 09b2179f018a7e152d6171d93734cea8ed2e15bf..e9b2c4bdfc1dfe0c507413b7c705a7ceec1f9a9d 100644 (file)
@@ -35,6 +35,10 @@ public enum RuleTypeRestEnum {
     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))
index 95ab51416dddbaf49600058bb52a7f1c345168c8..59e749a5f77366ff3bf8b70f5a4fb76a46f5da48 100644 (file)
@@ -35,6 +35,10 @@ public enum SoftwareQualityRestEnum {
     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))
index fde9c475ae6d4f35f22a5611acd1f62445bc9c20..ff8ba1b7a15e06cce6d27337c1f61554975ab659 100644 (file)
@@ -26,9 +26,10 @@ import javax.validation.Valid;
 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(
 
@@ -53,7 +54,7 @@ public record RuleCreateRestRequest(
 
   @Nullable
   @Schema(description = "Rule status", defaultValue = "READY")
-  RuleStatus status,
+  RuleStatusRestEnum status,
 
   @Nullable
   @Schema(description = "Custom rule parameters")
@@ -61,7 +62,7 @@ public record RuleCreateRestRequest(
 
   @NotNull
   @Schema(description = "Clean code attribute")
-  CleanCodeAttribute cleanCodeAttribute,
+  CleanCodeAttributeRestEnum cleanCodeAttribute,
 
   @Valid
   @NotEmpty
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Impact.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Impact.java
new file mode 100644 (file)
index 0000000..a9c1b25
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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
+) {
+}
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Parameter.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/Parameter.java
new file mode 100644 (file)
index 0000000..9d77f3d
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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
+) {
+}
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/package-info.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/resource/package-info.java
new file mode 100644 (file)
index 0000000..1dfa76e
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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;
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/response/RuleImpactRestResponse.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/response/RuleImpactRestResponse.java
deleted file mode 100644 (file)
index a0efcf7..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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
-) {
-}
index aef6c0d2a6a2444a8863c7bae037ab75f8a6bdff..c7ad59c6cfe0b87cd3c382023a9f50fab578e997 100644 (file)
@@ -26,7 +26,8 @@ import org.sonar.server.v2.api.rule.enums.CleanCodeAttributeCategoryRestEnum;
 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(
@@ -37,7 +38,7 @@ public record RuleRestResponse(
   @Nullable
   String severity,
   RuleTypeRestEnum type,
-  List<RuleImpactRestResponse> impacts,
+  List<Impact> impacts,
   @Nullable
   CleanCodeAttributeRestEnum cleanCodeAttribute,
   @Nullable
@@ -81,7 +82,7 @@ public record RuleRestResponse(
     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;
@@ -141,7 +142,7 @@ public record RuleRestResponse(
       return this;
     }
 
-    public Builder setImpacts(List<RuleImpactRestResponse> impacts) {
+    public Builder setImpacts(List<Impact> impacts) {
       this.impacts = impacts;
       return this;
     }
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/Parameter.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/Parameter.java
deleted file mode 100644 (file)
index 9724542..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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
-) {
-}
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/package-info.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/ressource/package-info.java
deleted file mode 100644 (file)
index cfa25b5..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * 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;
index 69b6c04a6fdbc370ada0523080ca32d4edb3bd93..121b32594eeb80df27a0ccec183c8b1d172c433b 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.db.rule.RuleDto;
 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;
 
@@ -172,7 +173,8 @@ public class CreateAction implements RulesWsAction {
     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());
@@ -204,25 +206,26 @@ public class CreateAction implements RulesWsAction {
     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()