@VisibleForTesting public DefaultProjectFileSystem(DefaultInputProject project, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(project.getBaseDir()); setFields(project, mode, statusDetection); }
public DefaultProjectFileSystem(InputComponentStore inputComponentStore, DefaultInputProject project, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(project.getBaseDir(), inputComponentStore); setFields(project, mode, statusDetection); }
public Path metadataFilePath() { Optional<String> metadataFilePath = configuration.get(METADATA_FILE_PATH_KEY); if (metadataFilePath.isPresent()) { Path metadataPath = Paths.get(metadataFilePath.get()); if (!metadataPath.isAbsolute()) { throw MessageException.of(String.format("Property '%s' must point to an absolute path: %s", METADATA_FILE_PATH_KEY, metadataFilePath.get())); } return project.getBaseDir().resolve(metadataPath); } else { return project.getWorkDir().resolve(METADATA_DUMP_FILENAME); } }
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; }
private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) { InputComponent component = location.inputComponent(); Optional<Path> dirOrModulePath = Optional.empty(); if (component instanceof DefaultInputDir) { DefaultInputDir dirComponent = (DefaultInputDir) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path())); } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) { DefaultInputModule moduleComponent = (DefaultInputModule) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir())); } if (dirOrModulePath.isPresent()) { String path = PathUtils.sanitize(dirOrModulePath.get().toString()); DefaultIssueLocation fixedLocation = new DefaultIssueLocation(); fixedLocation.on(project); StringBuilder fullMessage = new StringBuilder(); if (!isNullOrEmpty(path)) { fullMessage.append("[").append(path).append("] "); } fullMessage.append(location.message()); fixedLocation.message(fullMessage.toString()); return fixedLocation; } else { return location; } } }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(project.getBaseDir()).thenReturn(rootBaseDir); provider = new ScmChangedFilesProvider(); }
@Before public void setUp() throws IOException { when(project.getBaseDir()).thenReturn(temp.newFolder().toPath()); when(project.getWorkDir()).thenReturn(temp.newFolder().toPath()); }
public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator, ProjectReactor projectReactor, ScannerComponentIdGenerator scannerComponentIdGenerator) { if (project == null) { // 1 Apply project builders projectBuildersExecutor.execute(projectReactor); // 2 Validate final reactor validator.validate(projectReactor); // 3 Create project project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt()); LOG.info("Project key: {}", project.key()); LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString()); LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString()); LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault()); } return project; } }
@Before public void setUp() { writer = new ScannerReportWriter(temp.getRoot()); when(branchConfiguration.isShortOrPullRequest()).thenReturn(true); when(scmConfiguration.isDisabled()).thenReturn(false); when(scmConfiguration.provider()).thenReturn(provider); when(branchConfiguration.targetScmBranch()).thenReturn(TARGET_BRANCH); when(project.getBaseDir()).thenReturn(BASE_DIR); }
public ScmChangedFiles provide(@Nullable ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, DefaultInputProject project) { if (scmBranchChangedFiles == null) { if (scmConfiguration == null) { scmBranchChangedFiles = new ScmChangedFiles(null); } else { Path rootBaseDir = project.getBaseDir(); Collection<Path> changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); validatePaths(changedFiles); scmBranchChangedFiles = new ScmChangedFiles(changedFiles); } } return scmBranchChangedFiles; }
if (!realAbsoluteFile.startsWith(project.getBaseDir())) { LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir()); return; return; Path projectRelativePath = project.getBaseDir().relativize(realAbsoluteFile); Path moduleRelativePath = module.getBaseDir().relativize(realAbsoluteFile); boolean included = evaluateInclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
@Test public void move_directory_issue_to_project_root() { SensorStorage storage = mock(SensorStorage.class); DefaultIssue issue = new DefaultIssue(project, storage) .at(new DefaultIssueLocation() .on(new DefaultInputDir("foo", "src/main").setModuleBaseDir(project.getBaseDir())) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .overrideSeverity(Severity.BLOCKER); assertThat(issue.primaryLocation().inputComponent()).isEqualTo(project); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.primaryLocation().textRange()).isNull(); assertThat(issue.primaryLocation().message()).isEqualTo("[src/main] Wrong way!"); assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER); issue.save(); verify(storage).store(issue); }
@Test public void move_submodule_issue_to_project_root() { File subModuleDirectory = new File(project.getBaseDir().toString(), "bar"); subModuleDirectory.mkdir(); ProjectDefinition subModuleDefinition = ProjectDefinition.create() .setKey("foo/bar") .setBaseDir(subModuleDirectory) .setWorkDir(subModuleDirectory); project.definition().addSubProject(subModuleDefinition); DefaultInputModule subModule = new DefaultInputModule(subModuleDefinition); SensorStorage storage = mock(SensorStorage.class); DefaultIssue issue = new DefaultIssue(project, storage) .at(new DefaultIssueLocation() .on(subModule) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .overrideSeverity(Severity.BLOCKER); assertThat(issue.primaryLocation().inputComponent()).isEqualTo(project); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.primaryLocation().textRange()).isNull(); assertThat(issue.primaryLocation().message()).isEqualTo("[bar] Wrong way!"); assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER); issue.save(); verify(storage).store(issue); }
public DefaultProjectFileSystem(InputComponentStore inputComponentStore, DefaultInputProject project, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(project.getBaseDir(), inputComponentStore); setFields(project, mode, statusDetection); }
@VisibleForTesting public DefaultProjectFileSystem(DefaultInputProject project, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(project.getBaseDir()); setFields(project, mode, statusDetection); }
public Path metadataFilePath() { Optional<String> metadataFilePath = configuration.get(METADATA_FILE_PATH_KEY); if (metadataFilePath.isPresent()) { Path metadataPath = Paths.get(metadataFilePath.get()); if (!metadataPath.isAbsolute()) { throw MessageException.of(String.format("Property '%s' must point to an absolute path: %s", METADATA_FILE_PATH_KEY, metadataFilePath.get())); } return project.getBaseDir().resolve(metadataPath); } else { return project.getWorkDir().resolve(METADATA_DUMP_FILENAME); } }
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; }
private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) { InputComponent component = location.inputComponent(); Optional<Path> dirOrModulePath = Optional.empty(); if (component instanceof DefaultInputDir) { DefaultInputDir dirComponent = (DefaultInputDir) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path())); } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) { DefaultInputModule moduleComponent = (DefaultInputModule) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir())); } if (dirOrModulePath.isPresent()) { String path = PathUtils.sanitize(dirOrModulePath.get().toString()); DefaultIssueLocation fixedLocation = new DefaultIssueLocation(); fixedLocation.on(project); StringBuilder fullMessage = new StringBuilder(); if (!isNullOrEmpty(path)) { fullMessage.append("[").append(path).append("] "); } fullMessage.append(location.message()); fixedLocation.message(fullMessage.toString()); return fixedLocation; } else { return location; } } }
public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator, ProjectReactor projectReactor, ScannerComponentIdGenerator scannerComponentIdGenerator) { if (project == null) { // 1 Apply project builders projectBuildersExecutor.execute(projectReactor); // 2 Validate final reactor validator.validate(projectReactor); // 3 Create project project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt()); LOG.info("Project key: {}", project.key()); LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString()); LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString()); LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault()); } return project; } }
public ScmChangedFiles provide(@Nullable ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, DefaultInputProject project) { if (scmBranchChangedFiles == null) { if (scmConfiguration == null) { scmBranchChangedFiles = new ScmChangedFiles(null); } else { Path rootBaseDir = project.getBaseDir(); Collection<Path> changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); validatePaths(changedFiles); scmBranchChangedFiles = new ScmChangedFiles(changedFiles); } } return scmBranchChangedFiles; }