Search in sources :

Example 1 with BuiltInQProfile

use of org.sonar.server.qualityprofile.builtin.BuiltInQProfile in project sonarqube by SonarSource.

the class RegisterQualityProfiles method start.

@Override
public void start() {
    List<BuiltInQProfile> builtInQProfiles = builtInQProfileRepository.get();
    if (builtInQProfiles.isEmpty()) {
        return;
    }
    Profiler profiler = Profiler.create(Loggers.get(getClass())).startInfo("Register quality profiles");
    try (DbSession dbSession = dbClient.openSession(false);
        DbSession batchDbSession = dbClient.openSession(true)) {
        long startDate = system2.now();
        Map<QProfileName, RulesProfileDto> persistedRuleProfiles = loadPersistedProfiles(dbSession);
        Multimap<QProfileName, ActiveRuleChange> changedProfiles = ArrayListMultimap.create();
        builtInQProfiles.forEach(builtIn -> {
            RulesProfileDto ruleProfile = persistedRuleProfiles.get(builtIn.getQProfileName());
            if (ruleProfile == null) {
                create(dbSession, batchDbSession, builtIn);
            } else {
                List<ActiveRuleChange> changes = update(dbSession, builtIn, ruleProfile);
                changedProfiles.putAll(builtIn.getQProfileName(), changes.stream().filter(change -> {
                    String inheritance = change.getActiveRule().getInheritance();
                    return inheritance == null || NONE.name().equals(inheritance);
                }).collect(MoreCollectors.toList()));
            }
        });
        if (!changedProfiles.isEmpty()) {
            long endDate = system2.now();
            builtInQualityProfilesNotification.onChange(changedProfiles, startDate, endDate);
        }
        ensureBuiltInDefaultQPContainsRules(dbSession);
    }
    profiler.stopDebug();
}
Also used : QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) DbSession(org.sonar.db.DbSession) Profiler(org.sonar.api.utils.log.Profiler) BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto)

Example 2 with BuiltInQProfile

use of org.sonar.server.qualityprofile.builtin.BuiltInQProfile in project sonarqube by SonarSource.

the class RegisterQualityProfilesTest method persist_built_in_profiles_that_are_not_persisted_yet.

@Test
public void persist_built_in_profiles_that_are_not_persisted_yet() {
    BuiltInQProfile builtInQProfile = builtInQProfileRepositoryRule.add(FOO_LANGUAGE, "Sonar way");
    builtInQProfileRepositoryRule.initialize();
    underTest.start();
    assertThat(insert.callLogs).containsExactly(builtInQProfile);
    assertThat(update.callLogs).isEmpty();
    assertThat(logTester.logs(LoggerLevel.INFO)).contains("Register profile foo/Sonar way");
}
Also used : BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) Test(org.junit.Test)

Example 3 with BuiltInQProfile

use of org.sonar.server.qualityprofile.builtin.BuiltInQProfile in project sonarqube by SonarSource.

the class RegisterQualityProfilesTest method dont_persist_built_in_profiles_that_are_already_persisted.

@Test
public void dont_persist_built_in_profiles_that_are_already_persisted() {
    String name = "doh";
    BuiltInQProfile persistedBuiltIn = builtInQProfileRepositoryRule.add(FOO_LANGUAGE, name, true);
    BuiltInQProfile nonPersistedBuiltIn = builtInQProfileRepositoryRule.add(BAR_LANGUAGE, name, true);
    builtInQProfileRepositoryRule.initialize();
    insertRulesProfile(persistedBuiltIn);
    underTest.start();
    assertThat(insert.callLogs).containsExactly(nonPersistedBuiltIn);
    assertThat(update.callLogs).containsExactly(persistedBuiltIn);
}
Also used : BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) Test(org.junit.Test)

Example 4 with BuiltInQProfile

use of org.sonar.server.qualityprofile.builtin.BuiltInQProfile in project sonarqube by SonarSource.

the class RegisterQualityProfilesTest method update_built_in_profile_if_it_already_exists.

@Test
public void update_built_in_profile_if_it_already_exists() {
    RulesProfileDto ruleProfile = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setName("Sonar way").setLanguage(FOO_LANGUAGE.getKey()));
    db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfile);
    db.commit();
    BuiltInQProfile builtIn = builtInQProfileRepositoryRule.add(FOO_LANGUAGE, ruleProfile.getName(), false);
    builtInQProfileRepositoryRule.initialize();
    underTest.start();
    assertThat(insert.callLogs).isEmpty();
    assertThat(update.callLogs).containsExactly(builtIn);
    assertThat(logTester.logs(LoggerLevel.INFO)).contains("Update profile foo/Sonar way");
}
Also used : BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Aggregations

BuiltInQProfile (org.sonar.server.qualityprofile.builtin.BuiltInQProfile)4 Test (org.junit.Test)3 RulesProfileDto (org.sonar.db.qualityprofile.RulesProfileDto)2 Profiler (org.sonar.api.utils.log.Profiler)1 DbSession (org.sonar.db.DbSession)1 QProfileName (org.sonar.server.qualityprofile.builtin.QProfileName)1