-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from swarajsaaj/add_sonar
Add sonar
- Loading branch information
Showing
14 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...n/java/tech/jhipster/lite/generator/server/sonar/application/SonarApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/tech/jhipster/lite/generator/server/sonar/domain/Sonar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/tech/jhipster/lite/generator/server/sonar/domain/SonarDomainService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/tech/jhipster/lite/generator/server/sonar/domain/SonarService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
25 changes: 25 additions & 0 deletions
25
...ch/jhipster/lite/generator/server/sonar/infrastructure/config/SonarBeanConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../java/tech/jhipster/lite/generator/server/sonar/infrastructure/primary/SonarResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/tech/jhipster/lite/generator/server/sonar/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
35 changes: 35 additions & 0 deletions
35
src/main/resources/generator/server/sonar/sonar-project.properties.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/resources/generator/server/sonar/sonar.yml.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.