Search in sources :

Example 1 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class AbstractDeprecatedXooRuleSensor method doAnalyse.

private void doAnalyse(SensorContext context, String languageKey) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, getRuleKey());
    if (activeRules.find(ruleKey) == null) {
        return;
    }
    for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(languageKey))) {
        File sonarFile = File.create(inputFile.relativePath());
        sonarFile = context.getResource(sonarFile);
        processFile(inputFile, sonarFile, context, ruleKey, languageKey);
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) InputFile(org.sonar.api.batch.fs.InputFile) File(org.sonar.api.resources.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 2 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class OneIssuePerDirectorySensor method analyse.

private static void analyse(SensorContext context) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    StreamSupport.stream(fs.inputFiles(p.hasType(Type.MAIN)).spliterator(), false).map(file -> fs.inputDir(file.file().getParentFile())).filter(Objects::nonNull).distinct().forEach(inputDir -> {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(inputDir).message("This issue is generated for any non-empty directory")).save();
    });
}
Also used : Objects(java.util.Objects) RuleKey(org.sonar.api.rule.RuleKey) FileSystem(org.sonar.api.batch.fs.FileSystem) Type(org.sonar.api.batch.fs.InputFile.Type) StreamSupport(java.util.stream.StreamSupport) Sensor(org.sonar.api.batch.sensor.Sensor) SensorContext(org.sonar.api.batch.sensor.SensorContext) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) FilePredicates(org.sonar.api.batch.fs.FilePredicates) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) Objects(java.util.Objects)

Example 3 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RandomAccessSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    NewIssue newIssue = context.newIssue();
    newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(file.selectLine(1)).message("This issue is generated on each file")).save();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Example 4 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class MultilineIssuesSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context, Map<Integer, TextPointer> startPositions, Map<Integer, TextPointer> endPositions, Map<Integer, Table<Integer, Integer, TextPointer>> startFlowsPositions, Map<Integer, Table<Integer, Integer, TextPointer>> endFlowsPositions) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    for (Map.Entry<Integer, TextPointer> entry : startPositions.entrySet()) {
        NewIssue newIssue = context.newIssue().forRule(ruleKey);
        Integer issueId = entry.getKey();
        NewIssueLocation primaryLocation = newIssue.newLocation().on(file).at(file.newRange(entry.getValue(), endPositions.get(issueId)));
        newIssue.at(primaryLocation.message("Primary location"));
        if (startFlowsPositions.containsKey(issueId)) {
            Table<Integer, Integer, TextPointer> flows = startFlowsPositions.get(issueId);
            for (Map.Entry<Integer, Map<Integer, TextPointer>> flowEntry : flows.rowMap().entrySet()) {
                Integer flowId = flowEntry.getKey();
                List<NewIssueLocation> flowLocations = Lists.newArrayList();
                List<Integer> flowNums = Lists.newArrayList(flowEntry.getValue().keySet());
                Collections.sort(flowNums);
                for (Integer flowNum : flowNums) {
                    TextPointer start = flowEntry.getValue().get(flowNum);
                    TextPointer end = endFlowsPositions.get(issueId).row(flowId).get(flowNum);
                    NewIssueLocation newLocation = newIssue.newLocation().on(file).at(file.newRange(start, end)).message("Flow step #" + flowNum);
                    flowLocations.add(newLocation);
                }
                if (flowLocations.size() == 1) {
                    newIssue.addLocation(flowLocations.get(0));
                } else {
                    newIssue.addFlow(flowLocations);
                }
            }
        }
        newIssue.save();
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextPointer(org.sonar.api.batch.fs.TextPointer) Map(java.util.Map)

Example 5 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class OneBugIssuePerLineSensor method createIssues.

private void createIssues(InputFile file, SensorContext context, String repo) {
    RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
    for (int line = 1; line <= file.lines(); line++) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(file.selectLine(line)).message("This bug issue is generated on each line")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3