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();
}
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();
}
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);
}
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;
}
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);
}
Aggregations