Search in sources :

Example 11 with RulesProfileDto

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

the class ActiveRuleIndexer method doIndexRuleProfiles.

private IndexingResult doIndexRuleProfiles(DbSession dbSession, Map<String, EsQueueDto> ruleProfileItems) {
    IndexingResult result = new IndexingResult();
    for (Map.Entry<String, EsQueueDto> entry : ruleProfileItems.entrySet()) {
        String ruleProfileUUid = entry.getKey();
        EsQueueDto item = entry.getValue();
        IndexingResult profileResult;
        RulesProfileDto profile = dbClient.qualityProfileDao().selectRuleProfile(dbSession, ruleProfileUUid);
        if (profile == null) {
            // profile does not exist anymore in db --> related documents must be deleted from index rules/activeRule
            SearchRequest search = EsClient.prepareSearch(TYPE_ACTIVE_RULE.getMainType()).source(new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(termQuery(FIELD_ACTIVE_RULE_PROFILE_UUID, ruleProfileUUid))));
            profileResult = BulkIndexer.delete(esClient, TYPE_ACTIVE_RULE, search);
        } else {
            BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, IndexingListener.FAIL_ON_ERROR);
            bulkIndexer.start();
            dbClient.activeRuleDao().scrollByRuleProfileForIndexing(dbSession, ruleProfileUUid, i -> bulkIndexer.add(newIndexRequest(i)));
            profileResult = bulkIndexer.stop();
        }
        if (profileResult.isSuccess()) {
            deleteQueueDto(dbSession, item);
        }
        result.add(profileResult);
    }
    return result;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IndexingResult(org.sonar.server.es.IndexingResult) EsQueueDto(org.sonar.db.es.EsQueueDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) HashMap(java.util.HashMap) Map(java.util.Map) BulkIndexer(org.sonar.server.es.BulkIndexer) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 12 with RulesProfileDto

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

the class RegisterQualityProfilesTest method insertRulesProfile.

private void insertRulesProfile(BuiltInQProfile builtIn) {
    RulesProfileDto dto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(builtIn.getLanguage()).setName(builtIn.getName()));
    dbClient.qualityProfileDao().insert(db.getSession(), dto);
    db.commit();
}
Also used : RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto)

Example 13 with RulesProfileDto

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

the class RegisterQualityProfilesTest method update_built_in_profile_if_it_already_exists.

@Test
public void update_built_in_profile_if_it_already_exists() {
    RulesProfileDto ruleProfile = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setName("Sonar way").setLanguage(FOO_LANGUAGE.getKey()));
    db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfile);
    db.commit();
    BuiltInQProfile builtIn = builtInQProfileRepositoryRule.add(FOO_LANGUAGE, ruleProfile.getName(), false);
    builtInQProfileRepositoryRule.initialize();
    underTest.start();
    assertThat(insert.callLogs).isEmpty();
    assertThat(update.callLogs).containsExactly(builtIn);
    assertThat(logTester.logs(LoggerLevel.INFO)).contains("Update profile foo/Sonar way");
}
Also used : BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Example 14 with RulesProfileDto

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

the class RegisterQualityProfilesTest method update_default_built_in_quality_profile.

@Test
public void update_default_built_in_quality_profile() {
    RulesProfileDto ruleProfileWithoutRule = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setName("Sonar way").setLanguage(FOO_LANGUAGE.getKey()));
    RulesProfileDto ruleProfileWithOneRule = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setName("Sonar way 2").setLanguage(FOO_LANGUAGE.getKey()));
    QProfileDto qProfileWithoutRule = newQualityProfileDto().setIsBuiltIn(true).setLanguage(FOO_LANGUAGE.getKey()).setRulesProfileUuid(ruleProfileWithoutRule.getUuid());
    QProfileDto qProfileWithOneRule = newQualityProfileDto().setIsBuiltIn(true).setLanguage(FOO_LANGUAGE.getKey()).setRulesProfileUuid(ruleProfileWithOneRule.getUuid());
    db.qualityProfiles().insert(qProfileWithoutRule, qProfileWithOneRule);
    db.qualityProfiles().setAsDefault(qProfileWithoutRule);
    RuleDefinitionDto ruleDefinition = db.rules().insert();
    db.qualityProfiles().activateRule(qProfileWithOneRule, ruleDefinition);
    db.commit();
    builtInQProfileRepositoryRule.add(FOO_LANGUAGE, ruleProfileWithoutRule.getName(), true);
    builtInQProfileRepositoryRule.add(FOO_LANGUAGE, ruleProfileWithOneRule.getName(), false);
    builtInQProfileRepositoryRule.initialize();
    underTest.start();
    logTester.logs(LoggerLevel.INFO).contains(format("Default built-in quality profile for language [foo] has been updated from [%s] to [%s] since previous default does not have active rules.", qProfileWithoutRule.getName(), qProfileWithOneRule.getName()));
    assertThat(selectUuidOfDefaultProfile(FOO_LANGUAGE.getKey())).isPresent().get().isEqualTo(qProfileWithOneRule.getKee());
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Example 15 with RulesProfileDto

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

the class QProfileFactoryImplTest method delete_builtin_profile_marked_as_default.

@Test
public void delete_builtin_profile_marked_as_default() {
    RulesProfileDto builtInProfile = createBuiltInProfile();
    QProfileDto profile = associateBuiltInProfile(builtInProfile);
    db.qualityProfiles().setAsDefault(profile);
    underTest.delete(dbSession, asList(profile));
    verifyNoCallsActiveRuleIndexerDelete();
    // remove only from org_qprofiles and default_qprofiles
    assertThat(db.getDbClient().qualityProfileDao().selectAll(dbSession)).isEmpty();
    assertThat(db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, profile.getLanguage())).isNull();
    assertThatRulesProfileExists(builtInProfile);
}
Also used : 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