Skip to content

Commit

Permalink
Merge pull request #1 from mulesoft-consulting/develop
Browse files Browse the repository at this point in the history
Full Version
  • Loading branch information
fperezpa authored Apr 4, 2020
2 parents 6627293 + 46f3cb2 commit ef1bc9f
Show file tree
Hide file tree
Showing 30 changed files with 1,732 additions and 2 deletions.
83 changes: 83 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
##############################
## Java
##############################
.mtj.tmp/
*.class
*.jar
*.war
*.ear
*.nar
hs_err_pid*

##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

##############################
## Gradle
##############################
bin/
build/
.gradle
.gradletasknamecache
gradle-app.setting
!gradle-wrapper.jar

##############################
## IntelliJ
##############################
out/
.idea/
.idea_modules/
*.iml
*.ipr
*.iws

##############################
## Eclipse
##############################
.settings/
bin/
tmp/
.metadata
.classpath
.project
*.tmp
*.bak
*.swp
*~.nib
local.properties
.loadpath
.factorypath

##############################
## NetBeans
##############################
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml

##############################
## Visual Studio Code
##############################
.vscode/

##############################
## OS X
##############################
.DS_Store
114 changes: 112 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,112 @@
# mule-sonarqube-plugin
Mule Sonarqube Plugin
# Mule Sonarqube Validation Plugin

Sonarqube plugin:

* Ensure the consistency of Mule Applications by a set of predefined rules focused on specific quality attributes like: _Conceptual Integrity_, _Maintainability_, _Reusability_, _Security_, _Traceability_ and _Availability_.

**This module requires Java 8**.

## Prerequisites

## Install mule-validation-xpath-core dependency

1. Download the module source code.
2. Open a terminal window and browse to module mule-validation-xpath-core folder.
3. Install this module by running `mvn install`.

## Plugin Generation

1. Download the module source code.
2. Open a terminal window and browse to module root folder.
3. Build the mule plugin for Mule rules running `mvn clean package sonar-packaging:sonar-plugin -Dlanguage=mule`.

## Maven Configuration
1. In your settings file add sonar profile.
```
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
```


## Sonar Configuration
1. Copy the generated file, mule-validation-sonarqube-plugin-{version}-mule.jar to *sonar-home*/extensions/plugins
2. Copy rules [Mule 3 Rules](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/rules-3.xml) or [Mule 4 Rules](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/rules-4.xml) to *sonar-home*/extensions/plugins
3. Start the server

## Project Configuration
### Alternative 1
1. Modify project's pom.xml file to add language and location of the source files to analysis.
```
<properties>
...
<sonar.sources>src</sonar.sources>
...
</properties>
```
2. Analyze the project executing mvn sonar:sonar

### Alternative 2
1. Analyze the project executing mvn sonar:sonar -Dsonar.sources=src

## Release Notes

#### 1.0.0
##### Changes
- Added Line Number processing in Coverage Sensor

#### 0.0.11
##### Changes
- Added Rule Template.
You could create new custom rules from SonarQube Console.

#### 0.0.10
##### Changes
- Added Rule description.
Now you could add an extended description to the rules. The description supports HTML.

#### 0.0.9
##### Changes
- Added property sonar.mule.ruleset.categories. It allows to filter ruleset categories to apply in the project.
Value should be a string separated by commas.
For example run, `mvn sonar:sonar -Dsonar.mule.ruleset.categories=flows` to only apply the flows category ruleset

#### 0.0.8
##### Changes
- Bug Fixes
- Added Metric LOC
- Added Support for SonarQube 7.2.1
#### 0.0.7
##### Changes
- Added new rules + http namespace
#### 0.0.6
##### Changes
- Added Munit Coverage and Minor Improvements
#### 0.0.5
##### Changes
- Externalized Rules to $SONARQUBE_HOME/extensions/plugins/[rules-3.xml|rules-4.xml].
Basic set of rules for [mule3](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/rules-3.xml), [mule4](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/rules-4.xml)

**Adding new rules: if it is needed to add a new namespace you will have to added to [mule3-namespaces](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/namespace-3.properties) or to [mule4-namespaces](https://github.com/mulesoft-consulting/mule-validation-toolkit/blob/master/mule-validation-xpath-core/src/main/resources/namespace-4.properties) and regenerate all plugins**

**Updating existing rule: if you need to update an existing rule, you will also have to update its id number**

#### 0.0.4
##### Changes
- Added Measures
- Number of Flows
- Number of SubFlows
- Number of DW Transformations
- Application Complexity

## Final Note
Enjoy and provide feedback / contribute :)
82 changes: 82 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mulesoft.services</groupId>
<artifactId>mule-validation-sonarqube-plugin</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<url>http://maven.apache.org</url>
<description>Sonar Plugin for Mule Projects</description>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.apiVersion>7.7</sonar.apiVersion>
<sonar.language>java</sonar.language> <!-- Language to Scan -->
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<!-- minimal version of SonarQube to support. Note that the groupId was
"org.codehaus.sonar" before version 5.2 -->
<version>${sonar.apiVersion}</version>
<!-- mandatory scope -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>com.mulesoft.services</groupId>
<artifactId>mule-validation-xpath-core</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}-mule</finalName>
<plugins>
<plugin>
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
<artifactId>sonar-packaging-maven-plugin</artifactId>
<version>1.17</version>
<extensions>true</extensions>
<configuration>
<!-- the entry-point class that extends org.sonar.api.SonarPlugin -->
<pluginClass>com.mulesoft.services.tools.sonarqube.MulePlugin</pluginClass>
<pluginName>MulePlugin</pluginName>
<pluginKey>mulevalidationsonarqubeplugin-mule</pluginKey>
<!-- advanced properties can be set here. See paragraph "Advanced Build
Properties". -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mulesoft.services.tools.sonarqube;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;

import com.mulesoft.services.tools.sonarqube.language.MuleLanguage;
import com.mulesoft.services.tools.sonarqube.measures.MuleFlowCount;
import com.mulesoft.services.tools.sonarqube.measures.MuleSizeRating;
import com.mulesoft.services.tools.sonarqube.measures.MuleSubFlowCount;
import com.mulesoft.services.tools.sonarqube.measures.MuleTransformationCount;
import com.mulesoft.services.tools.sonarqube.metrics.ConfigurationFilesSensor;
import com.mulesoft.services.tools.sonarqube.metrics.CoverageSensor;
import com.mulesoft.services.tools.sonarqube.metrics.MUnitSensor;
import com.mulesoft.services.tools.sonarqube.metrics.MuleMetrics;
import com.mulesoft.services.tools.sonarqube.profile.MuleQualityProfile;
import com.mulesoft.services.tools.sonarqube.rule.MuleRulesDefinition;
import com.mulesoft.services.tools.sonarqube.sensor.MuleSensor;

public class MulePlugin implements Plugin {

// public static final String LANGUAGE_NAME = "Mule";
private static final String GENERAL = "General";

Logger logger = LoggerFactory.getLogger(getClass());

@Override
public void define(Context context) {
if (logger.isDebugEnabled())
logger.debug("Configuring Mule Plugin");

// Added Language
context.addExtensions(MuleLanguage.class, MuleSensor.class);
// context.addExtension(getProperties());

// Added Rules
context.addExtension(MuleRulesDefinition.class);

// Added Profile
context.addExtension(MuleQualityProfile.class);

context.addExtension(PropertyDefinition.builder(MuleLanguage.FILE_SUFFIXES_KEY)
.defaultValue(MuleLanguage.FILE_SUFFIXES_DEFAULT_VALUE).name("File Suffixes")
.description("List of suffixes for files to analyze.").subCategory(GENERAL)
.category(MuleLanguage.LANGUAGE_NAME).multiValues(true).onQualifiers(Qualifiers.PROJECT).build());

context.addExtensions(MuleMetrics.class, ConfigurationFilesSensor.class, MuleSizeRating.class,
MuleFlowCount.class, MuleSubFlowCount.class, MuleTransformationCount.class, CoverageSensor.class,
MUnitSensor.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mulesoft.services.tools.sonarqube.filter;

import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

public class MuleFilePredicate implements FilePredicate {
private final Logger logger = Loggers.get(MuleFilePredicate.class);
SAXBuilder saxBuilder = new SAXBuilder();
String muleNamespace = "http://www.mulesoft.org/schema/mule/core";
String fileExtension = ".xml";

@Override
public boolean apply(InputFile inputFile) {
if (logger.isDebugEnabled()) {
logger.debug("Executing Mule Sensor on file:" + inputFile.filename());
}

if (inputFile.filename().endsWith(fileExtension)) {
try {
Document document = saxBuilder.build(inputFile.inputStream());

String namespace = document.getRootElement().getNamespaceURI();
if (muleNamespace.equals(namespace))
return true;
} catch (JDOMException | IOException e) {
logger.error("Parsing document:" + inputFile.filename(), e);
}
}
return false;
}

}
Loading

0 comments on commit ef1bc9f

Please sign in to comment.