Search in sources :

Example 16 with FilePredicates

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

the class CfnLintImporter method saveAsExternalIssue.

private static void saveAsExternalIssue(SensorContext context, JSONObject issueJson) {
    String path = (String) issueJson.get("Filename");
    FilePredicates predicates = context.fileSystem().predicates();
    InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasAbsolutePath(path), predicates.hasRelativePath(path)));
    Objects.requireNonNull(inputFile);
    String ruleId = (String) ((JSONObject) issueJson.get("Rule")).get("Id");
    if (!CfnLintRulesDefinition.RULE_LOADER.ruleKeys().contains(ruleId)) {
        ruleId = "cfn-lint.fallback";
    }
    NewExternalIssue externalIssue = context.newExternalIssue().ruleId(ruleId).type(CfnLintRulesDefinition.RULE_LOADER.ruleType(ruleId)).engineId(CfnLintRulesDefinition.LINTER_KEY).severity(CfnLintRulesDefinition.RULE_LOADER.ruleSeverity(ruleId)).remediationEffortMinutes(CfnLintRulesDefinition.RULE_LOADER.ruleConstantDebtMinutes(ruleId));
    externalIssue.at(getIssueLocation(issueJson, externalIssue, inputFile));
    externalIssue.save();
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) NewExternalIssue(org.sonar.api.batch.sensor.issue.NewExternalIssue) InputFile(org.sonar.api.batch.fs.InputFile)

Example 17 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-openedge by Riverside-Software.

the class OpenEdgeDBRulesSensor method execute.

@Override
public void execute(SensorContext context) {
    Map<String, Long> ruleTime = new HashMap<>();
    long parseTime = 0L;
    components.init(context);
    for (Map.Entry<ActiveRule, OpenEdgeDumpFileCheck> entry : components.getDumpFileRules().entrySet()) {
        ruleTime.put(entry.getKey().ruleKey().toString(), 0L);
    }
    FilePredicates predicates = context.fileSystem().predicates();
    for (InputFile file : context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.DB_LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
        try {
            LOG.debug("Generating ParseTree for dump file {}", file);
            long time = System.currentTimeMillis();
            ParseTree tree = DumpFileUtils.getDumpFileParseTree(file.inputStream(), file.charset());
            parseTime += (System.currentTimeMillis() - time);
            for (Map.Entry<ActiveRule, OpenEdgeDumpFileCheck> entry : components.getDumpFileRules().entrySet()) {
                LOG.debug("ActiveRule - Internal key {} - Repository {} - Rule {}", entry.getKey().internalKey(), entry.getKey().ruleKey().repository(), entry.getKey().ruleKey().rule());
                long startTime = System.currentTimeMillis();
                entry.getValue().execute(file, tree);
                ruleTime.put(entry.getKey().ruleKey().toString(), ruleTime.get(entry.getKey().ruleKey().toString()) + System.currentTimeMillis() - startTime);
            }
        } catch (IOException caught) {
            LOG.error("Unable to analyze {}", file, caught);
        }
    }
    LOG.info("AST Generation | time={} ms", parseTime);
    for (Entry<String, Long> entry : ruleTime.entrySet()) {
        LOG.info("Rule {} | time={} ms", entry.getKey(), entry.getValue());
    }
}
Also used : HashMap(java.util.HashMap) ActiveRule(org.sonar.api.batch.rule.ActiveRule) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) OpenEdgeDumpFileCheck(org.sonar.plugins.openedge.api.checks.OpenEdgeDumpFileCheck) FilePredicates(org.sonar.api.batch.fs.FilePredicates) HashMap(java.util.HashMap) Map(java.util.Map) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 18 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-openedge by Riverside-Software.

the class OpenEdgeDBSensor method computeBaseMetrics.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void computeBaseMetrics(SensorContext sensorContext) {
    FilePredicates predicates = sensorContext.fileSystem().predicates();
    for (InputFile file : sensorContext.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.DB_LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
        try {
            LOG.info("Analyzing {}", file);
            DatabaseDescription desc = DumpFileUtils.getDatabaseDescription(file.inputStream(), file.charset(), Files.getNameWithoutExtension(file.filename()));
            sensorContext.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_TABLES).withValue(desc.getTables().size()).save();
            sensorContext.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_SEQUENCES).withValue(desc.getSequences().size()).save();
            int numFlds = 0;
            int numIdx = 0;
            int numTriggers = 0;
            for (Table tab : desc.getTables()) {
                numFlds += tab.getFields().size();
                numIdx += tab.getIndexes().size();
                numTriggers += tab.getTriggers().size();
                for (Field f : tab.getFields()) {
                    numTriggers += f.getTriggers().size();
                }
            }
            sensorContext.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_FIELDS).withValue(numFlds).save();
            sensorContext.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_INDEXES).withValue(numIdx).save();
            sensorContext.newMeasure().on(file).forMetric((Metric) OpenEdgeMetrics.NUM_TRIGGERS).withValue(numTriggers).save();
        } catch (IOException caught) {
            LOG.error("Can not analyze file", caught);
        }
    }
}
Also used : Field(eu.rssw.antlr.database.objects.Field) Table(eu.rssw.antlr.database.objects.Table) FilePredicates(org.sonar.api.batch.fs.FilePredicates) IOException(java.io.IOException) DatabaseDescription(eu.rssw.antlr.database.objects.DatabaseDescription) InputFile(org.sonar.api.batch.fs.InputFile)

Example 19 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-openedge by Riverside-Software.

the class OpenEdgeProparseSensor method execute.

@Override
public void execute(SensorContext context) {
    if (settings.skipProparseSensor())
        return;
    settings.init();
    components.init(context);
    for (Map.Entry<ActiveRule, OpenEdgeProparseCheck> entry : components.getProparseRules().entrySet()) {
        ruleTime.put(entry.getKey().ruleKey().toString(), 0L);
    }
    IRefactorSessionEnv sessions = settings.getProparseSessions();
    FilePredicates predicates = context.fileSystem().predicates();
    // Counting total number of files
    long totFiles = StreamSupport.stream(context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN))).spliterator(), false).count();
    long prevMessage = System.currentTimeMillis();
    for (InputFile file : context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
        LOG.debug("Parsing {}", file);
        numFiles++;
        if (System.currentTimeMillis() - prevMessage > 30000L) {
            prevMessage = System.currentTimeMillis();
            LOG.info("{}/{} - Current file: {}", numFiles, totFiles, file.relativePath());
        }
        IProparseEnvironment session = sessions.getSession(file.relativePath());
        if (settings.isIncludeFile(file.filename())) {
            parseIncludeFile(context, file, session);
        } else {
            parseMainFile(context, file, session);
        }
        if (context.isCancelled()) {
            LOG.info("Analysis cancelled...");
            return;
        }
    }
    computeAnalytics(context);
    logStatistics();
    context.addContextProperty("sonar.oe.ncloc", Integer.toString(ncLocs));
    generateProparseDebugIndex();
}
Also used : OpenEdgeProparseCheck(org.sonar.plugins.openedge.api.checks.OpenEdgeProparseCheck) IProparseEnvironment(org.prorefactor.proparse.support.IProparseEnvironment) ActiveRule(org.sonar.api.batch.rule.ActiveRule) FilePredicates(org.sonar.api.batch.fs.FilePredicates) IRefactorSessionEnv(org.sonar.plugins.openedge.foundation.IRefactorSessionEnv) Map(java.util.Map) HashMap(java.util.HashMap) InputFile(org.sonar.api.batch.fs.InputFile)

Example 20 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-openedge by Riverside-Software.

the class OpenEdgeSensor method computeBaseMetrics.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void computeBaseMetrics(SensorContext context) {
    FilePredicates predicates = context.fileSystem().predicates();
    for (InputFile file : context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
        LOG.trace("Computing basic metrics on {}", file);
        // Depending on file extension
        String fileExt = FilenameUtils.getExtension(file.filename());
        if ("w".equalsIgnoreCase(fileExt)) {
            context.newMeasure().on(file).withValue(1).forMetric((Metric) OpenEdgeMetrics.WINDOWS).save();
        } else if ("p".equalsIgnoreCase(fileExt)) {
            context.newMeasure().on(file).withValue(1).forMetric((Metric) OpenEdgeMetrics.PROCEDURES).save();
        } else if ("i".equalsIgnoreCase(fileExt)) {
            context.newMeasure().on(file).withValue(1).forMetric((Metric) OpenEdgeMetrics.INCLUDES).save();
        } else if ("cls".equalsIgnoreCase(fileExt)) {
            context.newMeasure().on(file).withValue(1).forMetric((Metric) OpenEdgeMetrics.CLASSES).save();
        }
    }
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) Metric(org.sonar.api.batch.measure.Metric) 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