org.sonarsource.sonarlint.core.serverconnection.events.ruleset.UpdateStorageOnRuleSetChanged Maven / Gradle / Ivy
/*
* SonarLint Core - Server Connection
* Copyright (C) 2016-2023 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.sonarlint.core.serverconnection.events.ruleset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.sonarsource.sonarlint.core.serverapi.push.RuleSetChangedEvent;
import org.sonarsource.sonarlint.core.serverapi.rules.ServerActiveRule;
import org.sonarsource.sonarlint.core.serverconnection.AnalyzerConfiguration;
import org.sonarsource.sonarlint.core.serverconnection.ConnectionStorage;
import org.sonarsource.sonarlint.core.serverconnection.RuleSet;
import org.sonarsource.sonarlint.core.serverconnection.events.ServerEventHandler;
public class UpdateStorageOnRuleSetChanged implements ServerEventHandler {
private final ConnectionStorage storage;
public UpdateStorageOnRuleSetChanged(ConnectionStorage storage) {
this.storage = storage;
}
@Override
public void handle(RuleSetChangedEvent event) {
event.getProjectKeys().forEach(projectKey -> storage.project(projectKey).analyzerConfiguration().update(currentConfiguration -> {
var newRuleSetByLanguageKey = incorporate(event, currentConfiguration.getRuleSetByLanguageKey());
return new AnalyzerConfiguration(currentConfiguration.getSettings(), newRuleSetByLanguageKey, currentConfiguration.getSchemaVersion());
}));
}
private static Map incorporate(RuleSetChangedEvent event, Map ruleSetByLanguageKey) {
Map resultingRuleSetsByLanguageKey = new HashMap<>(ruleSetByLanguageKey);
event.getDeactivatedRules().forEach(deactivatedRule -> deactivate(deactivatedRule, resultingRuleSetsByLanguageKey));
event.getActivatedRules().forEach(activatedRule -> activate(activatedRule, resultingRuleSetsByLanguageKey));
return resultingRuleSetsByLanguageKey;
}
private static void activate(RuleSetChangedEvent.ActiveRule activatedRule, Map ruleSetsByLanguageKey) {
var ruleLanguageKey = activatedRule.getLanguageKey();
var currentRuleSet = ruleSetsByLanguageKey.computeIfAbsent(ruleLanguageKey, k -> new RuleSet(Collections.emptyList(), ""));
var languageRulesByKey = new HashMap<>(currentRuleSet.getRulesByKey());
var ruleTemplateKey = activatedRule.getTemplateKey();
languageRulesByKey.put(activatedRule.getKey(), new ServerActiveRule(
activatedRule.getKey(),
activatedRule.getSeverity(),
activatedRule.getParameters(),
ruleTemplateKey == null ? "" : ruleTemplateKey));
ruleSetsByLanguageKey.put(ruleLanguageKey, new RuleSet(new ArrayList<>(languageRulesByKey.values()), currentRuleSet.getLastModified()));
}
private static void deactivate(String deactivatedRuleKey, Map ruleSetsByLanguageKey) {
var ruleSetsIterator = ruleSetsByLanguageKey.entrySet().iterator();
while (ruleSetsIterator.hasNext()) {
var ruleSetEntry = ruleSetsIterator.next();
var ruleSet = ruleSetEntry.getValue();
var newRules = new HashMap<>(ruleSet.getRulesByKey());
newRules.remove(deactivatedRuleKey);
if (newRules.isEmpty()) {
ruleSetsIterator.remove();
} else {
ruleSetsByLanguageKey.put(ruleSetEntry.getKey(), new RuleSet(List.copyOf(newRules.values()), ruleSet.getLastModified()));
}
}
}
}