Search in sources :

Example 21 with WebhookDto

use of org.sonar.db.webhook.WebhookDto in project sonarqube by SonarSource.

the class CreateAction method doHandle.

private WebhookDto doHandle(DbSession dbSession, @Nullable ProjectDto project, String name, String url, @Nullable String secret) {
    WebhookDto dto = new WebhookDto().setUuid(uuidFactory.create()).setName(name).setUrl(url).setSecret(secret);
    if (project != null) {
        checkNumberOfWebhook(numberOfWebhookOf(dbSession, project), project.getKey());
        dto.setProjectUuid(project.getUuid());
    } else {
        checkNumberOfGlobalWebhooks(dbSession);
    }
    return dto;
}
Also used : WebhookDto(org.sonar.db.webhook.WebhookDto)

Example 22 with WebhookDto

use of org.sonar.db.webhook.WebhookDto in project sonarqube by SonarSource.

the class CreateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    String name = request.mandatoryParam(NAME_PARAM);
    String url = request.mandatoryParam(URL_PARAM);
    String projectKey = request.param(PROJECT_KEY_PARAM);
    String secret = request.param(SECRET_PARAM);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto projectDto = null;
        if (isNotBlank(projectKey)) {
            projectDto = componentFinder.getProjectByKey(dbSession, projectKey);
            webhookSupport.checkPermission(projectDto);
        } else {
            webhookSupport.checkPermission();
        }
        webhookSupport.checkUrlPattern(url, "Url parameter with value '%s' is not a valid url", url);
        WebhookDto dto = doHandle(dbSession, projectDto, name, url, secret);
        String projectName = projectDto == null ? null : projectDto.getName();
        dbClient.webhookDao().insert(dbSession, dto, projectKey, projectName);
        dbSession.commit();
        writeResponse(request, response, dto);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) WebhookDto(org.sonar.db.webhook.WebhookDto) DbSession(org.sonar.db.DbSession)

Example 23 with WebhookDto

use of org.sonar.db.webhook.WebhookDto in project sonarqube by SonarSource.

the class ListAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.param(PROJECT_KEY_PARAM);
    userSession.checkLoggedIn();
    try (DbSession dbSession = dbClient.openSession(true)) {
        List<WebhookDto> webhookDtos = doHandle(dbSession, projectKey);
        Map<String, WebhookDeliveryLiteDto> lastDeliveries = loadLastDeliveriesOf(dbSession, webhookDtos);
        writeResponse(request, response, webhookDtos, lastDeliveries);
    }
}
Also used : WebhookDto(org.sonar.db.webhook.WebhookDto) DbSession(org.sonar.db.DbSession) WebhookDeliveryLiteDto(org.sonar.db.webhook.WebhookDeliveryLiteDto)

Example 24 with WebhookDto

use of org.sonar.db.webhook.WebhookDto in project sonarqube by SonarSource.

the class UpdateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    String webhookKey = request.param(KEY_PARAM);
    String name = request.mandatoryParam(NAME_PARAM);
    String url = request.mandatoryParam(URL_PARAM);
    String secret = request.param(SECRET_PARAM);
    webhookSupport.checkUrlPattern(url, "Url parameter with value '%s' is not a valid url", url);
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<WebhookDto> dtoOptional = dbClient.webhookDao().selectByUuid(dbSession, webhookKey);
        WebhookDto webhookDto = checkFoundWithOptional(dtoOptional, "No webhook with key '%s'", webhookKey);
        String projectUuid = webhookDto.getProjectUuid();
        if (projectUuid != null) {
            ProjectDto projectDto = componentFinder.getProjectByUuid(dbSession, projectUuid);
            webhookSupport.checkPermission(projectDto);
            updateWebhook(dbSession, webhookDto, name, url, secret, projectDto.getKey(), projectDto.getName());
        } else {
            webhookSupport.checkPermission();
            updateWebhook(dbSession, webhookDto, name, url, secret, null, null);
        }
        dbSession.commit();
    }
    response.noContent();
}
Also used : WebhookDto(org.sonar.db.webhook.WebhookDto) ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession)

Example 25 with WebhookDto

use of org.sonar.db.webhook.WebhookDto in project sonarqube by SonarSource.

the class DeleteAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    String webhookKey = request.param(KEY_PARAM);
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<WebhookDto> dtoOptional = dbClient.webhookDao().selectByUuid(dbSession, webhookKey);
        WebhookDto webhookDto = checkFoundWithOptional(dtoOptional, "No webhook with key '%s'", webhookKey);
        String projectUuid = webhookDto.getProjectUuid();
        if (projectUuid != null) {
            Optional<ProjectDto> optionalDto = dbClient.projectDao().selectByUuid(dbSession, projectUuid);
            ProjectDto projectDto = checkStateWithOptional(optionalDto, "the requested project '%s' was not found", projectUuid);
            webhookSupport.checkPermission(projectDto);
            deleteWebhook(dbSession, webhookDto);
        } else {
            webhookSupport.checkPermission();
            deleteWebhook(dbSession, webhookDto);
        }
        dbSession.commit();
    }
    response.noContent();
}
Also used : WebhookDto(org.sonar.db.webhook.WebhookDto) ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession)

Aggregations

WebhookDto (org.sonar.db.webhook.WebhookDto)28 Test (org.junit.Test)22 ProjectDto (org.sonar.db.project.ProjectDto)16 TestRequest (org.sonar.server.ws.TestRequest)9 Webhooks (org.sonarqube.ws.Webhooks)6 ListResponse (org.sonarqube.ws.Webhooks.ListResponse)6 DbSession (org.sonar.db.DbSession)5 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)5 TestResponse (org.sonar.server.ws.TestResponse)5 WebhookDeliveryLiteDto (org.sonar.db.webhook.WebhookDeliveryLiteDto)2 WsActionTester (org.sonar.server.ws.WsActionTester)2 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 AssertionsForClassTypes.tuple (org.assertj.core.api.AssertionsForClassTypes.tuple)1 Rule (org.junit.Rule)1 Mockito.mock (org.mockito.Mockito.mock)1 Configuration (org.sonar.api.config.Configuration)1 ResourceTypes (org.sonar.api.resources.ResourceTypes)1 WebService (org.sonar.api.server.ws.WebService)1