Search in sources :

Example 1 with PluginDto

use of org.sonar.db.plugin.PluginDto in project sonarqube by SonarSource.

the class RegisterPluginsTest method update_removed_plugins.

@Test
public void update_removed_plugins() throws IOException {
    // will be flagged as removed
    dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto().setUuid("a").setKee("java").setBasePluginKey(null).setFileHash("bd451e47a1aa76e73da0359cef63dd63").setType(Type.BUNDLED).setCreatedAt(1L).setUpdatedAt(1L));
    // already flagged as removed
    dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto().setUuid("b").setKee("java2").setBasePluginKey(null).setFileHash("bd451e47a1aa76e73da0359cef63dd63").setType(Type.BUNDLED).setRemoved(true).setCreatedAt(1L).setUpdatedAt(1L));
    dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto().setUuid("c").setKee("csharp").setBasePluginKey(null).setFileHash("a20d785dbacb8f41a3b7392aa7d03b78").setType(Type.EXTERNAL).setCreatedAt(1L).setUpdatedAt(1L));
    dbTester.commit();
    addPlugin("csharp", PluginType.EXTERNAL, null);
    register.start();
    Map<String, PluginDto> pluginsByKey = selectAllPlugins();
    assertThat(pluginsByKey).hasSize(3);
    verify(pluginsByKey.get("java"), Type.BUNDLED, null, "bd451e47a1aa76e73da0359cef63dd63", true, 1L, now);
    verify(pluginsByKey.get("java2"), Type.BUNDLED, null, "bd451e47a1aa76e73da0359cef63dd63", true, 1L, 1L);
    verify(pluginsByKey.get("csharp"), Type.EXTERNAL, null, "a20d785dbacb8f41a3b7392aa7d03b78", false, 1L, 1L);
}
Also used : PluginDto(org.sonar.db.plugin.PluginDto) Test(org.junit.Test)

Example 2 with PluginDto

use of org.sonar.db.plugin.PluginDto in project sonarqube by SonarSource.

the class DetectPluginChange method anyChange.

private boolean anyChange() {
    try (DbSession dbSession = dbClient.openSession(false)) {
        Map<String, PluginDto> dbPluginsByKey = dbClient.pluginDao().selectAll(dbSession).stream().filter(p -> !p.isRemoved()).collect(Collectors.toMap(PluginDto::getKee, identity()));
        Map<String, ServerPlugin> filePluginsByKey = serverPluginRepository.getPlugins().stream().collect(Collectors.toMap(p -> p.getPluginInfo().getKey(), p -> p));
        if (!dbPluginsByKey.keySet().equals(filePluginsByKey.keySet())) {
            return true;
        }
        for (ServerPlugin installed : filePluginsByKey.values()) {
            PluginDto dbPlugin = dbPluginsByKey.get(installed.getPluginInfo().getKey());
            if (changed(dbPlugin, installed)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Loggers(org.sonar.api.utils.log.Loggers) DbClient(org.sonar.db.DbClient) Preconditions(org.sonar.api.utils.Preconditions) Profiler(org.sonar.api.utils.log.Profiler) Startable(org.sonar.api.Startable) Map(java.util.Map) Function.identity(java.util.function.Function.identity) PluginDto(org.sonar.db.plugin.PluginDto) Collectors(java.util.stream.Collectors) DbSession(org.sonar.db.DbSession) Logger(org.sonar.api.utils.log.Logger) DbSession(org.sonar.db.DbSession) PluginDto(org.sonar.db.plugin.PluginDto)

Example 3 with PluginDto

use of org.sonar.db.plugin.PluginDto in project sonarqube by SonarSource.

the class RegisterPlugins method updateDB.

private void updateDB() {
    long now = system.now();
    try (DbSession dbSession = dbClient.openSession(false)) {
        Map<String, PluginDto> allPreviousPluginsByKey = dbClient.pluginDao().selectAll(dbSession).stream().collect(Collectors.toMap(PluginDto::getKee, identity()));
        for (ServerPlugin installed : serverPluginRepository.getPlugins()) {
            PluginInfo info = installed.getPluginInfo();
            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()))) {
                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);
            }
        }
        // keep uninstalled plugins with a 'removed' flag, because corresponding rules and active rules are also not deleted
        for (PluginDto dto : allPreviousPluginsByKey.values()) {
            if (dto.isRemoved()) {
                continue;
            }
            if (serverPluginRepository.findPlugin(dto.getKee()).isEmpty()) {
                dto.setRemoved(true).setUpdatedAt(now);
                dbClient.pluginDao().update(dbSession, dto);
            }
        }
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) PluginDto(org.sonar.db.plugin.PluginDto) ServerPlugin(org.sonar.server.plugins.ServerPlugin) PluginInfo(org.sonar.core.platform.PluginInfo)

Example 4 with PluginDto

use of org.sonar.db.plugin.PluginDto in project sonarqube by SonarSource.

the class InstalledAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String typeParam = request.param(PARAM_TYPE);
    SortedSet<ServerPlugin> installedPlugins = loadInstalledPlugins(typeParam);
    Map<String, PluginDto> dtosByKey;
    try (DbSession dbSession = dbClient.openSession(false)) {
        dtosByKey = dbClient.pluginDao().selectAll(dbSession).stream().collect(toMap(PluginDto::getKee, Function.identity()));
    }
    List<String> additionalFields = request.paramAsStrings(WebService.Param.FIELDS);
    Map<String, Plugin> updateCenterPlugins = (additionalFields == null || additionalFields.isEmpty()) ? emptyMap() : compatiblePluginsByKey(updateCenterMatrixFactory);
    List<PluginDetails> pluginList = new LinkedList<>();
    for (ServerPlugin installedPlugin : installedPlugins) {
        PluginInfo pluginInfo = installedPlugin.getPluginInfo();
        PluginDto pluginDto = dtosByKey.get(pluginInfo.getKey());
        Objects.requireNonNull(pluginDto, () -> format("Plugin %s is installed but not in DB", pluginInfo.getKey()));
        Plugin updateCenterPlugin = updateCenterPlugins.get(pluginInfo.getKey());
        pluginList.add(buildPluginDetails(installedPlugin, pluginInfo, pluginDto, updateCenterPlugin));
    }
    InstalledPluginsWsResponse wsResponse = InstalledPluginsWsResponse.newBuilder().addAllPlugins(pluginList).build();
    writeProtobuf(wsResponse, request, response);
}
Also used : InstalledPluginsWsResponse(org.sonarqube.ws.Plugins.InstalledPluginsWsResponse) ServerPlugin(org.sonar.server.plugins.ServerPlugin) LinkedList(java.util.LinkedList) DbSession(org.sonar.db.DbSession) PluginDto(org.sonar.db.plugin.PluginDto) PluginDetails(org.sonarqube.ws.Plugins.PluginDetails) PluginWSCommons.buildPluginDetails(org.sonar.server.plugins.ws.PluginWSCommons.buildPluginDetails) PluginInfo(org.sonar.core.platform.PluginInfo) ServerPlugin(org.sonar.server.plugins.ServerPlugin) Plugin(org.sonar.updatecenter.common.Plugin)

Aggregations

PluginDto (org.sonar.db.plugin.PluginDto)4 DbSession (org.sonar.db.DbSession)3 PluginInfo (org.sonar.core.platform.PluginInfo)2 ServerPlugin (org.sonar.server.plugins.ServerPlugin)2 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Function.identity (java.util.function.Function.identity)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Startable (org.sonar.api.Startable)1 Preconditions (org.sonar.api.utils.Preconditions)1 Logger (org.sonar.api.utils.log.Logger)1 Loggers (org.sonar.api.utils.log.Loggers)1 Profiler (org.sonar.api.utils.log.Profiler)1 DbClient (org.sonar.db.DbClient)1 PluginWSCommons.buildPluginDetails (org.sonar.server.plugins.ws.PluginWSCommons.buildPluginDetails)1 Plugin (org.sonar.updatecenter.common.Plugin)1 InstalledPluginsWsResponse (org.sonarqube.ws.Plugins.InstalledPluginsWsResponse)1 PluginDetails (org.sonarqube.ws.Plugins.PluginDetails)1