Search in sources :

Example 1 with QualityProfileDto

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

the class UpdateQualityProfilesLastUsedDateStep method execute.

@Override
public void execute() {
    try (DbSession dbSession = dbClient.openSession(true)) {
        Component root = treeRootHolder.getRoot();
        Metric metric = metricRepository.getByKey(QUALITY_PROFILES_KEY);
        Set<QualityProfile> qualityProfiles = parseQualityProfiles(measureRepository.getRawMeasure(root, metric));
        if (qualityProfiles.isEmpty()) {
            return;
        }
        List<QualityProfileDto> dtos = dbClient.qualityProfileDao().selectByKeys(dbSession, qualityProfiles.stream().map(QualityProfile::getQpKey).collect(Collectors.toList()));
        dtos.addAll(getAncestors(dbSession, dtos));
        long analysisDate = analysisMetadataHolder.getAnalysisDate();
        dtos.forEach(dto -> {
            dto.setLastUsed(analysisDate);
            dbClient.qualityProfileDao().update(dbSession, dto);
        });
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Metric(org.sonar.server.computation.task.projectanalysis.metric.Metric) QualityProfile(org.sonar.server.qualityprofile.QualityProfile) Component(org.sonar.server.computation.task.projectanalysis.component.Component) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 2 with QualityProfileDto

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

the class UpdateQualityProfilesLastUsedDateStep method incrementAncestors.

private void incrementAncestors(DbSession session, QualityProfileDto profile, List<QualityProfileDto> ancestors) {
    String parentKey = profile.getParentKee();
    if (parentKey != null) {
        QualityProfileDto parentDto = dbClient.qualityProfileDao().selectOrFailByKey(session, parentKey);
        ancestors.add(parentDto);
        incrementAncestors(session, parentDto, ancestors);
    }
}
Also used : QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 3 with QualityProfileDto

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

the class QProfileFactory method create.

public QualityProfileDto create(DbSession dbSession, QProfileName name) {
    QualityProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), dbSession);
    checkRequest(dto == null, "Quality profile already exists: %s", name);
    return doCreate(dbSession, name);
}
Also used : QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 4 with QualityProfileDto

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

the class QProfileFactory method doCreate.

private QualityProfileDto doCreate(DbSession dbSession, QProfileName name) {
    if (StringUtils.isEmpty(name.getName())) {
        throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
    }
    Date now = new Date();
    for (int i = 0; i < 20; i++) {
        String key = Slug.slugify(String.format("%s %s %s", name.getLanguage(), name.getName(), RandomStringUtils.randomNumeric(5)));
        QualityProfileDto dto = QualityProfileDto.createFor(key).setName(name.getName()).setOrganizationUuid(defaultOrganizationProvider.get().getUuid()).setLanguage(name.getLanguage()).setRulesUpdatedAtAsDate(now);
        if (db.qualityProfileDao().selectByKey(dbSession, dto.getKey()) == null) {
            db.qualityProfileDao().insert(dbSession, dto);
            return dto;
        }
    }
    throw new IllegalStateException("Failed to create an unique quality profile for " + name);
}
Also used : Date(java.util.Date) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 5 with QualityProfileDto

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

the class QProfileFactory method rename.

// ------------- RENAME
public boolean rename(String key, String newName) {
    checkRequest(StringUtils.isNotBlank(newName), "Name must be set");
    checkRequest(newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
    DbSession dbSession = db.openSession(false);
    try {
        QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, key);
        if (profile == null) {
            throw new NotFoundException("Quality profile not found: " + key);
        }
        if (!StringUtils.equals(newName, profile.getName())) {
            checkRequest(db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) == null, "Quality profile already exists: %s", newName);
            profile.setName(newName);
            db.qualityProfileDao().update(dbSession, profile);
            dbSession.commit();
            return true;
        }
        return false;
    } finally {
        dbSession.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Aggregations

QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)126 Test (org.junit.Test)76 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 WsTester (org.sonar.server.ws.WsTester)21 RuleQuery (org.sonar.server.rule.index.RuleQuery)14 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)12 DbSession (org.sonar.db.DbSession)11 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)9 SearchOptions (org.sonar.server.es.SearchOptions)9 ComponentDto (org.sonar.db.component.ComponentDto)8 OrganizationDto (org.sonar.db.organization.OrganizationDto)7 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)6 Date (java.util.Date)5 QualityProfileTesting.newQualityProfileDto (org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto)5 RuleParamDto (org.sonar.db.rule.RuleParamDto)5 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Language (org.sonar.api.resources.Language)4 RuleKey (org.sonar.api.rule.RuleKey)4