Search in sources :

Example 1 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarlint-core by SonarSource.

the class StandaloneActiveRulesProvider method registerProfile.

private static void registerProfile(ActiveRulesBuilder builder, String language, Map.Entry<String, Collection<RulesProfile>> entry) {
    for (RulesProfile rp : entry.getValue()) {
        for (ActiveRule ar : rp.getActiveRules()) {
            NewActiveRule newAr = builder.create(RuleKey.of(ar.getRepositoryKey(), ar.getRuleKey())).setLanguage(language).setName(ar.getRule().getName()).setSeverity(ar.getSeverity().name()).setInternalKey(ar.getConfigKey());
            for (ActiveRuleParam param : ar.getActiveRuleParams()) {
                newAr.setParam(param.getKey(), param.getValue());
            }
            newAr.activate();
        }
    }
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRule(org.sonar.api.rules.ActiveRule)

Example 2 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarlint-core by SonarSource.

the class SonarQubeActiveRulesProvider method createNewActiveRule.

private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) {
    RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey());
    Rule rule = rules.find(ruleKey);
    Sonarlint.Rules.Rule storageRule;
    try {
        storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString());
    } catch (IllegalArgumentException e) {
        throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding.");
    }
    NewActiveRule newActiveRule = builder.create(ruleKey).setLanguage(language).setName(rule.name()).setInternalKey(rule.internalKey()).setSeverity(activeRule.getSeverity());
    if (!StringUtils.isEmpty(storageRule.getTemplateKey())) {
        RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey());
        newActiveRule.setTemplateRuleKey(templateRuleKey.rule());
    }
    for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) {
        newActiveRule.setParam(param.getKey(), param.getValue());
    }
    newActiveRule.activate();
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) MessageException(org.sonarsource.sonarlint.core.client.api.exceptions.MessageException) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Rule(org.sonar.api.batch.rule.Rule) ActiveRule(org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Rules(org.sonar.api.batch.rule.Rules) Map(java.util.Map)

Example 3 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonar-web by SonarSource.

the class WebSensorTest method setUp.

@Before
public void setUp() throws Exception {
    WebRulesDefinition rulesDefinition = new WebRulesDefinition();
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository repository = context.repository(WebRulesDefinition.REPOSITORY_KEY);
    List<NewActiveRule> ar = new ArrayList<>();
    for (RulesDefinition.Rule rule : repository.rules()) {
        ar.add(new ActiveRulesBuilder().create(RuleKey.of(WebRulesDefinition.REPOSITORY_KEY, rule.key())));
    }
    ActiveRules activeRules = new DefaultActiveRules(ar);
    CheckFactory checkFactory = new CheckFactory(activeRules);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
    sensor = new WebSensor(new NoSonarFilter(), fileLinesContextFactory, checkFactory);
    tester = SensorContextTester.create(TEST_DIR);
}
Also used : FileLinesContext(org.sonar.api.measures.FileLinesContext) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) WebRulesDefinition(org.sonar.plugins.web.rules.WebRulesDefinition) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) CheckFactory(org.sonar.api.batch.rule.CheckFactory) WebRulesDefinition(org.sonar.plugins.web.rules.WebRulesDefinition) FileLinesContext(org.sonar.api.measures.FileLinesContext) Before(org.junit.Before)

Example 4 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesProvider method transform.

private static ActiveRules transform(Collection<LoadedActiveRule> loadedRules) {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    for (LoadedActiveRule activeRule : loadedRules) {
        NewActiveRule newActiveRule = builder.create(activeRule.getRuleKey());
        newActiveRule.setName(activeRule.getName());
        newActiveRule.setSeverity(activeRule.getSeverity());
        newActiveRule.setCreatedAt(activeRule.getCreatedAt());
        newActiveRule.setLanguage(activeRule.getLanguage());
        newActiveRule.setInternalKey(activeRule.getInternalKey());
        newActiveRule.setTemplateRuleKey(activeRule.getTemplateRuleKey());
        // load parameters
        if (activeRule.getParams() != null) {
            for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) {
                newActiveRule.setParam(params.getKey(), params.getValue());
            }
        }
        newActiveRule.activate();
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesPublisherTest method write.

@Test
public void write() throws Exception {
    File outputDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(outputDir);
    NewActiveRule ar = new ActiveRulesBuilder().create(RuleKey.of("java", "S001")).setSeverity("BLOCKER").setParam("p1", "v1");
    ActiveRules activeRules = new DefaultActiveRules(Arrays.asList(ar));
    ActiveRulesPublisher underTest = new ActiveRulesPublisher(activeRules);
    underTest.publish(writer);
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    try (CloseableIterator<ScannerReport.ActiveRule> readIt = reader.readActiveRules()) {
        ScannerReport.ActiveRule reportAr = readIt.next();
        assertThat(reportAr.getRuleRepository()).isEqualTo("java");
        assertThat(reportAr.getRuleKey()).isEqualTo("S001");
        assertThat(reportAr.getSeverity()).isEqualTo(Constants.Severity.BLOCKER);
        assertThat(reportAr.getParamsByKey()).hasSize(1);
        assertThat(reportAr.getParamsByKey().entrySet().iterator().next().getKey()).isEqualTo("p1");
        assertThat(reportAr.getParamsByKey().entrySet().iterator().next().getValue()).isEqualTo("v1");
        assertThat(readIt.hasNext()).isFalse();
    }
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesPublisher(org.sonar.scanner.report.ActiveRulesPublisher) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) File(java.io.File) Test(org.junit.Test)

Aggregations

NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)7 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)5 ActiveRules (org.sonar.api.batch.rule.ActiveRules)4 Map (java.util.Map)3 HashMap (java.util.HashMap)2 InputFile (org.sonar.api.batch.fs.InputFile)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RuleKey (org.sonar.api.rule.RuleKey)2 ActiveRule (org.sonar.api.rules.ActiveRule)2 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1