Skip to content

Commit

Permalink
Merge pull request #381 from swarajsaaj/add_sonar
Browse files Browse the repository at this point in the history
Add sonar
  • Loading branch information
pascalgrimaud authored Dec 22, 2021
2 parents 129e427 + d5b0da8 commit 7046f2b
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.jhipster.lite.generator.server.sonar.application;

import org.springframework.stereotype.Component;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.sonar.domain.SonarService;

@Component
public class SonarApplicationService {

private final SonarService sonarService;

public SonarApplicationService(SonarService sonarService) {
this.sonarService = sonarService;
}

public void init(Project project) {
this.sonarService.init(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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.jhipster.lite.generator.server.sonar.domain;

public class Sonar {

public static final String SONARSOURCE_MAVEN_PLUGIN_VERSION = "3.9.1.2184";
public static final String SONARQUBE_DOCKER_IMAGE = "sonarqube:9.2.4-community";

private Sonar() {}

public static String getMavenPluginVersion() {
return SONARSOURCE_MAVEN_PLUGIN_VERSION;
}

public static String getSonarqubeDockerImage() {
return SONARQUBE_DOCKER_IMAGE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package tech.jhipster.lite.generator.server.sonar.domain;

import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PROJECT_NAME;
import static tech.jhipster.lite.generator.server.sonar.domain.Sonar.getMavenPluginVersion;

import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.buildtool.generic.domain.Plugin;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class SonarDomainService implements SonarService {

public static final String SOURCE = "server/sonar";

private final ProjectRepository projectRepository;
private final BuildToolService buildToolService;

public SonarDomainService(ProjectRepository projectRepository, BuildToolService buildToolService) {
this.projectRepository = projectRepository;
this.buildToolService = buildToolService;
}

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

@Override
public void addPropertiesPlugin(Project project) {
Plugin plugin = Plugin
.builder()
.groupId("org.codehaus.mojo")
.artifactId("properties-maven-plugin")
.version("\\${properties-maven-plugin.version}")
.additionalElements(
"""
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>sonar-project.properties</file>
</files>
</configuration>
</execution>
</executions>"""
)
.build();
buildToolService.addProperty(project, "properties-maven-plugin", "1.0.0");
buildToolService.addPlugin(project, plugin);
}

@Override
public void addSonarScannerPluginManagement(Project project) {
Plugin plugin = Plugin
.builder()
.groupId("org.sonarsource.scanner.maven")
.artifactId("sonar-maven-plugin")
.version("\\${sonar-maven-plugin.version}")
.build();
buildToolService.addProperty(project, "sonar-maven-plugin", getMavenPluginVersion());
buildToolService.addPluginManagement(project, plugin);
}

@Override
public 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) {
project.addDefaultConfig(BASE_NAME);
project.addConfig("sonarqubeDockerImage", Sonar.getSonarqubeDockerImage());
projectRepository.template(project, SOURCE, "sonar.yml", "src/main/docker", "sonar.yml");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tech.jhipster.lite.generator.server.sonar.domain;

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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tech.jhipster.lite.generator.server.sonar.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.sonar.domain.SonarDomainService;
import tech.jhipster.lite.generator.server.sonar.domain.SonarService;

@Configuration
public class SonarBeanConfiguration {

private final ProjectRepository projectRepository;
private final BuildToolService buildToolService;

public SonarBeanConfiguration(ProjectRepository projectRepository, BuildToolService buildToolService) {
this.projectRepository = projectRepository;
this.buildToolService = buildToolService;
}

@Bean
public SonarService sonarService() {
return new SonarDomainService(projectRepository, buildToolService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.jhipster.lite.generator.server.sonar.infrastructure.primary;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.sonar.application.SonarApplicationService;

@RestController
@RequestMapping("/api/servers/sonar")
@Tag(name = "Sonar")
class SonarResource {

private final SonarApplicationService sonarApplicationService;

public SonarResource(SonarApplicationService sonarApplicationService) {
this.sonarApplicationService = sonarApplicationService;
}

@Operation(summary = "Add Sonar configuration to inspect code quality")
@ApiResponses({ @ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration") })
@PostMapping
public void addSonar(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.init(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.server.sonar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
sonar.projectKey={{baseName}}
sonar.projectName={{projectName}}

sonar.sources=src/main/
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.sourceEncoding=UTF-8
sonar.exclusions=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=squid: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
13 changes: 13 additions & 0 deletions src/main/resources/generator/server/sonar/sonar.yml.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
version: '3.8'
services:
sonar:
image: {{sonarqubeDockerImage}}
# Authentication is turned off for out of the box experience while trying out SonarQube
# For real use cases delete sonar.forceAuthentication variable or set sonar.forceAuthentication=true
environment:
- sonar.forceAuthentication=false
# If you want to expose these ports outside your dev PC,
# remove the "127.0.0.1:" prefix
ports:
- 127.0.0.1:9001:9000
Loading

0 comments on commit 7046f2b

Please sign in to comment.