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) { // no information returned by the SCM, we write nothing in the report and // the compute engine will use SCM dates to estimate which lines are new return count; } for (Map.Entry<Path, DefaultInputFile> e : changedFiles.entrySet()) { Set<Integer> changedLines = pathSetMap.getOrDefault(e.getKey(), Collections.emptySet()); if (changedLines.isEmpty()) { LOG.warn("File '{}' was detected as changed but without having changed lines", e.getKey().toAbsolutePath()); } count++; writeChangedLines(writer, e.getValue().scannerId(), changedLines); } return count; }
@Test public void skip_if_scm_is_disabled() { when(scmConfiguration.isDisabled()).thenReturn(true); publisher.publish(writer); verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider); assertNotPublished(); }
@Override public void publish(ScannerReportWriter writer) { String targetScmBranch = branchConfiguration.targetScmBranch(); if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetScmBranch == null) { return; } ScmProvider provider = scmConfiguration.provider(); if (provider == null) { return; } Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); int count = writeChangedLines(provider, writer, targetScmBranch); LOG.debug("SCM reported changed lines for {} {} in the branch", count, ScannerUtils.pluralize("file", count)); profiler.stopInfo(); }
@Test public void write_changed_files() { DefaultInputFile fileWithChangedLines = createInputFile("path1"); DefaultInputFile fileWithoutChangedLines = createInputFile("path2"); Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2"))); Set<Integer> lines = new HashSet<>(Arrays.asList(1, 10)); when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines)); when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines)); publisher.publish(writer); assertPublished(fileWithChangedLines, lines); assertPublished(fileWithoutChangedLines, Collections.emptySet()); }
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) { // no information returned by the SCM, we write nothing in the report and // the compute engine will use SCM dates to estimate which lines are new return count; } for (Map.Entry<Path, DefaultInputFile> e : changedFiles.entrySet()) { Set<Integer> changedLines = pathSetMap.getOrDefault(e.getKey(), Collections.emptySet()); if (changedLines.isEmpty()) { LOG.warn("File '{}' was detected as changed but without having changed lines", e.getKey().toAbsolutePath()); } count++; writeChangedLines(writer, e.getValue().scannerId(), changedLines); } return count; }
@Test public void skip_if_no_scm_provider() { when(scmConfiguration.provider()).thenReturn(null); publisher.publish(writer); verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider); assertNotPublished(); }
@Override public void publish(ScannerReportWriter writer) { String targetScmBranch = branchConfiguration.targetScmBranch(); if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetScmBranch == null) { return; } ScmProvider provider = scmConfiguration.provider(); if (provider == null) { return; } Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); int count = writeChangedLines(provider, writer, targetScmBranch); LOG.debug("SCM reported changed lines for {} {} in the branch", count, ScannerUtils.pluralize("file", count)); profiler.stopInfo(); }
@Test public void skip_if_not_pr_or_slb() { when(branchConfiguration.isShortOrPullRequest()).thenReturn(false); publisher.publish(writer); verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider); assertNotPublished(); }
@Test public void skip_if_scm_provider_returns_null() { publisher.publish(writer); assertNotPublished(); }
@Test public void skip_if_target_branch_is_null() { when(branchConfiguration.targetScmBranch()).thenReturn(null); publisher.publish(writer); verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider); assertNotPublished(); }