Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 91de3ee

Browse files
nchakarovvmwNikolay Chakarov
andauthored
Feature/add external configuration (#18)
* Fix comments regex. Signed-off-by: Nikolay Chakarov <[email protected]> * Add support for external configuration. Signed-off-by: Nikolay Chakarov <[email protected]> --------- Signed-off-by: Nikolay Chakarov <[email protected]> Co-authored-by: Nikolay Chakarov <[email protected]>
1 parent 1804d70 commit 91de3ee

File tree

8 files changed

+110
-16
lines changed

8 files changed

+110
-16
lines changed

.its-sonar-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[{
2+
"filepath": "src/main/java/org/sonar/plugins/its/java/SourceComment.java",
3+
"terms": "master"
4+
}]

build.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Copyright 2022 VMware, Inc.
2+
# Copyright 2023 VMware, Inc.
33
# SPDX-License-Identifier: BSD-2
44
pwd
55
mkdir plugins

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
Copyright 2022 VMware, Inc.
3+
Copyright 2023 VMware, Inc.
44
SPDX-License-Identifier: BSD-2
55
-->
66
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -102,6 +102,16 @@
102102
<version>1.2.0</version>
103103
<scope>test</scope>
104104
</dependency>
105+
<dependency>
106+
<groupId>org.json</groupId>
107+
<artifactId>json</artifactId>
108+
<version>20230227</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>com.vaadin.external.google</groupId>
112+
<artifactId>android-json</artifactId>
113+
<version>0.0.20131108.vaadin1</version>
114+
</dependency>
105115
</dependencies>
106116

107117
<build>

src/main/java/org/sonar/plugins/its/java/SourceComment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/***********************************************************
2-
* Copyright 2022 VMware, Inc.
2+
* Copyright 2023 VMware, Inc.
33
* SPDX-License-Identifier: BSD-2
44
***********************************************************/
55
package org.sonar.plugins.its.java;
66

7+
/**
8+
* Class describing the start and the end of a comment.
9+
* The term master shouldn't be detected here because of the external configuration.
10+
*/
711
public class SourceComment {
812
private long start;
913
private long end;

src/main/java/org/sonar/plugins/its/java/checks/ITSCommentsRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***********************************************************
2-
* Copyright 2022 VMware, Inc.
2+
* Copyright 2023 VMware, Inc.
33
* SPDX-License-Identifier: BSD-2
44
***********************************************************/
55
package org.sonar.plugins.its.java.checks;
@@ -17,7 +17,7 @@
1717
* It should detect master, slave, kill, etc
1818
*/
1919
@Rule(key = ITSCommentsRule.KEY, name = "Inclusive Terminology Comments Scanner",
20-
description = "Scan text/source files comments for presense of offensive terms",
20+
description = "Scan text/source files comments for presence of offensive terms",
2121
priority = Priority.MAJOR)
2222
public class ITSCommentsRule extends BaseTreeVisitor implements JavaFileScanner {
2323

src/main/java/org/sonar/plugins/its/java/checks/ITSJavaFilesSensor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***********************************************************
2-
* Copyright 2022 VMware, Inc.
2+
* Copyright 2023 VMware, Inc.
33
* SPDX-License-Identifier: BSD-2
44
***********************************************************/
55
package org.sonar.plugins.its.java.checks;
@@ -12,9 +12,13 @@
1212
import org.sonar.api.batch.sensor.Sensor;
1313
import org.sonar.api.batch.sensor.SensorContext;
1414
import org.sonar.api.batch.sensor.SensorDescriptor;
15+
import org.sonar.plugins.its.service.ConfigService;
1516
import org.sonar.plugins.its.service.ItsFileScanner;
1617
import org.sonar.plugins.java.api.JavaFileScannerContext;
1718

19+
import java.util.List;
20+
import java.util.Map;
21+
1822
/**
1923
* Generates issues on all java files at line 1. This rule must be activated in
2024
* the Quality profile.
@@ -41,6 +45,9 @@ public void describe(SensorDescriptor descriptor) {
4145
public void execute(SensorContext context) {
4246
FileSystem fs = context.fileSystem();
4347
Iterable<InputFile> files = fs.inputFiles(fs.predicates().all());
48+
49+
Map<String, List<String>> config = ConfigService.loadConfig(context);
50+
4451
for (InputFile file : files) {
4552
try {
4653
String filename = file.filename();
@@ -50,9 +57,8 @@ public void execute(SensorContext context) {
5057
continue;
5158
}
5259
logger.info("Scanning " + file.filename());
53-
5460
ItsFileScanner scanner = new ItsFileScanner();
55-
scanner.scanFile(context, this, file);
61+
scanner.scanFile(context, file, config.get(file.relativePath()));
5662
} catch (Exception e) {
5763
logger.error("An error occurred with file " + file.filename());
5864
logger.error(e.getMessage(), e.getStackTrace());
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***********************************************************
2+
* Copyright 2023 VMware, Inc.
3+
* SPDX-License-Identifier: BSD-2
4+
***********************************************************/
5+
package org.sonar.plugins.its.service;
6+
7+
import org.apache.commons.io.IOUtils;
8+
import org.json.JSONArray;
9+
import org.json.JSONObject;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.sonar.api.batch.fs.FileSystem;
13+
import org.sonar.api.batch.sensor.SensorContext;
14+
15+
import java.io.File;
16+
import java.io.FileInputStream;
17+
import java.io.InputStream;
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
/**
24+
* Class responsible for loading the external configuration for the plugin.
25+
*/
26+
public class ConfigService {
27+
private static final Logger logger = LoggerFactory.getLogger(ConfigService.class);
28+
29+
public static Map<String, List<String>> loadConfig(SensorContext context) {
30+
FileSystem fs = context.fileSystem();
31+
32+
File[] configFiles = fs.baseDir().listFiles((dir, name) -> name.contains("its-sonar-config"));
33+
34+
Map<String, List<String>> config = new HashMap<>();
35+
36+
if (configFiles.length > 0) {
37+
try {
38+
InputStream is = new FileInputStream(configFiles[0]);
39+
String jsonTxt = IOUtils.toString(is, "UTF-8");
40+
logger.info("ConfigLoader: " + jsonTxt);
41+
JSONArray jsonConfig = new JSONArray(jsonTxt);
42+
for (int i = 0; i < jsonConfig.length(); i++) {
43+
JSONObject property = jsonConfig.getJSONObject(i);
44+
String filepath = property.getString("filepath");
45+
String terms = property.getString("terms");
46+
config.put(filepath, Arrays.asList(terms.split(",")));
47+
}
48+
} catch (Exception e) {
49+
logger.error("Config file can't be opened", e);
50+
}
51+
}
52+
53+
return config;
54+
}
55+
}

src/main/java/org/sonar/plugins/its/service/ItsFileScanner.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***********************************************************
2-
* Copyright 2022 VMware, Inc.
2+
* Copyright 2023 VMware, Inc.
33
* SPDX-License-Identifier: BSD-2
44
***********************************************************/
55
package org.sonar.plugins.its.service;
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.regex.Matcher;
1111
import java.util.regex.Pattern;
12+
import java.util.stream.Collectors;
1213

1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
@@ -23,14 +24,18 @@
2324
import org.sonar.plugins.its.utils.LargeFileEncounteredException;
2425
import org.sonar.plugins.its.utils.LineNumberFinderUtil;
2526

26-
/** Implementation of regex scanner using ITS rules */
27+
/**
28+
* Implementation of regex scanner using ITS rules
29+
*/
2730
public class ItsFileScanner {
2831

2932
private static final Logger logger = LoggerFactory.getLogger(ItsFileScanner.class);
3033
protected static final int MAX_CHARACTERS_SCANNED = 10 * 1024 * 1024; // 10 Mb limit
3134

32-
/** Scan file for offensive terms */
33-
public void scanFile(SensorContext context, Sensor sensor, InputFile file) {
35+
/**
36+
* Scan file for offensive terms
37+
*/
38+
public void scanFile(SensorContext context, InputFile file, List<String> excludeTerms) {
3439
int lineNumberOfTriggerMatch = -1;
3540

3641
List<SourceComment> comments = new ArrayList<SourceComment>();
@@ -71,7 +76,17 @@ public void scanFile(SensorContext context, Sensor sensor, InputFile file) {
7176
}
7277
}
7378

74-
List<ItsScanRule> rules = ItsRulesManager.getRules();
79+
List<ItsScanRule> rules = ItsRulesManager.getRules()
80+
.stream()
81+
.filter(rule -> {
82+
if (excludeTerms != null && !excludeTerms.isEmpty()) {
83+
return !excludeTerms.contains(rule.getTerm());
84+
} else {
85+
return true;
86+
}
87+
})
88+
.collect(Collectors.toList());
89+
7590
if (rules == null)
7691
return;
7792

@@ -89,11 +104,11 @@ public void scanFile(SensorContext context, Sensor sensor, InputFile file) {
89104
// checks if in comment and sets different rule
90105
if (isInComment(matcher, comments)) {
91106
String message = "Offensive term: " + rule.getTerm() + ", replace with: " + rule.getReplacements();
92-
logger.info("COMMENT Offensive Term "+path + ":" + lineNumberOfTriggerMatch + " - " + message);
107+
logger.info("COMMENT Offensive Term " + path + ":" + lineNumberOfTriggerMatch + " - " + message);
93108
createViolation(context, file, rule, lineNumberOfTriggerMatch, message, true);
94109
} else {
95110
String message = "SOURCE Offensive term: " + rule.getTerm() + ", replace with: " + rule.getReplacements();
96-
logger.info("SOURCE Offensive Term "+path + ":" + lineNumberOfTriggerMatch + " - " + message);
111+
logger.info("SOURCE Offensive Term " + path + ":" + lineNumberOfTriggerMatch + " - " + message);
97112
createViolation(context, file, rule, lineNumberOfTriggerMatch, message, false);
98113
}
99114
}
@@ -115,7 +130,7 @@ private boolean isInComment(Matcher matcher, List<SourceComment> comments) {
115130
}
116131

117132
private void createViolation(SensorContext context, InputFile file, ItsScanRule rule, int line, String message,
118-
boolean commentRule) {
133+
boolean commentRule) {
119134
// no need to define the severity as it is automatically set according to the
120135
// configured Quality profile
121136
NewIssue issue;

0 commit comments

Comments
 (0)