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

[ISSUE 92] Centralized rules repository #178

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
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ecoCode rules specification repository

## Description

This project contains the specifications of the ecoCode rules.

All the existing rules can be found in the [rules folder](src/main/rules).

## Description language

The description of the rules uses the ASCIIDOC format (with [Markdown compatibility](https://docs.asciidoctor.org/asciidoc/latest/syntax-quick-reference/#markdown-compatibility)) in order to allow the inclusion of other pages (this feature is not available in standard with Markdown).

See:
* [AsciiDoc Syntax Quick Reference](https://docs.asciidoctor.org/asciidoc/latest/syntax-quick-reference/)
* [Compare AsciiDoc to Markdown](https://docs.asciidoctor.org/asciidoc/latest/asciidoc-vs-markdown/)
238 changes: 238 additions & 0 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.ecocode</groupId>
<artifactId>ecocode-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>
</parent>

<artifactId>ecocode-rules-specifications</artifactId>

<name>ecoCode Rules Specifications repository</name>
<description>Repository that contains the specifications of every static-analysis rules available in ecoCode plugins.</description>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<url>https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications</url>

<dependencies>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<!--
This plugin convert ASCIIDOC rule specification in HTML format
ASCIIDOC format is used with custom features such as :
- syntax highlighting (see code blocks on ASCIIDOC rules)
- inclusions (see: php/EC74.asciidoc)
- table data generation from CSV (see: php/EC34.asciidoc)
-->
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<version>2.2.4</version>
<executions>
<execution>
<id>convert-to-html</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/rules</sourceDirectory>
<outputDirectory>${project.build.directory}/rules</outputDirectory>
<attributes>
<source-highlighter>coderay</source-highlighter>
<coderay-css>style</coderay-css>
</attributes>
<preserveDirectories>true</preserveDirectories>
<headerFooter>false</headerFooter>
<relativeBaseDir>true</relativeBaseDir>
<logHandler>
<failIf>
<severity>ERROR</severity>
</failIf>
</logHandler>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<!--
Target resources tree need to be "flat".
Each metadata JSON file must be in the same tree as the HTML description file for the corresponding language.
The only way currently found to generate the correct file tree is to use antrun-plugin with `flattenmapper`
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/java">
<fileset dir="${project.build.directory}/rules">
<include name="*/java/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/php">
<fileset dir="${project.build.directory}/rules">
<include name="*/php/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/python">
<fileset dir="${project.build.directory}/rules">
<include name="*/python/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/js">
<fileset dir="${project.build.directory}/rules">
<include name="*/js/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/ts">
<fileset dir="${project.build.directory}/rules">
<include name="*/ts/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<!--
This module produce one artifact by language (with corresponding classifier)
For example, to add rule specifications for Python language, add following dependency in python sonarqube plugin:

<dependency>
<groupId>io.ecocode</groupId>
<artifactId>ecocode-rules-specifications</artifactId>
<version>${project.version}</version>
<classifier>python</classifier>
</dependency>
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
jycr marked this conversation as resolved.
Show resolved Hide resolved
<version>3.6.0</version>
<executions>
<execution>
<id>assembly-java</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/java.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-php</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/php.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-python</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/python.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-js</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/js.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-ts</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/ts.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/java.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>java</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/java/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/js.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>js</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/js/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/php.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>php</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/php/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/python.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>python</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/python/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/ts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>ts</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/ts/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
23 changes: 23 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.

## Noncompliant Code Example

```java
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

List<Employee> employees = new ArrayList<>();

for (Integer id: ids) {
Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant
if (employee.isPresent()) {
employees.add(employee.get());
}
}
```

## Compliant Solution

```java
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Employee> employees = employeeRepository.findAllById(ids);
```
14 changes: 14 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC10/EC10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Avoid using unoptimized vector images",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "60min"
},
"tags": [
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
}
Loading