From 992c531ba3250e2621498a9dcbcb4bae66d03e46 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh <117642191+georgii-borovinskikh-sonarsource@users.noreply.github.com> Date: Thu, 7 Mar 2024 12:33:37 +0100 Subject: [PATCH] Add ListAllStandaloneRulesDefinitions to IRulesRpcService (#5284) Part of #5265 --- .../GetEffectiveRuleDetailsResponseTests.cs | 2 + .../Rules/GetEffectiveRuleDetailsParams.cs | 18 +----- .../Rules/GetEffectiveRuleDetailsResponse.cs | 13 +---- src/SLCore/Service/Rules/IRulesRpcService.cs | 22 +++++--- ...stAllStandaloneRulesDefinitionsResponse.cs | 28 ++++++++++ .../Service/Rules/Models/AbstractRuleDto.cs | 46 ++++----------- .../Rules/Models/EffectiveRuleDetailsDto.cs | 56 ++++++++----------- .../Service/Rules/Models/RuleDefinitionDto.cs | 48 ++++++++++++++++ 8 files changed, 129 insertions(+), 104 deletions(-) create mode 100644 src/SLCore/Service/Rules/ListAllStandaloneRulesDefinitionsResponse.cs create mode 100644 src/SLCore/Service/Rules/Models/RuleDefinitionDto.cs diff --git a/src/SLCore.UnitTests/Service/Rules/GetEffectiveRuleDetailsResponseTests.cs b/src/SLCore.UnitTests/Service/Rules/GetEffectiveRuleDetailsResponseTests.cs index ef00526dc0..f5ef98eba3 100644 --- a/src/SLCore.UnitTests/Service/Rules/GetEffectiveRuleDetailsResponseTests.cs +++ b/src/SLCore.UnitTests/Service/Rules/GetEffectiveRuleDetailsResponseTests.cs @@ -107,6 +107,8 @@ public void DeserializeSlcoreResponse_AsExpected() .Should() .BeEquivalentTo(expectedRuleDetails, options => options + .ComparingByMembers() + .ComparingByMembers() .WithStrictOrdering() .RespectingDeclaredTypes() .Excluding((IMemberInfo info) => info.RuntimeType == typeof(string) && info.SelectedMemberPath.EndsWith(".content.Left.htmlContent"))); diff --git a/src/SLCore/Service/Rules/GetEffectiveRuleDetailsParams.cs b/src/SLCore/Service/Rules/GetEffectiveRuleDetailsParams.cs index 28ab536702..00bf5fdc83 100644 --- a/src/SLCore/Service/Rules/GetEffectiveRuleDetailsParams.cs +++ b/src/SLCore/Service/Rules/GetEffectiveRuleDetailsParams.cs @@ -18,20 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System.Diagnostics.CodeAnalysis; +namespace SonarLint.VisualStudio.SLCore.Service.Rules; -namespace SonarLint.VisualStudio.SLCore.Service.Rules -{ - [ExcludeFromCodeCoverage] - public class GetEffectiveRuleDetailsParams - { - public string configurationScopeId { get; } - public string ruleKey { get; } - - public GetEffectiveRuleDetailsParams(string configurationScopeId, string ruleKey) - { - this.configurationScopeId = configurationScopeId; - this.ruleKey = ruleKey; - } - } -} +public record GetEffectiveRuleDetailsParams(string configurationScopeId, string ruleKey); diff --git a/src/SLCore/Service/Rules/GetEffectiveRuleDetailsResponse.cs b/src/SLCore/Service/Rules/GetEffectiveRuleDetailsResponse.cs index 29ca9eaf04..52ebb76dea 100644 --- a/src/SLCore/Service/Rules/GetEffectiveRuleDetailsResponse.cs +++ b/src/SLCore/Service/Rules/GetEffectiveRuleDetailsResponse.cs @@ -20,15 +20,6 @@ using SonarLint.VisualStudio.SLCore.Service.Rules.Models; -namespace SonarLint.VisualStudio.SLCore.Service.Rules -{ - public class GetEffectiveRuleDetailsResponse - { - public GetEffectiveRuleDetailsResponse(EffectiveRuleDetailsDto details) - { - this.details = details; - } +namespace SonarLint.VisualStudio.SLCore.Service.Rules; - public EffectiveRuleDetailsDto details { get; } - } -} +public record GetEffectiveRuleDetailsResponse(EffectiveRuleDetailsDto details); diff --git a/src/SLCore/Service/Rules/IRulesRpcService.cs b/src/SLCore/Service/Rules/IRulesRpcService.cs index 9a1c401a77..77e7050e55 100644 --- a/src/SLCore/Service/Rules/IRulesRpcService.cs +++ b/src/SLCore/Service/Rules/IRulesRpcService.cs @@ -22,14 +22,18 @@ using SonarLint.VisualStudio.SLCore.Core; using SonarLint.VisualStudio.SLCore.Protocol; -namespace SonarLint.VisualStudio.SLCore.Service.Rules +namespace SonarLint.VisualStudio.SLCore.Service.Rules; + +[JsonRpcClass("rule")] +public interface IRulesRpcService : ISLCoreService { - [JsonRpcClassAttribute("rule")] - public interface IRulesRpcService : ISLCoreService - { - /// - /// Gets Rule Meta Data from SLCORE - /// - Task GetEffectiveRuleDetailsAsync(GetEffectiveRuleDetailsParams parameters); - } + /// + /// Gets Rule Meta Data from SLCORE + /// + Task GetEffectiveRuleDetailsAsync(GetEffectiveRuleDetailsParams parameters); + + /// + /// Lists all available standalone rule definitions + /// + Task ListAllStandaloneRulesDefinitionsAsync(); } diff --git a/src/SLCore/Service/Rules/ListAllStandaloneRulesDefinitionsResponse.cs b/src/SLCore/Service/Rules/ListAllStandaloneRulesDefinitionsResponse.cs new file mode 100644 index 0000000000..8683f6ab67 --- /dev/null +++ b/src/SLCore/Service/Rules/ListAllStandaloneRulesDefinitionsResponse.cs @@ -0,0 +1,28 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 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. + */ + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using SonarLint.VisualStudio.SLCore.Service.Rules.Models; + +namespace SonarLint.VisualStudio.SLCore.Service.Rules; + +[ExcludeFromCodeCoverage] +public record ListAllStandaloneRulesDefinitionsResponse(Dictionary rulesByKey); diff --git a/src/SLCore/Service/Rules/Models/AbstractRuleDto.cs b/src/SLCore/Service/Rules/Models/AbstractRuleDto.cs index 2abab41293..1ad3a3b9b5 100644 --- a/src/SLCore/Service/Rules/Models/AbstractRuleDto.cs +++ b/src/SLCore/Service/Rules/Models/AbstractRuleDto.cs @@ -21,39 +21,15 @@ using System.Collections.Generic; using SonarLint.VisualStudio.SLCore.Common.Models; -namespace SonarLint.VisualStudio.SLCore.Service.Rules.Models -{ - public abstract class AbstractRuleDto - { - protected AbstractRuleDto(string key, - string name, - IssueSeverity severity, - RuleType type, - CleanCodeAttribute? cleanCodeAttribute, - CleanCodeAttributeCategory? cleanCodeAttributeCategory, - List defaultImpacts, - Language language, - VulnerabilityProbability? vulnerabilityProbability) - { - this.key = key; - this.name = name; - this.severity = severity; - this.type = type; - this.cleanCodeAttribute = cleanCodeAttribute; - this.cleanCodeAttributeCategory = cleanCodeAttributeCategory; - this.defaultImpacts = defaultImpacts; - this.language = language; - this.vulnerabilityProbability = vulnerabilityProbability; - } +namespace SonarLint.VisualStudio.SLCore.Service.Rules.Models; - public string key { get; } - public string name { get; } - public IssueSeverity severity { get; } - public RuleType type { get; } - public CleanCodeAttribute? cleanCodeAttribute { get; } - public CleanCodeAttributeCategory? cleanCodeAttributeCategory { get; } - public List defaultImpacts { get; } - public Language language { get; } - public VulnerabilityProbability? vulnerabilityProbability { get; } - } -} +public abstract record AbstractRuleDto( + string key, + string name, + IssueSeverity severity, + RuleType type, + CleanCodeAttribute? cleanCodeAttribute, + CleanCodeAttributeCategory? cleanCodeAttributeCategory, + List defaultImpacts, + Language language, + VulnerabilityProbability? vulnerabilityProbability); diff --git a/src/SLCore/Service/Rules/Models/EffectiveRuleDetailsDto.cs b/src/SLCore/Service/Rules/Models/EffectiveRuleDetailsDto.cs index 06343d2625..4d214045b9 100644 --- a/src/SLCore/Service/Rules/Models/EffectiveRuleDetailsDto.cs +++ b/src/SLCore/Service/Rules/Models/EffectiveRuleDetailsDto.cs @@ -23,37 +23,27 @@ using SonarLint.VisualStudio.SLCore.Common.Models; using SonarLint.VisualStudio.SLCore.Protocol; -namespace SonarLint.VisualStudio.SLCore.Service.Rules.Models -{ - public class EffectiveRuleDetailsDto : AbstractRuleDto - { - public EffectiveRuleDetailsDto(string key, - string name, - IssueSeverity severity, - RuleType type, - CleanCodeAttribute? cleanCodeAttribute, - CleanCodeAttributeCategory? cleanCodeAttributeCategory, - List defaultImpacts, - Language language, - VulnerabilityProbability? vulnerabilityProbability, - Either description, - List @params) - : base(key, - name, - severity, - type, - cleanCodeAttribute, - cleanCodeAttributeCategory, - defaultImpacts, - language, - vulnerabilityProbability) - { - this.description = description; - this.@params = @params; - } +namespace SonarLint.VisualStudio.SLCore.Service.Rules.Models; - [JsonConverter(typeof(EitherJsonConverter))] - public Either description { get; } - public List @params { get; } - } -} +public record EffectiveRuleDetailsDto( + string key, + string name, + IssueSeverity severity, + RuleType type, + CleanCodeAttribute? cleanCodeAttribute, + CleanCodeAttributeCategory? cleanCodeAttributeCategory, + List defaultImpacts, + Language language, + VulnerabilityProbability? vulnerabilityProbability, + [property: JsonConverter(typeof(EitherJsonConverter))] + Either description, + List @params) + : AbstractRuleDto(key, + name, + severity, + type, + cleanCodeAttribute, + cleanCodeAttributeCategory, + defaultImpacts, + language, + vulnerabilityProbability); diff --git a/src/SLCore/Service/Rules/Models/RuleDefinitionDto.cs b/src/SLCore/Service/Rules/Models/RuleDefinitionDto.cs new file mode 100644 index 0000000000..761b507ebf --- /dev/null +++ b/src/SLCore/Service/Rules/Models/RuleDefinitionDto.cs @@ -0,0 +1,48 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 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. + */ + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using SonarLint.VisualStudio.SLCore.Common.Models; + +namespace SonarLint.VisualStudio.SLCore.Service.Rules.Models; + +[ExcludeFromCodeCoverage] +public record RuleDefinitionDto( + string key, + string name, + IssueSeverity severity, + RuleType type, + CleanCodeAttribute? cleanCodeAttribute, + CleanCodeAttributeCategory? cleanCodeAttributeCategory, + List defaultImpacts, + Language language, + VulnerabilityProbability? vulnerabilityProbability, + Dictionary paramsByKey, // object because we ignore rule parameters at the moment + bool isActiveByDefault) + : AbstractRuleDto(key, + name, + severity, + type, + cleanCodeAttribute, + cleanCodeAttributeCategory, + defaultImpacts, + language, + vulnerabilityProbability);