@Test public void sanityCheck() { Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> { NewBuiltInQualityProfile profile1 = c.createBuiltInQualityProfile("Foo1", "xoo"); NewBuiltInActiveRule rule = profile1.activateRule("repo", "rule"); profile1.done(); NewBuiltInQualityProfile profile2 = c.createBuiltInQualityProfile("Foo2", "xoo"); profile2.done(); NewBuiltInQualityProfile profile3 = c.createBuiltInQualityProfile("Foo1", "xoo2"); profile3.done(); assertThat(profile1).isEqualTo(profile1);
@Test public void activate_rule_on_built_in_profile_resets_params_to_default_if_not_overridden() { RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")); RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleHasParams(db, activeRules.get(0), tuple("min", "10")); // emulate an upgrade of analyzer that changes the default value of parameter min ruleParam.setDefaultValue("20"); db.getDbClient().ruleDao().updateRuleParam(db.getSession(), rule, ruleParam); underTest.update(db.getSession(), builtIn, persistedProfile); activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleHasParams(db, activeRules.get(0), tuple("min", "20")); }
@Test public void bridgeProfileDefinitions() { BuiltInQProfileDefinitionsBridge bridge = new BuiltInQProfileDefinitionsBridge(new Profile1(), new NullProfile(), new ProfileWithError()); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); bridge.define(context); assertThat(context.profilesByLanguageAndName()).hasSize(1); assertThat(context.profilesByLanguageAndName().get("xoo")).hasSize(1); BuiltInQualityProfile profile1 = context.profile("xoo", "Profile 1"); assertThat(profile1).isNotNull(); assertThat(profile1.rules()).hasSize(3); BuiltInActiveRule defaultSeverity = profile1.rule(RuleKey.of("repo1", "defaultSeverity")); assertThat(defaultSeverity).isNotNull(); assertThat(defaultSeverity.overriddenSeverity()).isNull(); assertThat(defaultSeverity.overriddenParams()).isEmpty(); assertThat(profile1.rule(RuleKey.of("repo1", "overrideSeverity")).overriddenSeverity()).isEqualTo(Severity.CRITICAL); assertThat(profile1.rule(RuleKey.of("repo1", "overrideParam")).overriddenParams()) .extracting(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue).containsOnly(tuple("param", "value")); }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile("Sonar way", YamlLanguage.KEY); profile.setDefault(true);
@Override public void define(Context context) { LOGGER.info("Creating Objective-C Profile"); NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile("Objective-C", ObjectiveC.KEY); nbiqp.setDefault(true); try(Reader config = new InputStreamReader(getClass().getResourceAsStream(OCLintProfile.PROFILE_PATH))) { RulesProfile ocLintRulesProfile = ocLintProfileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating Objective-C Profile",ex); } try(Reader config = new InputStreamReader(getClass().getResourceAsStream(FauxPasProfile.PROFILE_PATH))){ RulesProfile fauxPasRulesProfile = fauxPasProfileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : fauxPasRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(),rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating Objective-C Profile",ex); } nbiqp.done(); } }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(PROFILE_NAME, Constants.LANGUAGE_KEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_214_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_12115_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_15090_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_14786_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_14789_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_18494_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_2965_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.PROPARSE_ERROR_RULEKEY); profile.activateRule(Constants.STD_REPOSITORY_KEY, SharedObjectsAnalyzer.class.getName()); profile.activateRule(Constants.STD_REPOSITORY_KEY, LargeTransactionScope.class.getName()); profile.activateRule(Constants.STD_REPOSITORY_KEY, ClumsySyntax.class.getName()); profile.done(); } }
private static void define(Context context, RulesProfile profile) { NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage()) .setDefault(profile.getDefaultProfile().booleanValue()); for (org.sonar.api.rules.ActiveRule ar : profile.getActiveRules()) { NewBuiltInActiveRule newActiveRule = newQp.activateRule(ar.getRepositoryKey(), ar.getRuleKey()); RulePriority overriddenSeverity = ar.getOverriddenSeverity(); if (overriddenSeverity != null) { newActiveRule.overrideSeverity(overriddenSeverity.name()); } for (ActiveRuleParam param : ar.getActiveRuleParams()) { newActiveRule.overrideParam(param.getKey(), param.getValue()); } } newQp.done(); }
@Override public void define(BuiltInQualityProfilesDefinition.Context context) { LOGGER.info("Creating Swift Profile"); BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile("Swift", Swift.KEY); nbiqp.setDefault(true); try (Reader config = new InputStreamReader(getClass().getResourceAsStream(SwiftLintProfile.PROFILE_PATH))) { RulesProfile ocLintRulesProfile = swiftLintProfileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex) { LOGGER.error("Error Creating Swift Profile", ex); } nbiqp.done(); } }
@Override public void define(Context context) { LOGGER.info("Creating Tailor Profile"); NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile(TailorRulesDefinition.REPOSITORY_KEY, Swift.KEY); try(Reader config = new InputStreamReader(getClass().getResourceAsStream(PROFILE_PATH))) { RulesProfile ocLintRulesProfile = profileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating Tailor Profile",ex); } nbiqp.done(); } }
@Override public void define(Context context) { LOGGER.info("Creating OCLint Profile"); NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile(OCLintRulesDefinition.REPOSITORY_KEY, ObjectiveC.KEY); try(Reader config = new InputStreamReader(getClass().getResourceAsStream(OCLintProfile.PROFILE_PATH))) { RulesProfile ocLintRulesProfile = profileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating OCLint Profile",ex); } nbiqp.done(); } }
@Override public void define(Context context) { LOGGER.info("Creating SwiftLint Profile"); NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile(SwiftLintRulesDefinition.REPOSITORY_KEY, Swift.KEY); try(Reader config = new InputStreamReader(getClass().getResourceAsStream(SwiftLintProfile.PROFILE_PATH))) { RulesProfile ocLintRulesProfile = profileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating SwiftLint Profile",ex); } nbiqp.done(); } }
@Override public void define(Context context) { LOGGER.info("Creating FauxPas Profile"); NewBuiltInQualityProfile nbiqp = context.createBuiltInQualityProfile(FauxPasRulesDefinition.REPOSITORY_KEY, ObjectiveC.KEY); try(Reader config = new InputStreamReader(getClass().getResourceAsStream(OCLintProfile.PROFILE_PATH))) { RulesProfile ocLintRulesProfile = profileImporter.importProfile(config, ValidationMessages.create()); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { nbiqp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); } } catch (IOException ex){ LOGGER.error("Error Creating FauxPas Profile",ex); } nbiqp.done(); } }
@Override public void define(Context context) { context.createBuiltInQualityProfile(PROFILE_NAME, Constants.DB_LANGUAGE_KEY).done(); }
@Test public void should_create_sonar_way_profile() { ValidationMessages validation = ValidationMessages.create(); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); new SonarWayProfile().define(context); assertThat(context.profilesByLanguageAndName()).hasSize(1); BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("go", "Sonar way"); assertThat(profile.language()).isEqualTo("go"); assertThat(profile.name()).isEqualTo("Sonar way"); assertThat(profile.rules()).extracting("repoKey").containsOnly(GoRulesDefinition.REPOSITORY_KEY); assertThat(validation.hasErrors()).isFalse(); assertThat(profile.rules()).extracting("ruleKey").contains("S2068"); assertThat(profile.rules()).extracting("ruleKey").doesNotContain("S3801"); }
@Override public void initialize() { checkState(qProfiles == null, "initialize must be called only once"); Profiler profiler = Profiler.create(LOGGER).startInfo("Load quality profiles"); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); for (BuiltInQualityProfilesDefinition definition : definitions) { definition.define(context); } Map<String, Map<String, BuiltInQualityProfile>> rulesProfilesByLanguage = validateAndClean(context); this.qProfiles = toFlatList(rulesProfilesByLanguage); ensureAllLanguagesHaveAtLeastOneBuiltInQP(); profiler.stopDebug(); }
@Override public void define(Context context) { NewBuiltInQualityProfile sonarWay = context.createBuiltInQualityProfile("Sonar way", Java.KEY); sonarWay.activateRule("common-" + Java.KEY, "DuplicatedBlocks"); Profile jsonProfile = readProfile(); Map<String, String> keys = legacyKeys(); for (String key : jsonProfile.ruleKeys) { if (shouldActivateRule(key)) { sonarWay.activateRule(CheckList.REPOSITORY_KEY, keys.get(key)); } } getSecurityRuleKeys().forEach(key -> sonarWay.activateRule(CheckList.REPOSITORY_KEY, key)); sonarWay.done(); }
private Map<String, Map<String, BuiltInQualityProfile>> validateAndClean(BuiltInQualityProfilesDefinition.Context context) { Map<String, Map<String, BuiltInQualityProfile>> profilesByLanguageAndName = context.profilesByLanguageAndName(); profilesByLanguageAndName.entrySet() .removeIf(entry -> { String language = entry.getKey(); if (languages.get(language) == null) { LOGGER.info("Language {} is not installed, related quality profiles are ignored", language); return true; } return false; }); return profilesByLanguageAndName; }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(NAME, HtmlConstants.JSP_LANGUAGE_KEY); profile.done(); }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(PROFILE_NAME, Python.KEY); BuiltInQualityProfileJsonLoader.load(profile, CheckList.REPOSITORY_KEY, PROFILE_LOCATION); profile.done(); }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(NAME, HtmlConstants.LANGUAGE_KEY); BuiltInQualityProfileJsonLoader.load(profile, REPOSITORY_KEY, JSON_PROFILE_PATH, RESOURCE_BASE_PATH, sonarRuntime); profile.done(); }