]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5473 Fix fetching of remote SCM data
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 25 Sep 2014 14:23:37 +0000 (16:23 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 2 Oct 2014 15:52:23 +0000 (17:52 +0200)
30 files changed:
plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitBlameCommand.java
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java
pom.xml
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/not_returned_secured_settings_with_only_preview_permission.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_active_rules.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_project_settings.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_project_with_module_settings.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_project_with_module_settings_inherited_from_project.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_project_with_module_with_sub_module.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_project_with_two_modules.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_provisioned_project_profile.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profile_from_default_profile.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profile_from_given_profile_name.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_sub_module_settings.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_sub_module_settings_including_settings_from_parent_modules.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_sub_module_settings_inherited_from_project.json
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_sub_module_settings_inherited_from_project_and_module.json
sonar-application/pom.xml
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java
sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetection.java
sonar-batch/src/main/java/org/sonar/batch/scm/ScmActivitySensor.java
sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java

index 0d8b447cdd8b19fa665d7df40c0442c44e6cac96..a8c56b7ab9c208bf54c69a6f095a3ec6247b1869 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.plugins.scm.git;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.InstantiationStrategy;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.scm.BlameCommand;
@@ -30,7 +32,8 @@ import org.sonar.api.utils.command.StreamConsumer;
 
 import java.io.File;
 
-public class GitBlameCommand implements BlameCommand {
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
+public class GitBlameCommand implements BlameCommand, BatchComponent {
 
   private static final Logger LOG = LoggerFactory.getLogger(GitBlameCommand.class);
   private final CommandExecutor commandExecutor;
index 7d492cc315bc3bd24bbe23c65e59b320cc9565b8..4e74361378862a72af73214cdc4b8568376998d3 100644 (file)
@@ -23,6 +23,8 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.InstantiationStrategy;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.scm.BlameCommand;
@@ -35,7 +37,8 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
-public class XooBlameCommand implements BlameCommand {
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
+public class XooBlameCommand implements BlameCommand, BatchComponent {
 
   private static final String SCM_EXTENSION = ".scm";
 
diff --git a/pom.xml b/pom.xml
index efd7f37738625655e2950ccbcacee04b19a5f02d..4217be58420d4879852e9a885738f88820337c05 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <version>${project.version}</version>
         <scope>test</scope>
       </dependency>
+      <dependency>
+        <groupId>org.codehaus.sonar.plugins</groupId>
+        <artifactId>sonar-git-plugin</artifactId>
+        <version>${project.version}</version>
+        <type>sonar-plugin</type>
+      </dependency>
       <dependency>
         <groupId>org.codehaus.sonar</groupId>
         <artifactId>sonar-squid</artifactId>
index 495631ea4dca2328a48574c2f1c0faf8b4cf7d02..77451cd99224c513fbf23849508ba512d3ee0e07 100644 (file)
       <type>sonar-plugin</type>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.sonar.plugins</groupId>
+      <artifactId>sonar-git-plugin</artifactId>
+      <type>sonar-plugin</type>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>org.sonatype.jsw-binaries</groupId>
       <artifactId>jsw-binaries</artifactId>
index 5e2bfafd59d67863020f26ac274184358116d6b2..d967e933de47af796a55763101b53a2762c0f7d8 100644 (file)
@@ -39,7 +39,7 @@ public class ProjectReferentials {
   private Map<String, QProfile> qprofilesByLanguage = new HashMap<String, QProfile>();
   private Collection<ActiveRule> activeRules = new ArrayList<ActiveRule>();
   private Map<String, Map<String, String>> settingsByModule = new HashMap<String, Map<String, String>>();
-  private Map<String, FileData> fileDataPerPath = new HashMap<String, FileData>();
+  private Map<String, Map<String, FileData>> fileDataByModuleAndPath = new HashMap<String, Map<String, FileData>>();
 
   public Map<String, String> settings(String projectKey) {
     return settingsByModule.containsKey(projectKey) ? settingsByModule.get(projectKey) : Collections.<String, String>emptyMap();
@@ -49,9 +49,9 @@ public class ProjectReferentials {
     Map<String, String> existingSettings = settingsByModule.get(projectKey);
     if (existingSettings == null) {
       existingSettings = new HashMap<String, String>();
+      settingsByModule.put(projectKey, existingSettings);
     }
     existingSettings.putAll(settings);
-    settingsByModule.put(projectKey, existingSettings);
     return this;
   }
 
@@ -73,13 +73,23 @@ public class ProjectReferentials {
     return this;
   }
 
-  public Map<String, FileData> fileDataPerPath() {
-    return fileDataPerPath;
+  public Map<String, FileData> fileDataByPath(String projectKey) {
+    return fileDataByModuleAndPath.containsKey(projectKey) ? fileDataByModuleAndPath.get(projectKey) : Collections.<String, FileData>emptyMap();
+  }
+
+  public ProjectReferentials addFileData(String projectKey, String path, FileData fileData) {
+    Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(projectKey);
+    if (existingFileDataByPath == null) {
+      existingFileDataByPath = new HashMap<String, FileData>();
+      fileDataByModuleAndPath.put(projectKey, existingFileDataByPath);
+    }
+    existingFileDataByPath.put(path, fileData);
+    return this;
   }
 
   @CheckForNull
-  public FileData fileDataPerPath(String path) {
-    return fileDataPerPath.get(path);
+  public FileData fileData(String projectKey, String path) {
+    return fileDataByPath(projectKey).get(path);
   }
 
   public long timestamp() {
index ed98c51e46b40cdffaa2922a7086d2b62a2675f5..5e537a1f523a8d807e0a32052ee61ab9bd7f5942 100644 (file)
@@ -49,7 +49,7 @@ public class ProjectReferentialsTest {
     activeRule.addParam("param1", "value1");
     ref.addActiveRule(activeRule);
     ref.setTimestamp(10);
-    ref.fileDataPerPath().put("src/main/java/Foo.java", new FileData("xyz", "1=12345,2=3456", "1=345,2=345", "1=henryju,2=gaudin"));
+    ref.addFileData("foo", "src/main/java/Foo.java", new FileData("xyz", "1=12345,2=3456", "1=345,2=345", "1=henryju,2=gaudin"));
 
     System.out.println(ref.toJson());
     JSONAssert
@@ -58,7 +58,7 @@ public class ProjectReferentialsTest {
           + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
           + "activeRules:[{repositoryKey:repo,ruleKey:rule,name:Rule,severity:MAJOR,internalKey:rule,language:java,params:{param1:value1}}],"
           + "settingsByModule:{foo:{prop1:value1,prop2:value2,prop:value}},"
-          + "fileDataPerPath:{\"src/main/java/Foo.java\":{hash:xyz,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}}",
+          + "fileDataByModuleAndPath:{foo:{\"src/main/java/Foo.java\":{hash:xyz,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}}}",
         ref.toJson(), true);
   }
 
@@ -69,7 +69,7 @@ public class ProjectReferentialsTest {
         + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
         + "activeRules:[{repositoryKey:repo,ruleKey:rule,name:Rule,severity:MAJOR,internalKey:rule1,language:java,params:{param1:value1}}],"
         + "settingsByModule:{foo:{prop:value}},"
-        + "fileDataPerPath:{\"src/main/java/Foo.java\":{hash:xyz,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}}");
+        + "fileDataByModuleAndPath:{foo:{\"src/main/java/Foo.java\":{hash:xyz,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}}}");
 
     assertThat(ref.timestamp()).isEqualTo(1);
 
@@ -88,9 +88,9 @@ public class ProjectReferentialsTest {
     assertThat(qProfile.rulesUpdatedAt()).isEqualTo(new SimpleDateFormat("dd/MM/yyyy").parse("14/03/1984"));
     assertThat(ref.settings("foo")).includes(MapAssert.entry("prop", "value"));
 
-    assertThat(ref.fileDataPerPath("src/main/java/Foo.java").hash()).isEqualTo("xyz");
-    assertThat(ref.fileDataPerPath("src/main/java/Foo.java").scmAuthorsByLine()).isEqualTo("1=henryju,2=gaudin");
-    assertThat(ref.fileDataPerPath("src/main/java/Foo.java").scmLastCommitDatetimesByLine()).isEqualTo("1=12345,2=3456");
-    assertThat(ref.fileDataPerPath("src/main/java/Foo.java").scmRevisionsByLine()).isEqualTo("1=345,2=345");
+    assertThat(ref.fileData("foo", "src/main/java/Foo.java").hash()).isEqualTo("xyz");
+    assertThat(ref.fileData("foo", "src/main/java/Foo.java").scmAuthorsByLine()).isEqualTo("1=henryju,2=gaudin");
+    assertThat(ref.fileData("foo", "src/main/java/Foo.java").scmLastCommitDatetimesByLine()).isEqualTo("1=12345,2=3456");
+    assertThat(ref.fileData("foo", "src/main/java/Foo.java").scmRevisionsByLine()).isEqualTo("1=345,2=345");
   }
 }
index 40df91b4ab486ff144f2964a047a37eab2b17a1b..565417efad0b56885319a45708b963a04153899a 100644 (file)
@@ -25,7 +25,6 @@ import org.sonar.api.batch.TimeMachine;
 import org.sonar.api.batch.TimeMachineQuery;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.MeasureModel;
-import org.sonar.api.database.model.ResourceModel;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.Metric;
@@ -39,7 +38,6 @@ import org.sonar.batch.index.DefaultIndex;
 import javax.annotation.Nullable;
 import javax.persistence.Query;
 
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -150,41 +148,6 @@ public class DefaultTimeMachine implements TimeMachine {
     return jpaQuery.getResultList();
   }
 
-  // Temporary implementation for SONAR-5473
-  public List<MeasureModel> query(String resourceKey, Integer... metricIds) {
-    StringBuilder sb = new StringBuilder();
-    Map<String, Object> params = Maps.newHashMap();
-
-    sb.append("SELECT m");
-    sb.append(" FROM ")
-      .append(MeasureModel.class.getSimpleName())
-      .append(" m, ")
-      .append(ResourceModel.class.getSimpleName())
-      .append(" r, ")
-      .append(Snapshot.class.getSimpleName())
-      .append(" s WHERE m.snapshotId=s.id AND s.resourceId=r.id AND r.kee=:kee AND s.status=:status AND s.qualifier<>:lib");
-    params.put("kee", resourceKey);
-    params.put("status", Snapshot.STATUS_PROCESSED);
-    params.put("lib", Qualifiers.LIBRARY);
-
-    sb.append(" AND m.characteristicId IS NULL");
-    sb.append(" AND m.personId IS NULL");
-    sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL");
-    if (metricIds.length > 0) {
-      sb.append(" AND m.metricId IN (:metricIds) ");
-      params.put("metricIds", Arrays.asList(metricIds));
-    }
-    sb.append(" AND s.last=true ");
-    sb.append(" ORDER BY s.createdAt ");
-
-    Query jpaQuery = session.createQuery(sb.toString());
-
-    for (Map.Entry<String, Object> entry : params.entrySet()) {
-      jpaQuery.setParameter(entry.getKey(), entry.getValue());
-    }
-    return jpaQuery.getResultList();
-  }
-
   public Map<Integer, Metric> getMetricsById(TimeMachineQuery query) {
     Collection<Metric> metrics = metricFinder.findAll(query.getMetricKeys());
     Map<Integer, Metric> result = Maps.newHashMap();
index 98c46ef117254004277affe0af627ea186fc0a60..d3a85a912a962374f705bdc58dc1782a0b74ab4b 100644 (file)
 package org.sonar.batch.referential;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.MeasureModel;
+import org.sonar.api.database.model.ResourceModel;
+import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.batch.DefaultTimeMachine;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.bootstrap.TaskProperties;
 import org.sonar.batch.protocol.input.FileData;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.batch.rule.ModuleQProfiles;
-import org.sonar.batch.scan.filesystem.PreviousFileHashLoader;
+import org.sonar.core.source.SnapshotDataTypes;
+import org.sonar.core.source.db.SnapshotDataDao;
+import org.sonar.core.source.db.SnapshotDataDto;
+
+import javax.persistence.Query;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -50,17 +61,15 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad
 
   private final ServerClient serverClient;
   private final AnalysisMode analysisMode;
-  private final PreviousFileHashLoader fileHashLoader;
-  private final MetricFinder metricFinder;
-  private final DefaultTimeMachine defaultTimeMachine;
+  private final SnapshotDataDao dao;
+  private final DatabaseSession session;
 
-  public DefaultProjectReferentialsLoader(ServerClient serverClient, AnalysisMode analysisMode, PreviousFileHashLoader fileHashLoader, MetricFinder finder,
-    DefaultTimeMachine defaultTimeMachine) {
+  public DefaultProjectReferentialsLoader(DatabaseSession session, ServerClient serverClient, AnalysisMode analysisMode,
+    SnapshotDataDao dao) {
+    this.session = session;
     this.serverClient = serverClient;
     this.analysisMode = analysisMode;
-    this.fileHashLoader = fileHashLoader;
-    this.metricFinder = finder;
-    this.defaultTimeMachine = defaultTimeMachine;
+    this.dao = dao;
   }
 
   @Override
@@ -77,28 +86,78 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad
     url += "&preview=" + analysisMode.isPreview();
     ProjectReferentials ref = ProjectReferentials.fromJson(serverClient.request(url));
 
-    Integer lastCommitsId = metricFinder.findByKey(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE.key()).getId();
-    Integer revisionsId = metricFinder.findByKey(CoreMetrics.SCM_REVISIONS_BY_LINE.key()).getId();
-    Integer authorsId = metricFinder.findByKey(CoreMetrics.SCM_AUTHORS_BY_LINE.key()).getId();
-    for (Map.Entry<String, String> hashByPaths : fileHashLoader.hashByRelativePath().entrySet()) {
-      String path = hashByPaths.getKey();
-      String hash = hashByPaths.getValue();
-      String lastCommits = null;
-      String revisions = null;
-      String authors = null;
-      List<MeasureModel> measures = defaultTimeMachine.query(projectKey + ":" + path, lastCommitsId, revisionsId, authorsId);
-      for (MeasureModel m : measures) {
-        if (m.getMetricId() == lastCommitsId) {
-          lastCommits = m.getData(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE);
-        } else if (m.getMetricId() == revisionsId) {
-          revisions = m.getData(CoreMetrics.SCM_REVISIONS_BY_LINE);
-        }
-        if (m.getMetricId() == authorsId) {
-          authors = m.getData(CoreMetrics.SCM_AUTHORS_BY_LINE);
+    for (ProjectDefinition module : reactor.getProjects()) {
+
+      for (Map.Entry<String, String> hashByPaths : hashByRelativePath(module.getKeyWithBranch()).entrySet()) {
+        String path = hashByPaths.getKey();
+        String hash = hashByPaths.getValue();
+        String lastCommits = null;
+        String revisions = null;
+        String authors = null;
+        List<Object[]> measuresByKey = query(projectKey + ":" + path, CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, CoreMetrics.SCM_REVISIONS_BY_LINE_KEY,
+          CoreMetrics.SCM_AUTHORS_BY_LINE_KEY);
+        for (Object[] measureByKey : measuresByKey) {
+          if (measureByKey[0].equals(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY)) {
+            lastCommits = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE);
+          } else if (measureByKey[0].equals(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY)) {
+            revisions = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_REVISIONS_BY_LINE);
+          } else if (measureByKey[0].equals(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY)) {
+            authors = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_AUTHORS_BY_LINE);
+          }
         }
+        ref.addFileData(projectKey, path, new FileData(hash, lastCommits, revisions, authors));
       }
-      ref.fileDataPerPath().put(path, new FileData(hash, lastCommits, revisions, authors));
     }
     return ref;
   }
+
+  public Map<String, String> hashByRelativePath(String projectKey) {
+    Map<String, String> map = Maps.newHashMap();
+    Collection<SnapshotDataDto> selectSnapshotData = dao.selectSnapshotDataByComponentKey(
+      projectKey,
+      Arrays.asList(SnapshotDataTypes.FILE_HASHES)
+      );
+    if (!selectSnapshotData.isEmpty()) {
+      SnapshotDataDto snapshotDataDto = selectSnapshotData.iterator().next();
+      String data = snapshotDataDto.getData();
+      map = KeyValueFormat.parse(data);
+    }
+    return map;
+  }
+
+  public List<Object[]> query(String resourceKey, String... metricKeys) {
+    StringBuilder sb = new StringBuilder();
+    Map<String, Object> params = Maps.newHashMap();
+
+    sb.append("SELECT met.key, m");
+    sb.append(" FROM ")
+      .append(MeasureModel.class.getSimpleName())
+      .append(" m, ")
+      .append(Metric.class.getSimpleName())
+      .append(" met, ")
+      .append(ResourceModel.class.getSimpleName())
+      .append(" r, ")
+      .append(Snapshot.class.getSimpleName())
+      .append(" s WHERE met.id=m.metricId AND m.snapshotId=s.id AND s.resourceId=r.id AND r.key=:kee AND s.status=:status AND s.qualifier<>:lib");
+    params.put("kee", resourceKey);
+    params.put("status", Snapshot.STATUS_PROCESSED);
+    params.put("lib", Qualifiers.LIBRARY);
+
+    sb.append(" AND m.characteristicId IS NULL");
+    sb.append(" AND m.personId IS NULL");
+    sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL");
+    if (metricKeys.length > 0) {
+      sb.append(" AND met.key IN (:metricKeys) ");
+      params.put("metricKeys", Arrays.asList(metricKeys));
+    }
+    sb.append(" AND s.last=true ");
+    sb.append(" ORDER BY s.createdAt ");
+
+    Query jpaQuery = session.createQuery(sb.toString());
+
+    for (Map.Entry<String, Object> entry : params.entrySet()) {
+      jpaQuery.setParameter(entry.getKey(), entry.getValue());
+    }
+    return jpaQuery.getResultList();
+  }
 }
index 4e8ed7eedc9e653256dac7419d0bc44f165892fb..cc099ba9f9c722d8424a58a4b775de1a8d501652 100644 (file)
@@ -98,7 +98,7 @@ class InputFileBuilder {
     FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(inputFile.file(), fs.encoding());
     inputFile.setLines(metadata.lines);
     inputFile.setHash(metadata.hash);
-    inputFile.setStatus(statusDetection.status(inputFile.relativePath(), metadata.hash));
+    inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), metadata.hash));
     if (analysisMode.isIncremental() && inputFile.status() == InputFile.Status.SAME) {
       return null;
     }
index 96840779d22daff3e37fc5af3fa53b604be1d146..8843e6049894c039192c39028a2513a054a0d01d 100644 (file)
@@ -32,8 +32,8 @@ class StatusDetection {
     this.projectReferentials = projectReferentials;
   }
 
-  InputFile.Status status(String relativePath, String hash) {
-    FileData fileDataPerPath = projectReferentials.fileDataPerPath(relativePath);
+  InputFile.Status status(String projectKey, String relativePath, String hash) {
+    FileData fileDataPerPath = projectReferentials.fileData(projectKey, relativePath);
     if (fileDataPerPath == null) {
       return InputFile.Status.ADDED;
     }
index 350135a07803afad71094b0a3f867e8441dde78c..ba7a1fe0ee7ca6bccbf9be6762a1af2a7ce0fb88 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.scm;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.fs.InputFile.Status;
@@ -51,11 +52,14 @@ public final class ScmActivitySensor implements Sensor {
   private static final Pattern NON_ASCII_CHARS = Pattern.compile("[^\\x00-\\x7F]");
   private static final Pattern ACCENT_CODES = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
 
+  private final ProjectDefinition projectDefinition;
   private final ScmActivityConfiguration configuration;
   private final FileSystem fs;
   private final ProjectReferentials projectReferentials;
 
-  public ScmActivitySensor(ScmActivityConfiguration configuration, ProjectReferentials projectReferentials, FileSystem fs) {
+  public ScmActivitySensor(ProjectDefinition projectDefinition, ScmActivityConfiguration configuration,
+    ProjectReferentials projectReferentials, FileSystem fs) {
+    this.projectDefinition = projectDefinition;
     this.configuration = configuration;
     this.projectReferentials = projectReferentials;
     this.fs = fs;
@@ -81,7 +85,7 @@ public final class ScmActivitySensor implements Sensor {
 
     List<InputFile> filesToBlame = new LinkedList<InputFile>();
     for (InputFile f : fs.inputFiles(fs.predicates().all())) {
-      FileData fileData = projectReferentials.fileDataPerPath(f.relativePath());
+      FileData fileData = projectReferentials.fileData(projectDefinition.getKeyWithBranch(), f.relativePath());
       if (f.status() == Status.SAME
         && fileData != null
         && fileData.scmAuthorsByLine() != null
index 95821910b74bfe8c54ec6d471c779c32c05b4dfe..d190df314c149ac7c9f583531a5cd0148c2c3473 100644 (file)
@@ -24,14 +24,12 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.batch.DefaultTimeMachine;
+import org.sonar.api.database.DatabaseSession;
 import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.bootstrap.TaskProperties;
 import org.sonar.batch.rule.ModuleQProfiles;
-import org.sonar.batch.scan.filesystem.PreviousFileHashLoader;
+import org.sonar.core.source.db.SnapshotDataDao;
 
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
@@ -50,9 +48,7 @@ public class DefaultProjectReferentialsLoaderTest {
   public void prepare() {
     serverClient = mock(ServerClient.class);
     analysisMode = mock(AnalysisMode.class);
-    MetricFinder metricFinder = mock(MetricFinder.class);
-    when(metricFinder.findByKey(anyString())).thenReturn(new Metric().setId(1));
-    loader = new DefaultProjectReferentialsLoader(serverClient, analysisMode, mock(PreviousFileHashLoader.class), metricFinder, mock(DefaultTimeMachine.class));
+    loader = new DefaultProjectReferentialsLoader(mock(DatabaseSession.class), serverClient, analysisMode, mock(SnapshotDataDao.class));
     when(serverClient.request(anyString())).thenReturn("");
     reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
     taskProperties = new TaskProperties(Maps.<String, String>newHashMap(), "");
index 067f1f99d5899407faaacd680db52c74c6d8d9d2..91fcbfa0c78d54a2785e277ab589cada9d24bb2b 100644 (file)
@@ -62,7 +62,7 @@ public class InputFileBuilderTest {
     when(langDetection.language(any(InputFile.class))).thenReturn("java");
 
     // status
-    when(statusDetection.status("src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
+    when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
@@ -135,7 +135,7 @@ public class InputFileBuilderTest {
     when(langDetection.language(any(InputFile.class))).thenReturn("java");
 
     // status
-    when(statusDetection.status("src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
+    when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
@@ -164,7 +164,7 @@ public class InputFileBuilderTest {
     when(langDetection.language(any(InputFile.class))).thenReturn("php");
 
     // status
-    when(statusDetection.status("src/Bar.php", "6c1d64c0b3555892fe7273e954f6fb5a"))
+    when(statusDetection.status("foo", "src/Bar.php", "6c1d64c0b3555892fe7273e954f6fb5a"))
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
index ecadbe3b1be2dc41a0fe7aa0f190c25edc5ef5e4..e40df46c17244084688caada0844cf56fb81e291 100644 (file)
@@ -30,12 +30,12 @@ public class StatusDetectionTest {
   @Test
   public void detect_status() throws Exception {
     ProjectReferentials ref = new ProjectReferentials();
-    ref.fileDataPerPath().put("src/Foo.java", new FileData("ABCDE", null, null, null));
-    ref.fileDataPerPath().put("src/Bar.java", new FileData("FGHIJ", null, null, null));
+    ref.addFileData("foo", "src/Foo.java", new FileData("ABCDE", null, null, null));
+    ref.addFileData("foo", "src/Bar.java", new FileData("FGHIJ", null, null, null));
     StatusDetection statusDetection = new StatusDetection(ref);
 
-    assertThat(statusDetection.status("src/Foo.java", "ABCDE")).isEqualTo(InputFile.Status.SAME);
-    assertThat(statusDetection.status("src/Foo.java", "XXXXX")).isEqualTo(InputFile.Status.CHANGED);
-    assertThat(statusDetection.status("src/Other.java", "QWERT")).isEqualTo(InputFile.Status.ADDED);
+    assertThat(statusDetection.status("foo", "src/Foo.java", "ABCDE")).isEqualTo(InputFile.Status.SAME);
+    assertThat(statusDetection.status("foo", "src/Foo.java", "XXXXX")).isEqualTo(InputFile.Status.CHANGED);
+    assertThat(statusDetection.status("foo", "src/Other.java", "QWERT")).isEqualTo(InputFile.Status.ADDED);
   }
 }