Search in sources :

Example 11 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonarqube by SonarSource.

the class OneIssuePerTestFileSensor method execute.

@Override
public void execute(SensorContext context) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    if (activeRules.find(ruleKey) == null) {
        return;
    }
    FilePredicates p = fs.predicates();
    for (InputFile inputFile : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.TEST)))) {
        processFile(inputFile, context, ruleKey);
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 12 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonarqube by SonarSource.

the class OnePredefinedRuleExternalIssuePerLineSensor method execute.

@Override
public void execute(SensorContext context) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) {
        createIssues(file, context);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 13 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonarqube by SonarSource.

the class OneQuickFixPerLineSensor method analyse.

private void analyse(SensorContext context, String language, String repo) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(language), p.hasType(Type.MAIN)))) {
        createIssues(file, context, repo);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 14 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonarqube by SonarSource.

the class ChecksSensor method execute.

@Override
public void execute(SensorContext context) {
    Checks<Check> checks = checkFactory.create(XooRulesDefinition.XOO_REPOSITORY);
    checks.addAnnotatedChecks((Object[]) Check.ALL);
    FilePredicates p = context.fileSystem().predicates();
    for (InputFile file : context.fileSystem().inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) {
        for (Check check : checks.all()) {
            check.execute(context, file, checks.ruleKey(check));
        }
    }
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) Check(org.sonar.xoo.checks.Check) InputFile(org.sonar.api.batch.fs.InputFile)

Example 15 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-apple by insideapp-oss.

the class ReportIssueRecorder method recordIssues.

public void recordIssues(List<ReportIssue> issues, String repository) {
    final FileSystem fs = sensorContext.fileSystem();
    final FilePredicates predicates = fs.predicates();
    FilePredicate mainPredicate = predicates.hasType(InputFile.Type.MAIN);
    // Record issues
    for (ReportIssue issue : issues) {
        // The issue to be record
        NewIssue sonarIssue = sensorContext.newIssue().forRule(RuleKey.of(repository, issue.getRuleId()));
        // The location of the issue to be record
        NewIssueLocation sonarIssueLocation = new DefaultIssueLocation().message(issue.getMessage());
        final String filePath = issue.getFilePath();
        // We have a file location associated with the generated issue
        if (filePath != null) {
            final FilePredicate relativePathPredicate = predicates.hasRelativePath(filePath);
            final FilePredicate absolutePathPredicate = predicates.hasAbsolutePath(filePath);
            final FilePredicate pathPredicate = predicates.or(absolutePathPredicate, relativePathPredicate);
            final FilePredicate filePredicate = predicates.and(pathPredicate, mainPredicate);
            InputFile inputFile = fs.inputFile(filePredicate);
            // Making sure the file is part of SonarQube FS
            if (fs.hasFiles(filePredicate) && inputFile != null) {
                // Adding the location of the file
                sonarIssueLocation.on(inputFile);
                final Integer lineNumber = issue.getLineNumber();
                // We have a line number for that file
                if (lineNumber != null) {
                    // Adding the line number
                    sonarIssueLocation.at(inputFile.selectLine(lineNumber));
                }
                // Associating the location to the issue and saving it.
                sonarIssue.at(sonarIssueLocation).save();
            } else {
                LOGGER.warn("File not included in SonarQube sources {}", filePath);
            }
        } else {
            // No location specified, so a global issue.
            // Setting the project as location.
            sonarIssueLocation.on(sensorContext.project());
            // Associating the location to the issue and saving it.
            sonarIssue.at(sonarIssueLocation).save();
        }
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) DefaultIssueLocation(org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation) FilePredicate(org.sonar.api.batch.fs.FilePredicate) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

FilePredicates (org.sonar.api.batch.fs.FilePredicates)43 InputFile (org.sonar.api.batch.fs.InputFile)38 FileSystem (org.sonar.api.batch.fs.FileSystem)22 File (java.io.File)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 FilePredicate (org.sonar.api.batch.fs.FilePredicate)4 SensorContext (org.sonar.api.batch.sensor.SensorContext)4 RuleKey (org.sonar.api.rule.RuleKey)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)3 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 StreamSupport (java.util.stream.StreamSupport)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2