Search in sources :

Example 1 with ProfileImporter

use of org.sonar.api.profiles.ProfileImporter in project sonarqube by SonarSource.

the class CreateAction method define.

@Override
public void define(WebService.NewController controller) {
    NewAction create = controller.createAction(ACTION_CREATE).setSince("5.2").setDescription("Create a quality profile.<br/>" + "Require Administer Quality Profiles permission.").setPost(true).setResponseExample(getClass().getResource("example-create.json")).setHandler(this);
    create.createParam(PARAM_PROFILE_NAME).setDescription("The name for the new quality profile. Since 6.1, this parameter has been renamed from '%s' to '%s'", DEPRECATED_PARAM_PROFILE_NAME, PARAM_PROFILE_NAME).setExampleValue("My Sonar way").setDeprecatedKey(DEPRECATED_PARAM_PROFILE_NAME, "6.3").setRequired(true);
    create.createParam(PARAM_LANGUAGE).setDescription("The language for the quality profile.").setExampleValue("js").setPossibleValues(LanguageParamUtils.getLanguageKeys(languages)).setRequired(true);
    for (ProfileImporter importer : importers) {
        create.createParam(getBackupParamName(importer.getKey())).setDescription(String.format("A configuration file for %s.", importer.getName()));
    }
}
Also used : ProfileImporter(org.sonar.api.profiles.ProfileImporter) NewAction(org.sonar.api.server.ws.WebService.NewAction)

Example 2 with ProfileImporter

use of org.sonar.api.profiles.ProfileImporter in project sonarqube by SonarSource.

the class CreateActionTest method createImporters.

private ProfileImporter[] createImporters() {
    class DefaultProfileImporter extends ProfileImporter {

        private DefaultProfileImporter() {
            super("xoo_lint", "Xoo Lint");
            setSupportedLanguages(XOO_LANGUAGE);
        }

        @Override
        public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
            RulesProfile rulesProfile = RulesProfile.create();
            rulesProfile.activateRule(org.sonar.api.rules.Rule.create(RULE.getRepositoryKey(), RULE.getRuleKey()), RulePriority.BLOCKER);
            return rulesProfile;
        }
    }
    class ProfileImporterGeneratingMessages extends ProfileImporter {

        private ProfileImporterGeneratingMessages() {
            super("with_messages", "With messages");
            setSupportedLanguages(XOO_LANGUAGE);
        }

        @Override
        public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
            RulesProfile rulesProfile = RulesProfile.create();
            messages.addWarningText("a warning");
            messages.addInfoText("an info");
            return rulesProfile;
        }
    }
    class ProfileImporterGeneratingErrors extends ProfileImporter {

        private ProfileImporterGeneratingErrors() {
            super("with_errors", "With errors");
            setSupportedLanguages(XOO_LANGUAGE);
        }

        @Override
        public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
            RulesProfile rulesProfile = RulesProfile.create();
            messages.addErrorText("error!");
            return rulesProfile;
        }
    }
    return new ProfileImporter[] { new DefaultProfileImporter(), new ProfileImporterGeneratingMessages(), new ProfileImporterGeneratingErrors() };
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileImporter(org.sonar.api.profiles.ProfileImporter) Reader(java.io.Reader) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 3 with ProfileImporter

use of org.sonar.api.profiles.ProfileImporter in project sonarqube by SonarSource.

the class QProfileExporters method importXml.

private QProfileResult importXml(QualityProfileDto profileDto, String importerKey, Reader xml, DbSession dbSession) {
    QProfileResult result = new QProfileResult();
    ValidationMessages messages = ValidationMessages.create();
    ProfileImporter importer = getProfileImporter(importerKey);
    RulesProfile rulesProfile = importer.importProfile(xml, messages);
    List<ActiveRuleChange> changes = importProfile(profileDto, rulesProfile, dbSession);
    result.addChanges(changes);
    processValidationMessages(messages, result);
    return result;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileImporter(org.sonar.api.profiles.ProfileImporter) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 4 with ProfileImporter

use of org.sonar.api.profiles.ProfileImporter in project sonarqube by SonarSource.

the class CreateAction method doHandle.

private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request) {
    QProfileResult result = new QProfileResult();
    QualityProfileDto profile = profileFactory.create(dbSession, QProfileName.createFor(createRequest.getLanguage(), createRequest.getProfileName()));
    result.setProfile(profile);
    for (ProfileImporter importer : importers) {
        String importerKey = importer.getKey();
        InputStream contentToImport = request.paramAsInputStream(getBackupParamName(importerKey));
        if (contentToImport != null) {
            result.add(exporters.importXml(profile, importerKey, contentToImport, dbSession));
        }
    }
    dbSession.commit();
    activeRuleIndexer.index(result.getChanges());
    return buildResponse(result);
}
Also used : InputStream(java.io.InputStream) ProfileImporter(org.sonar.api.profiles.ProfileImporter) QProfileResult(org.sonar.server.qualityprofile.QProfileResult) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 5 with ProfileImporter

use of org.sonar.api.profiles.ProfileImporter in project sonarqube by SonarSource.

the class ImportersAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    JsonWriter json = response.newJsonWriter().beginObject().name("importers").beginArray();
    for (ProfileImporter importer : importers) {
        json.beginObject().prop("key", importer.getKey()).prop("name", importer.getName()).name("languages").beginArray();
        for (String languageKey : importer.getSupportedLanguages()) {
            json.value(languageKey);
        }
        json.endArray().endObject();
    }
    json.endArray().endObject().close();
}
Also used : ProfileImporter(org.sonar.api.profiles.ProfileImporter) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

ProfileImporter (org.sonar.api.profiles.ProfileImporter)6 RulesProfile (org.sonar.api.profiles.RulesProfile)2 ValidationMessages (org.sonar.api.utils.ValidationMessages)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Before (org.junit.Before)1 I18n (org.sonar.api.i18n.I18n)1 Languages (org.sonar.api.resources.Languages)1 NewAction (org.sonar.api.server.ws.WebService.NewAction)1 JsonWriter (org.sonar.api.utils.text.JsonWriter)1 DbClient (org.sonar.db.DbClient)1 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)1 QProfileExporters (org.sonar.server.qualityprofile.QProfileExporters)1 QProfileFactory (org.sonar.server.qualityprofile.QProfileFactory)1 QProfileResult (org.sonar.server.qualityprofile.QProfileResult)1 QProfileService (org.sonar.server.qualityprofile.QProfileService)1 WsTester (org.sonar.server.ws.WsTester)1