*/
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;
import org.sonar.api.resources.Project;
import org.sonar.core.NotDryRun;
+import java.util.Iterator;
+
@NotDryRun
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<Event> 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.");
+ }
}
}
}
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;
public class VersionEventsSensorTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void shouldDoNothingIfNoVersion() {
VersionEventsSensor sensor = new VersionEventsSensor();
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);