Skip to content

Commit

Permalink
- sonar.login is deprecated, use sonar.token or environment variable …
Browse files Browse the repository at this point in the history
…SONAR_TOKEN instead

- remove 'sonar.cxx.unknown.rule.id' property
- add unknown to all repositories
- activate mapping only if 'unknown' rule is active
- add integration tests
- correct hashValue and equal of CxxReportLocation
  • Loading branch information
guwirth committed Nov 14, 2024
1 parent c4947eb commit fb4e5ec
Show file tree
Hide file tree
Showing 40 changed files with 659 additions and 452 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.rule.RuleKey;
import org.sonar.cxx.utils.CxxReportIssue;
import org.sonar.cxx.utils.CxxReportLocation;
Expand All @@ -48,75 +42,63 @@
*/
public abstract class CxxIssuesReportSensor extends CxxReportSensor {

/**
* Key for rules that are not yet defined in sonar
*/
public static final String UNKNOWN_RULE_ID = "sonar.cxx.unknown.rule.id";
public static final String DEFAULT_UNKNOWN_RULE_ID = "unknown";

private static final Logger LOG = LoggerFactory.getLogger(CxxIssuesReportSensor.class);

private final Set<CxxReportIssue> uniqueIssues = new HashSet<>();
public static final String DEFAULT_UNKNOWN_RULE_KEY = "unknown";

private final HashSet<CxxReportIssue> uniqueIssues = new HashSet<>();
private int savedNewIssues = 0;

private final Map<String, List<String>> knownRulesPerRepositoryKey = new HashMap<>();
private final HashMap<String, HashSet<String>> knownRulesPerRepositoryKey = new HashMap<>();
private final HashSet<String> mappedRuleIds = new HashSet<>();

/**
* {@inheritDoc}
*/
protected CxxIssuesReportSensor() {
}

public static List<PropertyDefinition> properties() {
return Collections.unmodifiableList(Arrays.asList(
PropertyDefinition.builder(UNKNOWN_RULE_ID)
.name("Key id to be used to upload not yet defined new rules")
.description(
"This key is used to upload e.g., compiler warnings that are not yet defined in sonar. "
+ "The used key must be defined for each cxx sensor in sonar, e.g. compiler-gcc:unknown. "
+ "Default value is \"unknown\""
)
.category("CXX")
.subCategory("(1) General")
.defaultValue(DEFAULT_UNKNOWN_RULE_ID)
.onQualifiers(Qualifiers.PROJECT)
.build()
));
}

/**
* {@inheritDoc}
*/
@Override
public void executeImpl() {
List<String> ruleKeys;
try {
LOG.info("Downloading rule keys for {} from {}", getRuleRepositoryKey(), context.config()
.get("sonar.host.url")
.orElse("http://localhost:9000"));
ruleKeys = SonarRulesKeyLoader.getRulesFor(context.config()
.get("sonar.host.url").orElse("http://localhost:9000"), context.config().get("sonar.login")
.orElse(""), "cxx", getRuleRepositoryKey());
knownRulesPerRepositoryKey.put(getRuleRepositoryKey(), ruleKeys);
LOG.info("Donwloaded {} known rule keys for {}", ruleKeys.size(), getRuleRepositoryKey());
} catch (IOException | InterruptedException e) {
LOG.warn("Could not download known rule ids for " + getRuleRepositoryKey(), e);
}
downloadRulesFromServer();
List<File> reports = getReports(getReportPathsKey());
for (var report : reports) {
executeReport(report);
}
}

private void saveIssue(String ruleId, CxxReportIssue issue) {
String unknownRuleId = context.config().get(UNKNOWN_RULE_ID).orElse(DEFAULT_UNKNOWN_RULE_ID);
if (knownRulesPerRepositoryKey.containsKey(getRuleRepositoryKey())
&& !knownRulesPerRepositoryKey.get(getRuleRepositoryKey()).contains(ruleId)) {
LOG.warn("Rule {} is not known in sonar, will map to {}", ruleId, unknownRuleId);
issue.overRideRuleId(unknownRuleId);
issue.getLocations().get(0).prefixInfo("Unknown warning: " + ruleId + "; ");
ruleId = unknownRuleId;
private void downloadRulesFromServer() {

// deactivate mapping if 'unknown' rule is not active
if (context.activeRules().find(RuleKey.of(getRuleRepositoryKey(), DEFAULT_UNKNOWN_RULE_KEY)) == null) {
LOG.info("Rule mapping to '{}:{}' is not active", getRuleRepositoryKey(), DEFAULT_UNKNOWN_RULE_KEY);
return;
}

try {
String url = context.config().get("sonar.host.url").orElse("http://localhost:9000");
LOG.info("Downloading rules for '{}' from server '{}'", getRuleRepositoryKey(), url);
var ruleKeys = SonarServerWebApi.getRuleKeys(
url,
context.config().get("sonar.token")
.or(() -> context.config().get("sonar.login")) // deprecated: can be removed in future
.orElse(System.getenv("SONAR_TOKEN")),
"cxx",
getRuleRepositoryKey());
if (!ruleKeys.isEmpty()) {
knownRulesPerRepositoryKey.put(getRuleRepositoryKey(), ruleKeys);
LOG.debug("{} rules for '{}' were loaded from server", ruleKeys.size(), getRuleRepositoryKey());
}
} catch (IOException e) {
LOG.warn("Rules for '{}' could not be loaded from server", getRuleRepositoryKey(), e);
}
}

private void saveIssue(String ruleId, CxxReportIssue issue) {
ruleId = mapUnknownRuleId(ruleId, issue);
var newIssue = context.newIssue();
if (addLocations(newIssue, ruleId, issue)) {
addFlow(newIssue, issue);
Expand All @@ -125,6 +107,21 @@ private void saveIssue(String ruleId, CxxReportIssue issue) {
}
}

private String mapUnknownRuleId(String ruleId, CxxReportIssue issue) {
var repository = knownRulesPerRepositoryKey.get(getRuleRepositoryKey());
if (repository != null && !repository.contains(ruleId)) {
if (mappedRuleIds.add(ruleId)) {
LOG.info("Rule '{}' is unknown in '{}' and will be mapped to '{}'",
ruleId,
getRuleRepositoryKey(),
DEFAULT_UNKNOWN_RULE_KEY);
}
issue.addMappedInfo();
ruleId = DEFAULT_UNKNOWN_RULE_KEY;
}
return ruleId;
}

/**
* Saves code violation only if it wasn't already saved
*
Expand Down

This file was deleted.

47 changes: 0 additions & 47 deletions cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/Rule.java

This file was deleted.

This file was deleted.

Loading

0 comments on commit fb4e5ec

Please sign in to comment.