|
| 1 | +/* |
| 2 | + * SonarLint Core - Implementation |
| 3 | + * Copyright (C) 2016-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarsource.sonarlint.core.plugin.resolvers; |
| 21 | + |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import javax.annotation.Nullable; |
| 28 | +import org.sonarsource.sonarlint.core.commons.Version; |
| 29 | +import org.sonarsource.sonarlint.core.commons.api.SonarLanguage; |
| 30 | +import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; |
| 31 | +import org.sonarsource.sonarlint.core.plugin.ArtifactState; |
| 32 | +import org.sonarsource.sonarlint.core.plugin.PluginJarUtils; |
| 33 | +import org.sonarsource.sonarlint.core.plugin.ResolvedArtifact; |
| 34 | +import org.sonarsource.sonarlint.core.plugin.ServerPluginsCache; |
| 35 | +import org.sonarsource.sonarlint.core.repository.connection.ConnectionConfigurationRepository; |
| 36 | +import org.sonarsource.sonarlint.core.serverapi.exception.ServerRequestException; |
| 37 | +import org.sonarsource.sonarlint.core.serverapi.plugins.ServerPlugin; |
| 38 | +import org.sonarsource.sonarlint.core.serverconnection.StoredPlugin; |
| 39 | +import org.sonarsource.sonarlint.core.storage.StorageService; |
| 40 | + |
| 41 | +import static org.sonarsource.sonarlint.core.plugin.PluginsService.isSonarQubeCloudOrVersionAtLeast; |
| 42 | + |
| 43 | +/** |
| 44 | + * Resolves analyzer plugins from the local storage of a SQS or SQC server connection. |
| 45 | + * |
| 46 | + * <p>{@link #resolve} handles language plugins. It returns immediately: if the locally stored copy is up-to-date its path is returned as |
| 47 | + * {@link ArtifactState#SYNCED}; otherwise a background download is scheduled and |
| 48 | + * {@link ArtifactState#DOWNLOADING} is returned. Concurrent downloads for the same |
| 49 | + * connection + plugin key are de-duplicated by the underlying downloader.</p> |
| 50 | + * |
| 51 | + * <p><b>Events:</b> when a background download completes, a {@link org.sonarsource.sonarlint.core.event.PluginStatusUpdateEvent} |
| 52 | + * is published — {@link ArtifactState#SYNCED} on success, {@link ArtifactState#FAILED} on |
| 53 | + * error.</p> |
| 54 | + */ |
| 55 | +public class ConnectedModeArtifactResolver implements ArtifactResolver { |
| 56 | + |
| 57 | + private static final SonarLintLogger LOG = SonarLintLogger.get(); |
| 58 | + private static final String PLUGIN_FETCH_ERROR = "Could not fetch server plugin list for connection '{}'"; |
| 59 | + |
| 60 | + public static final Version CUSTOM_SECRETS_MIN_SQ_VERSION = Version.create("10.4"); |
| 61 | + public static final Version ENTERPRISE_IAC_MIN_SQ_VERSION = Version.create("2025.1"); |
| 62 | + public static final Version ENTERPRISE_GO_MIN_SQ_VERSION = Version.create("2025.2"); |
| 63 | + |
| 64 | + /** Languages where a new enough server version overrides the embedded plugin. */ |
| 65 | + private static final Map<SonarLanguage, Version> FORCE_OVERRIDES_SINCE_VERSION = Map.of( |
| 66 | + SonarLanguage.SECRETS, CUSTOM_SECRETS_MIN_SQ_VERSION, |
| 67 | + SonarLanguage.AZURERESOURCEMANAGER, ENTERPRISE_IAC_MIN_SQ_VERSION, |
| 68 | + SonarLanguage.GO, ENTERPRISE_GO_MIN_SQ_VERSION); |
| 69 | + |
| 70 | + private final StorageService storageService; |
| 71 | + private final ConnectionConfigurationRepository connectionConfigurationRepository; |
| 72 | + private final ServerPluginsCache serverPluginsCache; |
| 73 | + private final ServerPluginDownloader downloader; |
| 74 | + private final Set<String> skipSyncPluginKeys; |
| 75 | + |
| 76 | + public ConnectedModeArtifactResolver(StorageService storageService, |
| 77 | + ConnectionConfigurationRepository connectionConfigurationRepository, |
| 78 | + ServerPluginsCache serverPluginsCache, |
| 79 | + ServerPluginDownloader downloader, |
| 80 | + Set<String> skipSyncPluginKeys) { |
| 81 | + this.storageService = storageService; |
| 82 | + this.connectionConfigurationRepository = connectionConfigurationRepository; |
| 83 | + this.serverPluginsCache = serverPluginsCache; |
| 84 | + this.downloader = downloader; |
| 85 | + this.skipSyncPluginKeys = skipSyncPluginKeys; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Optional<ResolvedArtifact> resolve(SonarLanguage language, @Nullable String connectionId) { |
| 90 | + if (connectionId == null) { |
| 91 | + return Optional.empty(); |
| 92 | + } |
| 93 | + if (!passesLanguageGate(language, connectionId)) { |
| 94 | + if (skipSyncPluginKeys.contains(language.getPluginKey())) { |
| 95 | + LOG.debug("[SYNC] Code analyzer '{}' is embedded in SonarLint. Skip downloading it.", language.getPluginKey()); |
| 96 | + } |
| 97 | + return Optional.empty(); |
| 98 | + } |
| 99 | + var pluginKey = language.getPluginKey(); |
| 100 | + var fallbackPluginKey = "iacenterprise".equals(pluginKey) ? "iac" : null; |
| 101 | + try { |
| 102 | + return serverPluginsCache.getPlugins(connectionId) |
| 103 | + .flatMap(plugins -> { |
| 104 | + var match = plugins.stream().filter(p -> p.getKey().equals(pluginKey)).findFirst(); |
| 105 | + if (match.isEmpty() && fallbackPluginKey != null) { |
| 106 | + match = plugins.stream().filter(p -> p.getKey().equals(fallbackPluginKey)).findFirst(); |
| 107 | + } |
| 108 | + return match; |
| 109 | + }) |
| 110 | + .map(serverPlugin -> resolveFromStorageOrSchedule(connectionId, serverPlugin, language)) |
| 111 | + .or(() -> resolveFromStorageWithFallback(connectionId, pluginKey, fallbackPluginKey)); |
| 112 | + } catch (ServerRequestException e) { |
| 113 | + LOG.debug(PLUGIN_FETCH_ERROR, connectionId); |
| 114 | + return resolveFromStorageWithFallback(connectionId, pluginKey, fallbackPluginKey); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private ResolvedArtifact resolveFromStorageOrSchedule(String connectionId, ServerPlugin serverPlugin, SonarLanguage language) { |
| 119 | + var fromStorage = resolveFromStorage(connectionId, serverPlugin); |
| 120 | + if (fromStorage.isPresent()) { |
| 121 | + LOG.debug("[SYNC] Code analyzer '{}' is up-to-date. Skip downloading it.", serverPlugin.getKey()); |
| 122 | + return fromStorage.get(); |
| 123 | + } |
| 124 | + downloader.scheduleLanguagePluginDownload(connectionId, serverPlugin, language); |
| 125 | + return new ResolvedArtifact(ArtifactState.DOWNLOADING, null, null, null); |
| 126 | + } |
| 127 | + |
| 128 | + private Optional<ResolvedArtifact> resolveFromStorage(String connectionId, ServerPlugin serverPlugin) { |
| 129 | + return findStoredPlugin(connectionId, serverPlugin.getKey()) |
| 130 | + .filter(s -> s.hasSameHash(serverPlugin)) |
| 131 | + .map(s -> toResolvedArtifact(s.getJarPath(), connectionId)); |
| 132 | + } |
| 133 | + |
| 134 | + private Optional<ResolvedArtifact> resolveFromStorageByKey(String connectionId, String pluginKey) { |
| 135 | + return findStoredPlugin(connectionId, pluginKey) |
| 136 | + .map(s -> toResolvedArtifact(s.getJarPath(), connectionId)); |
| 137 | + } |
| 138 | + |
| 139 | + private Optional<ResolvedArtifact> resolveFromStorageWithFallback(String connectionId, String pluginKey, @Nullable String fallbackPluginKey) { |
| 140 | + return resolveFromStorageByKey(connectionId, pluginKey) |
| 141 | + .or(() -> fallbackPluginKey != null ? resolveFromStorageByKey(connectionId, fallbackPluginKey) : Optional.empty()); |
| 142 | + } |
| 143 | + |
| 144 | + private Optional<StoredPlugin> findStoredPlugin(String connectionId, String pluginKey) { |
| 145 | + var stored = storageService.connection(connectionId).plugins().getStoredPluginsByKey().get(pluginKey); |
| 146 | + if (stored == null || !Files.exists(stored.getJarPath())) { |
| 147 | + return Optional.empty(); |
| 148 | + } |
| 149 | + return Optional.of(stored); |
| 150 | + } |
| 151 | + |
| 152 | + private boolean passesLanguageGate(SonarLanguage language, String connectionId) { |
| 153 | + if (FORCE_OVERRIDES_SINCE_VERSION.containsKey(language)) { |
| 154 | + var minVersion = FORCE_OVERRIDES_SINCE_VERSION.get(language); |
| 155 | + return isSonarQubeCloudOrVersionAtLeast(connectionConfigurationRepository, storageService, minVersion, connectionId); |
| 156 | + } |
| 157 | + return !skipSyncPluginKeys.contains(language.getPluginKey()); |
| 158 | + } |
| 159 | + |
| 160 | + private ResolvedArtifact toResolvedArtifact(Path pluginPath, String connectionId) { |
| 161 | + var source = downloader.sourceFor(connectionId); |
| 162 | + return new ResolvedArtifact(ArtifactState.SYNCED, pluginPath, source, PluginJarUtils.readVersion(pluginPath)); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments