Search in sources :

Example 1 with ScmProvider

use of org.sonar.api.batch.scm.ScmProvider in project sonarqube by SonarSource.

the class ScmChangedFilesProvider method loadChangedFilesIfNeeded.

@CheckForNull
private static Collection<Path> loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) {
    final String targetBranchName = branchConfiguration.targetBranchName();
    if (branchConfiguration.isPullRequest() && targetBranchName != null) {
        ScmProvider scmProvider = scmConfiguration.provider();
        if (scmProvider != null) {
            Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
            Collection<Path> changedFiles = scmProvider.branchChangedFiles(targetBranchName, rootBaseDir);
            profiler.stopInfo();
            if (changedFiles != null) {
                LOG.debug("SCM reported {} {} changed in the branch", changedFiles.size(), ScannerUtils.pluralize("file", changedFiles.size()));
                return changedFiles;
            }
        }
        LOG.debug("SCM information about changed files in the branch is not available");
    }
    return null;
}
Also used : ScmProvider(org.sonar.api.batch.scm.ScmProvider) Path(java.nio.file.Path) Profiler(org.sonar.api.utils.log.Profiler) CheckForNull(javax.annotation.CheckForNull)

Example 2 with ScmProvider

use of org.sonar.api.batch.scm.ScmProvider in project sonarqube by SonarSource.

the class MetadataPublisherTest method should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot.

@Test
public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() throws IOException {
    ScmProvider fakeScmProvider = new ScmProvider() {

        @Override
        public String key() {
            return "foo";
        }
    };
    when(scmConfiguration.provider()).thenReturn(fakeScmProvider);
    File outputDir = temp.newFolder();
    underTest.publish(new ScannerReportWriter(outputDir));
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    ScannerReport.Metadata metadata = reader.readMetadata();
    assertThat(metadata.getRelativePathFromScmRoot()).isEmpty();
}
Also used : ScmProvider(org.sonar.api.batch.scm.ScmProvider) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) File(java.io.File) Test(org.junit.Test)

Example 3 with ScmProvider

use of org.sonar.api.batch.scm.ScmProvider in project sonarqube by SonarSource.

the class MetadataPublisher method addModulesRelativePaths.

private void addModulesRelativePaths(ScannerReport.Metadata.Builder builder) {
    LinkedList<DefaultInputModule> queue = new LinkedList<>();
    queue.add(moduleHierarchy.root());
    while (!queue.isEmpty()) {
        DefaultInputModule module = queue.removeFirst();
        queue.addAll(moduleHierarchy.children(module));
        String relativePath = moduleHierarchy.relativePathToRoot(module);
        if (relativePath != null) {
            builder.putModulesProjectRelativePathByKey(module.key(), relativePath);
        }
    }
    ScmProvider scmProvider = scmConfiguration.provider();
    if (scmProvider == null) {
        return;
    }
    Path projectBasedir = moduleHierarchy.root().getBaseDir();
    try {
        builder.setRelativePathFromScmRoot(toSonarQubePath(scmProvider.relativePathFromScmRoot(projectBasedir)));
    } catch (UnsupportedOperationException e) {
        LOG.debug(e.getMessage());
    }
}
Also used : ScmProvider(org.sonar.api.batch.scm.ScmProvider) Path(java.nio.file.Path) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) LinkedList(java.util.LinkedList)

Example 4 with ScmProvider

use of org.sonar.api.batch.scm.ScmProvider in project sonarqube by SonarSource.

the class ChangedLinesPublisher method writeChangedLines.

private int writeChangedLines(ScmProvider provider, ScannerReportWriter writer, String targetScmBranch) {
    Path rootBaseDir = project.getBaseDir();
    Map<Path, DefaultInputFile> changedFiles = StreamSupport.stream(inputComponentStore.allChangedFilesToPublish().spliterator(), false).collect(Collectors.toMap(DefaultInputFile::path, f -> f));
    Map<Path, Set<Integer>> pathSetMap = provider.branchChangedLines(targetScmBranch, rootBaseDir, changedFiles.keySet());
    int count = 0;
    if (pathSetMap == null) {
        // the compute engine will use SCM dates to estimate which lines are new
        return count;
    }
    for (Map.Entry<Path, DefaultInputFile> e : changedFiles.entrySet()) {
        DefaultInputFile inputFile = e.getValue();
        Set<Integer> changedLines = pathSetMap.get(e.getKey());
        if (changedLines == null) {
            if (branchConfiguration.isPullRequest()) {
                LOG.warn("File '{}' was detected as changed but without having changed lines", e.getKey().toAbsolutePath());
            }
            // assume that no line was changed
            writeChangedLines(writer, e.getValue().scannerId(), Collections.emptySet());
        } else {
            // detect unchanged last empty line
            if (changedLines.size() + 1 == inputFile.lines() && inputFile.lineLength(inputFile.lines()) == 0) {
                changedLines.add(inputFile.lines());
            }
            count++;
            writeChangedLines(writer, e.getValue().scannerId(), changedLines);
        }
    }
    return count;
}
Also used : Path(java.nio.file.Path) BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) ScmConfiguration(org.sonar.scanner.scm.ScmConfiguration) Optional.empty(java.util.Optional.empty) ScmProvider(org.sonar.api.batch.scm.ScmProvider) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) Profiler(org.sonar.api.utils.log.Profiler) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Set(java.util.Set) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) Collectors(java.util.stream.Collectors) DefaultInputProject(org.sonar.api.batch.fs.internal.DefaultInputProject) ScannerUtils(org.sonar.api.impl.utils.ScannerUtils) Loggers(org.sonar.api.utils.log.Loggers) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) ReferenceBranchSupplier(org.sonar.scanner.repository.ReferenceBranchSupplier) Path(java.nio.file.Path) Collections(java.util.Collections) Logger(org.sonar.api.utils.log.Logger) Set(java.util.Set) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Map(java.util.Map)

Example 5 with ScmProvider

use of org.sonar.api.batch.scm.ScmProvider in project sonarqube by SonarSource.

the class SvnScmProviderTest method testAutodetection.

@Test
public void testAutodetection() throws IOException {
    ScmProvider scmBranchProvider = newScmProvider();
    File baseDirEmpty = temp.newFolder();
    assertThat(scmBranchProvider.supports(baseDirEmpty)).isFalse();
    File svnBaseDir = temp.newFolder();
    Files.createDirectory(svnBaseDir.toPath().resolve(".svn"));
    assertThat(scmBranchProvider.supports(svnBaseDir)).isTrue();
    File svnBaseDirSubFolder = temp.newFolder();
    Files.createDirectory(svnBaseDirSubFolder.toPath().resolve(".svn"));
    File projectBaseDir = new File(svnBaseDirSubFolder, "folder");
    Files.createDirectory(projectBaseDir.toPath());
    assertThat(scmBranchProvider.supports(projectBaseDir)).isTrue();
}
Also used : ScmProvider(org.sonar.api.batch.scm.ScmProvider) File(java.io.File) Test(org.junit.Test)

Aggregations

ScmProvider (org.sonar.api.batch.scm.ScmProvider)8 Path (java.nio.file.Path)3 Test (org.junit.Test)3 File (java.io.File)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 Profiler (org.sonar.api.utils.log.Profiler)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)2 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 CheckForNull (javax.annotation.CheckForNull)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)1 DefaultInputProject (org.sonar.api.batch.fs.internal.DefaultInputProject)1