Skip to content

Commit

Permalink
Merge pull request #46 from green-code-initiative/MIGRATION_DDC
Browse files Browse the repository at this point in the history
next step for migration with last decisions
  • Loading branch information
dedece35 authored Dec 9, 2024
2 parents a020b60 + a82d48e commit 4705926
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 113 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
18 changes: 9 additions & 9 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>1.0.0-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>0.0.3</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
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
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.HashMap;
import java.util.List;
Expand All @@ -31,6 +31,8 @@
import org.sonar.plugins.python.api.tree.IfStatement;
import org.sonar.plugins.python.api.tree.Statement;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

import static org.sonar.plugins.python.api.tree.Tree.Kind.*;

/**
Expand All @@ -42,7 +44,8 @@
* - an "ELSE" statement is considered as a second IF statement using the same variables used on previous
* - IF and ELSEIF statements are considered as an IF statement
*/
@Rule(key = "EC2")
@Rule(key = "GCI2")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC2")
public class AvoidMultipleIfElseStatementCheck extends PythonSubscriptionCheck {

public static final String ERROR_MESSAGE = "Use a match-case statement instead of multiple if-else if possible";
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.Arrays;
import java.util.HashSet;
Expand All @@ -36,7 +36,8 @@
import org.sonar.plugins.python.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC72")
@Rule(key = "GCI72")
@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC72")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S72")
public class AvoidSQLRequestInLoop extends PythonSubscriptionCheck {

Expand Down
Loading

0 comments on commit 4705926

Please sign in to comment.