Skip to content

Commit 66f2be6

Browse files
Initial doSonarAPI common rules
0 parents  commit 66f2be6

File tree

1,115 files changed

+67051
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,115 files changed

+67051
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target/
2+
.idea/
3+
*.iml
4+
.project
5+
.settings/
6+
# vim temp file
7+
*~
8+
.sonar/
9+
.settings/
10+
.classpath
11+
settings.json

.scripts/clean.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if [ -z "$1" ]
2+
then
3+
echo "No argument supplied. Correct use: clean.sh <path>"
4+
else
5+
find $1 -type f -name "*.yaml" -exec sed -i "/^\s*$/d" {} \;
6+
fi
7+

LICENSE

Lines changed: 858 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<p align="center">
2+
<a href="https://apiaddicts.org/">
3+
<img src="https://apiaddicts-web.s3.eu-west-1.amazonaws.com/wp-content/uploads/2022/03/17155736/cropped-APIAddicts-logotipo_rojo-2048x523.png">
4+
</a>
5+
</p>
6+
7+
## Contributors
8+
### CloudAPPi
9+
CloudAppi is one leader in APIs in global word. See the [CloudAPPi Services](https://cloudappi.net)
10+
11+
### Madrid Digital
12+
Madrid Digital is a public administration in Spain. See the [Comunidad de Madrid website](https://www.comunidad.madrid/)
13+
14+
## Configure scanner
15+
16+
### Maven plugin
17+
18+
#### Configure properties
19+
20+
In `pom.xml` configure:
21+
22+
````xml
23+
<properties>
24+
<!-- Optional, When is set only the language specified is analyzed -->
25+
<sonar.language>openapi</sonar.language>
26+
<!-- Optional, Default value is src/main,pom.xml -->
27+
<sonar.sources>.</sonar.sources>
28+
</properties>
29+
````
30+
31+
#### Run scanner
32+
33+
`mvn sonar:sonar -Dsonar.host.url=<HOST> -Dsonar.login=<KEY>`
34+
35+
### External `sonar-scanner`
36+
37+
#### Install `sonar-scanner`
38+
39+
Download the `sonar-scanner` from https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/ and make it accessible.
40+
41+
#### Configure properties
42+
43+
In `sonar-project.properties` (file in root project folder) configure:
44+
45+
````properties
46+
# must be unique in a given SonarQube instance
47+
sonar.projectKey=test:test
48+
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
49+
sonar.projectName=OpenAPI plugin tests
50+
sonar.projectVersion=1.0-SNAPSHOT
51+
52+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
53+
# This property is optional if sonar.modules is set.
54+
sonar.sources=.
55+
56+
# Encoding of the source code. Default is default system encoding
57+
sonar.sourceEncoding=UTF-8
58+
# Select the language to use for analysis
59+
sonar.language=openapi
60+
````
61+
62+
#### Run scanner
63+
64+
`sonar-scanner -Dsonar.host.url=<HOST> -Dsonar.login=<KEY>`
65+
66+
## Compatibility
67+
68+
This plugin is supported by SonarQube versions greater or equal to `6.7.4`
69+
70+
### Explicit compatibility versions tested
71+
72+
| Version |
73+
|---------|
74+
| `6.7.4` |
75+
| `7.9-community` |
76+
| `8.3-community` |

pom.xml

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.sonar.samples.openapi</groupId>
6+
<artifactId>dosonarapi-community</artifactId>
7+
<version>1.0.3-SNAPSHOT</version>
8+
<packaging>sonar-plugin</packaging>
9+
10+
<name>SonarQube OpenAPI Custom Rules Example</name>
11+
<description>OpenAPI Custom Rules Example for SonarQube</description>
12+
<inceptionYear>2018</inceptionYear>
13+
14+
<properties>
15+
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
18+
<sonar.version>8.7.0.41497</sonar.version>
19+
<sonarQubeMinVersion>6.7</sonarQubeMinVersion>
20+
<sonaropenapi.version>1.0.1</sonaropenapi.version>
21+
<sonaranalyzer.version>1.22.0.848</sonaranalyzer.version>
22+
<orgjson.version>20220320</orgjson.version>
23+
<junit.version>4.13.2</junit.version>
24+
<assertj.version>3.22.0</assertj.version>
25+
26+
<jacoco.maven.plugin.version>0.8.6</jacoco.maven.plugin.version>
27+
<sonar.maven.plugin.version>3.7.0.1746</sonar.maven.plugin.version>
28+
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
29+
<sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
30+
<sonar.language>java</sonar.language>
31+
</properties>
32+
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apiaddicts.apitools.dosonarapi</groupId>
37+
<artifactId>openapi-front-end</artifactId>
38+
<version>${sonaropenapi.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.sonarsource.sonarqube</groupId>
42+
<artifactId>sonar-plugin-api</artifactId>
43+
<version>${sonar.version}</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.sonarsource.analyzer-commons</groupId>
48+
<artifactId>sonar-analyzer-commons</artifactId>
49+
<version>${sonaranalyzer.version}</version>
50+
</dependency>
51+
<!-- https://mvnrepository.com/artifact/org.json/json -->
52+
<dependency>
53+
<groupId>org.json</groupId>
54+
<artifactId>json</artifactId>
55+
<version>${orgjson.version}</version>
56+
</dependency>
57+
58+
<!-- test dependencies -->
59+
<dependency>
60+
<groupId>org.apiaddicts.apitools.dosonarapi</groupId>
61+
<artifactId>openapi-test-tools</artifactId>
62+
<version>${sonaropenapi.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>junit</groupId>
66+
<artifactId>junit</artifactId>
67+
<version>${junit.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.assertj</groupId>
72+
<artifactId>assertj-core</artifactId>
73+
<version>${assertj.version}</version>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
77+
78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
82+
<artifactId>sonar-packaging-maven-plugin</artifactId>
83+
<version>1.17</version>
84+
<extensions>true</extensions>
85+
<configuration>
86+
<pluginKey>openapi-custom</pluginKey>
87+
<pluginName>OpenAPI Custom</pluginName>
88+
<pluginClass>org.sonar.samples.openapi.OpenAPICustomPlugin</pluginClass>
89+
<skipDependenciesPackaging>true</skipDependenciesPackaging>
90+
<sonarLintSupported>true</sonarLintSupported>
91+
<sonarQubeMinVersion>${sonarQubeMinVersion}</sonarQubeMinVersion>
92+
<basePlugin>openapi</basePlugin>
93+
</configuration>
94+
</plugin>
95+
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-compiler-plugin</artifactId>
99+
<version>3.6.0</version>
100+
<configuration>
101+
<source>1.8</source>
102+
<target>1.8</target>
103+
</configuration>
104+
</plugin>
105+
106+
<plugin>
107+
<artifactId>maven-shade-plugin</artifactId>
108+
<version>3.2.4</version>
109+
<executions>
110+
<execution>
111+
<phase>package</phase>
112+
<goals>
113+
<goal>shade</goal>
114+
</goals>
115+
<configuration>
116+
<shadedArtifactAttached>false</shadedArtifactAttached>
117+
<createDependencyReducedPom>false</createDependencyReducedPom>
118+
<minimizeJar>true</minimizeJar>
119+
<filters>
120+
<filter>
121+
<artifact>jaxen:jaxen</artifact>
122+
<includes>
123+
<include>**</include>
124+
</includes>
125+
</filter>
126+
<filter>
127+
<artifact>*:*</artifact>
128+
<excludes>
129+
<exclude>META-INF/LICENSE*</exclude>
130+
<exclude>META-INF/NOTICE*</exclude>
131+
<exclude>META-INF/*.RSA</exclude>
132+
<exclude>META-INF/*.SF</exclude>
133+
<exclude>LICENSE*</exclude>
134+
<exclude>NOTICE*</exclude>
135+
</excludes>
136+
</filter>
137+
</filters>
138+
</configuration>
139+
</execution>
140+
</executions>
141+
</plugin>
142+
143+
<plugin>
144+
<groupId>org.jacoco</groupId>
145+
<artifactId>jacoco-maven-plugin</artifactId>
146+
<version>${jacoco.maven.plugin.version}</version>
147+
<configuration>
148+
<destFile>${sonar.jacoco.reportPaths}</destFile>
149+
<append>true</append>
150+
</configuration>
151+
<executions>
152+
<execution>
153+
<id>agent</id>
154+
<goals>
155+
<goal>prepare-agent</goal>
156+
</goals>
157+
</execution>
158+
</executions>
159+
</plugin>
160+
</plugins>
161+
</build>
162+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.sonar.samples.openapi;
2+
3+
public class I18nContext {
4+
5+
private I18nContext() {
6+
// Intentional blank
7+
}
8+
9+
private static final String DEFAULT_LANG = "en";
10+
11+
private static String lang;
12+
13+
public static String getLang() {
14+
return lang == null ? DEFAULT_LANG : lang;
15+
}
16+
17+
public static void initializeFromUserLanguage() {
18+
if (lang == null) setLang(System.getProperty("user.language"));
19+
}
20+
21+
public static void setLang(String lang) {
22+
I18nContext.lang = lang;
23+
}
24+
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sonar.samples.openapi;
2+
3+
import org.sonar.api.Plugin;
4+
5+
/**
6+
* Entry point of your plugin containing your custom rules.
7+
*/
8+
public class OpenAPICustomPlugin implements Plugin {
9+
10+
@Override
11+
public void define(Context context) {
12+
context.addExtensions(
13+
// server extensions -> objects are instantiated during server start
14+
OpenAPICustomProfileDefinition.class,
15+
OpenAPICustomRulesDefinition.class,
16+
// batch extensions -> objects are instantiated during code analysis
17+
OpenAPICustomRuleRepository.class
18+
);
19+
}
20+
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.sonar.samples.openapi;
2+
3+
import org.apiaddicts.apitools.dosonarapi.api.OpenApiCustomRuleRepository;
4+
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
5+
import org.sonar.api.utils.AnnotationUtils;
6+
import org.sonar.check.Rule;
7+
import org.sonar.samples.openapi.checks.RulesLists;
8+
9+
import javax.annotation.Nullable;
10+
import java.util.List;
11+
12+
/**
13+
* Declare a new quality profile that comprises all the custom rules, plus the SonarOpenApi standard rules.
14+
* <p>
15+
* This allows to create a built-in profile that extends the Sonar Way profile, and that includes your rules.
16+
* This profile will automatically inherit any new rule brought in by the core plugin.
17+
*/
18+
public class OpenAPICustomProfileDefinition implements BuiltInQualityProfilesDefinition {
19+
public static final String MY_COMPANY_WAY = "Custom";
20+
21+
public OpenAPICustomProfileDefinition() {
22+
this(null);
23+
}
24+
25+
public OpenAPICustomProfileDefinition(@Nullable OpenApiCustomRuleRepository[] repositories) {
26+
}
27+
28+
@Override
29+
public void define(Context context) {
30+
NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(MY_COMPANY_WAY, "openapi");
31+
addRepositoryRules(profile, OpenAPICustomRulesDefinition.REPOSITORY_KEY, RulesLists.getAllChecks());
32+
profile.done();
33+
}
34+
35+
private void addRepositoryRules(NewBuiltInQualityProfile profile, String key, List<Class<?>> checks) {
36+
for (Class<?> check : checks) {
37+
Rule annotation = AnnotationUtils.getAnnotation(check, Rule.class);
38+
profile.activateRule(key, annotation.key());
39+
}
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.sonar.samples.openapi;
2+
3+
import org.sonar.api.ExtensionPoint;
4+
import org.sonar.api.scanner.ScannerSide;
5+
import org.apiaddicts.apitools.dosonarapi.api.OpenApiCustomRuleRepository;
6+
import org.sonar.samples.openapi.checks.RulesLists;
7+
import org.sonarsource.api.sonarlint.SonarLintSide;
8+
9+
import java.util.List;
10+
11+
import static org.sonar.samples.openapi.OpenAPICustomRulesDefinition.REPOSITORY_KEY;
12+
13+
/**
14+
* Makes the rules visible to the OpenAPI scanner sensor,
15+
* hence adds to the classes that are going to be executed during source code analysis.
16+
* <p>
17+
* This class is a batch extension by implementing the {@link OpenApiCustomRuleRepository}
18+
*/
19+
@SonarLintSide
20+
@ScannerSide
21+
@ExtensionPoint
22+
public class OpenAPICustomRuleRepository implements OpenApiCustomRuleRepository {
23+
@Override
24+
public String repositoryKey() {
25+
return REPOSITORY_KEY;
26+
}
27+
28+
@Override
29+
public List<Class<?>> checkClasses() {
30+
return RulesLists.getAllChecks();
31+
}
32+
}

0 commit comments

Comments
 (0)