From: Julien Lancelot Date: Mon, 21 Sep 2015 15:59:24 +0000 (+0200) Subject: SONAR-6548 Log a message on deprecated projects X-Git-Tag: 5.2-RC1~305 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F525%2Fhead;p=sonarqube.git SONAR-6548 Log a message on deprecated projects --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java index 565b7c5a5e8..5f94331b0b9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java @@ -26,6 +26,7 @@ import org.sonar.server.qualitygate.RegisterQualityGates; import org.sonar.server.qualityprofile.RegisterQualityProfiles; import org.sonar.server.rule.RegisterRules; import org.sonar.server.search.IndexSynchronizer; +import org.sonar.server.startup.DisplayLogOnDeprecatedProjects; import org.sonar.server.startup.GeneratePluginIndex; import org.sonar.server.startup.JdbcDriverDeployer; import org.sonar.server.startup.LogServerId; @@ -65,7 +66,9 @@ public class PlatformLevelStartup extends PlatformLevel { RegisterIssueFilters.class, RenameIssueWidgets.class, ServerLifecycleNotifier.class, - PurgeCeActivities.class); + PurgeCeActivities.class, + DisplayLogOnDeprecatedProjects.class + ); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjects.java b/server/sonar-server/src/main/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjects.java new file mode 100644 index 00000000000..5987657a84e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjects.java @@ -0,0 +1,93 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.startup; + +import org.picocontainer.Startable; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.loadedtemplate.LoadedTemplateDto; + +/** + * Display a log for each project that hasn't been analysed since SonarQube 4.2 (column DEPRECATED_KEE is null) + * + * Should be removed after next LTS + * + * @since 5.2 + */ +public class DisplayLogOnDeprecatedProjects implements Startable { + + private static final Logger LOG = Loggers.get(DisplayLogOnDeprecatedProjects.class); + + private static final String TEMPLATE_KEY = "DisplayLogOnDeprecatedProjects"; + + private final DbClient dbClient; + + public DisplayLogOnDeprecatedProjects(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public void start() { + DbSession session = dbClient.openSession(false); + try { + if (!hasAlreadyBeenExecuted(session)) { + return; + } + displayLogOnDeprecatedProjectKeys(session); + registerTasks(session); + session.commit(); + } finally { + dbClient.closeSession(session); + } + } + + private void displayLogOnDeprecatedProjectKeys(DbSession session) { + boolean hasDetectDeprecatedProjects = false; + for (ComponentDto project : dbClient.componentDao().selectProjects(session)) { + if (project.deprecatedKey() == null) { + if (!hasDetectDeprecatedProjects) { + LOG.warn("We detected that the following projects have not been analysed on a SonarQube version greater than 4.2 (included):"); + hasDetectDeprecatedProjects = true; + } + LOG.warn(" - {}", project.getKey()); + } + } + if (hasDetectDeprecatedProjects) { + LOG.warn("As a consequence, some features of the Web UI will be broken for them, and any new analysis will consider all issues as new issues."); + } + } + + private boolean hasAlreadyBeenExecuted(DbSession session) { + return dbClient.loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.ONE_SHOT_TASK_TYPE, TEMPLATE_KEY, session) == 0; + } + + private void registerTasks(DbSession session) { + dbClient.loadedTemplateDao().insert(new LoadedTemplateDto(TEMPLATE_KEY, LoadedTemplateDto.ONE_SHOT_TASK_TYPE), session); + } + + @Override + public void stop() { + // Nothing to do + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjectsTest.java b/server/sonar-server/src/test/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjectsTest.java new file mode 100644 index 00000000000..51c936c50b9 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/startup/DisplayLogOnDeprecatedProjectsTest.java @@ -0,0 +1,98 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.startup; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.LogTester; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentTesting; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DisplayLogOnDeprecatedProjectsTest { + + static final String PROJECT_KEY = "PROJECT_KEY"; + static final String DEPRECATED_PROJECT_KEY = "DEPRECATED_PROJECT_KEY"; + + @Rule + public LogTester logTester = new LogTester(); + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + DbClient dbClient = dbTester.getDbClient(); + DbSession dbSession = dbTester.getSession(); + + DisplayLogOnDeprecatedProjects underTest = new DisplayLogOnDeprecatedProjects(dbClient); + + @Test + public void display_log_on_deprecated_project() throws Exception { + dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto() + .setKey(DEPRECATED_PROJECT_KEY) + .setDeprecatedKey(null)); + dbSession.commit(); + + underTest.start(); + + assertThat(logTester.logs()).containsOnly( + "We detected that the following projects have not been analysed on a SonarQube version greater than 4.2 (included):", + " - " + DEPRECATED_PROJECT_KEY, + "As a consequence, some features of the Web UI will be broken for them, and any new analysis will consider all issues as new issues."); + } + + @Test + public void not_display_log_when_task_already_executed() throws Exception { + dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto() + .setKey(DEPRECATED_PROJECT_KEY) + .setDeprecatedKey(null)); + dbSession.commit(); + + underTest.start(); + assertThat(logTester.logs()).isNotEmpty(); + logTester.clear(); + + underTest.start(); + assertThat(logTester.logs()).isEmpty(); + } + + @Test + public void nothing_do_when_no_deprecated_project() throws Exception { + dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto() + .setKey(PROJECT_KEY) + .setDeprecatedKey(PROJECT_KEY)); + dbSession.commit(); + + underTest.start(); + + assertThat(logTester.logs()).isEmpty(); + } + + @Test + public void nothing_do_when_no_project() throws Exception { + underTest.start(); + + assertThat(logTester.logs()).isEmpty(); + } +}