Skip to content

Commit

Permalink
Allow setting of SonarQube module key bindings by applying a pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
swesteme committed Dec 20, 2024
1 parent 97b6deb commit bb9d553
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 19 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ signPlugin {
}

publishPlugin {
token = System.getenv("PUBLISH_TOKEN")
token = System.getenv("PUBLISH_TOKEN")
setHidden(findProperty("hidden").toBoolean())
channels = findProperty("preRelease").toBoolean() ? ["autoconfig-beta"] : ["default", "autoconfig-beta"]
}
Expand All @@ -72,7 +72,8 @@ intellij {
"Git4Idea",
"junit",
"org.jetbrains.plugins.yaml",
"org.jetbrains.idea.maven"
"org.jetbrains.idea.maven",
"org.sonarlint.idea:10.8.1.79205"
]
}
buildSearchableOptions.enabled = false // Disable because it takes a long time and the plugin doesn't need it
Expand Down Expand Up @@ -285,7 +286,7 @@ jsonSchema2Pojo {
useOptionalForGetters = false

// properties to exclude from generated toString
toStringExcludes = ["someProperty"]
toStringExcludes = []

// What Java version to target with generated source code (1.6, 1.8, 9, 11, etc).
// By default, the version will be taken from the Gradle Java plugin's 'sourceCompatibility',
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Autoconfig Plugin Properties
pluginVersion = 1.0.2
pluginVersion = 1.1.0
preRelease = false
hidden = false
pluginGroup = de.gebit.plugins.autoconfig
Expand Down
3 changes: 2 additions & 1 deletion metadata/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<title>Changelog</title>
</head>
<body>
<h2>1.0.3</h2>
<h2>1.1.0</h2>
<ul>
<li>support setting of module SDK</li>
<li>support setting of module SonarQube project keys</li>
</ul>
<h2>1.0.2</h2>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ default boolean acceptModule(T configuration, Module module) {
default UpdateTarget getUpdateTarget() {
return UpdateTarget.MODULE;
}

default boolean matchesAnyName(Module module, List<String> patterns) {
return patterns.stream().anyMatch(p -> module.getName().matches(p));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
* Allow setting module SDK for a limited number of matching modules.
Expand Down Expand Up @@ -54,10 +55,6 @@ public boolean acceptModule(GeneralModuleConfiguration configuration, Module mod
return matchesAnyName(module, configuration.getModuleFilter());
}

private static boolean matchesAnyName(Module module, List<String> patterns) {
return patterns.stream().anyMatch(p -> module.getName().matches(p));
}

@Override
public List<String> updateConfiguration(GeneralModuleConfiguration configuration, Module module) {
List<String> updatedConfigs = new ArrayList<>();
Expand All @@ -78,17 +75,12 @@ private void applyModuleSDKOptions(ModuleSDK sdkOptions, Module module, List<Str
if (projectSdk != null && projectSdk.equals(moduleSdk)) {
// reset module SDK in case project SDK and designated module SDK are identical
if (!modifiableModel.isSdkInherited()) {
WriteAction.runAndWait(() -> {
modifiableModel.inheritSdk();
modifiableModel.commit();
});
commitModifiableModelChange(modifiableModel, ModifiableRootModel::inheritSdk);
updatedConfigs.add("Module SDK");
}
} else {
applySetting(moduleSdk, currentModuleSdk, s -> WriteAction.runAndWait(() -> {
modifiableModel.setSdk(s);
modifiableModel.commit();
}), updatedConfigs, "Module SDK");
applySetting(moduleSdk, currentModuleSdk, s -> commitModifiableModelChange(modifiableModel,
modifiableRootModel -> modifiableRootModel.setSdk(s)), updatedConfigs, "Module SDK");
}
}
} else {
Expand All @@ -98,4 +90,11 @@ private void applyModuleSDKOptions(ModuleSDK sdkOptions, Module module, List<Str
}
}
}

private void commitModifiableModelChange(ModifiableRootModel modifiableModel, Consumer<ModifiableRootModel> consumer) {
WriteAction.runAndWait(() -> {
consumer.accept(modifiableModel);
modifiableModel.commit();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.gebit.plugins.autoconfig.handlers.sonarqube;

import com.intellij.openapi.module.Module;
import de.gebit.plugins.autoconfig.UpdateModuleHandler;
import de.gebit.plugins.autoconfig.handlers.AbstractHandler;
import de.gebit.plugins.autoconfig.model.SonarQubeConfiguration;
import org.jetbrains.annotations.NonNls;
import org.sonarlint.intellij.config.Settings;
import org.sonarlint.intellij.config.module.SonarLintModuleSettings;
import org.sonarlint.intellij.config.project.SonarLintProjectSettings;

import java.util.ArrayList;
import java.util.List;

/**
* Configuration handler used to synchronise SonarQube module bindings in projects.
*/
public class SonarQubeModuleHandler extends AbstractHandler implements UpdateModuleHandler<SonarQubeConfiguration> {
private static final @NonNls String CONFIG_SCHEMA_JSON = "/schema/sonarqubeModule.schema.json";

public static final @NonNls String CONFIG_FILE_NAME = "autoconfigSonarQubeModule.yaml";

@Override
public String getFileName() {
return CONFIG_FILE_NAME;
}

@Override
public String getJsonSchema() {
return CONFIG_SCHEMA_JSON;
}

@Override
public String getUpdaterName() {
return "SonarQube module configuration updater";
}

@Override
public Class<SonarQubeConfiguration> getConfigurationClass() {
return SonarQubeConfiguration.class;
}

@Override
public boolean acceptModule(SonarQubeConfiguration configuration, Module module) {
return matchesAnyName(module, configuration.getModuleFilter());
}

@Override
public List<String> updateConfiguration(SonarQubeConfiguration configuration, Module module) {
List<String> updatedConfigs = new ArrayList<>();
SonarLintProjectSettings projectSettings = Settings.getSettingsFor(module.getProject());
if (projectSettings.isBindingEnabled() && configuration.getProjectKey() != null) {
SonarLintModuleSettings moduleSettings = Settings.getSettingsFor(module);
applySetting(configuration.getProjectKey(), moduleSettings.getProjectKey(), moduleSettings::setProjectKey,
updatedConfigs, "SonarQube module key");
}
return updatedConfigs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.gebit.plugins.autoconfig.AutoconfigStartup;
import de.gebit.plugins.autoconfig.UpdateHandler;
import de.gebit.plugins.autoconfig.UpdateModuleHandler;
import de.gebit.plugins.autoconfig.util.Notifications;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -101,9 +102,19 @@ private <T> List<String> processModuleUpdateHandler(@NotNull List<ModuleDirector
private <T> List<String> processUpdateHandler(@NotNull Project project, UpdateHandler<T> updateHandler, ConfigurationLoaderService projectService, VirtualFile configDirectory) {
Optional<T> extensionConfiguration = projectService.getConfiguration(updateHandler.getConfigurationClass(),
updateHandler.getFileName(), configDirectory);
return extensionConfiguration.map(c -> updateHandler.updateConfiguration(c, project)).orElseGet(() -> {
final List<String> emptyList = Collections.emptyList();
return extensionConfiguration.map(c -> {
try {
return updateHandler.updateConfiguration(c, project);
} catch (Exception e) {
String errorMessage = "An error occurred trying to process configuration updates for \"" + updateHandler.getUpdaterName() + "\"";
LOG.error(errorMessage, e);
Notifications.showError(errorMessage, project);
}
return emptyList;
}).orElseGet(() -> {
LOG.info("No configuration for " + updateHandler.getUpdaterName() + " found.");
return Collections.emptyList();
return emptyList;
});
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-git.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.git.GitHandler"/>
</extensions>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-java.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.java.JavaHandler"/>
</extensions>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-maven.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.maven.MavenHandler"/>
</extensions>
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin-sonarqube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<moduleConfigurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.sonarqube.SonarQubeModuleHandler"/>
</extensions>
</idea-plugin>
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<depends optional="true" config-file="plugin-git.xml">Git4Idea</depends>
<depends optional="true" config-file="plugin-maven.xml">org.jetbrains.idea.maven</depends>
<depends optional="true" config-file="plugin-java.xml">com.intellij.modules.java</depends>
<depends optional="true" config-file="plugin-sonarqube.xml">org.sonarlint.idea</depends>

<extensionPoints>
<extensionPoint name="configurationUpdater" interface="de.gebit.plugins.autoconfig.UpdateHandler" dynamic="true"/>
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/schema/sonarqubeModule.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$id": "https://www.gebit.de/autoconfig-intellij-plugin/sonarqubeModule.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "SonarQube configuration",
"type": "object",
"required": [
"moduleFilter"
],
"properties": {
"moduleFilter": {
"type": "array",
"description": "List of modules/module regex patterns. Modules that match any of the names/patterns will be configured by this configuration.",
"items": {
"type": "string",
"description": "Module name/pattern"
}
},
"projectKey": {
"type": "string",
"description": "The SonarQube project key to use for all matching modules"
}
}
}

0 comments on commit bb9d553

Please sign in to comment.