Search in sources :

Example 1 with RuleSetChangedEvent

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

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

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

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

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

Aggregations

RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)10 Test (org.junit.Test)7 RuleChange (org.sonar.core.util.RuleChange)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ParamChange (org.sonar.core.util.ParamChange)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ProjectDto (org.sonar.db.project.ProjectDto)2 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)2 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)2 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)2 Map (java.util.Map)1 Set (java.util.Set)1 ArgumentMatchers.anySet (org.mockito.ArgumentMatchers.anySet)1 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)1 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)1 RuleDto (org.sonar.db.rule.RuleDto)1