From f4a9a7e8a3f2ab78d7b226f47eb5c88b9140417d Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 24 May 2012 18:42:13 +0200 Subject: [PATCH] SONAR-3308 Fail batch if trying to delete released version => Message given to the user: "A Sonar analysis can't delete a released version that already exists in the project history (version XXX). Please change the version of the project or clean its history first." --- .../core/sensors/VersionEventsSensor.java | 19 ++++++++---- .../core/sensors/VersionEventsSensorTest.java | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java index b1fab180258..39bb7264db3 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/VersionEventsSensor.java @@ -19,8 +19,6 @@ */ package org.sonar.plugins.core.sensors; -import java.util.Iterator; - import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.Event; import org.sonar.api.batch.Sensor; @@ -28,6 +26,8 @@ import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; import org.sonar.core.NotDryRun; +import java.util.Iterator; + @NotDryRun public class VersionEventsSensor implements Sensor { @@ -47,12 +47,19 @@ public class VersionEventsSensor implements Sensor { private void deleteDeprecatedEvents(Project project, SensorContext context) { String version = project.getAnalysisVersion(); - String snapshotVersionToDelete = (version.endsWith(SNAPSHOT_SUFFIX) ? "" : version + SNAPSHOT_SUFFIX); + boolean isReleaseVersion = !version.endsWith(SNAPSHOT_SUFFIX); + String snapshotVersionToDelete = isReleaseVersion ? version + SNAPSHOT_SUFFIX : ""; for (Iterator it = context.getEvents(project).iterator(); it.hasNext();) { Event event = it.next(); - if (event.isVersionCategory() && (version.equals(event.getName()) || snapshotVersionToDelete.equals(event.getName()))) { - it.remove(); - context.deleteEvent(event); + if (event.isVersionCategory()) { + if (snapshotVersionToDelete.equals(event.getName()) || (version.equals(event.getName()) && !isReleaseVersion)) { + it.remove(); + context.deleteEvent(event); + } else if (version.equals(event.getName()) && isReleaseVersion) { + // we try to delete a released version that already exists in the project history => this shouldn't happen + throw new IllegalStateException("A Sonar analysis can't delete a released version that already exists in the project history (version " + + version + "). Please change the version of the project or clean its history first."); + } } } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java index 65af02fcab2..b92dbf587cf 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/VersionEventsSensorTest.java @@ -20,7 +20,9 @@ package org.sonar.plugins.core.sensors; import com.google.common.collect.Lists; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.batch.Event; import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; @@ -39,6 +41,9 @@ import static org.mockito.Mockito.when; public class VersionEventsSensorTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void shouldDoNothingIfNoVersion() { VersionEventsSensor sensor = new VersionEventsSensor(); @@ -102,6 +107,31 @@ public class VersionEventsSensorTest { verify(context).createEvent(eq(project), eq("1.5"), (String) isNull(), eq(Event.CATEGORY_VERSION), (Date) isNull()); } + @Test + public void shouldFailIfTryingToDeleteReleasedVersion() { + // Given + Event snapshotEvent1 = mockVersionEvent("1.5-SNAPSHOT"); + Event releaseEvent2 = mockVersionEvent("1.4"); + Event snapshotEvent2 = mockVersionEvent("1.3-SNAPSHOT"); + + VersionEventsSensor sensor = new VersionEventsSensor(); + SensorContext context = mock(SensorContext.class); + + Project project = mock(Project.class); + when(project.getAnalysisVersion()).thenReturn("1.4"); + + when(context.getEvents(project)).thenReturn(Lists.newArrayList(snapshotEvent1, releaseEvent2, snapshotEvent2)); + + // Expect + thrown.expect(IllegalStateException.class); + thrown.expectMessage("A Sonar analysis can't delete a released version that already exists in the project history (version 1.4). " + + "Please change the version of the project or clean its history first."); + + // When + sensor.analyse(project, context); + + } + private Event mockVersionEvent(String version) { Event event = mock(Event.class); when(event.isVersionCategory()).thenReturn(true); -- 2.39.5