Search in sources :

Example 1 with RuleChange

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

the class QualityProfileChangeEventServiceImpl method publishRuleActivationToSonarLintClients.

@Override
public void publishRuleActivationToSonarLintClients(ProjectDto project, @Nullable QProfileDto activatedProfile, @Nullable QProfileDto deactivatedProfile) {
    List<RuleChange> activatedRules = new ArrayList<>();
    List<RuleChange> deactivatedRules = new ArrayList<>();
    if (activatedProfile != null) {
        activatedRules.addAll(createRuleChanges(activatedProfile));
    }
    if (deactivatedProfile != null) {
        deactivatedRules.addAll(createRuleChanges(deactivatedProfile));
    }
    if (activatedRules.isEmpty() && deactivatedRules.isEmpty()) {
        return;
    }
    RuleSetChangedEvent event = new RuleSetChangedEvent(new String[] { project.getKey() }, activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new RuleChange[0]));
    eventsDistributor.pushEvent(event);
}
Also used : ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) ArrayList(java.util.ArrayList) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent)

Example 2 with RuleChange

use of org.sonar.core.util.RuleChange 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 3 with RuleChange

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

the class SonarLintClientsRegistryTest method listen_givenUserNotPermittedToReceiveEvent_closeConnection.

@Test
public void listen_givenUserNotPermittedToReceiveEvent_closeConnection() {
    RuleChange javaRuleChange = createRuleChange();
    RuleChange[] activatedRules = {};
    RuleChange[] deactivatedRules = { javaRuleChange };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    SonarLintClient sonarLintClient = createSampleSLClient();
    underTest.registerClient(sonarLintClient);
    doThrow(new ForbiddenException("Access forbidden")).when(permissionsValidator).validateUserCanReceivePushEventForProjects(anyString(), anySet());
    underTest.listen(ruleSetChangedEvent);
    verify(sonarLintClient).close();
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 4 with RuleChange

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

the class SonarLintClientsRegistryTest method listen_givenUnregisteredClient_closeConnection.

@Test
public void listen_givenUnregisteredClient_closeConnection() throws IOException {
    RuleChange javaRuleChange = createRuleChange();
    RuleChange[] activatedRules = {};
    RuleChange[] deactivatedRules = { javaRuleChange };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    SonarLintClient sonarLintClient = createSampleSLClient();
    underTest.registerClient(sonarLintClient);
    doThrow(new IOException("Broken pipe")).when(sonarLintClient).writeAndFlush(anyString());
    underTest.listen(ruleSetChangedEvent);
    underTest.registerClient(sonarLintClient);
    doThrow(new IllegalStateException("Things went wrong")).when(sonarLintClient).writeAndFlush(anyString());
    underTest.listen(ruleSetChangedEvent);
    verify(sonarLintClient, times(2)).close();
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 5 with RuleChange

use of org.sonar.core.util.RuleChange 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)

Aggregations

RuleChange (org.sonar.core.util.RuleChange)11 RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)7 Test (org.junit.Test)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ArrayList (java.util.ArrayList)4 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)4 ParamChange (org.sonar.core.util.ParamChange)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)3 DbSession (org.sonar.db.DbSession)2 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)2 RuleDto (org.sonar.db.rule.RuleDto)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 NotNull (org.jetbrains.annotations.NotNull)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1