]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5077 Add option to import all files even if no language
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 15 Jan 2015 22:50:31 +0000 (23:50 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 15 Jan 2015 22:50:31 +0000 (23:50 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java

index cd9f54ee85734b70e2e5f213c58e7cc2d1436d65..167e7edf658559704893a467e88037cff84ef386 100644 (file)
@@ -270,6 +270,16 @@ import java.util.List;
     project = true,
     global = false,
     category = CoreProperties.CATEGORY_SCM
+  ),
+  @Property(
+    key = CoreProperties.INDEX_ALL_FILES_KEY,
+    defaultValue = "false",
+    name = "Index all files",
+    description = "Should all files be indexed even if there is no matching language plugin installed.",
+    module = false,
+    project = true,
+    global = true,
+    category = CoreProperties.CATEGORY_GENERAL
   )
 })
 public final class CorePlugin extends SonarPlugin {
index e83fb0b8962cabf66bcb720e636794fe0a0f0a5b..88f6ac854c4de306147b01161d567e18262562e6 100644 (file)
@@ -135,29 +135,33 @@ public class SourcePersister implements ScanPersister {
     String newData = getSourceData(inputFile);
     String newDataHash = newData != null ? DigestUtils.md5Hex(newData) : "0";
     Date now = system2.newDate();
-    if (previous == null) {
-      FileSourceDto newFileSource = new FileSourceDto()
-        .setProjectUuid(projectTree.getRootProject().getUuid())
-        .setFileUuid(fileUuid)
-        .setData(newData)
-        .setDataHash(newDataHash)
-        .setSrcHash(inputFile.hash())
-        .setLineHashes(lineHashesAsMd5Hex(inputFile))
-        .setCreatedAt(now.getTime())
-        .setUpdatedAt(now.getTime());
-      mapper.insert(newFileSource);
-      session.commit();
-    } else {
-      if (!newDataHash.equals(previous.getDataHash())) {
-        previous
+    try {
+      if (previous == null) {
+        FileSourceDto newFileSource = new FileSourceDto()
+          .setProjectUuid(projectTree.getRootProject().getUuid())
+          .setFileUuid(fileUuid)
           .setData(newData)
-          .setLineHashes(lineHashesAsMd5Hex(inputFile))
           .setDataHash(newDataHash)
           .setSrcHash(inputFile.hash())
+          .setLineHashes(lineHashesAsMd5Hex(inputFile))
+          .setCreatedAt(now.getTime())
           .setUpdatedAt(now.getTime());
-        mapper.update(previous);
+        mapper.insert(newFileSource);
         session.commit();
+      } else {
+        if (!newDataHash.equals(previous.getDataHash())) {
+          previous
+            .setData(newData)
+            .setLineHashes(lineHashesAsMd5Hex(inputFile))
+            .setDataHash(newDataHash)
+            .setSrcHash(inputFile.hash())
+            .setUpdatedAt(now.getTime());
+          mapper.update(previous);
+          session.commit();
+        }
       }
+    } catch (Exception e) {
+      throw new IllegalStateException("Unable to save file sources for " + inputPath.absolutePath(), e);
     }
   }
 
index 8b00b2640a46563c477ebd3e97ba5f6e47acf55a..49303a056fd09d058b2d824c2ed5013e7af151b2 100644 (file)
@@ -22,9 +22,11 @@ package org.sonar.batch.scan.filesystem;
 import org.apache.commons.io.FilenameUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
+import org.sonar.api.config.Settings;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.util.DeprecatedKeyUtils;
@@ -44,15 +46,17 @@ class InputFileBuilder {
   private final StatusDetection statusDetection;
   private final DefaultModuleFileSystem fs;
   private final AnalysisMode analysisMode;
+  private final Settings settings;
 
   InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection,
-    StatusDetection statusDetection, DefaultModuleFileSystem fs, AnalysisMode analysisMode) {
+    StatusDetection statusDetection, DefaultModuleFileSystem fs, AnalysisMode analysisMode, Settings settings) {
     this.moduleKey = moduleKey;
     this.pathResolver = pathResolver;
     this.langDetection = langDetection;
     this.statusDetection = statusDetection;
     this.fs = fs;
     this.analysisMode = analysisMode;
+    this.settings = settings;
   }
 
   String moduleKey() {
@@ -98,7 +102,7 @@ class InputFileBuilder {
     inputFile.setEncoding(fs.encoding().name());
 
     String lang = langDetection.language(inputFile);
-    if (lang == null) {
+    if (lang == null && !settings.getBoolean(CoreProperties.INDEX_ALL_FILES_KEY)) {
       return null;
     }
     inputFile.setLanguage(lang);
index 59f1e667edd59b88585c30dfe7865ef93edadd7e..ac8c79db2ab51e8c9a2e58523d725db2a3707067 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.scan.filesystem;
 
 import org.sonar.api.BatchComponent;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.config.Settings;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.batch.bootstrap.AnalysisMode;
 
@@ -31,22 +32,24 @@ public class InputFileBuilderFactory implements BatchComponent {
   private final LanguageDetectionFactory langDetectionFactory;
   private final StatusDetectionFactory statusDetectionFactory;
   private final AnalysisMode analysisMode;
+  private final Settings settings;
 
   public InputFileBuilderFactory(ProjectDefinition def, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory,
-    StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode) {
-    this(def.getKeyWithBranch(), pathResolver, langDetectionFactory, statusDetectionFactory, analysisMode);
+    StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode, Settings settings) {
+    this(def.getKeyWithBranch(), pathResolver, langDetectionFactory, statusDetectionFactory, analysisMode, settings);
   }
 
   private InputFileBuilderFactory(String effectiveKey, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory,
-    StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode) {
+    StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode, Settings settings) {
     this.moduleKey = effectiveKey;
     this.pathResolver = pathResolver;
     this.langDetectionFactory = langDetectionFactory;
     this.statusDetectionFactory = statusDetectionFactory;
     this.analysisMode = analysisMode;
+    this.settings = settings;
   }
 
   InputFileBuilder create(DefaultModuleFileSystem fs) {
-    return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode);
+    return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode, settings);
   }
 }
index 9dd41b52076d6ab90af570a448d6b4939afa8e22..893bc1a132a0bd981b0a6f9af9a9d6ec486a2918 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.batch.mediumtest.fs;
 
-import org.sonar.batch.mediumtest.TaskResult;
-
 import com.google.common.collect.ImmutableMap;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
@@ -34,6 +32,7 @@ import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.System2;
 import org.sonar.batch.mediumtest.BatchMediumTester;
+import org.sonar.batch.mediumtest.TaskResult;
 import org.sonar.xoo.XooPlugin;
 
 import java.io.File;
@@ -205,4 +204,28 @@ public class FileSystemMediumTest {
     }
   }
 
+  @Test
+  public void indexAnyFile() throws IOException {
+    File srcDir = new File(baseDir, "src");
+    srcDir.mkdir();
+
+    File xooFile = new File(srcDir, "sample.xoo");
+    FileUtils.write(xooFile, "Sample xoo\ncontent");
+
+    File otherFile = new File(srcDir, "sample.other");
+    FileUtils.write(otherFile, "Sample other\ncontent");
+
+    TaskResult result = tester.newTask()
+      .properties(builder
+        .put("sonar.sources", "src")
+        .put("sonar.index_all_files", "true")
+        .build())
+      .start();
+
+    assertThat(result.inputFiles()).hasSize(2);
+    assertThat(result.inputFile("src/sample.other").type()).isEqualTo(InputFile.Type.MAIN);
+    assertThat(result.inputFile("src/sample.other").relativePath()).isEqualTo("src/sample.other");
+    assertThat(result.inputFile("src/sample.other").language()).isNull();
+  }
+
 }
index 48941e1e20807f5248025eff58f11a0d46905f5a..9a753c7e66f48e00cba483f298a3a3e52ac7dcaf 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.batch.scan.filesystem;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.config.Settings;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.batch.bootstrap.AnalysisMode;
 
@@ -38,7 +39,7 @@ public class InputFileBuilderFactoryTest {
     AnalysisMode analysisMode = mock(AnalysisMode.class);
 
     InputFileBuilderFactory factory = new InputFileBuilderFactory(ProjectDefinition.create().setKey("struts"), pathResolver, langDetectionFactory,
-      statusDetectionFactory, analysisMode);
+      statusDetectionFactory, analysisMode, new Settings());
     InputFileBuilder builder = factory.create(fs);
 
     assertThat(builder.langDetection()).isNotNull();
index cf2a1cbe22b6d789eb3fe54845c4f512e3d00e20..3d1eb9b050d48283be0a6d343f9a382c58c5a141 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
+import org.sonar.api.config.Settings;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.api.utils.PathUtils;
 import org.sonar.batch.bootstrap.AnalysisMode;
@@ -66,7 +67,7 @@ public class InputFileBuilderTest {
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
-      langDetection, statusDetection, fs, analysisMode);
+      langDetection, statusDetection, fs, analysisMode, new Settings());
     DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
     inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
 
@@ -92,7 +93,7 @@ public class InputFileBuilderTest {
     when(fs.baseDir()).thenReturn(basedir);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
-      langDetection, statusDetection, fs, analysisMode);
+      langDetection, statusDetection, fs, analysisMode, new Settings());
     DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
 
     assertThat(inputFile).isNull();
@@ -112,7 +113,7 @@ public class InputFileBuilderTest {
     when(langDetection.language(any(InputFile.class))).thenReturn(null);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
-      langDetection, statusDetection, fs, analysisMode);
+      langDetection, statusDetection, fs, analysisMode, new Settings());
     DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
     inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
 
@@ -139,7 +140,7 @@ public class InputFileBuilderTest {
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
-      langDetection, statusDetection, fs, analysisMode);
+      langDetection, statusDetection, fs, analysisMode, new Settings());
     DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
     inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
 
@@ -168,7 +169,7 @@ public class InputFileBuilderTest {
       .thenReturn(InputFile.Status.ADDED);
 
     InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
-      langDetection, statusDetection, fs, analysisMode);
+      langDetection, statusDetection, fs, analysisMode, new Settings());
     DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
     inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
 
index 38626a5c126feb07e25936cbe4c9e247630d186f..30e727324ad6bf4531d70581571226063748a641 100644 (file)
@@ -552,4 +552,9 @@ public interface CoreProperties {
    * @since 5.0
    */
   String SCM_PROVIDER_KEY = "sonar.scm.provider";
+
+  /**
+   * @since 5.1
+   */
+  String INDEX_ALL_FILES_KEY = "sonar.index_all_files";
 }
index 357a7c991035055413a6e7f67d945615df218bbe..2cf8ee33c37b9da0d84081340e010a41966ab190 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.api.batch.fs;
 
+import javax.annotation.CheckForNull;
+
 import java.io.File;
 
 /**
@@ -70,9 +72,9 @@ public interface InputFile extends InputPath {
   File file();
 
   /**
-   * Language, for example "java" or "php". It's automatically guessed if it is not
-   * set in project settings.
+   * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
    */
+  @CheckForNull
   String language();
 
   /**