Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIP-414 PR #24

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.avioconsulting.mule.linter.model

import com.avioconsulting.mule.linter.model.pom.PomFile

class Application {

static final String APPLICATION_DOES_NOT_EXIST = 'Application directory does not exists: '
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.avioconsulting.mule.linter.model

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult
import groovy.xml.slurpersupport.Node

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.avioconsulting.mule.linter.model

import com.avioconsulting.mule.linter.parser.MuleXmlParser

class MuleComponent {

private final String componentName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

/**
* Example munit-maven-plugin
*
* <plugin>
* <groupId>com.mulesoft.munit.tools</groupId>
* <artifactId>munit-maven-plugin</artifactId>
* <version>${munit.version}</version>
* <executions>
* <execution>
* <id>test</id>
* <phase>test</phase>
* <goals>
* <goal>test</goal>
* <goal>coverage-report</goal>
* </goals>
* </execution>
* </executions>
* <configuration>
* <runtimeProduct>MULE_EE</runtimeProduct>
* <runtimeVersion>${app.runtime}</runtimeVersion>
* <coverage>
* <runCoverage>true</runCoverage>
* <failBuild>true</failBuild>
* <requiredApplicationCoverage>80</requiredApplicationCoverage>
* <requiredResourceCoverage>80</requiredResourceCoverage>
* <requiredFlowCoverage>80</requiredFlowCoverage>
* <ignoreFiles>
* <ignoreFile>nextep-salesforce-sapi.xml</ignoreFile>
* <ignoreFile>global-config.xml</ignoreFile>
* <ignoreFile>error-handler.xml</ignoreFile>
* </ignoreFiles>
* <formats>
* <format>console</format>
* <format>html</format>
* </formats>
* </coverage>
* </configuration>
* </plugin>
*/
class MunitMavenPlugin extends PomPlugin {

static final String GROUP_ID = 'com.mulesoft.munit.tools'
static final String ARTIFACT_ID = 'munit-maven-plugin'

MunitMavenPlugin(GPathResult pluginXml, PomFile pomFile) {
super(pluginXml, pomFile)
}

List<String> getIgnoreFiles() {
List<String> ignoreFiles = []
pluginXml.configuration.coverage.ignoreFiles.ignoreFile.each {
ignoreFiles.add(it.text())
}
return ignoreFiles
}

Integer getIgnoreFilesLineNo() {
MuleXmlParser.getNodeLineNumber(pluginXml.configuration.coverage.ignoreFiles)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.avioconsulting.mule.linter.model
package com.avioconsulting.mule.linter.model.pom

class PomProperty {
class PomElement {

private String name
private String value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.avioconsulting.mule.linter.model
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.model.ProjectFile
import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class PomFile extends ProjectFile {
Expand Down Expand Up @@ -42,13 +44,13 @@ class PomFile extends ProjectFile {
return file.absolutePath
}

PomProperty getPomProperty(String propertyName) throws IllegalArgumentException {
PomElement getPomProperty(String propertyName) throws IllegalArgumentException {
GPathResult p = pomProperties[propertyName] as GPathResult
if (p == null) {
throw new IllegalArgumentException('Property doesn\'t exist')
}

PomProperty prop = new PomProperty()
PomElement prop = new PomElement()
prop.name = propertyName
prop.value = p.text()
prop.lineNo = parser.getNodeLineNumber(p)
Expand All @@ -59,6 +61,23 @@ class PomFile extends ProjectFile {
return parser.getNodeLineNumber(pomProperties)
}

PomPlugin getPlugin(String groupId, String artifactId) {
PomPlugin plugin
GPathResult pluginPath = pomXml.build.plugins.plugin.find {
it.groupId == groupId && it.artifactId == artifactId
} as GPathResult

if (pluginPath != null && pluginPath.size() > 0) {
plugin = new PomPlugin(pluginPath, this)
}
return plugin
}

MunitMavenPlugin getMunitPlugin() {
PomPlugin pp = getPlugin(MunitMavenPlugin.GROUP_ID, MunitMavenPlugin.ARTIFACT_ID)
return pp == null ? null : new MunitMavenPlugin(pp.pluginXml, this)
}

private GPathResult getPomProperties() {
kkingavio marked this conversation as resolved.
Show resolved Hide resolved
return pomXml[PROPERTIES] as GPathResult
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class PomPlugin {

String groupId
String artifactId
String version
Integer lineNo
PomFile pomFile
GPathResult pluginXml

PomPlugin(GPathResult pluginXml, PomFile pomFile) {
this.pluginXml = pluginXml
this.groupId = pluginXml.groupId as String
this.artifactId = pluginXml.artifactId as String
this.lineNo = MuleXmlParser.getNodeLineNumber(pluginXml)
this.pomFile = pomFile
this.version = isExpression(pluginXml.version as String) ?
resolveExpression(pluginXml.version as String) : pluginXml.version
}

PomElement getConfigProperty(String propertyName) {
PomElement pElement = null
pluginXml.configuration.depthFirst().each {
if (it.name() == propertyName) {
pElement = new PomElement()
pElement.name = propertyName
pElement.value = it.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(it)
}
}
return pElement
}

private Boolean isExpression(String expression) {
expression.startsWith('${')
}

private String variableName(String expression) {
expression.takeAfter('${').takeBefore('}')
}

private String resolveExpression(String expression) {
try {
return pomFile.getPomProperty(variableName(expression)).value
} catch (IllegalArgumentException iae) {
return null
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.avioconsulting.mule.linter.model
package com.avioconsulting.mule.linter.parser

import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.GPathResult
Expand Down Expand Up @@ -34,7 +34,8 @@ class MuleXmlParser extends XmlSlurper {
super.startElement(uri, localName, qName, newAttrs)
}

Integer getNodeLineNumber(GPathResult node) {
@SuppressWarnings('StaticMethodsBeforeInstanceMethods')
static Integer getNodeLineNumber(GPathResult node) {
return Integer.valueOf(String.valueOf(node["@${START_LINE_NO_NAMESPACE_PREFIX}:${START_LINE_NO_ATTRIBUTE}"]))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.avioconsulting.mule.linter.rule.cicd

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.JenkinsFile
import com.avioconsulting.mule.linter.model.PomFile
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.avioconsulting.mule.linter.rule.pom

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation
import com.avioconsulting.mule.linter.model.pom.MunitMavenPlugin
import com.avioconsulting.mule.linter.model.pom.PomElement
import com.avioconsulting.mule.linter.model.pom.PomFile

class MunitMavenPluginAttributesRule extends Rule {

static final String RULE_ID = 'MUNIT_MAVEN_PLUGIN_ATTRIBUTES'
static final String RULE_NAME = 'Munit Maven plugins attribute values'
static final String RULE_MESSAGE = 'Munit Maven plugin has incorrect or missing configuration coverage value '
static final String RULE_MESSAGE_MISSING = 'Munit Maven plugin is missing element '
static final Map<String, String> COVERAGE_DEFAULTS = ['runCoverage':'true',
'failBuild':'true',
'requiredApplicationCoverage':'80',
'requiredResourceCoverage':'80',
'requiredFlowCoverage':'80']
private static final String IGNORE_FILES = 'ignoreFiles'
Map<String, String> coverageAttributeMap
List<String> ignoreFiles = []

MunitMavenPluginAttributesRule() {
this([:], true, [])
}

MunitMavenPluginAttributesRule(List<String> ignoreFiles) {
this([:], true, ignoreFiles)
}

MunitMavenPluginAttributesRule(Map<String,String> coverageAttributeMap,
Boolean includeDefaults) {
this(coverageAttributeMap, includeDefaults, [])
}

MunitMavenPluginAttributesRule(Map<String,String> coverageAttributeMap,
Boolean includeDefaults, List<String> ignoreFiles) {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
this.coverageAttributeMap = includeDefaults ? COVERAGE_DEFAULTS + coverageAttributeMap : coverageAttributeMap
this.ignoreFiles = ignoreFiles
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []
MunitMavenPlugin plugin = application.pomFile.munitPlugin
if (plugin != null) {
coverageAttributeMap.each { key, val ->
PomElement pe = plugin.getConfigProperty(key)
if (pe?.value != val) {
violations.add(new RuleViolation(this, PomFile.POM_XML,
pe == null ? plugin.lineNo : pe.lineNo, RULE_MESSAGE + key + '|' + val))
}
}
if (ignoreFiles.size() > 0) {
List<String> diff = ignoreFiles - plugin.getIgnoreFiles()
diff.each {
violations.add(new RuleViolation(this, PomFile.POM_XML,
plugin.getIgnoreFilesLineNo(), RULE_MESSAGE_MISSING + 'ignoreFile|' + it ))
}
}
} else {
violations.add(new RuleViolation(this, PomFile.POM_XML, 0, 'Missing munit-maven-plugin'))
}
return violations
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.avioconsulting.mule.linter.rule.pom

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.PomFile
import com.avioconsulting.mule.linter.model.pom.PomFile
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.avioconsulting.mule.linter.rule.pom

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.PomProperty
import com.avioconsulting.mule.linter.model.pom.PomElement
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation

Expand Down Expand Up @@ -33,7 +33,7 @@ class PomPropertyValueRule extends Rule {
List<RuleViolation> violations = []

try {
PomProperty pomProperty = app.pomFile.getPomProperty(propertyName)
PomElement pomProperty = app.pomFile.getPomProperty(propertyName)
if (!pomProperty.value.equalsIgnoreCase(propertyValue)) {
violations.add(new RuleViolation(this, app.pomFile.path,
pomProperty.lineNo, pomProperty.name + RULE_VIOLATION_MESSAGE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.avioconsulting.mule.linter

import com.avioconsulting.mule.linter.model.JenkinsFile
import com.avioconsulting.mule.linter.model.MuleArtifact
import com.avioconsulting.mule.linter.model.PomFile
import com.avioconsulting.mule.linter.model.pom.PomFile
import com.avioconsulting.mule.linter.model.GitIgnoreFile

@SuppressWarnings('StaticFieldsBeforeInstanceFields')
@SuppressWarnings(['StaticFieldsBeforeInstanceFields', 'BuilderMethodWithSideEffects', 'FactoryMethodName'])
class TestApplication {

static final String SAMPLE_APP_NAME = 'SampleMuleApp'
Expand All @@ -21,7 +21,7 @@ class TestApplication {
TestApplication() {
}

void create() {
void initialize() {
appDir = File.createTempDir()
buildDirectoryStructure()
println 'Created temporary app: ' + appDir.path
Expand Down Expand Up @@ -138,4 +138,5 @@ out/'''
'''
private static final String MULE_CONFIG_END = '''
</mule>'''

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JenkinsFileExistsRuleTest extends Specification {
private Application app

def setup() {
testApp.create()
testApp.initialize()
testApp.addJenkinsfile()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.avioconsulting.mule.linter.model

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult
import spock.lang.Specification

Expand Down
Loading