org.sonar.api.batch.sensor.issue.NewIssueLocation Java Examples

The following examples show how to use org.sonar.api.batch.sensor.issue.NewIssueLocation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LeinNvdSensor.java    From sonar-clojure with MIT License 6 votes vote down vote up
private void saveVulnerabilities(List<Vulnerability> vulnerabilities, SensorContext context) {
    Optional<InputFile> fileOptional = getFile("project.clj", context.fileSystem());

    fileOptional.ifPresent(projectFile -> {
        for (Vulnerability v : vulnerabilities) {
            LOG.debug("Processing vulnerability: " +v.toString());
            RuleKey ruleKey = RuleKey.of(ClojureLintRulesDefinition.REPOSITORY_KEY, "nvd-" + v.getSeverity().toLowerCase());
            NewIssue newIssue = context.newIssue().forRule(ruleKey);
            NewIssueLocation primaryLocation = newIssue
                    .newLocation()
                    .on(projectFile)
                    .message(v.getName()
                            + ";" + v.getCwes()
                            + ";" + v.getFileName())
                    .at(projectFile.selectLine(1));
            newIssue.at(primaryLocation);
            newIssue.save();
        }
    });
}
 
Example #2
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveViolations(SensorContext context, InputFile inputFile, SourceFile squidFile) {
  Collection<CheckMessage> messages = squidFile.getCheckMessages();
  if (messages != null) {

    for (CheckMessage message : messages) {
      RuleKey ruleKey = checks.ruleKey((SquidCheck<LexerlessGrammar>) message.getCheck());
      NewIssue newIssue = context.newIssue()
        .forRule(ruleKey)
        .gap(message.getCost());
      Integer line = message.getLine();
      NewIssueLocation location = newIssue.newLocation()
        .on(inputFile)
        .message(message.getText(Locale.ENGLISH));
      if (line != null) {
        location.at(inputFile.selectLine(line));
      }
      newIssue.at(location);
      newIssue.save();
    }
  }
}
 
Example #3
Source File: EsqlSensor.java    From sonar-esql-plugin with Apache License 2.0 6 votes vote down vote up
private void processRecognitionException(RecognitionException e, SensorContext sensorContext, InputFile inputFile) {
    if (parsingErrorRuleKey != null) {
        NewIssue newIssue = sensorContext.newIssue();

        NewIssueLocation primaryLocation = newIssue.newLocation()
                .message(ParsingErrorCheck.MESSAGE)
                .on(inputFile)
                .at(inputFile.selectLine(e.getLine()));

        newIssue
                .forRule(parsingErrorRuleKey)
                .at(primaryLocation)
                .save();
    }

    sensorContext.newAnalysisError()
            .onFile(inputFile)
            .at(inputFile.newPointer(e.getLine(), 0))
            .message(e.getMessage())
            .save();
}
 
Example #4
Source File: MetricsSaver.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
public static void saveIssues(SensorContext context, HtmlSourceCode sourceCode) {
    InputFile inputFile = sourceCode.inputFile();

    for (HtmlIssue issue : sourceCode.getIssues()) {
        NewIssue newIssue = context.newIssue()
            .forRule(issue.ruleKey())
            .gap(issue.cost());
        Integer line = issue.line();
        NewIssueLocation location = newIssue.newLocation()
            .on(inputFile)
            .message(issue.message());
        if (line != null && line > 0) {
            location.at(inputFile.selectLine(line));
        }
        newIssue.at(location);
        newIssue.save();
    }
}
 
Example #5
Source File: IssueSaver.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveLineIssue(LineIssue issue) {
  NewIssue newIssue = sensorContext.newIssue();
  InputFile primaryFile = Preconditions.checkNotNull(fileSystem.inputFile(fileSystem.predicates().is(issue.file())));

  NewIssueLocation primaryLocation = newIssue.newLocation()
    .message(issue.message())
    .on(primaryFile)
    .at(primaryFile.selectLine(issue.line()));

  newIssue
    .forRule(ruleKey(issue.check()))
    .at(primaryLocation);

  if (issue.cost() != null) {
    newIssue.gap(issue.cost());
  }

  newIssue.save();
}
 
Example #6
Source File: RubocopSensor.java    From sonar-ruby-plugin with MIT License 6 votes vote down vote up
private void saveNewIssue(RubocopIssue issue, InputFile inputFile, Set<String> ruleNames, SensorContext sensorContext) {
    // Make sure the rule we're violating is one we recognise - if not, we'll
    // fall back to the generic 'rubocop-issue' rule
    String ruleName = issue.getRuleName();
    if (!ruleNames.contains(ruleName)) {
        ruleName = RubyRulesDefinition.RUBY_LINT_UNKNOWN_RULE.key;
    }

    NewIssue newIssue =
        sensorContext
            .newIssue()
            .forRule(RuleKey.of("rubocop", ruleName));

    NewIssueLocation newIssueLocation =
        newIssue
            .newLocation()
            .on(inputFile)
            .message(issue.getFailure())
            .at(inputFile.selectLine(issue.getPosition().getLine()));

    newIssue.at(newIssueLocation);
    newIssue.save();
}
 
Example #7
Source File: AbstractSensor.java    From sonar-clojure with MIT License 6 votes vote down vote up
protected void saveIssue(Issue issue, SensorContext context) {
    try {
        Optional<InputFile> fileOptional = getFile(issue.getFilePath(), context.fileSystem());

        if (fileOptional.isPresent()) {
            InputFile file = fileOptional.get();
            RuleKey ruleKey = RuleKey.of(ClojureLintRulesDefinition.REPOSITORY_KEY, issue.getExternalRuleId().trim());

            NewIssue newIssue = context.newIssue().forRule(ruleKey);

            NewIssueLocation primaryLocation = newIssue
                    .newLocation()
                    .on(file)
                    .message(issue.getDescription().trim());

            primaryLocation.at(file.selectLine(issue.getLine()));

            newIssue.at(primaryLocation);
            newIssue.save();
        } else {
            LOG.warn("Not able to find a file with path '{}'", issue.getFilePath());
        }
    } catch (Exception e) {
        LOG.error("Can not save the issue due to: " + e.getMessage());
    }
}
 
Example #8
Source File: AbstractAnsibleSensor.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
/**
 * Saves the found issues in SonarQube
 *
 * @param context the context
 * @param inputFile the file where the issue was found
 * @param line the line where the issue was found
 * @param ruleId the Id of the rule that raised the issue
 * @param message a message describing the issue
 */
protected void saveIssue(SensorContext context, InputFile inputFile, int line, String ruleId, String message) {
    RuleKey ruleKey = getRuleKey(context, ruleId);

    // Old rules (ansible-lint < 3.5) had id ANSIBLE... but now it is E... so we may need to add the heading E back
    if (ruleKey == null) {
        ruleKey = getRuleKey(context, "E" + ruleId);
    }

    if (ruleKey == null) {
        LOGGER.debug("Rule " + ruleId + " ignored, not found in repository");
        return;
    }

    NewIssue newIssue = context.newIssue().forRule(ruleKey);
    NewIssueLocation location = newIssue.newLocation()
            .on(inputFile)
            .message(message)
            .at(inputFile.selectLine(line));
    newIssue.at(location).save();
    LOGGER.debug("Issue {} saved for {}", ruleId, inputFile.filename());
}
 
Example #9
Source File: IssueSaver.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveLineIssue(LineIssue issue) {
  NewIssue newIssue = sensorContext.newIssue();
  InputFile primaryFile = Preconditions.checkNotNull(fileSystem.inputFile(fileSystem.predicates().is(issue.file())));

  NewIssueLocation primaryLocation = newIssue.newLocation()
    .message(issue.message())
    .on(primaryFile)
    .at(primaryFile.selectLine(issue.line()));

  newIssue
    .forRule(ruleKey(issue.check()))
    .at(primaryLocation);

  if (issue.cost() != null) {
    newIssue.gap(issue.cost());
  }

  newIssue.save();
}
 
Example #10
Source File: IssueSaver.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void saveFileIssue(FileIssue issue) {
  NewIssue newIssue = sensorContext.newIssue();
  InputFile primaryFile = Preconditions.checkNotNull(fileSystem.inputFile(fileSystem.predicates().is(issue.file())));

  NewIssueLocation primaryLocation = newIssue.newLocation()
    .message(issue.message())
    .on(primaryFile);

  newIssue
    .forRule(ruleKey(issue.check()))
    .at(primaryLocation);

  if (issue.cost() != null) {
    newIssue.gap(issue.cost());
  }

  InputFile secondaryFile;
  for (IssueLocation secondary : issue.secondaryLocations()) {
    secondaryFile = fileSystem.inputFile(fileSystem.predicates().is(secondary.file()));
    if (secondaryFile == null) {
      secondaryFile = primaryFile;
    }
    newIssue.addLocation(newLocation(secondaryFile, newIssue, secondary));
  }

  newIssue.save();
}
 
Example #11
Source File: AncientSensor.java    From sonar-clojure with MIT License 5 votes vote down vote up
private void saveOutdated(List<OutdatedDependency> outdatedDependencies, SensorContext context) {

        Optional<InputFile> fileOptional = getFile("project.clj", context.fileSystem());

        fileOptional.ifPresent(projectFile -> outdatedDependencies.forEach(outdatedDependency -> {
            ProjectFile pr;
            try {
                pr = new ProjectFile(projectFile.contents());
            } catch (IOException e) {
                LOG.warn("project.clj could not be read");
                return;
            }
            LOG.debug("Processing outdated dependencies");

            RuleKey ruleKey = RuleKey.of(ClojureLintRulesDefinition.REPOSITORY_KEY, "ancient-clj-dependency");
            NewIssue newIssue = context.newIssue().forRule(ruleKey);
            int lineNumber = pr.findLineNumber(
                    outdatedDependency.getName() + " \"" + outdatedDependency.getCurrentVersion() + "\"");

            NewIssueLocation primaryLocation = newIssue
                    .newLocation()
                    .on(projectFile)
                    .message(outdatedDependency.toString())
                    .at(projectFile.selectLine(lineNumber));
            newIssue.at(primaryLocation);
            newIssue.save();
        }));
    }
 
Example #12
Source File: EsqlSensor.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private static void saveIssue(NewIssue newIssue, NewIssueLocation primaryLocation, RuleKey ruleKey, Issue issue) {
    newIssue
            .forRule(ruleKey)
            .at(primaryLocation);

    if (issue.cost() != null) {
        newIssue.gap(issue.cost());
    }

    newIssue.save();
}
 
Example #13
Source File: EsqlSensor.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private static void saveFileIssue(SensorContext sensorContext, InputFile inputFile, RuleKey ruleKey, FileIssue issue) {
    NewIssue newIssue = sensorContext.newIssue();

    NewIssueLocation primaryLocation = newIssue.newLocation()
            .message(issue.message())
            .on(inputFile);

    saveIssue(newIssue, primaryLocation, ruleKey, issue);
}
 
Example #14
Source File: EsqlSensor.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private static void saveLineIssue(SensorContext sensorContext, InputFile inputFile, RuleKey ruleKey, LineIssue issue) {
    NewIssue newIssue = sensorContext.newIssue();

    NewIssueLocation primaryLocation = newIssue.newLocation()
            .message(issue.message())
            .on(inputFile)
            .at(inputFile.selectLine(issue.line()));

    saveIssue(newIssue, primaryLocation, ruleKey, issue);
}
 
Example #15
Source File: HtlFilesAnalyzer.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void processRecognitionExceptionForCustomRule(SightlyCompilerException e, SensorContext sensorContext, InputFile inputFile) {
    NewIssue newIssue = sensorContext.newIssue();

    NewIssueLocation primaryLocation = newIssue.newLocation()
        .message("Parse error: " + e.getMessage())
        .on(inputFile)
        .at(inputFile.selectLine(e.getLine()));

    newIssue
        .forRule(parsingErrorRuleKey)
        .at(primaryLocation)
        .save();
}
 
Example #16
Source File: AbstractLanguageAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processRecognitionException(RecognitionException e, SensorContext sensorContext, InputFile inputFile) {
  if (parsingErrorRuleKey != null) {
    NewIssue newIssue = sensorContext.newIssue();

    NewIssueLocation primaryLocation = newIssue.newLocation()
      .message(e.getMessage())
      .on(inputFile)
      .at(inputFile.selectLine(e.getLine()));

    newIssue
      .forRule(parsingErrorRuleKey)
      .at(primaryLocation)
      .save();
  }
}
 
Example #17
Source File: GherkinSquidSensor.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processRecognitionException(RecognitionException e, SensorContext sensorContext, InputFile inputFile) {
  if (parsingErrorRuleKey != null) {
    NewIssue newIssue = sensorContext.newIssue();

    NewIssueLocation primaryLocation = newIssue.newLocation()
      .message(e.getMessage())
      .on(inputFile)
      .at(inputFile.selectLine(e.getLine()));

    newIssue
      .forRule(parsingErrorRuleKey)
      .at(primaryLocation)
      .save();
  }
}
 
Example #18
Source File: CFLintAnalysisResultImporter.java    From sonar-coldfusion with Apache License 2.0 5 votes vote down vote up
private void createNewIssue(IssueAttributes issueAttributes, LocationAttributes locationAttributes, InputFile inputFile) {
    if(issueAttributes == null){
        logger.debug("Problem creating issue for file {} issueAttributes is null", inputFile);
    }
    if(locationAttributes == null){
        logger.debug("Problem creating issue for file {} locationAttributes is null", inputFile);
    }
    if(inputFile==null){
        logger.debug("Problem creating issue for file inputFile is null");
    }
    if(issueAttributes == null || locationAttributes == null || inputFile == null){
        return;
    }

    if(locationAttributes.getLine().isPresent() && locationAttributes.getLine().get()>inputFile.lines()){
        logger
            .error("Problem creating issue for file {}, issue is line {} but file has {} lines", inputFile, locationAttributes.getLine().get(), inputFile.lines());
        return;
    }

    logger.debug("create New Issue {} for file {}", issueAttributes, inputFile.filename());
    final NewIssue issue = sensorContext.newIssue();

    final NewIssueLocation issueLocation = issue.newLocation();
    issueLocation.on(inputFile);
    issueLocation.at(inputFile.selectLine(locationAttributes.getLine().get()));
    issueLocation.message(locationAttributes.getMessage().get());

    issue.forRule(RuleKey.of(ColdFusionPlugin.REPOSITORY_KEY, issueAttributes.getId().get()));
    issue.at(issueLocation);
    issue.save();
}
 
Example #19
Source File: CheckstyleAuditListenerTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addErrorTestForLine(final int pLineNo) {
    final ActiveRule rule = setupRule("repo", "key");

    final NewIssue newIssue = mock(NewIssue.class);
    final NewIssueLocation newLocation = mock(NewIssueLocation.class);
    when(context.newIssue()).thenReturn(newIssue);
    when(newIssue.newLocation()).thenReturn(newLocation);
    when(newIssue.forRule(rule.ruleKey())).thenReturn(newIssue);
    when(newIssue.at(newLocation)).thenReturn(newIssue);
    when(newLocation.on(any(InputComponent.class))).thenReturn(newLocation);
    when(newLocation.at(any(TextRange.class))).thenReturn(newLocation);
    when(newLocation.message(anyString())).thenReturn(newLocation);

    when(inputFile.selectLine(anyInt())).thenReturn(new DefaultTextRange(
        new DefaultTextPointer(1, 1), new DefaultTextPointer(1, 2)));

    final AuditEvent eventAdded = new AuditEvent(this, file.getAbsolutePath(),
        new LocalizedMessage(pLineNo, "", "", null, "", CheckstyleAuditListenerTest.class,
            "msg"));
    addErrorToListener(eventAdded);

    verify(newIssue, times(1)).save();
    verify(newIssue, times(1)).forRule(rule.ruleKey());
    verify(newIssue, times(1)).at(newLocation);
    verify(newIssue, times(1)).newLocation();
    verify(newLocation, times(1)).on(any());
    verify(newLocation, times(1)).at(any());
    verify(newLocation, times(1)).message(any());
}
 
Example #20
Source File: CheckstyleAuditListener.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void addError(AuditEvent event) {
    final String ruleKey = getRuleKey(event);

    if (Objects.nonNull(ruleKey)) {
        final String message = getMessage(event);
        // In Checkstyle 5.5 exceptions are reported as an events from
        // TreeWalker
        if ("com.puppycrawl.tools.checkstyle.TreeWalker".equals(ruleKey)) {
            LOG.warn("{} : {}", event.getFileName(), message);
        }

        initResource(event);

        final NewIssue issue = context.newIssue();
        final ActiveRule rule = ruleFinder.find(
                RuleKey.of(CheckstyleConstants.REPOSITORY_KEY, ruleKey));
        if (Objects.nonNull(issue) && Objects.nonNull(rule)) {
            final NewIssueLocation location = issue.newLocation()
                    .on(currentResource)
                    .at(currentResource.selectLine(getLineId(event)))
                    .message(message);
            issue.forRule(rule.ruleKey())
                    .at(location)
                    .save();
        }
    }
}
 
Example #21
Source File: IssueSaver.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void saveFileIssue(FileIssue issue) {
  NewIssue newIssue = sensorContext.newIssue();
  InputFile primaryFile = Preconditions.checkNotNull(fileSystem.inputFile(fileSystem.predicates().is(issue.file())));

  NewIssueLocation primaryLocation = newIssue.newLocation()
    .message(issue.message())
    .on(primaryFile);

  newIssue
    .forRule(ruleKey(issue.check()))
    .at(primaryLocation);

  if (issue.cost() != null) {
    newIssue.gap(issue.cost());
  }

  InputFile secondaryFile;
  for (IssueLocation secondary : issue.secondaryLocations()) {
    secondaryFile = fileSystem.inputFile(fileSystem.predicates().is(secondary.file()));
    if (secondaryFile == null) {
      secondaryFile = primaryFile;
    }
    newIssue.addLocation(newLocation(secondaryFile, newIssue, secondary));
  }

  newIssue.save();
}
 
Example #22
Source File: FlowIssue.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Sets the primary location.
 * @param file
 * Reference to the file
 * @param message
 * Message that was raised
 * @param startLine
 * line to start
 * @param startLineOffset
 * offset on startline
 * @param endLine
 * line to end
 * @param endLineOffset
 * offset on endline
 * @return this flowIssue
 */
public FlowIssue setPrimaryLocation(InputFile file, String message, int startLine,
    int startLineOffset, int endLine, int endLineOffset) {
  NewIssueLocation newIssueLocation;
  if (startLineOffset == -1) {
    newIssueLocation = newIssue.newLocation().on(file).at(file.selectLine(startLine))
        .message(message);
  } else {
    newIssueLocation = newIssue.newLocation().on(file)
        .at(file.newRange(startLine, startLineOffset, endLine, endLineOffset)).message(message);
  }
  newIssue.at(newIssueLocation);
  return this;
}
 
Example #23
Source File: FlowIssue.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Sets the primary location.
 * @param file
 * Reference to the file
 * @param message
 * Message that was raised
 * @return this flowIssue
 */
public FlowIssue setPrimaryLocation(InputFile file, CheckMessage message) {
  NewIssueLocation newIssueLocation;
  newIssueLocation = newIssue.newLocation().on(file).at(file.selectLine(message.getLine()))
      .message(message.getDefaultMessage());
  newIssue.at(newIssueLocation);
  return this;
}