diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-08-26 18:30:15 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-09-18 20:07:13 +0000 |
commit | d189558e9c7b3994254eaa8d67dff0169c21d5dd (patch) | |
tree | db9d6937b26ad9ce923c408a8d3ee94045203d82 /server/sonar-webserver-core | |
parent | 13ef209f1b2cc5f82d0c3bde502ba02649b6c5e2 (diff) | |
download | sonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.tar.gz sonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.zip |
SONAR-13643 Save plugins with type
Diffstat (limited to 'server/sonar-webserver-core')
4 files changed, 104 insertions, 56 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java index 78609fa838d..045ce07e2de 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java @@ -33,8 +33,8 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.server.platform.ServerFileSystem; -import org.sonar.server.plugins.InstalledPlugin; -import org.sonar.server.plugins.PluginFileSystem; +import org.sonar.server.plugins.ServerPlugin; +import org.sonar.server.plugins.ServerPluginRepository; /** * The file deploy/plugins/index.txt is required for old versions of SonarLint. @@ -48,11 +48,11 @@ public final class GeneratePluginIndex implements Startable { private static final Logger LOG = Loggers.get(GeneratePluginIndex.class); private final ServerFileSystem serverFs; - private final PluginFileSystem pluginFs; + private final ServerPluginRepository serverPluginRepository; - public GeneratePluginIndex(ServerFileSystem serverFs, PluginFileSystem pluginFs) { + public GeneratePluginIndex(ServerFileSystem serverFs, ServerPluginRepository serverPluginRepository) { this.serverFs = serverFs; - this.pluginFs = pluginFs; + this.serverPluginRepository = serverPluginRepository; } @Override @@ -71,7 +71,7 @@ public final class GeneratePluginIndex implements Startable { try { FileUtils.forceMkdir(indexFile.getParentFile()); try (Writer writer = new OutputStreamWriter(new FileOutputStream(indexFile), StandardCharsets.UTF_8)) { - for (InstalledPlugin plugin : pluginFs.getInstalledFiles()) { + for (ServerPlugin plugin : serverPluginRepository.getPlugins()) { writer.append(toRow(plugin)); writer.append(CharUtils.LF); } @@ -82,16 +82,15 @@ public final class GeneratePluginIndex implements Startable { } } - private static String toRow(InstalledPlugin file) { - StringBuilder sb = new StringBuilder(); - sb.append(file.getPluginInfo().getKey()) + private static String toRow(ServerPlugin file) { + return new StringBuilder().append(file.getPluginInfo().getKey()) .append(",") .append(file.getPluginInfo().isSonarLintSupported()) .append(",") - .append(file.getLoadedJar().getFile().getName()) + .append(file.getJar().getFile().getName()) .append("|") - .append(file.getLoadedJar().getMd5()); - return sb.toString(); + .append(file.getJar().getMd5()) + .toString(); } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPlugins.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPlugins.java index 85710a00b12..9055589c4ee 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPlugins.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPlugins.java @@ -32,8 +32,9 @@ import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.plugin.PluginDto; -import org.sonar.server.plugins.InstalledPlugin; -import org.sonar.server.plugins.PluginFileSystem; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPlugin; +import org.sonar.server.plugins.ServerPluginRepository; import static java.util.function.Function.identity; @@ -45,13 +46,13 @@ public class RegisterPlugins implements Startable { private static final Logger LOG = Loggers.get(RegisterPlugins.class); - private final PluginFileSystem pluginFileSystem; + private final ServerPluginRepository serverPluginRepository; private final DbClient dbClient; private final UuidFactory uuidFactory; private final System2 system; - public RegisterPlugins(PluginFileSystem pluginFileSystem, DbClient dbClient, UuidFactory uuidFactory, System2 system) { - this.pluginFileSystem = pluginFileSystem; + public RegisterPlugins(ServerPluginRepository serverPluginRepository, DbClient dbClient, UuidFactory uuidFactory, System2 system) { + this.serverPluginRepository = serverPluginRepository; this.dbClient = dbClient; this.uuidFactory = uuidFactory; this.system = system; @@ -74,7 +75,7 @@ public class RegisterPlugins implements Startable { try (DbSession dbSession = dbClient.openSession(false)) { Map<String, PluginDto> allPreviousPluginsByKey = dbClient.pluginDao().selectAll(dbSession).stream() .collect(Collectors.toMap(PluginDto::getKee, identity())); - for (InstalledPlugin installed : pluginFileSystem.getInstalledFiles()) { + for (ServerPlugin installed : serverPluginRepository.getPlugins()) { PluginInfo info = installed.getPluginInfo(); PluginDto previousDto = allPreviousPluginsByKey.get(info.getKey()); if (previousDto == null) { @@ -83,15 +84,17 @@ public class RegisterPlugins implements Startable { .setUuid(uuidFactory.create()) .setKee(info.getKey()) .setBasePluginKey(info.getBasePlugin()) - .setFileHash(installed.getLoadedJar().getMd5()) + .setFileHash(installed.getJar().getMd5()) + .setType(toTypeDto(installed.getType())) .setCreatedAt(now) .setUpdatedAt(now); dbClient.pluginDao().insert(dbSession, pluginDto); - } else if (!previousDto.getFileHash().equals(installed.getLoadedJar().getMd5())) { + } else if (!previousDto.getFileHash().equals(installed.getJar().getMd5()) || !previousDto.getType().equals(toTypeDto(installed.getType()))) { LOG.debug("Update plugin {}", info.getKey()); previousDto .setBasePluginKey(info.getBasePlugin()) - .setFileHash(installed.getLoadedJar().getMd5()) + .setFileHash(installed.getJar().getMd5()) + .setType(toTypeDto(installed.getType())) .setUpdatedAt(now); dbClient.pluginDao().update(dbSession, previousDto); } @@ -101,4 +104,15 @@ public class RegisterPlugins implements Startable { } } + private static PluginDto.Type toTypeDto(PluginType type) { + switch (type) { + case EXTERNAL: + return PluginDto.Type.EXTERNAL; + case BUNDLED: + return PluginDto.Type.BUNDLED; + default: + throw new IllegalStateException("Unknown type: " + type); + } + } + } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java index 73d262e9ff4..d93d0df6b5d 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/GeneratePluginIndexTest.java @@ -29,14 +29,15 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.core.platform.PluginInfo; import org.sonar.server.platform.ServerFileSystem; -import org.sonar.server.plugins.InstalledPlugin; -import org.sonar.server.plugins.InstalledPlugin.FileAndMd5; -import org.sonar.server.plugins.PluginFileSystem; +import org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5; +import org.sonar.server.plugins.ServerPlugin; +import org.sonar.server.plugins.ServerPluginRepository; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.server.plugins.PluginType.BUNDLED; public class GeneratePluginIndexTest { @@ -44,7 +45,7 @@ public class GeneratePluginIndexTest { public TemporaryFolder temp = new TemporaryFolder(); private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class); - private PluginFileSystem pluginFileSystem = mock(PluginFileSystem.class); + private ServerPluginRepository serverPluginRepository = mock(ServerPluginRepository.class); private File index; @Before @@ -55,17 +56,17 @@ public class GeneratePluginIndexTest { @Test public void shouldWriteIndex() throws IOException { - InstalledPlugin javaPlugin = newInstalledPlugin("java", true); - InstalledPlugin gitPlugin = newInstalledPlugin("scmgit", false); - when(pluginFileSystem.getInstalledFiles()).thenReturn(asList(javaPlugin, gitPlugin)); + ServerPlugin javaPlugin = newInstalledPlugin("java", true); + ServerPlugin gitPlugin = newInstalledPlugin("scmgit", false); + when(serverPluginRepository.getPlugins()).thenReturn(asList(javaPlugin, gitPlugin)); - GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, pluginFileSystem); + GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, serverPluginRepository); underTest.start(); List<String> lines = FileUtils.readLines(index); assertThat(lines).containsExactly( - "java,true," + javaPlugin.getLoadedJar().getFile().getName() + "|" + javaPlugin.getLoadedJar().getMd5(), - "scmgit,false," + gitPlugin.getLoadedJar().getFile().getName() + "|" + gitPlugin.getLoadedJar().getMd5()); + "java,true," + javaPlugin.getJar().getFile().getName() + "|" + javaPlugin.getJar().getMd5(), + "scmgit,false," + gitPlugin.getJar().getFile().getName() + "|" + gitPlugin.getJar().getMd5()); underTest.stop(); } @@ -77,12 +78,12 @@ public class GeneratePluginIndexTest { File wrongIndex = new File(wrongParent, "index.txt"); when(serverFileSystem.getPluginIndex()).thenReturn(wrongIndex); - new GeneratePluginIndex(serverFileSystem, pluginFileSystem).start(); + new GeneratePluginIndex(serverFileSystem, serverPluginRepository).start(); } - private InstalledPlugin newInstalledPlugin(String key, boolean supportSonarLint) throws IOException { + private ServerPlugin newInstalledPlugin(String key, boolean supportSonarLint) throws IOException { FileAndMd5 jar = new FileAndMd5(temp.newFile()); PluginInfo pluginInfo = new PluginInfo(key).setJarFile(jar.getFile()).setSonarLintSupported(supportSonarLint); - return new InstalledPlugin(pluginInfo, jar, null); + return new ServerPlugin(pluginInfo, BUNDLED, null, jar, null, null); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/RegisterPluginsTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/RegisterPluginsTest.java index 18d4b454f79..cb13a5200bb 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/RegisterPluginsTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/startup/RegisterPluginsTest.java @@ -35,8 +35,11 @@ import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.plugin.PluginDto; -import org.sonar.server.plugins.InstalledPlugin; -import org.sonar.server.plugins.PluginFileSystem; +import org.sonar.db.plugin.PluginDto.Type; +import org.sonar.server.plugins.PluginFilesAndMd5; +import org.sonar.server.plugins.PluginType; +import org.sonar.server.plugins.ServerPlugin; +import org.sonar.server.plugins.ServerPluginRepository; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -52,10 +55,10 @@ public class RegisterPluginsTest { public DbTester dbTester = DbTester.create(System2.INSTANCE); private final long now = 12345L; - private DbClient dbClient = dbTester.getDbClient(); - private PluginFileSystem pluginFileSystem = mock(PluginFileSystem.class); - private UuidFactory uuidFactory = mock(UuidFactory.class); - private System2 system2 = mock(System2.class); + private final DbClient dbClient = dbTester.getDbClient(); + private final ServerPluginRepository serverPluginRepository = mock(ServerPluginRepository.class); + private final UuidFactory uuidFactory = mock(UuidFactory.class); + private final System2 system2 = mock(System2.class); @Before public void setUp() { @@ -71,17 +74,17 @@ public class RegisterPluginsTest { FileUtils.write(fakeJavaJar, "fakejava", StandardCharsets.UTF_8); File fakeJavaCustomJar = temp.newFile(); FileUtils.write(fakeJavaCustomJar, "fakejavacustom", StandardCharsets.UTF_8); - when(pluginFileSystem.getInstalledFiles()).thenReturn(asList( + when(serverPluginRepository.getPlugins()).thenReturn(asList( newPlugin("java", fakeJavaJar, null), newPlugin("javacustom", fakeJavaCustomJar, "java"))); when(uuidFactory.create()).thenReturn("a").thenReturn("b").thenThrow(new IllegalStateException("Should be called only twice")); - RegisterPlugins register = new RegisterPlugins(pluginFileSystem, dbClient, uuidFactory, system2); + RegisterPlugins register = new RegisterPlugins(serverPluginRepository, dbClient, uuidFactory, system2); register.start(); Map<String, PluginDto> pluginsByKey = selectAllPlugins(); assertThat(pluginsByKey).hasSize(2); - verify(pluginsByKey.get("java"), null, "bd451e47a1aa76e73da0359cef63dd63", now, now); - verify(pluginsByKey.get("javacustom"), "java", "de9b2de3ddc0680904939686c0dba5be", now, now); + verify(pluginsByKey.get("java"), Type.BUNDLED, null, "bd451e47a1aa76e73da0359cef63dd63", now, now); + verify(pluginsByKey.get("javacustom"), Type.BUNDLED, "java", "de9b2de3ddc0680904939686c0dba5be", now, now); register.stop(); } @@ -96,6 +99,7 @@ public class RegisterPluginsTest { .setKee("java") .setBasePluginKey(null) .setFileHash("bd451e47a1aa76e73da0359cef63dd63") + .setType(Type.BUNDLED) .setCreatedAt(1L) .setUpdatedAt(1L)); dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto() @@ -103,39 +107,69 @@ public class RegisterPluginsTest { .setKee("javacustom") .setBasePluginKey("java") .setFileHash("de9b2de3ddc0680904939686c0dba5be") + .setType(Type.BUNDLED) .setCreatedAt(1L) .setUpdatedAt(1L)); + dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto() + .setUuid("c") + .setKee("csharp") + .setBasePluginKey(null) + .setFileHash("a4813b6d879c4ec852747c175cdd6141") + .setType(Type.EXTERNAL) + .setCreatedAt(1L) + .setUpdatedAt(1L)); + dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto() + .setUuid("d") + .setKee("new-measures") + .setBasePluginKey(null) + .setFileHash("6d24712cf701c41ce5eaa948e0bd6d22") + .setType(Type.EXTERNAL) + .setCreatedAt(1L) + .setUpdatedAt(1L)); + dbTester.commit(); File fakeJavaCustomJar = temp.newFile(); FileUtils.write(fakeJavaCustomJar, "fakejavacustomchanged", StandardCharsets.UTF_8); - when(pluginFileSystem.getInstalledFiles()).thenReturn(asList( - newPlugin("javacustom", fakeJavaCustomJar, "java2"))); - new RegisterPlugins(pluginFileSystem, dbClient, uuidFactory, system2).start(); + File fakeCSharpJar = temp.newFile(); + FileUtils.write(fakeCSharpJar, "fakecsharp", StandardCharsets.UTF_8); + + when(serverPluginRepository.getPlugins()).thenReturn(asList( + newPlugin("javacustom", PluginType.BUNDLED, fakeJavaCustomJar, "java2"), + // csharp plugin type changed + newPlugin("csharp", PluginType.BUNDLED, fakeCSharpJar, null))); + + new RegisterPlugins(serverPluginRepository, dbClient, uuidFactory, system2).start(); Map<String, PluginDto> pluginsByKey = selectAllPlugins(); - assertThat(pluginsByKey).hasSize(2); - verify(pluginsByKey.get("java"), null, "bd451e47a1aa76e73da0359cef63dd63", 1L, 1L); - verify(pluginsByKey.get("javacustom"), "java2", "d22091cff5155e892cfe2f9dab51f811", 1L, now); + assertThat(pluginsByKey).hasSize(4); + verify(pluginsByKey.get("java"), Type.BUNDLED, null, "bd451e47a1aa76e73da0359cef63dd63", 1L, 1L); + verify(pluginsByKey.get("javacustom"), Type.BUNDLED, "java2", "d22091cff5155e892cfe2f9dab51f811", 1L, now); + verify(pluginsByKey.get("csharp"), Type.BUNDLED, null, "a4813b6d879c4ec852747c175cdd6141", 1L, now); + verify(pluginsByKey.get("new-measures"), Type.EXTERNAL, null, "6d24712cf701c41ce5eaa948e0bd6d22", 1L, 1L); + } + + private static ServerPlugin newPlugin(String key, File file, @Nullable String basePlugin) { + return newPlugin(key, PluginType.BUNDLED, file, basePlugin); } - private static InstalledPlugin newPlugin(String key, File file, @Nullable String basePlugin) { - InstalledPlugin.FileAndMd5 jar = new InstalledPlugin.FileAndMd5(file); + private static ServerPlugin newPlugin(String key, PluginType type, File file, @Nullable String basePlugin) { + PluginFilesAndMd5.FileAndMd5 jar = new PluginFilesAndMd5.FileAndMd5(file); PluginInfo info = new PluginInfo(key) .setBasePlugin(basePlugin) .setJarFile(file); - return new InstalledPlugin(info, jar, null); + return new ServerPlugin(info, PluginType.BUNDLED, null, jar, null, null); } private Map<String, PluginDto> selectAllPlugins() { - return dbTester.getDbClient().pluginDao().selectAll(dbTester.getSession()) - .stream() + return dbTester.getDbClient().pluginDao().selectAll(dbTester.getSession()).stream() .collect(uniqueIndex(PluginDto::getKee)); } - private void verify(PluginDto java, @Nullable String basePluginKey, String fileHash, @Nullable Long createdAt, long updatedAt) { + private void verify(PluginDto java, Type type, @Nullable String basePluginKey, String fileHash, @Nullable Long createdAt, long updatedAt) { assertThat(java.getBasePluginKey()).isEqualTo(basePluginKey); + assertThat(java.getType()).isEqualTo(type); assertThat(java.getFileHash()).isEqualTo(fileHash); assertThat(java.getCreatedAt()).isEqualTo(createdAt); assertThat(java.getUpdatedAt()).isEqualTo(updatedAt); |