]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7803 creates events only on PROJECTS
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 21 Jun 2016 15:48:12 +0000 (17:48 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 27 Jun 2016 08:54:57 +0000 (10:54 +0200)
14 files changed:
it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java
it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-delete-version.html
it/it-tests/src/test/resources/projectAdministration/ProjectAdministrationTest/project-administration/multimodule-project-modify-version.html
server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepository.java
server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/add_version_event-result.xml
server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/keep_one_event_by_version.xml
server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/nothing_to_do_when_no_events_in_report.xml
server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistEventsStepTest/persist_report_events_with_component_children-result.xml
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb
sonar-core/src/main/resources/org/sonar/l10n/core.properties

index 5394a9f53465b02f97c2ad8ab03759045f4c25c9..a335d3ead4a7b21f5fd7eea1dd5dd1112e6a27fa 100644 (file)
@@ -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);
   }
 
   /**
index 891986cbc9fae7a067c3714702aef69fb0ee6d8d..a8894aca7ff71f6e47187e11e5d298f93d69b591 100644 (file)
@@ -76,7 +76,7 @@
   <tr>
     <td>waitForText</td>
     <td>infomsg</td>
-    <td>glob:*Version &quot;RELEASE&quot; was removed from current project and all its sub-projects*</td>
+    <td>glob:*Version &quot;RELEASE&quot; was removed from current project*</td>
   </tr>
   <tr>
     <td>assertNotText</td>
index a1d31f93d3a59612089ddbf3938a3d152c84d1a3..f3eae7f1cba4b3f89b0aa47543e938e95445474c 100644 (file)
@@ -91,7 +91,7 @@
   <tr>
     <td>waitForText</td>
     <td>infomsg</td>
-    <td>Version &quot;RELEASE&quot; was created for current project and all its sub-projects.</td>
+    <td>Version &quot;RELEASE&quot; was created for current project.</td>
   </tr>
   <tr>
     <td>waitForText</td>
index 5986d9a8bc23bb25bcbdcf06d5f0c091c9f68fd6..282c8bebda261a53c40b2566054967de49963f09 100644 (file)
@@ -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<Event> getEvents(Component component);
 }
index 42d9beae85b1bcae1044fe76108e9586c71d68f6..4f7ebba0c17d975dc20050f2c8e88ae3d7c9ae27 100644 (file)
@@ -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));
   }
 
index 0a2d3db39d000c1985b7bf2319f99ec249641e89..0a952701b9dfb1f3ee7466b7d183fd5fd09dfc40 100644 (file)
@@ -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<Event, EventDto> 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);
-    }
   }
 }
index e4df104d3c02bf8455f2909c4d1d3bce30566d31..2872cef0bfc598c0b6da6140debe853081106e01 100644 (file)
  */
 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();
   }
 }
index 0d11cc44bbfa68acca2b3fd8df19503c8603c7e0..89cd11586d83241283454d9305122a3b164d49df 100644 (file)
@@ -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();
 
index 458a04255860f6d633e9718eded6773662a3fde1..1b72b63d5e7097dd97004ba8c954a975214ca871 100644 (file)
@@ -1,6 +1,13 @@
 <dataset>
 
-  <events id="1" name="1.0" category="Version" description="[null]" event_data="[null]" event_date="150000000"
-          component_uuid="ABCD" snapshot_id="1000" created_at="1225630680000" />
+  <events id="1"
+          name="1.0"
+          category="Version"
+          description="[null]"
+          event_data="[null]"
+          event_date="150000000"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
 
 </dataset>
index 44c19d1b92198b491dec6dd0656c6c5a08f9bfb1..93bfc6001b85fb3e24c6a2c0b3fccbc761e52306 100644 (file)
@@ -1,10 +1,31 @@
 <dataset>
 
-  <events id="1" name="1.3-SNAPSHOT" category="Version" description="[null]" event_data="[null]" event_date="120000000"
-          component_uuid="ABCD" snapshot_id="1000" created_at="120000000" />
-  <events id="2" name="1.4" category="Version" description="[null]" event_data="[null]" event_date="130000000"
-          component_uuid="ABCD" snapshot_id="1000" created_at="130000000" />
-  <events id="3" name="1.5-SNAPSHOT" category="Version" description="[null]" event_data="[null]" event_date="140000000"
-          component_uuid="ABCD" snapshot_id="1000" created_at="140000000" />
+  <events id="1"
+          name="1.3-SNAPSHOT"
+          category="Version"
+          description="[null]"
+          event_data="[null]"
+          event_date="120000000"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="120000000"/>
+  <events id="2"
+          name="1.4"
+          category="Version"
+          description="[null]"
+          event_data="[null]"
+          event_date="130000000"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="130000000"/>
+  <events id="3"
+          name="1.5-SNAPSHOT"
+          category="Version"
+          description="[null]"
+          event_data="[null]"
+          event_date="140000000"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="140000000"/>
 
 </dataset>
index 0002deec3c29e1b1459e751dbf306f1d2f2d70ff..0d9badce82dc5639dd21d3914b691ea92947c396 100644 (file)
@@ -1,13 +1,41 @@
 <dataset>
 
-  <events id="1" name="1.0" category="Version" description="Version 1.0" event_data="some data" event_date="1413407091086"
-          component_uuid="ABCD" snapshot_id="1000" created_at="1225630680000" />
-  <events id="2" name="Red (was Orange)" category="Alert" description="Critical issues variation > 0 since previous version (1.0 - 2015 Feb 09), Open issues > 0"
-          event_data="[null]" event_date="1413407091086" component_uuid="ABCD" snapshot_id="1000" created_at="1225630680000" />
-  <events id="3" name="Changes in 'Default' (Java)" category="Profile" description="Version 1.0" event_data="from=2014-10-12T08:36:25+0000;key=java-default;to=2014-10-12T10:36:25+0000"
-          event_date="1413407091086" component_uuid="ABCD" snapshot_id="1000" created_at="1225630680000" />
+  <events id="1"
+          name="1.0"
+          category="Version"
+          description="Version 1.0"
+          event_data="some data"
+          event_date="1413407091086"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
+  <events id="2"
+          name="Red (was Orange)"
+          category="Alert"
+          description="Critical issues variation > 0 since previous version (1.0 - 2015 Feb 09), Open issues > 0"
+          event_data="[null]"
+          event_date="1413407091086"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
+  <events id="3"
+          name="Changes in 'Default' (Java)"
+          category="Profile"
+          description="Version 1.0"
+          event_data="from=2014-10-12T08:36:25+0000;key=java-default;to=2014-10-12T10:36:25+0000"
+          event_date="1413407091086"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
 
-  <events id="4" name="1.0" category="Version" description="Version 1.0" event_data="some data" event_date="1413407091086"
-          component_uuid="BCDE" snapshot_id="1000" created_at="1225630680000" />
+  <events id="4"
+          name="1.0"
+          category="Version"
+          description="Version 1.0"
+          event_data="some data"
+          event_date="1413407091086"
+          component_uuid="BCDE"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
 
 </dataset>
index 1b186afb32a423c232187c2339ece1b4527f870a..9eba6a21ee8c375de1fa4c7391c420a2a1f1691d 100644 (file)
@@ -1,10 +1,13 @@
 <dataset>
 
-  <events id="1" name="Red (was Orange)" category="Alert" description="Open issues > 0"
+  <events id="1"
+          name="Red (was Orange)"
+          category="Alert"
+          description="Open issues > 0"
           event_data="[null]"
-          event_date="150000000" component_uuid="ABCD" snapshot_id="1000" created_at="1225630680000" />
-  <events id="2" name="Red (was Orange)" category="Alert" description="Open issues > 0"
-          event_data="[null]"
-          event_date="150000000" component_uuid="BCDE" snapshot_id="1001" created_at="1225630680000" />
+          event_date="150000000"
+          component_uuid="ABCD"
+          snapshot_id="1000"
+          created_at="1225630680000"/>
 
 </dataset>
index 62de84287c3bb01e74ed68ad1a8a484490b1d890..5034d1fd93832f809f89b5a61ffce93018a62772 100644 (file)
@@ -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
index d98bb24d3cfa61e654e45020ff52185df3c9ad04..2f7820ffcf4da9a8bcff2ceac2aceeaacee8d388 100644 (file)
@@ -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