Browse Source

SONAR-8353 purge webhook deliveries when deleting project

tags/6.2-RC1
Simon Brandhof 7 years ago
parent
commit
feda9c29f8

+ 4
- 11
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookDeliveryStorageTest.java View File

@@ -20,8 +20,6 @@
package org.sonar.server.computation.task.projectanalysis.webhook;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
@@ -31,10 +29,11 @@ import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.webhook.WebhookDeliveryDto;

import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto;
import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids;

public class WebhookDeliveryStorageTest {

@@ -96,9 +95,8 @@ public class WebhookDeliveryStorageTest {

underTest.purge("PROJECT_1");

List<Map<String, Object>> uuids = dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries");
// do not purge another project PROJECT_2
assertThat(uuids).extracting(column -> column.get("uuid")).containsOnly("D2", "D3");
assertThat(selectAllDeliveryUuids(dbTester, dbSession)).containsOnly("D2", "D3");
}

private static WebhookDelivery.Builder newBuilderTemplate() {
@@ -111,14 +109,9 @@ public class WebhookDeliveryStorageTest {
}

private static WebhookDeliveryDto newDto(String uuid, String componentUuid, long at) {
return new WebhookDeliveryDto()
return newWebhookDeliveryDto()
.setUuid(uuid)
.setComponentUuid(componentUuid)
.setCeTaskUuid(randomAlphanumeric(40))
.setName("Jenkins")
.setUrl("http://jenkins")
.setSuccess(true)
.setPayload("{json}")
.setCreatedAt(at);
}
}

+ 7
- 0
sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java View File

@@ -210,4 +210,11 @@ class PurgeCommands {
session.commit();
profiler.stop();
}

public void deleteWebhookDeliveries(String rootUuid) {
profiler.start("deleteWebhookDeliveries (webhook_deliveries)");
purgeMapper.deleteWebhookDeliveriesByProjectUuid(rootUuid);
session.commit();
profiler.stop();
}
}

+ 1
- 0
sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java View File

@@ -162,6 +162,7 @@ public class PurgeDao implements Dao {
commands.deleteComponents(childrenIds);
commands.deleteFileSources(rootUuid);
commands.deleteCeActivity(rootUuid);
commands.deleteWebhookDeliveries(rootUuid);
}

public void deleteAnalyses(DbSession session, PurgeProfiler profiler, List<IdUuidPair> analysisIdUuids) {

+ 2
- 0
sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java View File

@@ -89,4 +89,6 @@ public interface PurgeMapper {
void deleteFileSourcesByUuid(@Param("fileUuids") List<String> fileUuids);

void deleteCeActivityByProjectUuid(String projectUuid);

void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid);
}

+ 4
- 0
sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml View File

@@ -319,5 +319,9 @@
delete from ce_activity where component_uuid=#{rootProjectUuid}
</delete>

<delete id="deleteWebhookDeliveriesByProjectUuid">
delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR}
</delete>

</mapper>


+ 12
- 0
sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java View File

@@ -43,6 +43,8 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.db.ce.CeTaskTypes.REPORT;
import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto;
import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids;

public class PurgeDaoTest {

@@ -210,6 +212,16 @@ public class PurgeDaoTest {
dbTester.assertDbUnit(getClass(), "should_delete_all_closed_issues-result.xml", "issues", "issue_changes");
}

@Test
public void deleteProject_deletes_webhook_deliveries() {
dbClient.webhookDeliveryDao().insert(dbSession, newWebhookDeliveryDto().setComponentUuid("P1").setUuid("D1"));
dbClient.webhookDeliveryDao().insert(dbSession, newWebhookDeliveryDto().setComponentUuid("P2").setUuid("D2"));

underTest.deleteProject(dbSession, "P1");

assertThat(selectAllDeliveryUuids(dbTester, dbSession)).containsOnly("D2");
}

private CeActivityDto insertCeActivity(String componentUuid) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setUuid(Uuids.create());

+ 58
- 0
sonar-db/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java View File

@@ -0,0 +1,58 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.webhook;

import java.util.List;
import java.util.stream.Collectors;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;

import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.apache.commons.lang.math.RandomUtils.nextBoolean;
import static org.apache.commons.lang.math.RandomUtils.nextInt;
import static org.apache.commons.lang.math.RandomUtils.nextLong;

public class WebhookDbTesting {

private WebhookDbTesting() {
// only statics
}

public static WebhookDeliveryDto newWebhookDeliveryDto() {
return new WebhookDeliveryDto()
.setUuid(randomAlphanumeric(40))
.setComponentUuid(randomAlphanumeric(40))
.setCeTaskUuid(randomAlphanumeric(40))
.setName(randomAlphanumeric(10))
.setUrl(randomAlphanumeric(10))
.setDurationMs(nextInt())
.setHttpStatus(nextInt())
.setSuccess(nextBoolean())
.setPayload(randomAlphanumeric(10))
.setCreatedAt(nextLong());
}

public static List<String> selectAllDeliveryUuids(DbTester dbTester, DbSession dbSession) {
return dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries")
.stream()
.map(columns -> (String)columns.get("uuid"))
.collect(Collectors.toList());
}
}

+ 8
- 13
sonar-db/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java View File

@@ -31,6 +31,7 @@ import org.sonar.db.DbSession;
import org.sonar.db.DbTester;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto;

public class WebhookDeliveryDaoTest {

@@ -53,7 +54,10 @@ public class WebhookDeliveryDaoTest {

@Test
public void insert_row_with_only_mandatory_columns() {
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1");
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1")
.setHttpStatus(null)
.setDurationMs(null)
.setErrorStacktrace(null);

underTest.insert(dbSession, dto);

@@ -68,11 +72,7 @@ public class WebhookDeliveryDaoTest {

@Test
public void insert_row_with_all_columns() {
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1")
.setDurationMs(10000)
.setHttpStatus(200)
.setErrorStacktrace("timeout")
.setPayload("{json}");
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1");

underTest.insert(dbSession, dto);

@@ -128,15 +128,10 @@ public class WebhookDeliveryDaoTest {
* Optional fields are kept null.
*/
private static WebhookDeliveryDto newDto(String uuid, String componentUuid, String ceTaskUuid) {
return new WebhookDeliveryDto()
return newWebhookDeliveryDto()
.setUuid(uuid)
.setComponentUuid(componentUuid)
.setCeTaskUuid(ceTaskUuid)
.setName("Jenkins")
.setUrl("http://jenkins")
.setSuccess(true)
.setPayload("{json}")
.setCreatedAt(DATE_1);
.setCeTaskUuid(ceTaskUuid);
}

private WebhookDeliveryDto selectByUuid(String uuid) {

Loading…
Cancel
Save