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;
}
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();
}
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());
}
}
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;
}
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();
}
Aggregations