Skip to content

Commit

Permalink
LineNumbers displaying on Issue mulesoft-catalyst#15
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-stefan-cordes committed Dec 3, 2020
1 parent 0fb9eb7 commit ee74852
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<!-- mandatory scope -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-xml-parsing</artifactId>
<version>1.12.0.632</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public interface PARAMS {
String CATEGORY = "category";
String SCOPE = "scope";
String XPATH = "xpath-expression";
String XPATH_LOCATION_HINT = "xpath-location-hint";
}

@Override
Expand Down Expand Up @@ -97,8 +98,13 @@ private void addRuleTemplate(NewRepository repository, String language) {
.setType(RuleParamType.STRING);
x1Rule.createParam(PARAMS.XPATH).setDescription(prop.getProperty("rule.template.parameter.xpath"))
.setType(RuleParamType.STRING);
x1Rule.createParam(PARAMS.XPATH_LOCATION_HINT).setDescription(prop.getProperty("rule.template.parameter.xpathlocationhint"))
.setType(RuleParamType.STRING);
x1Rule.createParam(PARAMS.SCOPE).setDescription(prop.getProperty("rule.template.parameter.scope"))
.setType(RuleParamType.STRING);

logger.info("addRuleTemplate x1Rule="+x1Rule);

}

private void addRule(NewRepository repository, Ruleset ruleset,
Expand All @@ -113,6 +119,8 @@ private void addRule(NewRepository repository, Ruleset ruleset,
x1Rule.addTags(language);
x1Rule.createParam(PARAMS.CATEGORY).setDefaultValue(ruleset.getCategory()).setType(RuleParamType.STRING);
x1Rule.createParam(PARAMS.XPATH).setDefaultValue(rule.getValue()).setType(RuleParamType.STRING);
logger.info("LocationHint="+rule.getLocationHint()+" for "+rule.getName());
x1Rule.createParam(PARAMS.XPATH_LOCATION_HINT).setDefaultValue(rule.getLocationHint()).setType(RuleParamType.STRING);
if (rule.getApplies() != null) {
x1Rule.createParam(PARAMS.SCOPE).setDefaultValue(rule.getApplies()).setType(RuleParamType.STRING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void validate(XPathProcessor xpathValidator, Map<RuleKey, List<NewIssue>>
String ruleId = rule.ruleKey().toString();
boolean valid = xpathValidator.processXPath(rule.param(MuleRulesDefinition.PARAMS.XPATH).trim(),
rootElement, Boolean.class).booleanValue();
logger.info("Validation Result: " + valid + " : File: " + t.filename() + " :Rule:" + rule.ruleKey());
logger.info("Validation Result: " + valid + " : File: " + t.filename() + " :Rule:" + rule.ruleKey()+" internalKey="+rule.internalKey());
if (!valid && !valids.contains(ruleId) && !issues.containsKey(rule.ruleKey())) {
NewIssue newIssue = context.newIssue().forRule(rule.ruleKey());
NewIssueLocation primaryLocation = newIssue.newLocation().on(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.sensor.SensorContext;
Expand All @@ -18,6 +23,10 @@
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import org.sonarsource.analyzer.commons.xml.XmlFile;
import org.sonarsource.analyzer.commons.xml.XmlTextRange;
import org.w3c.dom.Node;

import com.mulesoft.services.tools.sonarqube.rule.MuleRulesDefinition;
import com.mulesoft.services.xpath.XPathProcessor;

Expand All @@ -35,8 +44,44 @@ public void validate(XPathProcessor xpathValidator, Map<RuleKey, List<NewIssue>>
rootElement, Boolean.class).booleanValue();
logger.info("Validation Result: " + valid + " : File: " + t.filename() + " :Rule:" + rule.ruleKey());
if (!valid) {

XmlTextRange textRange;
try {
// see org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck.reportIssue(Node, String)
SonarXmlCheckHelper tempSonarXmlCheckHelper = new SonarXmlCheckHelper();
XmlFile xmlFile = XmlFile.create(t);
tempSonarXmlCheckHelper.scanFile(context, rule.ruleKey(), xmlFile); // just to fill the properties
String locationFindingXPath = rule.param(MuleRulesDefinition.PARAMS.XPATH_LOCATION_HINT).trim();
if (locationFindingXPath == null || locationFindingXPath.length()==0) {
logger.info("No locationFindingXPath:params="+rule.params());
textRange = null;
} else {
Node locationElement = (Node)XPathFactory.newInstance().newXPath().compile(locationFindingXPath).evaluate(xmlFile.getDocument().getFirstChild(), XPathConstants.NODE);
if (locationElement == null) {
textRange= null;
logger.warn("Did not find node for "+locationFindingXPath);
} else {
textRange = XmlFile.nodeLocation(locationElement);
logger.info("Found textRange="+textRange);
}
}
} catch (RuntimeException | XPathExpressionException e) {
logger.error("Ignore",e);
textRange= null;
}

NewIssue newIssue = context.newIssue().forRule(rule.ruleKey());
NewIssueLocation primaryLocation = newIssue.newLocation().on(t);
NewIssueLocation primaryLocation;
if (textRange == null) {
primaryLocation = newIssue.newLocation().on(t);
} else {
primaryLocation = newIssue.newLocation().on(t) .at(t.newRange(
textRange.getStartLine(),
textRange.getStartColumn(),
textRange.getEndLine(),
textRange.getEndColumn()));
}

newIssue.at(primaryLocation);
addIssue(issues, rule, newIssue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mulesoft.services.tools.sonarqube.rule.scope;

import org.sonarsource.analyzer.commons.xml.XmlFile;
import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck;

/**
*
*/
public class SonarXmlCheckHelper extends SonarXmlCheck {

/**
*
*/
@Override
public void scanFile(XmlFile aFile) {
// nothing to do
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class Rule {
protected String applies;
@XmlAttribute(name = "type")
protected String type;
@XmlAttribute(name = "locationHint")
private String locationHint;

/**
* Gets the value of the value property.
Expand Down Expand Up @@ -269,4 +271,18 @@ public void setType(String value) {
this.type = value;
}

/**
*
*/
public String getLocationHint() {
return locationHint;
}

/**
* @see #locationHint
*/
public void setLocationHint(String aLocationHint) {
locationHint = aLocationHint;
}

}
1 change: 1 addition & 0 deletions src/main/resources/mule3.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rule.template.name=Track issues on Mule 3
rule.template.description=Rule Template for mule 3
rule.template.parameter.scope=The scope of the rules. It should either file or application
rule.template.parameter.xpath=XPath expression to be evaluated
rule.template.parameter.xpathlocationhint=XPath expression to find the node
rule.template.parameter.category=Category of the rule


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mule4.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rule.template.name=Track issues on Mule 4
rule.template.description=Rule Template for mule 4
rule.template.parameter.scope=The scope of the rules. It should either file or application
rule.template.parameter.xpath=XPath expression to be evaluated
rule.template.parameter.xpathlocationhint=XPath expression to find the node
rule.template.parameter.category=Category of the rule


Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/rules-3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@
</rule>
<rule id="10" name="HTTP Listener should use HTTPS protocol"
description="HTTP Listener should use HTTPS protocol"
severity="MAJOR" type="vulnerability">
severity="MAJOR" type="vulnerability"
locationHint="//*[local-name()='listener-config']">
count(//mule:mule/http:listener-config)=0
or
//mule:mule/http:listener-config/@protocol='HTTPS'
</rule>
<rule id="11"
name="HTTP Listener should use a specific port property"
description="HTTP Listener should use a specific port property"
severity="MAJOR" type="code_smell">
severity="MAJOR" type="code_smell"
locationHint="//*[local-name()='listener-config']">
count(//mule:mule/http:listener-config)=0
or
//mule:mule/http:listener-config/@port='${https.port}' or
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/rules-4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@
</rule>
<rule id="7" name="HTTP Listener should use HTTPS protocol"
description="HTTP Listener should use HTTPS protocol"
severity="MAJOR" type="vulnerability">
severity="MAJOR" type="vulnerability"
locationHint="//*[local-name()='listener-config']">
count(//mule:mule/http:listener-config)=0
or
//mule:mule/http:listener-config/http:listener-connection/@protocol='HTTPS'
</rule>
<rule id="8"
name="HTTP Listener should use a specific port property"
description="HTTP Listener should use a specific port property"
severity="MAJOR" type="code_smell">
severity="MAJOR" type="code_smell"
locationHint="//*[local-name()='listener-config']">
count(//mule:mule/http:listener-config)=0
or
//mule:mule/http:listener-config/http:listener-connection/@port='${https.port}'
Expand Down

0 comments on commit ee74852

Please sign in to comment.