diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2022-07-15 13:35:34 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-15 20:02:53 +0000 |
commit | 0fd99d9b189e58d753662270ccb4f4478673d672 (patch) | |
tree | 6c8ab1c1bff685c21d4b23e5885dc307dfc5c07b /server/sonar-webserver-core | |
parent | 6de2fa42c9de26c84ed58000ef66e099f08506db (diff) | |
download | sonarqube-0fd99d9b189e58d753662270ccb4f4478673d672.tar.gz sonarqube-0fd99d9b189e58d753662270ccb4f4478673d672.zip |
SONAR-17035 allow to re-install a previously removed plugin
Diffstat (limited to 'server/sonar-webserver-core')
2 files changed, 59 insertions, 17 deletions
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 0cf12b3bf3f..e7090915508 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 @@ -81,23 +81,17 @@ public class RegisterPlugins implements Startable { PluginDto previousDto = allPreviousPluginsByKey.get(info.getKey()); if (previousDto == null) { LOG.debug("Register new plugin {}", info.getKey()); - PluginDto pluginDto = new PluginDto() - .setUuid(uuidFactory.create()) - .setKee(info.getKey()) - .setBasePluginKey(info.getBasePlugin()) - .setFileHash(installed.getJar().getMd5()) - .setType(toTypeDto(installed.getType())) - .setCreatedAt(now) - .setUpdatedAt(now); - dbClient.pluginDao().insert(dbSession, pluginDto); - } else if (!previousDto.getFileHash().equals(installed.getJar().getMd5()) || !previousDto.getType().equals(toTypeDto(installed.getType()))) { + insertNewPluginDto(dbSession, installed, info); + continue; + } + if (pluginTypeOrJarHashChanged(installed, previousDto)) { LOG.debug("Update plugin {}", info.getKey()); - previousDto - .setBasePluginKey(info.getBasePlugin()) - .setFileHash(installed.getJar().getMd5()) - .setType(toTypeDto(installed.getType())) - .setUpdatedAt(now); - dbClient.pluginDao().update(dbSession, previousDto); + updatePluginDto(dbSession, installed, info, previousDto); + } + if (previousDto.isRemoved()) { + LOG.debug("Previously removed plugin {} was re-installed", info.getKey()); + previousDto.setRemoved(false); + updatePluginDto(dbSession, installed, info, previousDto); } } @@ -119,6 +113,33 @@ public class RegisterPlugins implements Startable { } } + private void insertNewPluginDto(DbSession dbSession, ServerPlugin installed, PluginInfo info) { + long now = system.now(); + PluginDto pluginDto = new PluginDto() + .setUuid(uuidFactory.create()) + .setKee(info.getKey()) + .setBasePluginKey(info.getBasePlugin()) + .setFileHash(installed.getJar().getMd5()) + .setType(toTypeDto(installed.getType())) + .setCreatedAt(now) + .setUpdatedAt(now); + dbClient.pluginDao().insert(dbSession, pluginDto); + } + + private void updatePluginDto(DbSession dbSession, ServerPlugin installed, PluginInfo info, PluginDto previousDto) { + long now = system.now(); + previousDto + .setBasePluginKey(info.getBasePlugin()) + .setFileHash(installed.getJar().getMd5()) + .setType(toTypeDto(installed.getType())) + .setUpdatedAt(now); + dbClient.pluginDao().update(dbSession, previousDto); + } + + private static boolean pluginTypeOrJarHashChanged(ServerPlugin installed, PluginDto previousDto) { + return !previousDto.getFileHash().equals(installed.getJar().getMd5()) || !previousDto.getType().equals(toTypeDto(installed.getType())); + } + private static PluginDto.Type toTypeDto(PluginType type) { switch (type) { case EXTERNAL: 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 6de38d62a45..b402bb89250 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 @@ -63,7 +63,7 @@ public class RegisterPluginsTest { @Before public void setUp() { - when(system2.now()).thenReturn(now).thenThrow(new IllegalStateException("Should be called only once")); + when(system2.now()).thenReturn(now); } /** @@ -125,6 +125,27 @@ public class RegisterPluginsTest { verify(pluginsByKey.get("csharp"), Type.EXTERNAL, null, "a20d785dbacb8f41a3b7392aa7d03b78", false, 1L, 1L); } + @Test + public void re_add_previously_removed_plugin() throws IOException { + dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto() + .setUuid("c") + .setKee("csharp") + .setBasePluginKey(null) + .setFileHash("a20d785dbacb8f41a3b7392aa7d03b78") + .setType(Type.EXTERNAL) + .setRemoved(true) + .setCreatedAt(1L) + .setUpdatedAt(1L)); + dbTester.commit(); + + addPlugin("csharp", PluginType.EXTERNAL, null); + register.start(); + + Map<String, PluginDto> pluginsByKey = selectAllPlugins(); + assertThat(pluginsByKey).hasSize(1); + verify(pluginsByKey.get("csharp"), Type.EXTERNAL, null, "a20d785dbacb8f41a3b7392aa7d03b78", false, 1L, now); + } + /** * Update existing plugins, only when checksum is different and don't remove uninstalled plugins */ |