Search in sources :

Example 1 with SEVERITY

use of se.bjurr.violations.lib.model.SEVERITY in project violations-lib by tomasbjerre.

the class PCLintParser method parseMisraViolation.

private void parseMisraViolation(final String line, final Set<Violation> violations) {
    final List<String> parts = getParts(line, "^([^\\(]+)\\(", "^([\\d]+)\\): ", "^(?:Error|Warning|Info|Note) [\\d]+: ([^\\[]*)", "^\\[(.*),", "(mandatory|required|advisory)\\]", "^(.*)$");
    if (parts.isEmpty()) {
        return;
    }
    final String filename = parts.get(0);
    final Integer lineNumber = parseInt(parts.get(1));
    final String severityString = parts.get(4);
    final SEVERITY severity = this.toMisraSeverity(severityString);
    final String rule = parts.get(3) + ", " + severityString;
    final String message = parts.get(2) + " " + parts.get(5);
    // 
    violations.add(// 
    violationBuilder().setParser(// 
    PCLINT).setStartLine(// 
    lineNumber).setFile(// 
    filename).setRule(// 
    rule).setSeverity(// 
    severity).setMessage(// 
    message).build());
}
Also used : SEVERITY(se.bjurr.violations.lib.model.SEVERITY)

Example 2 with SEVERITY

use of se.bjurr.violations.lib.model.SEVERITY in project violations-lib by tomasbjerre.

the class PMDParser method parseReportOutput.

@Override
public Set<Violation> parseReportOutput(final String content, final ViolationsLogger violationsLogger) throws Exception {
    final Set<Violation> violations = new TreeSet<>();
    try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
        final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
        String filename = null;
        Integer beginLine = null;
        Integer endLine = null;
        Optional<Integer> beginColumn = null;
        Optional<Integer> endColumn = null;
        String rule = null;
        Optional<String> ruleSetOpt = null;
        SEVERITY severity = null;
        String message = null;
        while (xmlr.hasNext()) {
            final int eventType = xmlr.next();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (xmlr.getLocalName().equalsIgnoreCase("file")) {
                    filename = getAttribute(xmlr, "name");
                }
                if (xmlr.getLocalName().equalsIgnoreCase("violation")) {
                    beginLine = getIntegerAttribute(xmlr, "beginline");
                    endLine = getIntegerAttribute(xmlr, "endline");
                    beginColumn = findIntegerAttribute(xmlr, "begincolumn");
                    endColumn = findIntegerAttribute(xmlr, "endcolumn");
                    rule = getAttribute(xmlr, "rule").trim();
                    ruleSetOpt = findAttribute(xmlr, "ruleset");
                    final Optional<String> externalInfoUrlOpt = findAttribute(xmlr, "externalInfoUrl");
                    final Integer priority = getIntegerAttribute(xmlr, "priority");
                    severity = this.toSeverity(priority);
                    final String violationContent = xmlr.getElementText();
                    message = violationContent + "\n\n" + ruleSetOpt.orElse("") + " " + externalInfoUrlOpt.orElse("");
                    final Violation violation = violationBuilder().setParser(PMD).setStartLine(beginLine).setEndLine(endLine).setColumn(beginColumn.orElse(null)).setEndColumn(endColumn.orElse(null)).setFile(filename).setSeverity(severity).setRule(rule).setCategory(ruleSetOpt.orElse(null)).setMessage(// 
                    message.trim()).build();
                    violations.add(violation);
                }
            }
        }
    }
    return violations;
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SEVERITY(se.bjurr.violations.lib.model.SEVERITY)

Example 3 with SEVERITY

use of se.bjurr.violations.lib.model.SEVERITY in project violations-lib by tomasbjerre.

the class CPPCheckParser method parseReportOutput.

@Override
public Set<Violation> parseReportOutput(final String reportContent, final ViolationsLogger violationsLogger) throws Exception {
    final Set<Violation> violations = new TreeSet<>();
    try (InputStream input = new ByteArrayInputStream(reportContent.getBytes(UTF_8))) {
        final XMLStreamReader xmlr = createXmlReader(input);
        SEVERITY severity = null;
        String msg = null;
        Optional<String> verbose = null;
        String id = null;
        int errorIndex = -1;
        boolean violationAddedFromError = false;
        String message = null;
        while (xmlr.hasNext()) {
            final int eventType = xmlr.next();
            if (eventType == XMLStreamConstants.END_ELEMENT) {
                if (xmlr.getLocalName().equalsIgnoreCase("error")) {
                    if (!violationAddedFromError) {
                        final Violation violation = // 
                        violationBuilder().setParser(// 
                        CPPCHECK).setStartLine(// 
                        0).setFile(// 
                        Violation.NO_FILE).setSeverity(// 
                        severity).setMessage(// 
                        message).setRule(// 
                        id).setGroup(// 
                        Integer.toString(errorIndex)).build();
                        violations.add(violation);
                    }
                }
            }
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (xmlr.getLocalName().equalsIgnoreCase("error")) {
                    violationAddedFromError = false;
                    errorIndex++;
                    final String severityStr = getAttribute(xmlr, "severity");
                    severity = this.toSeverity(severityStr);
                    msg = getAttribute(xmlr, "msg");
                    verbose = findAttribute(xmlr, "verbose");
                    id = getAttribute(xmlr, "id");
                    final Optional<String> resultFile = findAttribute(xmlr, "file");
                    final Optional<Integer> resultLine = findIntegerAttribute(xmlr, "line");
                    final Optional<Integer> resultColumn = findIntegerAttribute(xmlr, "column");
                    final Optional<String> resultInfo = findAttribute(xmlr, "info");
                    message = this.constructMessage(msg, verbose, resultInfo);
                    if (resultFile.isPresent() && resultLine.isPresent()) {
                        final Violation violation = // 
                        violationBuilder().setParser(// 
                        CPPCHECK).setStartLine(// 
                        resultLine.get()).setColumn(// 
                        resultColumn.orElse(0)).setFile(// 
                        resultFile.get()).setSeverity(// 
                        severity).setMessage(// 
                        message).setRule(// 
                        id).setGroup(// 
                        Integer.toString(errorIndex)).build();
                        violations.add(violation);
                        violationAddedFromError = true;
                    }
                } else if (xmlr.getLocalName().equalsIgnoreCase("location")) {
                    final Integer line = getIntegerAttribute(xmlr, "line");
                    final Optional<Integer> column = findIntegerAttribute(xmlr, "column");
                    final Optional<String> info = findAttribute(xmlr, "info");
                    message = this.constructMessage(msg, verbose, info);
                    final String file = getAttribute(xmlr, "file");
                    final Violation v = // 
                    violationBuilder().setParser(// 
                    CPPCHECK).setMessage(// 
                    message).setStartLine(// 
                    line).setColumn(// 
                    column.orElse(null)).setFile(// 
                    file).setSeverity(// 
                    severity).setRule(// 
                    id).setGroup(// 
                    Integer.toString(errorIndex)).build();
                    violations.add(v);
                    violationAddedFromError = true;
                }
            }
        }
    }
    return violations;
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) XMLStreamReader(javax.xml.stream.XMLStreamReader) Optional(java.util.Optional) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) SEVERITY(se.bjurr.violations.lib.model.SEVERITY)

Example 4 with SEVERITY

use of se.bjurr.violations.lib.model.SEVERITY in project violations-lib by tomasbjerre.

the class FxCopParser method parseReportOutput.

@Override
public Set<Violation> parseReportOutput(final String string, final ViolationsLogger violationsLogger) throws Exception {
    final Set<Violation> violations = new TreeSet<>();
    try (InputStream input = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))) {
        final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
        String targetName = null;
        String typeName = null;
        String classname = null;
        while (xmlr.hasNext()) {
            final int eventType = xmlr.next();
            if (eventType == START_ELEMENT) {
                if (xmlr.getLocalName().equalsIgnoreCase("Target")) {
                    targetName = getAttribute(xmlr, "Name").replaceAll("\\\\", "/");
                }
                if (xmlr.getLocalName().equalsIgnoreCase("Message")) {
                    typeName = getAttribute(xmlr, "TypeName");
                }
                if (xmlr.getLocalName().equalsIgnoreCase("Type")) {
                    classname = getAttribute(xmlr, "Name");
                }
                if (xmlr.getLocalName().equalsIgnoreCase("Issue")) {
                    final String level = getAttribute(xmlr, "Level");
                    final Optional<String> path = ViolationParserUtils.findAttribute(xmlr, "Path");
                    if (!path.isPresent()) {
                        violationsLogger.log(FINE, "Ignoring project level issue");
                        continue;
                    }
                    final String fileName = getAttribute(xmlr, "File");
                    final Integer line = getIntegerAttribute(xmlr, "Line");
                    final String message = xmlr.getElementText().replaceAll("\\s+", " ");
                    final String filename = path.get() + "/" + fileName;
                    final SEVERITY severity = this.toSeverity(level);
                    // 
                    violations.add(// 
                    violationBuilder().setParser(// 
                    FXCOP).setMessage(// 
                    message).setFile(// 
                    filename).setStartLine(// 
                    line).setRule(// 
                    typeName).setSeverity(// 
                    severity).setSource(// 
                    classname).setSpecific("TARGET_NAME", // 
                    targetName).build());
                }
            }
        }
    }
    return violations;
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SEVERITY(se.bjurr.violations.lib.model.SEVERITY)

Example 5 with SEVERITY

use of se.bjurr.violations.lib.model.SEVERITY in project violation-comments-to-gitlab-plugin by jenkinsci.

the class JvctglPerformer method doPerform.

@VisibleForTesting
public static void doPerform(final ViolationsToGitLabConfig config, final String apiToken, final File workspace, final TaskListener listener, final String proxyUser, final String proxyPassword) throws MalformedURLException {
    if (config.getMergeRequestIid() == null) {
        listener.getLogger().println("No merge request id defined, will not send violation comments. " + "\n\nPossible cause might be: mergeRequestId changed in version 2.0 and is now mergeRequestIid. " + "If you just updated the plugin, mergeRequestIid should have the same value that you previously used for mergeRequestId. " + "It is named wrong in the Java GitLab API and that wrong name has spread to this API." + "\n\n");
        return;
    }
    final ViolationsLogger violationsLogger = new ViolationsLogger() {

        @Override
        public void log(final Level level, final String string) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            if (level != Level.FINE) {
                listener.getLogger().println(level + " " + string);
            }
        }

        @Override
        public void log(final Level level, final String string, final Throwable t) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            final StringWriter sw = new StringWriter();
            t.printStackTrace(new PrintWriter(sw));
            listener.getLogger().println(level + " " + string + "\n" + sw.toString());
        }
    };
    final Set<Violation> allParsedViolations = new TreeSet<>();
    for (final ViolationConfig violationConfig : config.getViolationConfigs()) {
        if (!isNullOrEmpty(violationConfig.getPattern())) {
            Set<Violation> parsedViolations = violationsApi().withViolationsLogger(// 
            violationsLogger).findAll(// 
            violationConfig.getParser()).withReporter(// 
            violationConfig.getReporter()).inFolder(// 
            workspace.getAbsolutePath()).withPattern(// 
            violationConfig.getPattern()).violations();
            final SEVERITY minSeverity = config.getMinSeverity();
            if (minSeverity != null) {
                parsedViolations = Filtering.withAtLEastSeverity(parsedViolations, minSeverity);
            }
            allParsedViolations.addAll(parsedViolations);
            listener.getLogger().println("Found " + parsedViolations.size() + " violations from " + violationConfig + ".");
        }
    }
    final String hostUrl = config.getGitLabUrl();
    final String projectId = config.getProjectId();
    final String mergeRequestIid = config.getMergeRequestIid();
    listener.getLogger().println("Will comment PR " + hostUrl + " " + projectId + " " + mergeRequestIid);
    try {
        final TokenType tokenType = config.getApiTokenPrivate() ? TokenType.PRIVATE : TokenType.ACCESS;
        final Integer mergeRequestIidInteger = Integer.parseInt(mergeRequestIid);
        final boolean shouldKeepOldComments = config.getKeepOldComments();
        final boolean shouldSetWIP = config.getShouldSetWip();
        final String commentTemplate = config.getCommentTemplate();
        violationCommentsToGitLabApi().setProxyServer(config.getProxyUri()).setProxyUser(proxyUser).setProxyPassword(proxyPassword).setHostUrl(hostUrl).setProjectId(projectId).setMergeRequestIid(mergeRequestIidInteger).setApiToken(apiToken).setTokenType(tokenType).setCommentOnlyChangedContent(config.getCommentOnlyChangedContent()).setCommentOnlyChangedContentContext(config.getCommentOnlyChangedContentContext()).withShouldCommentOnlyChangedFiles(config.getCommentOnlyChangedFiles()).setCreateCommentWithAllSingleFileComments(config.getCreateCommentWithAllSingleFileComments()).setCreateSingleFileComments(config.getCreateSingleFileComments()).setIgnoreCertificateErrors(// 
        config.getIgnoreCertificateErrors()).setViolations(// 
        allParsedViolations).setShouldKeepOldComments(// 
        shouldKeepOldComments).setShouldSetWIP(// 
        shouldSetWIP).setCommentTemplate(// 
        commentTemplate).setViolationsLogger(// 
        violationsLogger).setMaxNumberOfViolations(// 
        config.getMaxNumberOfViolations()).toPullRequest();
    } catch (final Exception e) {
        Logger.getLogger(JvctglPerformer.class.getName()).log(SEVERE, "", e);
        final StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        listener.getLogger().println(sw.toString());
    }
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) ViolationConfig(org.jenkinsci.plugins.jvctgl.config.ViolationConfig) ViolationsLogger(se.bjurr.violations.lib.ViolationsLogger) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) TokenType(org.gitlab4j.api.Constants.TokenType) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Level(java.util.logging.Level) SEVERITY(se.bjurr.violations.lib.model.SEVERITY) PrintWriter(java.io.PrintWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SEVERITY (se.bjurr.violations.lib.model.SEVERITY)12 Violation (se.bjurr.violations.lib.model.Violation)10 TreeSet (java.util.TreeSet)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Level (java.util.logging.Level)2 Matcher (java.util.regex.Matcher)2 ViolationsLogger (se.bjurr.violations.lib.ViolationsLogger)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1