Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/maven/org.sonarsource.analyzer-co…
Browse files Browse the repository at this point in the history
…mmons-sonar-analyzer-commons-2.16.0.3141
  • Loading branch information
dedece35 authored Dec 13, 2024
2 parents 870287c + bc8e3df commit 20cae42
Show file tree
Hide file tree
Showing 30 changed files with 130 additions and 123 deletions.
31 changes: 11 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,27 @@ jobs:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Set up JDK 11
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 11

- name: Cache SonarQube packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar

- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2

- name: Verify
run: mvn -e -B verify

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17

- name: Cache SonarQube packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar

- name: SonarQube Scan
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -e -B org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=green-code-initiative_creedengo-python
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=green-code-initiative_creedengo-python
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [#43](https://github.com/green-code-initiative/creedengo-python/pull/43) Strong renaming plugin from `ecocode-python` to `creedengo-python` (and maven groupid from `io.ecocode` to `io.greencodeinitiative`)

### Deleted

## [2.0.0] - 2024-12-12

### Changed

- [#43](https://github.com/green-code-initiative/creedengo-python/pull/43) Strong renaming plugin from `ecocode-python` to `creedengo-python` (and maven groupid from `io.ecocode` to `org.green-code-initiative`)

## [1.5.0] - 2024-10-04

### Changed
Expand Down Expand Up @@ -101,7 +105,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Comparison list

[unreleased](https://github.com/green-code-initiative/creedengo-python/compare/1.5.0...HEAD)
[unreleased](https://github.com/green-code-initiative/creedengo-python/compare/2.0.0...HEAD)
[2.0.0](https://github.com/green-code-initiative/creedengo-python/compare/1.4.4...2.0.0)
[1.5.0](https://github.com/green-code-initiative/creedengo-python/compare/1.4.4...1.5.0)
[1.4.4](https://github.com/green-code-initiative/creedengo-python/compare/1.4.3...1.4.4)
[1.4.3](https://github.com/green-code-initiative/creedengo-python/compare/1.4.2...1.4.3)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ _creedengo_ is a collective project aiming to reduce environmental footprint of
the project is to provide a list of static code analyzers to highlight code structures that may have a negative
ecological impact: energy and resources over-consumption, "fatware", shortening terminals' lifespan, etc.

_ecoCode_ is based on evolving catalogs
of [good practices](https://github.com/green-code-initiative/ecoCode/blob/main/docs/rules), for various technologies.
_creedengo_ is based on evolving catalogs
of [good practices](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/docs/rules), for various technologies.
This
SonarQube plugin then implements these catalogs as rules for scanning your Python projects.

Expand Down Expand Up @@ -69,17 +69,17 @@ Ready to use binaries are available [from GitHub](https://github.com/green-code-
| 1.4.+ | 9.4.+ LTS to 10.4.1 | 11 / 17 |

> Compatibility table of versions lower than 1.4.+ are available from the
> main [ecoCode repository](https://github.com/green-code-initiative/ecoCode#-plugins-version-compatibility).
> main [creedengo repository](https://github.com/green-code-initiative/creedengo-rules-specifications#-plugins-version-compatibility).
🤝 Contribution
---------------

check [ecoCode repository](https://github.com/green-code-initiative/ecoCode#-contribution)
check [creedengo repository](https://github.com/green-code-initiative/creedengo-rules-specifications#-contribution)

🤓 Main contributors
--------------------

check [ecoCode repository](https://github.com/green-code-initiative/ecoCode#-main-contributors)
check [creedengo repository](https://github.com/green-code-initiative/creedengo-rules-specifications#-main-contributors)

Links
-----
Expand Down
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Please read [RULES.md](https://github.com/green-code-initiative/ecoCode/blob/main/RULES.md) in `ecoCode` repository.
Please read [RULES.md](https://github.com/green-code-initiative/creedengo-rules-specifications/blob/main/RULES.md) in `creedengo` repository.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<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>

<groupId>io.greencodeinitiative</groupId>
<groupId>org.green-code-initiative</groupId>
<artifactId>creedengo-python-plugin</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.0.1-SNAPSHOT</version>
<packaging>sonar-plugin</packaging>

<name>creedengo - Python language</name>
<description>Provides rules to reduce the environmental footprint of your Python programs</description>
<inceptionYear>2023</inceptionYear>
<inceptionYear>2024</inceptionYear>
<url>https://github.com/green-code-initiative/creedengo-python</url>

<organization>
Expand Down Expand Up @@ -60,16 +60,16 @@

<mockito.version>5.14.1</mockito.version>

<!-- temporary version waiting for real automatic release in creedengo repository -->
<ecocode-rules-specifications.version>1.6.4</ecocode-rules-specifications.version>
<!-- temporary version waiting for a real automatic release in creedengo repository -->
<creedengo-rules-specifications.version>2.0.0</creedengo-rules-specifications.version>

</properties>

<dependencies>
<dependency>
<groupId>io.ecocode</groupId>
<artifactId>ecocode-rules-specifications</artifactId>
<version>${ecocode-rules-specifications.version}</version>
<groupId>org.green-code-initiative</groupId>
<artifactId>creedengo-rules-specifications</artifactId>
<version>${creedengo-rules-specifications.version}</version>
<classifier>python</classifier>
</dependency>

Expand Down Expand Up @@ -173,7 +173,7 @@
<extensions>true</extensions>
<configuration>
<pluginKey>creedengopython</pluginKey>
<pluginClass>fr.greencodeinitiative.python.PythonPlugin</pluginClass>
<pluginClass>org.greencodeinitiative.creedengo.python.PythonPlugin</pluginClass>
<sonarLintSupported>true</sonarLintSupported>
<pluginApiMinVersion>${sonarqube.version}</pluginApiMinVersion>
<skipDependenciesPackaging>true</skipDependenciesPackaging>
Expand Down Expand Up @@ -221,7 +221,7 @@
</excludes>
</filter>
<filter>
<artifact>io.ecocode:ecocode-rules-specifications:*</artifact>
<artifact>org.green-code-initiative:creedengo-rules-specifications:*</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python;
package org.greencodeinitiative.creedengo.python;

import org.sonar.api.Plugin;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,9 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python;
package org.greencodeinitiative.creedengo.python;

import fr.greencodeinitiative.python.checks.*;
import org.greencodeinitiative.creedengo.python.checks.*;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.plugins.python.api.PythonCustomRuleRepository;
Expand All @@ -44,9 +44,9 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
);

public static final String LANGUAGE = "py";
public static final String NAME = "ecoCode";
public static final String RESOURCE_BASE_PATH = "io/ecocode/rules/python";
public static final String REPOSITORY_KEY = "ecocode-python";
public static final String NAME = "creedengo";
public static final String RESOURCE_BASE_PATH = "org/green-code-initiative/rules/python";
public static final String REPOSITORY_KEY = "creedengo-python";

private final SonarRuntime sonarRuntime;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python.checks;
package org.greencodeinitiative.creedengo.python.checks;


import org.sonar.check.Rule;
Expand All @@ -32,7 +32,8 @@
import java.util.Map;
import java.util.regex.Pattern;

@Rule(key = "EC74")
@Rule(key = "GCI74")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC74")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S74")
public class AvoidFullSQLRequest extends PythonSubscriptionCheck {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python.checks;
package org.greencodeinitiative.creedengo.python.checks;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -28,13 +28,13 @@
import org.sonar.plugins.python.api.tree.FunctionDef;
import org.sonar.plugins.python.api.tree.ParameterList;
import org.sonar.plugins.python.api.tree.QualifiedExpression;
import org.sonar.plugins.python.api.tree.ReturnStatement;
import org.sonar.plugins.python.api.tree.Statement;
import org.sonar.plugins.python.api.tree.StatementList;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC7")
@Rule(key = "GCI7")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC7")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "D7")
public class AvoidGettersAndSetters extends PythonSubscriptionCheck {

Expand Down Expand Up @@ -62,36 +62,36 @@ private boolean isConstructorMethod(FunctionDef functionDef) {
return functionDef.name() != null && "__init__".equals(functionDef.name().name());
}

public void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
private void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) {
AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0);
if (checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) assignmentStatement.children().get(0).children().get(0))) {
// Check if assignedValue is a parameter of the function
ParameterList parameters = functionDef.parameters();
if (parameters != null && !parameters.all().stream().filter(p -> checkAssignementFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()) {
if (parameters != null && !parameters.all().stream().filter(p -> checkAssignmentFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()) {
ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION);
}
}
}
}

public void checkAllGetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
private void checkAllGetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
Statement lastStatement = statements.get(statements.size() - 1);
if (lastStatement.is(Tree.Kind.RETURN_STMT)) {
List<Tree> returnStatementChildren = ((ReturnStatement) lastStatement).children();
List<Tree> returnStatementChildren = lastStatement.children();
if (returnStatementChildren.get(1).is(Tree.Kind.QUALIFIED_EXPR) &&
checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) returnStatementChildren.get(1))) {
ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION);
}
}
}

public boolean checkAssignementFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter) {
private boolean checkAssignmentFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter) {
String parameterToString = parameter.firstToken().value();
return assignmentStatement.assignedValue().firstToken().value().equalsIgnoreCase(parameterToString);
}

public boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression) {
private boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression) {
List<Tree> qualifedExpressionChildren = qualifiedExpression.children();
return qualifedExpressionChildren.size() == 3 &&
qualifedExpressionChildren.get(0).firstToken().value().equalsIgnoreCase("self") &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python.checks;
package org.greencodeinitiative.creedengo.python.checks;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -78,7 +78,8 @@
import org.sonar.plugins.python.api.tree.YieldStatement;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC4")
@Rule(key = "GCI4")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC4")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "D4")
public class AvoidGlobalVariableInFunctionCheck extends PythonSubscriptionCheck {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python.checks;
package org.greencodeinitiative.creedengo.python.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
Expand All @@ -26,6 +26,7 @@
import org.sonar.plugins.python.api.tree.ForStatement;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.tree.RegularArgument;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

import java.util.Objects;

Expand All @@ -34,7 +35,8 @@
import static org.sonar.plugins.python.api.tree.Tree.Kind.LIST_COMPREHENSION;
import static org.sonar.plugins.python.api.tree.Tree.Kind.REGULAR_ARGUMENT;

@Rule(key = "EC404")
@Rule(key = "GCI404")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC404")
public class AvoidListComprehensionInIterations extends PythonSubscriptionCheck {

public static final String DESCRIPTION = "Use generator comprehension instead of list comprehension in for loop declaration";
Expand Down
Loading

0 comments on commit 20cae42

Please sign in to comment.