Search in sources :

Example 96 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class ResetAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ResetRequest resetRequest = toWsRequest(request);
        Optional<ComponentDto> component = getComponent(dbSession, resetRequest);
        checkPermissions(component);
        resetRequest.getKeys().forEach(key -> {
            SettingData data = new SettingData(key, emptyList(), component.orElse(null));
            ImmutableList.of(validations.scope(), validations.qualifier()).forEach(validation -> validation.accept(data));
        });
        List<String> keys = getKeys(resetRequest);
        if (component.isPresent()) {
            settingsUpdater.deleteComponentSettings(dbSession, component.get(), keys);
        } else {
            settingsUpdater.deleteGlobalSettings(dbSession, keys);
        }
        dbSession.commit();
        response.noContent();
    }
}
Also used : DbSession(org.sonar.db.DbSession) SettingData(org.sonar.server.setting.ws.SettingValidations.SettingData) ComponentDto(org.sonar.db.component.ComponentDto) ResetRequest(org.sonarqube.ws.client.setting.ResetRequest)

Example 97 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class HashAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession session = dbClient.openSession(false)) {
        String componentKey = request.mandatoryParam("key");
        ComponentDto component = componentFinder.getByKey(session, componentKey);
        userSession.checkComponentPermission(UserRole.USER, component);
        response.stream().setMediaType("text/plain");
        try (OutputStreamWriter writer = new OutputStreamWriter(response.stream().output(), StandardCharsets.UTF_8)) {
            HashFunction hashFunction = new HashFunction(writer, componentKey);
            dbClient.fileSourceDao().readLineHashesStream(session, component.uuid(), hashFunction);
            if (!hashFunction.hasData()) {
                response.noContent();
            }
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) OutputStreamWriter(java.io.OutputStreamWriter)

Example 98 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class IndexAction method handle.

@Override
public void handle(Request request, Response response) {
    String fileKey = request.mandatoryParam("resource");
    int from = request.mandatoryParamAsInt("from");
    Integer to = request.paramAsInt("to");
    try (DbSession session = dbClient.openSession(false)) {
        ComponentDto component = componentFinder.getByKey(session, fileKey);
        userSession.checkComponentPermission(UserRole.CODEVIEWER, component);
        Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(session, component.uuid(), from, to == null ? Integer.MAX_VALUE : to - 1);
        JsonWriter json = response.newJsonWriter().beginArray().beginObject();
        if (lines.isPresent()) {
            int lineCounter = from;
            for (String line : lines.get()) {
                json.prop(String.valueOf(lineCounter), line);
                lineCounter++;
            }
        }
        json.endObject().endArray().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 99 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class CoveredFilesAction method buildComponentsByUuid.

private Map<String, ComponentDto> buildComponentsByUuid(List<CoveredFileDoc> coveredFiles) {
    List<String> sourceFileUuids = Lists.transform(coveredFiles, new CoveredFileToFileUuidFunction());
    List<ComponentDto> components;
    try (DbSession dbSession = dbClient.openSession(false)) {
        components = dbClient.componentDao().selectByUuids(dbSession, sourceFileUuids);
    }
    return Maps.uniqueIndex(components, ComponentDto::uuid);
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto)

Example 100 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class ShowAction method handle.

@Override
public void handle(Request request, Response response) {
    String fileKey = request.mandatoryParam("key");
    int from = Math.max(request.paramAsInt("from"), 1);
    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
        userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
        Iterable<String> linesHtml = checkFoundWithOptional(sourceService.getLinesAsHtml(dbSession, file.uuid(), from, to), "No source found for file '%s'", fileKey);
        JsonWriter json = response.newJsonWriter().beginObject();
        writeSource(linesHtml, from, json);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

DbSession (org.sonar.db.DbSession)254 ComponentDto (org.sonar.db.component.ComponentDto)63 OrganizationDto (org.sonar.db.organization.OrganizationDto)30 JsonWriter (org.sonar.api.utils.text.JsonWriter)24 UserDto (org.sonar.db.user.UserDto)20 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)16 Test (org.junit.Test)13 MetricDto (org.sonar.db.metric.MetricDto)13 Paging (org.sonar.api.utils.Paging)12 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)10 CeQueueDto (org.sonar.db.ce.CeQueueDto)8 DepthTraversalTypeAwareCrawler (org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler)8 SearchOptions (org.sonar.server.es.SearchOptions)8 NotFoundException (org.sonar.server.exceptions.NotFoundException)8 List (java.util.List)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 GroupDto (org.sonar.db.user.GroupDto)7 DbClient (org.sonar.db.DbClient)6 ProjectId (org.sonar.server.permission.ProjectId)6 RuleKey (org.sonar.api.rule.RuleKey)5