Search in sources :

Example 6 with RuleChange

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

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

the class SonarLintClientsRegistry method toJson.

private static String toJson(RuleSetChangedEvent ruleSetChangedEvent) {
    JSONObject data = new JSONObject();
    data.put("projects", ruleSetChangedEvent.getProjects());
    JSONArray activatedRulesJson = new JSONArray();
    for (RuleChange rule : ruleSetChangedEvent.getActivatedRules()) {
        activatedRulesJson.put(toJson(rule));
    }
    data.put("activatedRules", activatedRulesJson);
    JSONArray deactivatedRulesJson = new JSONArray();
    for (RuleChange rule : ruleSetChangedEvent.getDeactivatedRules()) {
        deactivatedRulesJson.put(toJson(rule));
    }
    data.put("deactivatedRules", deactivatedRulesJson);
    return data.toString();
}
Also used : RuleChange(org.sonar.core.util.RuleChange) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 8 with RuleChange

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

the class QualityProfileChangeEventServiceImpl method createRuleChanges.

private List<RuleChange> createRuleChanges(@NotNull QProfileDto profileDto) {
    List<RuleChange> ruleChanges = new ArrayList<>();
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profileDto);
        List<String> activeRuleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getUuid).collect(Collectors.toList());
        Map<String, List<ActiveRuleParamDto>> paramsByActiveRuleUuid = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, activeRuleUuids).stream().collect(Collectors.groupingBy(ActiveRuleParamDto::getActiveRuleUuid));
        Map<String, String> activeRuleUuidByRuleUuid = activeRuleDtos.stream().collect(Collectors.toMap(ActiveRuleDto::getRuleUuid, ActiveRuleDto::getUuid));
        List<String> ruleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
        List<RuleDto> ruleDtos = dbClient.ruleDao().selectByUuids(dbSession, ruleUuids);
        for (RuleDto ruleDto : ruleDtos) {
            String activeRuleUuid = activeRuleUuidByRuleUuid.get(ruleDto.getUuid());
            List<ActiveRuleParamDto> params = paramsByActiveRuleUuid.getOrDefault(activeRuleUuid, new ArrayList<>());
            RuleChange ruleChange = toRuleChange(ruleDto, params);
            ruleChanges.add(ruleChange);
        }
    }
    return ruleChanges;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ArrayList(java.util.ArrayList) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with RuleChange

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

the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInProjA_DontCheckPermissionsForProjB.

@Test
public void listen_givenOneClientInterestedInProjA_DontCheckPermissionsForProjB() throws IOException {
    when(defaultAsyncContext.getResponse()).thenReturn(response);
    when(response.getOutputStream()).thenReturn(outputStream);
    Set<String> clientProjectKeys = Set.of("projA");
    Set<String> eventProjectKeys = Set.of("projA", "projB");
    SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, clientProjectKeys, Set.of("java"), USER_UUID);
    underTest.registerClient(sonarLintClient);
    RuleChange javaRuleChange = createRuleChange();
    RuleChange[] activatedRules = {};
    RuleChange[] deactivatedRules = { javaRuleChange };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(eventProjectKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    underTest.listen(ruleSetChangedEvent);
    ArgumentCaptor<Set<String>> argument = ArgumentCaptor.forClass(Set.class);
    verify(permissionsValidator).validateUserCanReceivePushEventForProjects(anyString(), argument.capture());
    assertThat(argument.getValue()).isEqualTo(clientProjectKeys);
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Set(java.util.Set) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 10 with RuleChange

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

the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents.

@Test
public void listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents() throws IOException {
    Set<String> jsLanguageKey = Set.of("js");
    when(defaultAsyncContext.getResponse()).thenReturn(response);
    when(response.getOutputStream()).thenReturn(outputStream);
    SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, exampleKeys, jsLanguageKey, USER_UUID);
    underTest.registerClient(sonarLintClient);
    RuleChange javaRuleChange = createRuleChange();
    RuleChange[] activatedRules = {};
    RuleChange[] deactivatedRules = { javaRuleChange };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    underTest.listen(ruleSetChangedEvent);
    verifyNoInteractions(outputStream);
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

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