Search in sources :

Example 1 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class SonarLintClientsRegistryTest method createRuleChange.

private RuleChange createRuleChange() {
    RuleChange javaRule = new RuleChange();
    javaRule.setLanguage("java");
    javaRule.setParams(new ParamChange[] { new ParamChange("param-key", "param-value") });
    javaRule.setTemplateKey("template-key");
    javaRule.setSeverity(Severity.CRITICAL);
    javaRule.setKey("rule-key");
    return javaRule;
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ParamChange(org.sonar.core.util.ParamChange)

Example 2 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImplTest method distributeRuleChangeEvent.

@Test
public void distributeRuleChangeEvent() {
    QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
    // Template rule
    RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
    db.rules().insert(templateRule.getDefinition());
    // Custom rule
    RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition()).setLanguage("xoo").setDescription("<div>line1\nline2</div>").setDescriptionFormat(MARKDOWN);
    db.rules().insert(rule1);
    ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
    ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
    activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
    Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
    ProjectDto project = db.components().insertPrivateProjectDto();
    db.qualityProfiles().associateWithProject(project, qualityProfileDto);
    underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
    ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
    verify(eventsDistributor).pushEvent(eventCaptor.capture());
    RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
    assertThat(ruleSetChangedEvent).isNotNull();
    assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent, RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects).containsExactly("RuleSetChanged", "xoo", new String[] { project.getKey() });
    assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", null, "template-key"));
    assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
    assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly("paramChangeKey", "paramChangeValue");
    assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ParamChange(org.sonar.core.util.ParamChange) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 3 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImpl method toRuleChange.

@NotNull
private RuleChange toRuleChange(RuleDto ruleDto, List<ActiveRuleParamDto> activeRuleParamDtos) {
    RuleChange ruleChange = new RuleChange();
    ruleChange.setKey(ruleDto.getRuleKey());
    ruleChange.setLanguage(ruleDto.getLanguage());
    ruleChange.setSeverity(ruleDto.getSeverityString());
    List<ParamChange> paramChanges = new ArrayList<>();
    for (ActiveRuleParamDto activeRuleParam : activeRuleParamDtos) {
        paramChanges.add(new ParamChange(activeRuleParam.getKey(), activeRuleParam.getValue()));
    }
    ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
    String templateUuid = ruleDto.getTemplateUuid();
    if (templateUuid != null && !"".equals(templateUuid)) {
        try (DbSession dbSession = dbClient.openSession(false)) {
            RuleDto templateRule = dbClient.ruleDao().selectByUuid(templateUuid, dbSession).orElseThrow(() -> new IllegalStateException(String.format("Unknown Template Rule '%s'", templateUuid)));
            ruleChange.setTemplateKey(templateRule.getRuleKey());
        }
    }
    return ruleChange;
}
Also used : ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) DbSession(org.sonar.db.DbSession) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ParamChange(org.sonar.core.util.ParamChange) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImpl method distributeRuleChangeEvent.

public void distributeRuleChangeEvent(Collection<QProfileDto> profiles, List<ActiveRuleChange> activeRuleChanges, String language) {
    if (activeRuleChanges.isEmpty()) {
        return;
    }
    Set<RuleChange> activatedRules = new HashSet<>();
    Set<RuleChange> deactivatedRules = new HashSet<>();
    for (ActiveRuleChange arc : activeRuleChanges) {
        ActiveRuleDto activeRule = arc.getActiveRule();
        if (activeRule == null) {
            continue;
        }
        RuleChange ruleChange = new RuleChange();
        ruleChange.setKey(activeRule.getRuleKey().rule());
        ruleChange.setSeverity(arc.getSeverity());
        ruleChange.setLanguage(language);
        Optional<String> templateKey = templateKey(arc);
        templateKey.ifPresent(ruleChange::setTemplateKey);
        // params
        List<ParamChange> paramChanges = new ArrayList<>();
        for (Map.Entry<String, String> entry : arc.getParameters().entrySet()) {
            paramChanges.add(new ParamChange(entry.getKey(), entry.getValue()));
        }
        ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
        switch(arc.getType()) {
            case ACTIVATED:
            case UPDATED:
                activatedRules.add(ruleChange);
                break;
            case DEACTIVATED:
                deactivatedRules.add(ruleChange);
                break;
        }
    }
    Set<String> projectKeys = getProjectKeys(profiles);
    if (activatedRules.isEmpty() && deactivatedRules.isEmpty()) {
        return;
    }
    RuleSetChangedEvent event = new RuleSetChangedEvent(projectKeys.toArray(new String[0]), activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new RuleChange[0]));
    eventsDistributor.pushEvent(event);
}
Also used : ParamChange(org.sonar.core.util.ParamChange) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) ArrayList(java.util.ArrayList) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Map(java.util.Map) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) HashSet(java.util.HashSet)

Example 5 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class SonarLintClientsRegistry method toJson.

private static JSONObject toJson(RuleChange rule) {
    JSONObject ruleJson = new JSONObject();
    ruleJson.put("key", rule.getKey());
    ruleJson.put("language", rule.getLanguage());
    ruleJson.put("severity", rule.getSeverity());
    ruleJson.put("templateKey", rule.getTemplateKey());
    JSONArray params = new JSONArray();
    for (ParamChange paramChange : rule.getParams()) {
        params.put(toJson(paramChange));
    }
    ruleJson.put("params", params);
    return ruleJson;
}
Also used : JSONObject(org.json.JSONObject) ParamChange(org.sonar.core.util.ParamChange) JSONArray(org.json.JSONArray)

Aggregations

ParamChange (org.sonar.core.util.ParamChange)6 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 RuleChange (org.sonar.core.util.RuleChange)3 RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)3 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ProjectDto (org.sonar.db.project.ProjectDto)2 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)2 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)2 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)2 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)2 RuleDto (org.sonar.db.rule.RuleDto)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 DbSession (org.sonar.db.DbSession)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1