org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader Maven / Gradle / Ivy
/*
* SonarQube Analyzer Commons
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.analyzer.commons;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.sonar.api.SonarRuntime;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
/**
* Use to create {@link NewBuiltInQualityProfile} based on json file, for profiles implementing {@link org.sonar.api.server.profile.BuiltInQualityProfilesDefinition}
*
* Not designed for multi-threads
*/
public final class BuiltInQualityProfileJsonLoader {
private BuiltInQualityProfileJsonLoader() {
}
public static void load(NewBuiltInQualityProfile profile, String repositoryKey, String jsonProfilePath) {
Set activeKeys = loadActiveKeysFromJsonProfile(jsonProfilePath);
for (String activeKey : activeKeys) {
profile.activateRule(repositoryKey, activeKey);
}
}
/**
* Avoids activating Security Hotspots rules in a given profile if those are not supported (i.e. SQ version < 7.3)
*/
public static void load(
NewBuiltInQualityProfile profile,
String repositoryKey,
String jsonProfilePath,
String resourcePath,
SonarRuntime sonarRuntime
) {
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(resourcePath, jsonProfilePath, sonarRuntime);
Set activeKeys = loadActiveKeysFromJsonProfile(jsonProfilePath);
activeKeys.stream()
.filter(ruleKey -> {
Map ruleMetadata = ruleMetadataLoader.getMetadata(ruleKey);
return !ruleMetadataLoader.isSecurityHotspot(ruleMetadata) || ruleMetadataLoader.securityHotspotsSupported();
})
.forEach(activeKey -> profile.activateRule(repositoryKey, activeKey));
}
static Set loadActiveKeysFromJsonProfile(String profilePath) {
JsonParser jsonParser = new JsonParser();
Map root;
try {
root = jsonParser.parse(Resources.toString(profilePath, UTF_8));
} catch (IOException e) {
throw new IllegalStateException("Can't read resource: " + profilePath, e);
}
List ruleKeys = (List) root.get("ruleKeys");
if (ruleKeys == null) {
throw new IllegalStateException("missing 'ruleKeys'");
}
return new HashSet<>(ruleKeys);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy