Search in sources :

Example 11 with QProfileDto

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;
}
Also used : DefaultQProfileDto(org.sonar.db.qualityprofile.DefaultQProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Date(java.util.Date)

Example 12 with QProfileDto

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();
}
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 13 with QProfileDto

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;
}
Also used : QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) QProfileDto(org.sonar.db.qualityprofile.QProfileDto)

Example 14 with QProfileDto

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) QProfileDto(org.sonar.db.qualityprofile.QProfileDto)

Example 15 with QProfileDto

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();
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse)

Aggregations

QProfileDto (org.sonar.db.qualityprofile.QProfileDto)389 Test (org.junit.Test)329 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)139 UserDto (org.sonar.db.user.UserDto)72 DbSession (org.sonar.db.DbSession)38 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)38 ProjectDto (org.sonar.db.project.ProjectDto)36 RuleParamDto (org.sonar.db.rule.RuleParamDto)36 GroupDto (org.sonar.db.user.GroupDto)35 NotFoundException (org.sonar.server.exceptions.NotFoundException)31 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)23 TestRequest (org.sonar.server.ws.TestRequest)23 System2 (org.sonar.api.utils.System2)22 RuleQuery (org.sonar.server.rule.index.RuleQuery)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)21 DbTester (org.sonar.db.DbTester)21 BadRequestException (org.sonar.server.exceptions.BadRequestException)20 UserSessionRule (org.sonar.server.tester.UserSessionRule)20 List (java.util.List)19 Rule (org.junit.Rule)19