use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.
the class QProfileFactoryImpl method doCreate.
private QProfileDto doCreate(DbSession dbSession, QProfileName name, @Nullable String parentKey, boolean isDefault, boolean isBuiltIn) {
if (StringUtils.isEmpty(name.getName())) {
throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
}
Date now = new Date(system2.now());
QProfileDto dto = new QProfileDto().setKee(uuidFactory.create()).setRulesProfileUuid(uuidFactory.create()).setName(name.getName()).setLanguage(name.getLanguage()).setIsBuiltIn(isBuiltIn).setParentKee(parentKey).setRulesUpdatedAtAsDate(now);
db.qualityProfileDao().insert(dbSession, dto);
if (isDefault) {
db.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(dto));
}
return dto;
}
use of org.sonar.db.qualityprofile.QProfileDto 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.QProfileDto in project sonarqube by SonarSource.
the class QProfileCopier method prepareTarget.
private QProfileDto prepareTarget(DbSession dbSession, QProfileDto sourceProfile, String toName) {
verify(sourceProfile.getName(), toName);
QProfileName toProfileName = new QProfileName(sourceProfile.getLanguage(), toName);
QProfileDto toProfile = db.qualityProfileDao().selectByNameAndLanguage(dbSession, toProfileName.getName(), toProfileName.getLanguage());
if (toProfile == null) {
toProfile = factory.createCustom(dbSession, toProfileName, sourceProfile.getParentKee());
dbSession.commit();
}
return toProfile;
}
use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.
the class RenameAction method doHandle.
private void doHandle(String newName, String profileKey) {
checkRequest(newName.length() <= MAXIMUM_NAME_LENGTH, "Name is too long (>%d characters)", MAXIMUM_NAME_LENGTH);
userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto qualityProfile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey));
wsSupport.checkCanEdit(dbSession, qualityProfile);
if (newName.equals(qualityProfile.getName())) {
return;
}
String language = qualityProfile.getLanguage();
ofNullable(dbClient.qualityProfileDao().selectByNameAndLanguage(dbSession, newName, language)).ifPresent(found -> {
throw BadRequestException.create(format("Quality profile already exists: %s", newName));
});
qualityProfile.setName(newName);
dbClient.qualityProfileDao().update(dbSession, qualityProfile);
dbSession.commit();
}
}
use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.
the class SearchAction method buildResponse.
private SearchWsResponse buildResponse(SearchData data) {
List<QProfileDto> profiles = data.getProfiles();
Map<String, QProfileDto> profilesByKey = profiles.stream().collect(Collectors.toMap(QProfileDto::getKee, identity()));
boolean isGlobalQProfileAdmin = userSession.hasPermission(ADMINISTER_QUALITY_PROFILES);
SearchWsResponse.Builder response = SearchWsResponse.newBuilder();
response.setActions(SearchWsResponse.Actions.newBuilder().setCreate(isGlobalQProfileAdmin));
for (QProfileDto profile : profiles) {
QualityProfile.Builder profileBuilder = response.addProfilesBuilder();
String profileKey = profile.getKee();
profileBuilder.setKey(profileKey);
ofNullable(profile.getName()).ifPresent(profileBuilder::setName);
ofNullable(profile.getRulesUpdatedAt()).ifPresent(profileBuilder::setRulesUpdatedAt);
ofNullable(profile.getLastUsed()).ifPresent(last -> profileBuilder.setLastUsed(formatDateTime(last)));
ofNullable(profile.getUserUpdatedAt()).ifPresent(userUpdatedAt -> profileBuilder.setUserUpdatedAt(formatDateTime(userUpdatedAt)));
profileBuilder.setActiveRuleCount(data.getActiveRuleCount(profileKey));
profileBuilder.setActiveDeprecatedRuleCount(data.getActiveDeprecatedRuleCount(profileKey));
boolean isDefault = data.isDefault(profile);
profileBuilder.setIsDefault(isDefault);
if (!isDefault) {
profileBuilder.setProjectCount(data.getProjectCount(profileKey));
}
writeLanguageFields(profileBuilder, profile);
writeParentFields(profileBuilder, profile, profilesByKey);
profileBuilder.setIsInherited(profile.getParentKee() != null);
profileBuilder.setIsBuiltIn(profile.isBuiltIn());
profileBuilder.setActions(SearchWsResponse.QualityProfile.Actions.newBuilder().setEdit(!profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile))).setSetAsDefault(!isDefault && isGlobalQProfileAdmin).setCopy(isGlobalQProfileAdmin).setDelete(!isDefault && !profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile))).setAssociateProjects(!isDefault && (isGlobalQProfileAdmin || data.isEditable(profile))));
}
return response.build();
}
Aggregations