]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17035 allow to re-install a previously removed plugin
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Fri, 15 Jul 2022 11:35:34 +0000 (13:35 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 15 Jul 2022 20:02:53 +0000 (20:02 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPlugins.java
server/sonar-webserver-core/src/test/java/org/sonar/server/startup/RegisterPluginsTest.java

index 0cf12b3bf3fe33f05fa917d153718c38834ec52a..e709091550862c9e2fbff3e4a55362523e4cc3fe 100644 (file)
@@ -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:
index 6de38d62a457d0db572ea3a9148c4899a75c1924..b402bb89250829e8d1c5d1609dc65bf6cddc16b3 100644 (file)
@@ -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
    */