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

SONARPHP-1520 Migrate Custom Rules module to Gradle #1305

Merged
merged 13 commits into from
Nov 10, 2024
Merged
1 change: 0 additions & 1 deletion .cirrus/modules/build.star
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def whitesource_script():
return [
"source cirrus-env QA",
"source .cirrus/use-gradle-wrapper.sh",
"source ${PROJECT_VERSION_CACHE_DIR}/evaluated_project_version.txt",
GabinL21 marked this conversation as resolved.
Show resolved Hide resolved
"GRADLE_OPTS=\"-Xmx64m -Dorg.gradle.jvmargs='-Xmx3G' -Dorg.gradle.daemon=false\" ./gradlew ${GRADLE_COMMON_FLAGS} :php-frontend:processResources -Pkotlin.compiler.execution.strategy=in-process",
"source ws_scan.sh"
]
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

13 changes: 4 additions & 9 deletions its/plugin/projects/custom_rules/src/simple.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php

function f() {

$f1 = function () { // NOK - function expression
};

for (;;) { // NOK - for statement
}

}
some_function();
foo();
test();
buzz();
5 changes: 5 additions & 0 deletions its/plugin/tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ dependencies {
setIncludePatterns("Tests")
}

// Mandatory for the orchestrator in the "Tests" class, since it requires the custom rules plugin JAR
tasks.named("integrationTest") {
dependsOn(":php-custom-rules-plugin:shadowJar")
}

sonar {
isSkipProject = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static com.sonar.it.php.Tests.createScanner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonarqube.ws.Issues.Issue;

// TODO Enable back with SONARPHP-1520
@Disabled
class CustomRulesTest {

@RegisterExtension
public static OrchestratorExtension orchestrator = Tests.ORCHESTRATOR;
public static final OrchestratorExtension orchestrator = Tests.ORCHESTRATOR;
private static final String PROJECT_KEY = "custom-rules";
private static final String PROJECT_NAME = "Custom Rules";
private static List<Issue> issues;
Expand All @@ -54,17 +51,17 @@ static void prepare() {

@Test
void baseTreeVisitorCheck() {
assertSingleIssue("php-custom-rules:visitor", 5, "Function expression.", "5min");
assertSingleIssue("custom:S1", 4, "Remove the usage of this forbidden function.", "5min");
}

@Test
void subscriptionBaseVisitorCheck() {
assertSingleIssue("php-custom-rules:subscription", 8, "For statement.", "10min");
assertSingleIssue("custom:S2", 6, "Remove the usage of this other forbidden function.", "10min");
}

private void assertSingleIssue(String ruleKey, int expectedLine, String expectedMessage, String expectedDebt) {
private static void assertSingleIssue(String ruleKey, int expectedLine, String expectedMessage, String expectedDebt) {
assertThat(Tests.issuesForRule(issues, ruleKey)).hasSize(1);
Issue issue = Tests.issuesForRule(issues, ruleKey).get(0);
var issue = Tests.issuesForRule(issues, ruleKey).get(0);
assertThat(issue.getLine()).isEqualTo(expectedLine);
assertThat(issue.getMessage()).isEqualTo(expectedMessage);
assertThat(issue.getDebt()).isEqualTo(expectedDebt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonarqube.ws.Components;
Expand Down Expand Up @@ -75,8 +74,7 @@ class Tests {
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "drupal_profile.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "no_rules.xml"))
// Custom rules plugin
// TODO Fix in SONARPHP-1520 Migrate Custom Rules module to Gradle
// .addPlugin(FileLocation.byWildcardMavenFilename(new File("../plugins/php-custom-rules-plugin/target"), "php-custom-rules-plugin-*.jar"))
.addPlugin(FileLocation.byWildcardFilename(new File("../../../php-custom-rules-plugin/build/libs"), "php-custom-rules-plugin-*-all.jar"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "profile-php-custom-rules.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "nosonar.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "sleep.xml"))
Expand Down Expand Up @@ -201,8 +199,6 @@ private static void assertAnalyzerLogs(String logs) {

// TODO SONARPHP-1466 Replace nested classes in it-php-plugin-tests:Tests with a more elegant solution

// TODO Enable back with SONARPHP-1520
@Disabled
@Nested
class NestedCustomRulesTest extends CustomRulesTest {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
<language>php</language>
<rules>
<rule>
<repositoryKey>php-custom-rules</repositoryKey>
<key>visitor</key>
<repositoryKey>custom</repositoryKey>
<key>S1</key>
</rule>
<rule>
<repositoryKey>php-custom-rules</repositoryKey>
<key>subscription</key>
</rule>
<rule>
<repositoryKey>deprecated-php-custom-rules</repositoryKey>
<key>visitor</key>
</rule>
<rule>
<repositoryKey>deprecated-php-custom-rules</repositoryKey>
<key>subscription</key>
<repositoryKey>custom</repositoryKey>
<key>S2</key>
</rule>
</rules>
</profile>
File renamed without changes.
72 changes: 72 additions & 0 deletions php-custom-rules-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import org.sonarsource.php.registerCleanupTask

plugins {
id("java-library")
id("jacoco")
alias(libs.plugins.shadow)
}

dependencies {
compileOnly(libs.sonar.plugin.api)
compileOnly(project(":php-frontend"))

testImplementation(libs.junit.jupiter)
testImplementation(libs.sonar.plugin.api.impl)
testImplementation(testFixtures(project(":php-frontend")))
}

description = "PHP Custom Rules Example for SonarQube"

tasks.jar {
manifest {
attributes(
mapOf(
"Plugin-ChildFirstClassLoader" to "false",
"Plugin-Class" to "org.sonar.samples.php.PHPCustomRulesPlugin",
"Plugin-Description" to "PHP Custom Rules Example for SonarQube",
"Plugin-Developers" to "SonarSource Team",
"Plugin-Display-Version" to version,
"Plugin-Homepage" to "https://sonarsource.atlassian.net/browse/SONARPHP",
"Plugin-IssueTrackerUrl" to "https://sonarsource.atlassian.net/browse/SONARPHP",
"Plugin-Key" to "custom",
"Plugin-License" to "GNU LGPL 3",
"Plugin-Name" to "PHP Custom Rules",
"Plugin-Organization" to "SonarSource",
"Plugin-OrganizationUrl" to "https://www.sonarsource.com",
"Plugin-RequiredForLanguages" to "php",
"Plugin-SourcesUrl" to "https://github.com/SonarSource/sonar-php",
"Plugin-Version" to project.version,
"Sonar-Version" to "9.9",
"SonarLint-Supported" to "true",
"Version" to project.version.toString(),
"Jre-Min-Version" to java.sourceCompatibility.majorVersion,
),
)
}
}

tasks.withType<Test> {
useJUnitPlatform()
}

plugins.withType<JacocoPlugin> {
tasks["test"].finalizedBy("jacocoTestReport")
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
xml.required.set(true)
}
}

val cleanupTask = registerCleanupTask()

tasks.shadowJar {
dependsOn(cleanupTask)
exclude("**/*.php")
}

artifacts {
archives(tasks.shadowJar)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SonarQube PHP Plugin
* Copyright (C) 2016-2024 SonarSource SA
* Copyright (C) 2010-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -70,7 +70,7 @@ public void define(Context context) {
// Optionally define remediation costs
Map<String, String> remediationCosts = new HashMap<>();
remediationCosts.put(ForbiddenFunctionUseCheck.KEY, "5min");
remediationCosts.put(OtherForbiddenFunctionUseCheck.KEY, "5min");
remediationCosts.put(OtherForbiddenFunctionUseCheck.KEY, "10min");
repository.rules().forEach(rule -> rule.setDebtRemediationFunction(
rule.debtRemediationFunctions().constantPerIssue(remediationCosts.get(rule.key()))));

Expand Down
Loading