-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-6524 Generate built-in profiles from rule metadata #5705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
romainbrenguier
wants to merge
4
commits into
master
Choose a base branch
from
romain/generated-profiles-no-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d4ee84
Use directory-based profile composition to avoid merge conflicts
romainbrenguier 3392502
Fix CI: Updated JavaAgenticWayProfileTest to expect 468 rules (was 46…
romainbrenguier a68bc01
Address PR comment from gitar-bot[bot]
romainbrenguier 6d1550f
Remove profile JSONs from source control
romainbrenguier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
sonar-java-plugin/src/main/build/ProfileJsonGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * 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 Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class ProfileJsonGenerator { | ||
|
|
||
| private static final String PROFILES_RELATIVE_PATH = "src/main/resources/profiles"; | ||
|
|
||
| private static final Map<String, String> PROFILES = Map.of( | ||
| "sonar_way", "Sonar way", | ||
| "sonar_agentic_ai", "Sonar agentic AI" | ||
| ); | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| if (args.length != 2) { | ||
| throw new IllegalArgumentException("Expected module and output directories as arguments."); | ||
| } | ||
|
|
||
| Path profilesDirectory = Path.of(args[0]).resolve(PROFILES_RELATIVE_PATH); | ||
| Path outputDirectory = Path.of(args[1]).resolve("org/sonar/l10n/java/rules/java"); | ||
|
|
||
| Files.createDirectories(outputDirectory); | ||
|
|
||
| for (Map.Entry<String, String> profile : PROFILES.entrySet()) { | ||
| String profileKey = profile.getKey(); | ||
| String profileName = profile.getValue(); | ||
| Path profileDir = profilesDirectory.resolve(profileKey); | ||
|
|
||
| if (!Files.exists(profileDir)) { | ||
| throw new IllegalStateException("Profile directory does not exist: " + profileDir); | ||
| } | ||
|
|
||
| List<String> ruleKeys = collectRuleKeys(profileDir); | ||
| String outputFileName = profileName.replace(" ", "_") + "_profile.json"; | ||
| writeProfile(outputDirectory.resolve(outputFileName), profileName, ruleKeys); | ||
| } | ||
| } | ||
|
|
||
| private static List<String> collectRuleKeys(Path profileDirectory) throws IOException { | ||
| try (Stream<Path> files = Files.list(profileDirectory)) { | ||
| return files | ||
| .filter(Files::isRegularFile) | ||
| .map(Path::getFileName) | ||
| .map(Path::toString) | ||
| .peek(name -> { | ||
| if (!isValidRuleKey(name)) { | ||
| System.err.println("Ignoring non-rule-key file in profile directory: " + name); | ||
| } | ||
| }) | ||
| .filter(ProfileJsonGenerator::isValidRuleKey) | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| .sorted(Comparator.comparingInt(ProfileJsonGenerator::numericKey)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isValidRuleKey(String ruleKey) { | ||
| // Rule keys should start with 'S' followed by digits | ||
| return ruleKey.matches("S\\d+"); | ||
| } | ||
|
|
||
| private static int numericKey(String ruleKey) { | ||
| // Extract numeric part from rule key (e.g., "S100" -> 100) | ||
| return Integer.parseInt(ruleKey.substring(1)); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| private static void writeProfile(Path outputFile, String profileName, List<String> ruleKeys) throws IOException { | ||
| String renderedRuleKeys = ruleKeys.stream() | ||
| .map(" \"%s\""::formatted) | ||
| .collect(Collectors.joining(",\n")); | ||
|
|
||
| String content = """ | ||
| { | ||
| "name": "%s", | ||
| "ruleKeys": [ | ||
| %s | ||
| ] | ||
| } | ||
| """.formatted(profileName, renderedRuleKeys); | ||
|
|
||
| Files.writeString(outputFile, content, StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *_profile.json | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.