Search in sources :

Example 1 with RulesProfileDto

use of org.sonar.db.qualityprofile.RulesProfileDto in project sonarqube by SonarSource.

the class RegisterQualityProfiles method ensureBuiltInDefaultQPContainsRules.

/**
 * This method ensure that if a default built-in quality profile does not have any active rules but another built-in one for the same language
 * does have active rules, the last one will be the default one.
 *
 * @see <a href="https://jira.sonarsource.com/browse/SONAR-10363">SONAR-10363</a>
 */
private void ensureBuiltInDefaultQPContainsRules(DbSession dbSession) {
    Map<String, RulesProfileDto> rulesProfilesByLanguage = dbClient.qualityProfileDao().selectBuiltInRuleProfilesWithActiveRules(dbSession).stream().collect(toMap(RulesProfileDto::getLanguage, Function.identity(), (oldValue, newValue) -> oldValue));
    dbClient.qualityProfileDao().selectDefaultBuiltInProfilesWithoutActiveRules(dbSession, rulesProfilesByLanguage.keySet()).forEach(qp -> {
        RulesProfileDto rulesProfile = rulesProfilesByLanguage.get(qp.getLanguage());
        if (rulesProfile == null) {
            return;
        }
        QProfileDto qualityProfile = dbClient.qualityProfileDao().selectByRuleProfileUuid(dbSession, rulesProfile.getUuid());
        if (qualityProfile == null) {
            return;
        }
        Set<String> uuids = dbClient.defaultQProfileDao().selectExistingQProfileUuids(dbSession, Collections.singleton(qp.getKee()));
        dbClient.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids);
        dbClient.defaultQProfileDao().insertOrUpdate(dbSession, new DefaultQProfileDto().setQProfileUuid(qualityProfile.getKee()).setLanguage(qp.getLanguage()));
        LOGGER.info("Default built-in quality profile for language [{}] has been updated from [{}] to [{}] since previous default does not have active rules.", qp.getLanguage(), qp.getName(), rulesProfile.getName());
    });
    dbSession.commit();
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) DefaultQProfileDto(org.sonar.db.qualityprofile.DefaultQProfileDto) BuiltInQProfileInsert(org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsert) BuiltInQProfileUpdate(org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdate) Loggers(org.sonar.api.utils.log.Loggers) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) BuiltInQProfileRepository(org.sonar.server.qualityprofile.builtin.BuiltInQProfileRepository) Logger(org.sonar.api.utils.log.Logger) System2(org.sonar.api.utils.System2) Profiler(org.sonar.api.utils.log.Profiler) Startable(org.sonar.api.Startable) Collection(java.util.Collection) BuiltInQualityProfilesUpdateListener(org.sonar.server.qualityprofile.builtin.BuiltInQualityProfilesUpdateListener) Set(java.util.Set) QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) NONE(org.sonar.server.qualityprofile.ActiveRuleInheritance.NONE) List(java.util.List) ServerSide(org.sonar.api.server.ServerSide) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Collections(java.util.Collections) DefaultQProfileDto(org.sonar.db.qualityprofile.DefaultQProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) DefaultQProfileDto(org.sonar.db.qualityprofile.DefaultQProfileDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto)

Example 2 with RulesProfileDto

use of org.sonar.db.qualityprofile.RulesProfileDto 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 3 with RulesProfileDto

use of org.sonar.db.qualityprofile.RulesProfileDto in project sonarqube by SonarSource.

the class BuiltInQProfileInsertImpl method create.

@Override
public void create(DbSession batchDbSession, BuiltInQProfile builtInQProfile) {
    initRuleRepository(batchDbSession);
    Date now = new Date(system2.now());
    RulesProfileDto ruleProfile = insertRulesProfile(batchDbSession, builtInQProfile, now);
    List<ActiveRuleChange> changes = builtInQProfile.getActiveRules().stream().map(activeRule -> insertActiveRule(batchDbSession, ruleProfile, activeRule, now.getTime())).collect(MoreCollectors.toList());
    changes.forEach(change -> dbClient.qProfileChangeDao().insert(batchDbSession, change.toDto(null)));
    createDefaultAndOrgQProfiles(batchDbSession, builtInQProfile, ruleProfile);
    activeRuleIndexer.commitAndIndex(batchDbSession, changes);
}
Also used : ServerRuleFinder(org.sonar.server.rule.ServerRuleFinder) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) HashMap(java.util.HashMap) TypeValidations(org.sonar.server.util.TypeValidations) DbSession(org.sonar.db.DbSession) DefaultQProfileDto(org.sonar.db.qualityprofile.DefaultQProfileDto) HashSet(java.util.HashSet) BuiltInQualityProfilesDefinition(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition) Map(java.util.Map) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) Objects.requireNonNull(java.util.Objects.requireNonNull) Splitter(com.google.common.base.Splitter) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Collections.emptySet(java.util.Collections.emptySet) System2(org.sonar.api.utils.System2) UuidFactory(org.sonar.core.util.UuidFactory) Set(java.util.Set) RuleParamType(org.sonar.api.server.rule.RuleParamType) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgQProfileDto(org.sonar.db.qualityprofile.OrgQProfileDto) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) RuleParamDto(org.sonar.db.rule.RuleParamDto) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) RuleKey(org.sonar.api.rule.RuleKey) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) CheckForNull(javax.annotation.CheckForNull) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Date(java.util.Date)

Example 4 with RulesProfileDto

use of org.sonar.db.qualityprofile.RulesProfileDto in project sonarqube by SonarSource.

the class BuiltInQProfileInsertImpl method insertRulesProfile.

private RulesProfileDto insertRulesProfile(DbSession dbSession, BuiltInQProfile builtIn, Date now) {
    RulesProfileDto dto = new RulesProfileDto().setUuid(uuidFactory.create()).setName(builtIn.getName()).setLanguage(builtIn.getLanguage()).setIsBuiltIn(true).setRulesUpdatedAtAsDate(now);
    dbClient.qualityProfileDao().insert(dbSession, dto);
    return dto;
}
Also used : RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto)

Example 5 with RulesProfileDto

use of org.sonar.db.qualityprofile.RulesProfileDto in project sonarqube by SonarSource.

the class QProfileFactoryImplTest method delete_builtin_profile_associated_to_project.

@Test
public void delete_builtin_profile_associated_to_project() {
    RulesProfileDto builtInProfile = createBuiltInProfile();
    ProjectDto project = db.components().insertPrivateProjectDto();
    QProfileDto profile = associateBuiltInProfile(builtInProfile);
    db.qualityProfiles().associateWithProject(project, profile);
    assertThat(db.getDbClient().qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage())).isNotNull();
    underTest.delete(dbSession, asList(profile));
    verifyNoCallsActiveRuleIndexerDelete();
    // remove only from org_qprofiles and project_qprofiles
    assertThat(db.getDbClient().qualityProfileDao().selectAll(dbSession)).isEmpty();
    assertThat(db.getDbClient().qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage())).isNull();
    assertThatRulesProfileExists(builtInProfile);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) OrgQProfileDto(org.sonar.db.qualityprofile.OrgQProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Aggregations

RulesProfileDto (org.sonar.db.qualityprofile.RulesProfileDto)22 Test (org.junit.Test)10 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)9 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)7 Map (java.util.Map)6 BuiltInQProfile (org.sonar.server.qualityprofile.builtin.BuiltInQProfile)6 List (java.util.List)5 Multimap (com.google.common.collect.Multimap)4 RuleKey (org.sonar.api.rule.RuleKey)4 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)4 System2 (org.sonar.api.utils.System2)4 MoreCollectors (org.sonar.core.util.stream.MoreCollectors)4 DbClient (org.sonar.db.DbClient)4 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 QProfileName (org.sonar.server.qualityprofile.builtin.QProfileName)4 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 SecureRandom (java.security.SecureRandom)3 Arrays (java.util.Arrays)3 Collections.singleton (java.util.Collections.singleton)3 Random (java.util.Random)3