Search in sources :

Example 26 with FullFileModel

use of hudson.plugins.violations.model.FullFileModel in project violations-plugin by jenkinsci.

the class GendarmeParser method parseViolations.

private void parseViolations(List<Element> ruleElements) {
    // searching source files
    AbsoluteFileFinder finder = new AbsoluteFileFinder();
    // add the project path to the search source
    finder.addSourcePath(this.projectPath.getPath());
    // if there's additional paths, add them too
    if (this.sourcePaths != null) {
        finder.addSourcePaths(this.sourcePaths);
    }
    for (Element ruleElement : ruleElements) {
        String ruleName = ruleElement.getAttribute("Name");
        GendarmeRule rule = this.rules.get(ruleName);
        String problem = ruleElement.getElementsByTagName("problem").item(0).getTextContent();
        String solution = ruleElement.getElementsByTagName("solution").item(0).getTextContent();
        List<Element> targetElements = XmlElementUtil.getNamedChildElements(ruleElement, "target");
        for (Element targetElement : targetElements) {
            // String targetName = targetElement.getAttribute("Name");
            String targetAssembly = targetElement.getAttribute("Assembly");
            DotNetAssembly assembly = new DotNetAssembly(targetAssembly);
            List<Element> defectElements = XmlElementUtil.getNamedChildElements(targetElement, "defect");
            for (Element defectElement : defectElements) {
                String severityString = defectElement.getAttribute("Severity");
                String source = defectElement.getAttribute("Source");
                // String location = defectElement.getAttribute("Location");
                String confidence = defectElement.getAttribute("Confidence");
                String filePath = "";
                String fileName = "";
                int line = 0;
                if (rule.getType() != GendarmeRuleType.Assembly) {
                    Pattern pattern = Pattern.compile("^(.*)\\(.([0-9]*)\\)$");
                    Matcher matcher = pattern.matcher(source);
                    logger.info("matcher.groupCount() : " + matcher.groupCount());
                    logger.info("matcher.matches() : " + matcher.matches());
                    logger.info("source : " + source);
                    if (matcher.matches()) {
                        for (int cpt = 0; cpt < matcher.groupCount(); cpt++) {
                            logger.info("group(" + ((int) (cpt + 1)) + "): " + matcher.group(cpt + 1));
                        }
                    }
                    if (matcher.matches()) {
                        String fullPath = matcher.group(1);
                        File sourceFile = new File(fullPath);
                        fileName = sourceFile.getName();
                        filePath = sourceFile.getParent();
                        line = Integer.parseInt(matcher.group(2));
                    }
                }
                // create the violation
                Violation violation = new Violation();
                // construct the error message
                StringBuilder messageBuilder = new StringBuilder();
                if (rule.getUrl() != null) {
                    messageBuilder.append("<a href=\"").append(rule.getUrl().toString()).append("\">");
                    messageBuilder.append(rule.getName());
                    messageBuilder.append("</a>");
                } else {
                    messageBuilder.append(rule.getName());
                }
                messageBuilder.append(" - ").append(problem).append("<br/>");
                messageBuilder.append("Solution: ").append(solution).append("<br/>");
                messageBuilder.append("Confidence: ").append(confidence);
                violation.setMessage(messageBuilder.toString());
                violation.setPopupMessage(problem);
                // construct the severity
                if (severityString.equals("Low")) {
                    violation.setSeverityLevel(Severity.LOW_VALUE);
                    violation.setSeverity(Severity.LOW);
                } else if (severityString.equals("Medium")) {
                    violation.setSeverityLevel(Severity.MEDIUM_VALUE);
                    violation.setSeverity(Severity.MEDIUM);
                } else if (severityString.equals("High")) {
                    violation.setSeverityLevel(Severity.HIGH_VALUE);
                    violation.setSeverity(Severity.HIGH);
                } else if (severityString.equals("Critical")) {
                    violation.setSeverityLevel(Severity.HIGH_VALUE);
                    violation.setSeverity(Severity.HIGH);
                } else {
                    violation.setSeverityLevel(Severity.MEDIUM_VALUE);
                    violation.setSeverity(Severity.MEDIUM);
                }
                violation.setType(TYPE_NAME);
                violation.setSource(rule.getName());
                // try to get the file
                // TODO : test it with Linux Master => Windows Slave node. Unix/Windows path could be a problem.
                FullFileModel fileModel;
                if ((filePath.length() > 0) && (fileName.length() > 0)) {
                    violation.setLine(line);
                    // get the display name of the file
                    String displayName = ParseUtil.resolveAbsoluteName(this.projectPath, filePath + File.separatorChar + fileName);
                    // try to get the source file, add it if not already
                    // exists
                    fileModel = model.getFileModel(displayName);
                    if (fileModel.getSourceFile() == null) {
                        finder.addSourcePath(filePath);
                        File sourceFile = finder.getFileForName(fileName);
                        if (sourceFile != null && sourceFile.exists()) {
                            fileModel.setSourceFile(sourceFile);
                            fileModel.setLastModified(sourceFile.lastModified());
                            logger.info("fileModel.getSourceFile() : " + fileModel.getSourceFile().getAbsolutePath());
                        } else {
                            logger.info("sourceFile.exists()==false: " + filePath + "," + fileName);
                        }
                    } else {
                        logger.info("fileModel.getSourceFile() != null: " + displayName);
                    }
                } else {
                    // if there's no source files, just put the assembly
                    // name
                    fileModel = model.getFileModel(assembly.getName() + ".dll");
                    logger.info("fileModel.getSourceFile() : " + (fileModel.getSourceFile() == null ? "null" : fileModel.getSourceFile().getAbsolutePath()));
                }
                logger.info("fileModel.getDisplayName() : " + fileModel.getDisplayName());
                logger.info("reportParentFile : " + reportParentFile);
                logger.info("fileName : " + fileName);
                logger.info("filePath : " + filePath);
                // Add the violation to the model
                fileModel.addViolation(violation);
            }
        }
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FullFileModel(hudson.plugins.violations.model.FullFileModel) Element(org.w3c.dom.Element) AbsoluteFileFinder(hudson.plugins.violations.util.AbsoluteFileFinder) File(java.io.File)

Example 27 with FullFileModel

use of hudson.plugins.violations.model.FullFileModel in project violations-plugin by jenkinsci.

the class Pep8Parser method parseLine.

/**
 * Parses a PEP 8 line and adding a violation if regex
 * @param model build model to add violations to
 * @param line the line in the file.
 * @param projectPath the path to use to resolve the file.
 */
public void parseLine(FullBuildModel model, String line, File projectPath) {
    Pep8Violation pep8Violation = getPep8Violation(line);
    if (pep8Violation != null) {
        Violation violation = new Violation();
        violation.setType("pep8");
        violation.setLine(pep8Violation.getLineStr());
        violation.setMessage(pep8Violation.getMessage());
        violation.setSource(pep8Violation.getViolationId());
        setServerityLevel(violation, pep8Violation.getViolationId());
        FullFileModel fileModel = getFileModel(model, pep8Violation.getFileName(), absoluteFileFinder.getFileForName(pep8Violation.getFileName()));
        fileModel.addViolation(violation);
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) FullFileModel(hudson.plugins.violations.model.FullFileModel)

Example 28 with FullFileModel

use of hudson.plugins.violations.model.FullFileModel in project violations-plugin by jenkinsci.

the class PerlCriticParser method parseLine.

/**
 * Parses a Perl::Critic line and adds a Violation if this.pattern matches
 * @param model build model to which violations should be added
 * @param line the line in the source file on which violation occurred
 * @param projectPath the project path to use to resolve the source file
 */
public void parseLine(FullBuildModel model, String line, File projectPath) {
    PerlCriticViolation perlCriticViolation = getPerlCriticViolation(line);
    if (perlCriticViolation == null) {
        return;
    }
    String fileName = perlCriticViolation.getFileName();
    FullFileModel fileModel = this.getFileModel(model, fileName);
    fileModel.addViolation(perlCriticViolation);
}
Also used : FullFileModel(hudson.plugins.violations.model.FullFileModel)

Example 29 with FullFileModel

use of hudson.plugins.violations.model.FullFileModel in project violations-plugin by jenkinsci.

the class SimianParser method parseSetElement.

/**
 * Parses the <set/> block and adds the found duplication violations.
 * @throws IOException thrown by XmlPullParser
 * @throws XmlPullParserException thrown by XmlPullParser
 */
private void parseSetElement() throws IOException, XmlPullParserException {
    List<DuplicationBlock> blocks = new ArrayList<DuplicationBlock>();
    while (skipToTag("block")) {
        blocks.add(parseBlockElement());
        getParser().next();
        endElement();
    }
    for (DuplicationBlock block : blocks) {
        List<DuplicationBlock> otherBlocks = new ArrayList<DuplicationBlock>(blocks);
        otherBlocks.remove(block);
        Violation violation = createViolation(block);
        setViolationMessages(violation, block, otherBlocks);
        FullFileModel fileModel = getFileModel(block.absolutePath);
        fileModel.addViolation(violation);
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) FullFileModel(hudson.plugins.violations.model.FullFileModel) ArrayList(java.util.ArrayList)

Example 30 with FullFileModel

use of hudson.plugins.violations.model.FullFileModel in project violations-plugin by jenkinsci.

the class StyleCopParser method parseViolations.

/**
 * Parse the Violation tag and add it to the build model
 * @param elements list of Violation tags
 */
private void parseViolations(List<Element> elements) throws IOException {
    for (Element element : elements) {
        Violation violation = new Violation();
        violation.setLine(getString(element, "LineNumber"));
        violation.setMessage(element.getTextContent() + " (" + getString(element, "RuleId") + ")");
        violation.setSeverity(Severity.MEDIUM);
        violation.setSeverityLevel(Severity.MEDIUM_VALUE);
        violation.setType(TYPE_NAME);
        String temp = getString(element, "RuleNamespace");
        int i = temp.lastIndexOf('.');
        if (i != -1) {
            violation.setSource(temp.substring(i));
        } else {
            violation.setSource(getString(element, "RuleId"));
        }
        // Add the violation to the model
        String displayName = new FilePath(reportParentFile).child(getString(element, "Source")).getRemote();
        displayName = ParseUtil.resolveAbsoluteName(reportParentFile, displayName);
        /* TODO: apply heuristics to fine the source.

                StyleCop just puts whatever path representation it gets from MSBuild into @Source,
                which can be relative (to the current directory MSBuild run in, which we won't know.)
                In such a case, there's really no reliable way for us to deterministically figure out
                where the source code is in the source tree.

                The resolution against 'reportParentFile' is quite arbitrary in that sense and only
                works if the report is created into the same directory as the MSBuild current directory,
                but it is a backward compatible behaviour.
             */
        FullFileModel fileModel = model.getFileModel(displayName);
        fileModel.addViolation(violation);
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) FilePath(hudson.FilePath) FullFileModel(hudson.plugins.violations.model.FullFileModel) Element(org.w3c.dom.Element)

Aggregations

FullFileModel (hudson.plugins.violations.model.FullFileModel)32 File (java.io.File)15 Violation (hudson.plugins.violations.model.Violation)12 ViolationsParserTest (hudson.plugins.violations.ViolationsParserTest)3 FullBuildModel (hudson.plugins.violations.model.FullBuildModel)3 Test (org.junit.Test)3 Element (org.w3c.dom.Element)3 AbsoluteFileFinder (hudson.plugins.violations.util.AbsoluteFileFinder)2 FilePath (hudson.FilePath)1 IOException2 (hudson.util.IOException2)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1