Skip to content

Commit

Permalink
Merge pull request #676 from pascalgrimaud/sonar-update-for-front
Browse files Browse the repository at this point in the history
Sonar: add backend and frontend configuration
  • Loading branch information
pascalgrimaud authored Feb 12, 2022
2 parents 54bdb82 + 48c2396 commit c951853
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,11 @@ public SonarApplicationService(SonarService sonarService) {
this.sonarService = sonarService;
}

public void init(Project project) {
this.sonarService.init(project);
public void addSonarJavaBackend(Project project) {
this.sonarService.addSonarJavaBackend(project);
}

void addPropertiesPlugin(Project project) {
this.sonarService.addPropertiesPlugin(project);
}

void addSonarScannerPluginManagement(Project project) {
this.sonarService.addSonarScannerPluginManagement(project);
}

void addPropertiesFile(Project project) {
this.sonarService.addPropertiesFile(project);
}

void addDockerCompose(Project project) {
this.sonarService.addDockerCompose(project);
public void addSonarJavaBackendAndFrontend(Project project) {
this.sonarService.addSonarJavaBackendAndFrontend(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ public SonarDomainService(ProjectRepository projectRepository, BuildToolService
}

@Override
public void init(Project project) {
public void addSonarJavaBackend(Project project) {
addPropertiesPlugin(project);
addSonarScannerPluginManagement(project);
addPropertiesFile(project);
addDockerCompose(project);
}

@Override
public void addPropertiesPlugin(Project project) {
public void addSonarJavaBackendAndFrontend(Project project) {
addPropertiesPlugin(project);
addSonarScannerPluginManagement(project);
addFullstackPropertiesFile(project);
addDockerCompose(project);
}

private void addPropertiesPlugin(Project project) {
Plugin plugin = Plugin
.builder()
.groupId("org.codehaus.mojo")
Expand All @@ -57,8 +64,7 @@ public void addPropertiesPlugin(Project project) {
buildToolService.addPlugin(project, plugin);
}

@Override
public void addSonarScannerPluginManagement(Project project) {
private void addSonarScannerPluginManagement(Project project) {
Plugin plugin = Plugin
.builder()
.groupId("org.sonarsource.scanner.maven")
Expand All @@ -69,15 +75,19 @@ public void addSonarScannerPluginManagement(Project project) {
buildToolService.addPluginManagement(project, plugin);
}

@Override
public void addPropertiesFile(Project project) {
private void addPropertiesFile(Project project) {
project.addDefaultConfig(BASE_NAME);
project.addDefaultConfig(PROJECT_NAME);
projectRepository.template(project, SOURCE, "sonar-project.properties");
}

@Override
public void addDockerCompose(Project project) {
private void addFullstackPropertiesFile(Project project) {
project.addDefaultConfig(BASE_NAME);
project.addDefaultConfig(PROJECT_NAME);
projectRepository.template(project, SOURCE, "sonar-fullstack-project.properties", "", "sonar-project.properties");
}

private void addDockerCompose(Project project) {
project.addDefaultConfig(BASE_NAME);
project.addConfig("sonarqubeDockerImage", Sonar.getSonarqubeDockerImage());
projectRepository.template(project, SOURCE, "sonar.yml", "src/main/docker", "sonar.yml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
import tech.jhipster.lite.generator.project.domain.Project;

public interface SonarService {
void init(Project project);

void addPropertiesPlugin(Project project);

void addSonarScannerPluginManagement(Project project);

void addPropertiesFile(Project project);

void addDockerCompose(Project project);
void addSonarJavaBackend(Project project);
void addSonarJavaBackendAndFrontend(Project project);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.jhipster.lite.generator.server.sonar.infrastructure.primary;
package tech.jhipster.lite.generator.server.sonar.infrastructure.primary.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -23,12 +23,21 @@ public SonarResource(SonarApplicationService sonarApplicationService) {
this.sonarApplicationService = sonarApplicationService;
}

@Operation(summary = "Add Sonar configuration to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration")
@PostMapping
@GeneratorStep(id = "sonar")
public void addSonar(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add Sonar configuration for Java Backend to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration for Java Backend")
@PostMapping("/java-backend")
@GeneratorStep(id = "sonar-java-backend")
public void addSonarJavaBackend(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.init(project);
sonarApplicationService.addSonarJavaBackend(project);
}

@Operation(summary = "Add Sonar configuration for Java Backend and Frontend to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration for Java Backend and Frontend")
@PostMapping("/java-backend-and-frontend")
@GeneratorStep(id = "sonar-java-backend-and-frontend")
public void addSonarJavaBackendAndFrontend(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.addSonarJavaBackendAndFrontend(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
sonar.projectKey={{baseName}}
sonar.projectName={{projectName}}

sonar.sources=src/main/
sonar.tests=src/test/
sonar.host.url=http://localhost:9001

sonar.test.inclusions=src/test/**/*.*, src/main/webapp/app/**/*.spec.ts
sonar.coverage.jacoco.xmlReportPaths=target/jacoco/jacoco.xml
sonar.java.codeCoveragePlugin=jacoco
sonar.junit.reportPaths=target/surefire-reports,target/failsafe-reports

sonar.testExecutionReportPaths=target/test-results/jest/TESTS-results-sonar.xml
sonar.javascript.lcov.reportPaths=target/test-results/lcov.info

sonar.sourceEncoding=UTF-8
sonar.exclusions=src/main/webapp/app/main.ts, src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.*

sonar.issue.ignore.multicriteria=S3437,S4502,S4684,S4032,UndocumentedApi

# Rule https://rules.sonarsource.com/java/RSPEC-3437 is ignored, as a JPA-managed field cannot be transient
sonar.issue.ignore.multicriteria.S3437.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S3437.ruleKey=squid:S3437

# Rule https://rules.sonarsource.com/java/RSPEC-1176 is ignored, as we want to follow "clean code" guidelines and classes, methods and arguments names should be self-explanatory
sonar.issue.ignore.multicriteria.UndocumentedApi.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.UndocumentedApi.ruleKey=squid:UndocumentedApi

# Rule https://rules.sonarsource.com/java/RSPEC-4502 is ignored, as for JWT tokens we are not subject to CSRF attack
sonar.issue.ignore.multicriteria.S4502.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4502.ruleKey=java:S4502

# Rule https://rules.sonarsource.com/java/RSPEC-4684
sonar.issue.ignore.multicriteria.S4684.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4684.ruleKey=java:S4684

# Rule: Packages containing only "package-info.java" should be removed
sonar.issue.ignore.multicriteria.S4032.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4032.ruleKey=java:S4032
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sonar.projectKey={{baseName}}
sonar.projectName={{projectName}}

sonar.sources=src/main/
sonar.tests=src/test/
sonar.host.url=http://localhost:9001

sonar.test.inclusions=src/test/**/*.*, src/main/webapp/app/**/*.spec.ts
Expand All @@ -10,7 +11,7 @@ sonar.java.codeCoveragePlugin=jacoco
sonar.junit.reportPaths=target/surefire-reports,target/failsafe-reports

sonar.sourceEncoding=UTF-8
sonar.exclusions=src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.*
sonar.exclusions=src/main/webapp/app/main.ts, src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.*

sonar.issue.ignore.multicriteria=S3437,S4502,S4684,S4032,UndocumentedApi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import tech.jhipster.lite.generator.buildtool.maven.application.MavenApplicationService;
import tech.jhipster.lite.generator.init.application.InitApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.sonar.domain.Sonar;

@IntegrationTest
class SonarApplicationServiceIT {
Expand All @@ -25,108 +24,27 @@ class SonarApplicationServiceIT {
MavenApplicationService mavenApplicationService;

@Test
void shouldInit() {
void shouldAddSonarJavaBackend() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.init(project);
sonarApplicationService.addSonarJavaBackend(project);

assertFileExist(project, "src/main/docker/sonar.yml");
assertFileExist(project, "sonar-project.properties");

assertFileContent(project, POM_XML, "<properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "</properties-maven-plugin.version>");

assertFileContent(project, POM_XML, "<sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, "</sonar-maven-plugin.version>");

assertFileContent(project, POM_XML, sonarSourcePlugin());

assertFileContent(project, POM_XML, propertiesPlugin());
SonarAssert.assertFiles(project);
SonarAssert.assertPomXml(project);
}

@Test
void shouldAddDockerCompose() {
void shouldAddSonarJavaBackendAndFrontend() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.addDockerCompose(project);

assertFileExist(project, "src/main/docker/sonar.yml");
assertFileContent(project, "src/main/docker/sonar.yml", "image: " + Sonar.SONARQUBE_DOCKER_IMAGE);
}

@Test
void shouldAddSonarProperties() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.addPropertiesFile(project);

assertFileExist(project, "sonar-project.properties");
assertFileContent(project, "sonar-project.properties", List.of("sonar.sources=src/main/"));
}

@Test
void shouldAddPropertiesPlugin() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.addPropertiesPlugin(project);

assertFileContent(project, POM_XML, "<properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "</properties-maven-plugin.version>");

assertFileContent(project, POM_XML, propertiesPlugin());
}

@Test
void shouldAddSonarScannerPluginManagement() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.addSonarScannerPluginManagement(project);

assertFileContent(project, POM_XML, "<sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, "</sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, sonarSourcePlugin());
}

private List<String> propertiesPlugin() {
return List.of(
"<plugin>",
"<groupId>org.codehaus.mojo</groupId>",
"<artifactId>properties-maven-plugin</artifactId>",
"<version>${properties-maven-plugin.version}</version>",
"<executions>",
"<execution>",
"<phase>initialize</phase>",
"<goals>",
"<goal>read-project-properties</goal>",
"</goals>",
"<configuration>",
"<files>",
"<file>sonar-project.properties</file>",
"</files>",
"</configuration>",
"</execution>",
"</executions>",
"</plugin>"
);
}
sonarApplicationService.addSonarJavaBackendAndFrontend(project);

private List<String> sonarSourcePlugin() {
return List.of(
"<plugin>",
"<groupId>org.sonarsource.scanner.maven</groupId>",
"<artifactId>sonar-maven-plugin</artifactId>",
"<version>${sonar-maven-plugin.version}</version>",
"</plugin>"
);
SonarAssert.assertFiles(project);
SonarAssert.assertFrontProperties(project);
SonarAssert.assertPomXml(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tech.jhipster.lite.generator.server.sonar.application;

import static tech.jhipster.lite.TestUtils.assertFileContent;
import static tech.jhipster.lite.TestUtils.assertFileExist;
import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML;

import java.util.List;
import tech.jhipster.lite.generator.project.domain.Project;

public class SonarAssert {

private SonarAssert() {}

public static void assertFiles(Project project) {
assertFileExist(project, "src/main/docker/sonar.yml");
assertFileExist(project, "sonar-project.properties");
}

public static void assertFrontProperties(Project project) {
assertFileContent(project, "sonar-project.properties", "sonar.testExecutionReportPaths");
assertFileContent(project, "sonar-project.properties", "sonar.javascript.lcov.reportPaths");
}

public static void assertPomXml(Project project) {
assertFileContent(project, POM_XML, "<properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "</properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "<sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, "</sonar-maven-plugin.version>");

assertFileContent(project, POM_XML, sonarSourcePlugin());
assertFileContent(project, POM_XML, propertiesPlugin());
}

private static List<String> sonarSourcePlugin() {
return List.of(
"<plugin>",
"<groupId>org.sonarsource.scanner.maven</groupId>",
"<artifactId>sonar-maven-plugin</artifactId>",
"<version>${sonar-maven-plugin.version}</version>",
"</plugin>"
);
}

private static List<String> propertiesPlugin() {
return List.of(
"<plugin>",
"<groupId>org.codehaus.mojo</groupId>",
"<artifactId>properties-maven-plugin</artifactId>",
"<version>${properties-maven-plugin.version}</version>",
"<executions>",
"<execution>",
"<phase>initialize</phase>",
"<goals>",
"<goal>read-project-properties</goal>",
"</goals>",
"<configuration>",
"<files>",
"<file>sonar-project.properties</file>",
"</files>",
"</configuration>",
"</execution>",
"</executions>",
"</plugin>"
);
}
}
Loading

0 comments on commit c951853

Please sign in to comment.