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