From fba02ff24c49e30a36ea7da8b4558fb622a379d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 21 Jun 2016 17:48:12 +0200 Subject: [PATCH] SONAR-7803 creates events only on PROJECTS --- .../ProjectAdministrationTest.java | 4 +- .../multimodule-project-delete-version.html | 2 +- .../multimodule-project-modify-version.html | 2 +- .../computation/event/EventRepository.java | 9 +++ .../event/EventRepositoryImpl.java | 2 + .../computation/step/PersistEventsStep.java | 24 +----- .../event/EventRepositoryImplTest.java | 26 ++++++- .../step/PersistEventsStepTest.java | 78 ++++++++++++++----- .../add_version_event-result.xml | 11 ++- .../keep_one_event_by_version.xml | 33 ++++++-- ...nothing_to_do_when_no_events_in_report.xml | 44 +++++++++-- ..._events_with_component_children-result.xml | 13 ++-- .../app/controllers/project_controller.rb | 25 +++--- .../resources/org/sonar/l10n/core.properties | 6 +- 14 files changed, 196 insertions(+), 83 deletions(-) diff --git a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java index 5394a9f5346..a335d3ead4a 100644 --- a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java +++ b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java @@ -148,7 +148,7 @@ public class ProjectAdministrationTest { ).build(); new SeleneseTest(selenese).runOn(orchestrator); - assertThat(count("events where category='Version'")).as("Different number of events").isEqualTo(14); + assertThat(count("events where category='Version'")).as("Different number of events").isEqualTo(2); selenese = Selenese.builder() .setHtmlTestsInClasspath("delete_version_of_multimodule_project", @@ -156,7 +156,7 @@ public class ProjectAdministrationTest { ).build(); new SeleneseTest(selenese).runOn(orchestrator); - assertThat(count("events where category='Version'")).as("Different number of events").isEqualTo(7); + assertThat(count("events where category='Version'")).as("Different number of events").isEqualTo(1); } /** diff --git a/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-delete-version.html b/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-delete-version.html index 891986cbc9f..a8894aca7ff 100644 --- a/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-delete-version.html +++ b/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-delete-version.html @@ -76,7 +76,7 @@ waitForText infomsg - glob:*Version "RELEASE" was removed from current project and all its sub-projects* + glob:*Version "RELEASE" was removed from current project* assertNotText diff --git a/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-modify-version.html b/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-modify-version.html index a1d31f93d3a..f3eae7f1cba 100644 --- a/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-modify-version.html +++ b/it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-modify-version.html @@ -91,7 +91,7 @@ waitForText infomsg - Version "RELEASE" was created for current project and all its sub-projects. + Version "RELEASE" was created for current project. waitForText diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepository.java b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepository.java index 5986d9a8bc2..282c8bebda2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepository.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepository.java @@ -21,8 +21,17 @@ package org.sonar.server.computation.event; import org.sonar.server.computation.component.Component; +import static org.sonar.server.computation.component.Component.Type; + public interface EventRepository { + /** + * @throws NullPointerException if {@code component} or {@code event} is {@code null} + * @throws IllegalArgumentException if type of {@code component} is not {@link Type#PROJECT} + */ void add(Component component, Event event); + /** + * @throws NullPointerException if {@code component} is {@code null} + */ Iterable getEvents(Component component); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java index 42d9beae85b..4f7ebba0c17 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java @@ -23,6 +23,7 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import org.sonar.server.computation.component.Component; +import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; public class EventRepositoryImpl implements EventRepository { @@ -30,6 +31,7 @@ public class EventRepositoryImpl implements EventRepository { @Override public void add(Component component, Event event) { + checkArgument(component.getType() == Component.Type.PROJECT, "Component must be of type PROJECT"); events.put(component.getReportAttributes().getRef(), requireNonNull(event)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java index 0a2d3db39d0..0a952701b9d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java @@ -70,11 +70,6 @@ public class PersistEventsStep implements ComputationStep { } } - private void processComponent(Component component, DbSession session, long analysisDate) { - processEvents(session, component, analysisDate); - saveVersionEvent(session, component, analysisDate); - } - private void processEvents(DbSession session, Component component, Long analysisDate) { Function eventToEventDto = event -> newBaseEvent(component, analysisDate) .setName(event.getName()) @@ -134,29 +129,16 @@ public class PersistEventsStep implements ComputationStep { private final long analysisDate; public PersistEventComponentVisitor(DbSession session, long analysisDate) { - super(CrawlerDepthLimit.FILE, ComponentVisitor.Order.PRE_ORDER); + super(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER); this.session = session; this.analysisDate = analysisDate; } @Override public void visitProject(Component project) { - processComponent(project, session, analysisDate); - } - - @Override - public void visitModule(Component module) { - processComponent(module, session, analysisDate); + processEvents(session, project, analysisDate); + saveVersionEvent(session, project, analysisDate); } - @Override - public void visitDirectory(Component directory) { - processComponent(directory, session, analysisDate); - } - - @Override - public void visitFile(Component file) { - processComponent(file, session, analysisDate); - } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java index e4df104d3c0..2872cef0bfc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java @@ -19,11 +19,14 @@ */ package org.sonar.server.computation.event; +import java.util.Arrays; import org.junit.Test; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.ReportComponent; +import org.sonar.server.computation.component.ViewsComponent; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; public class EventRepositoryImplTest { private static final Component COMPONENT_1 = newComponent(1); @@ -57,6 +60,27 @@ public class EventRepositoryImplTest { underTest.add(COMPONENT_1, null); } + @Test + public void add_throws_IAE_for_any_component_type_but_PROJECT() { + Arrays.stream(Component.Type.values()) + .filter(type -> type != Component.Type.PROJECT) + .map(type -> { + if (type.isReportType()) { + return ReportComponent.builder(type, 1).build(); + } else { + return ViewsComponent.builder(type, 1).build(); + } + }) + .forEach(component -> { + try { + underTest.add(component, EVENT_1); + fail("should have raised an IAE"); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Component must be of type PROJECT"); + } + }); + } + @Test public void can_add_and_retrieve_many_events_per_component() { underTest.add(COMPONENT_1, EVENT_1); @@ -66,6 +90,6 @@ public class EventRepositoryImplTest { } private static Component newComponent(int i) { - return ReportComponent.builder(Component.Type.FILE, i).build(); + return ReportComponent.builder(Component.Type.PROJECT, i).build(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java index 0d11cc44bbf..89cd11586d8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java @@ -38,18 +38,36 @@ import org.sonar.server.computation.event.EventRepository; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - +import static org.sonar.server.computation.component.Component.Type.DIRECTORY; +import static org.sonar.server.computation.component.Component.Type.FILE; +import static org.sonar.server.computation.component.Component.Type.MODULE; +import static org.sonar.server.computation.component.Component.Type.PROJECT; +import static org.sonar.server.computation.component.ReportComponent.builder; public class PersistEventsStepTest extends BaseStepTest { + private static final ReportComponent ROOT = builder(PROJECT, 1) + .setUuid("ABCD") + .addChildren( + builder(MODULE, 2) + .setUuid("BCDE") + .addChildren( + builder(DIRECTORY, 3) + .setUuid("Q") + .addChildren( + builder(FILE, 4) + .setUuid("Z") + .build()) + .build()) + .build()) + .build(); + System2 system2 = mock(System2.class); @Rule public DbTester dbTester = DbTester.create(system2); - @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); - @Rule public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); @@ -77,7 +95,7 @@ public class PersistEventsStepTest extends BaseStepTest { public void nothing_to_do_when_no_events_in_report() { dbTester.prepareDbUnit(getClass(), "nothing_to_do_when_no_events_in_report.xml"); - treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build()); + treeRootHolder.setRoot(ROOT); step.execute(); @@ -88,19 +106,13 @@ public class PersistEventsStepTest extends BaseStepTest { public void persist_report_events_with_component_children() { dbTester.prepareDbUnit(getClass(), "empty.xml"); - ReportComponent module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").build(); - ReportComponent root = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(module).build(); - treeRootHolder.setRoot(root); - - dbIdsRepository.setSnapshotId(root, 1000L); - dbIdsRepository.setSnapshotId(module, 1001L); + treeRootHolder.setRoot(ROOT); - Component child = root.getChildren().get(0); + dbIdsRepository.setSnapshotId(ROOT, 1000L); - when(eventRepository.getEvents(root)).thenReturn(ImmutableList.of(Event.createAlert("Red (was Orange)", null, "Open issues > 0"))); - when(eventRepository.getEvents(child)).thenReturn(ImmutableList.of(Event.createAlert("Red (was Orange)", null, "Open issues > 0"))); + when(eventRepository.getEvents(ROOT)).thenReturn(ImmutableList.of(Event.createAlert("Red (was Orange)", null, "Open issues > 0"))); - treeRootHolder.setRoot(root); + treeRootHolder.setRoot(ROOT); step.execute(); dbTester.assertDbUnit(getClass(), "persist_report_events_with_component_children-result.xml", "events"); @@ -110,9 +122,24 @@ public class PersistEventsStepTest extends BaseStepTest { public void create_version_event() { dbTester.prepareDbUnit(getClass(), "empty.xml"); - Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setVersion("1.0").build(); + Component project = builder(PROJECT, 1) + .setUuid("ABCD") + .setVersion("1.0") + .addChildren( + builder(MODULE, 2) + .setUuid("BCDE") + .addChildren( + builder(DIRECTORY, 3) + .setUuid("Q") + .addChildren( + builder(FILE, 4) + .setUuid("Z") + .build()) + .build()) + .build()) + .build(); treeRootHolder.setRoot(project); - dbIdsRepository.setSnapshotId(project, 1000L); + dbIdsRepository.setSnapshotId(ROOT, 1000L); step.execute(); @@ -123,9 +150,24 @@ public class PersistEventsStepTest extends BaseStepTest { public void keep_one_event_by_version() { dbTester.prepareDbUnit(getClass(), "keep_one_event_by_version.xml"); - Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setVersion("1.5-SNAPSHOT").build(); + Component project = builder(PROJECT, 1) + .setUuid("ABCD") + .setVersion("1.5-SNAPSHOT") + .addChildren( + builder(MODULE, 2) + .setUuid("BCDE") + .addChildren( + builder(DIRECTORY, 3) + .setUuid("Q") + .addChildren( + builder(FILE, 4) + .setUuid("Z") + .build()) + .build()) + .build()) + .build(); treeRootHolder.setRoot(project); - dbIdsRepository.setSnapshotId(project, 1001L); + dbIdsRepository.setSnapshotId(ROOT, 1001L); step.execute(); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/add_version_event-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/add_version_event-result.xml index 458a0425586..1b72b63d5e7 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/add_version_event-result.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/add_version_event-result.xml @@ -1,6 +1,13 @@ - + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/keep_one_event_by_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/keep_one_event_by_version.xml index 44c19d1b921..93bfc6001b8 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/keep_one_event_by_version.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/keep_one_event_by_version.xml @@ -1,10 +1,31 @@ - - - + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/nothing_to_do_when_no_events_in_report.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/nothing_to_do_when_no_events_in_report.xml index 0002deec3c2..0d9badce82d 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/nothing_to_do_when_no_events_in_report.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/nothing_to_do_when_no_events_in_report.xml @@ -1,13 +1,41 @@ - - - + + + - + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/persist_report_events_with_component_children-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/persist_report_events_with_component_children-result.xml index 1b186afb32a..9eba6a21ee8 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/persist_report_events_with_component_children-result.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/persist_report_events_with_component_children-result.xml @@ -1,10 +1,13 @@ - - + event_date="150000000" + component_uuid="ABCD" + snapshot_id="1000" + created_at="1225630680000"/> diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb index 62de84287c3..5034d1fd938 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb @@ -294,25 +294,20 @@ class ProjectController < ApplicationController if Event.already_exists(snapshot.id, params[:version_name], EventCategory::KEY_VERSION) flash[:error] = message('project_history.version_already_exists', :params => h(params[:version_name])) else - snapshots = find_project_snapshots(snapshot.id) - # We update all the related snapshots to have a version attribute in sync with the new name - snapshots.each do |snapshot| - snapshot.version = params[:version_name] - snapshot.save! - end - # And then we update/create the event on each snapshot + # We update the snapshot to have a version attribute in sync with the new name + snapshot.version = params[:version_name] + snapshot.save! + # And then we update/create the event on the snapshot if snapshot.event(EventCategory::KEY_VERSION) - # This is an update: we update all the related events + # This is an update: we update the event Event.update_all({:name => params[:version_name]}, - ["category = ? AND snapshot_id IN (?)", EventCategory::KEY_VERSION, snapshots.map { |s| s.id }]) + ["category = ? AND snapshot_id = ?", EventCategory::KEY_VERSION, snapshot.id]) flash[:notice] = message('project_history.version_updated', :params => h(params[:version_name])) else - # We create an event for every concerned snapshot - snapshots.each do |snapshot| - event = Event.create!(:name => params[:version_name], :snapshot => snapshot, - :component_uuid => snapshot.project.uuid, :category => EventCategory::KEY_VERSION, - :event_date => snapshot.created_at) - end + # We create an event on the snapshot + event = Event.create!(:name => params[:version_name], :snapshot => snapshot, + :component_uuid => snapshot.project.uuid, :category => EventCategory::KEY_VERSION, + :event_date => snapshot.created_at) flash[:notice] = message('project_history.version_created', :params => h(params[:version_name])) end end diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index d98bb24d3cf..2f7820ffcf4 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -1568,9 +1568,9 @@ project_history.rename_version=Rename project_history.create_version=Create project_history.remove_version=Remove project_history.do_you_want_to_remove_version=Are you sure you want to remove "{0}" from this snapshot? -project_history.version_updated=Version was updated to "{0}" for current project and all its sub-projects. -project_history.version_created=Version "{0}" was created for current project and all its sub-projects. -project_history.version_removed=Version "{0}" was removed from current project and all its sub-projects. +project_history.version_updated=Version was updated to "{0}" for current project. +project_history.version_created=Version "{0}" was created for current project. +project_history.version_removed=Version "{0}" was removed from current project. project_history.version_already_exists=Version "{0}" already exists. project_history.rename_event=Rename project_history.create_event=Create -- 2.39.5