Browse Source

SONAR-10345 Ensure that no orphaned webhooks remains when deleting an organization or a project.

tags/7.5
Guillaume Jambet 6 years ago
parent
commit
f44552d26b

+ 9
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java View File

@@ -72,4 +72,13 @@ public class WebhookDao implements Dao {
private static WebhookMapper mapper(DbSession dbSession) {
return dbSession.getMapper(WebhookMapper.class);
}

public void cleanWebhooks(DbSession dbSession, OrganizationDto organization) {
mapper(dbSession).deleteForOrganizationUuid(organization.getUuid());
}

public void cleanWebhooks(DbSession dbSession, ComponentDto componentDto) {
mapper(dbSession).deleteForProjectUuid(componentDto.uuid());
}

}

+ 3
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookMapper.java View File

@@ -38,4 +38,7 @@ public interface WebhookMapper {

void delete(@Param("uuid") String uuid);

void deleteForOrganizationUuid(@Param("organizationUuid") String organizationUuid);

void deleteForProjectUuid(@Param("projectUuid") String organizationUuid);
}

+ 12
- 0
server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookMapper.xml View File

@@ -69,4 +69,16 @@
uuid = #{uuid,jdbcType=VARCHAR}
</delete>

<delete id="deleteForOrganizationUuid" parameterType="String">
delete from webhooks
where
organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
</delete>

<delete id="deleteForProjectUuid" parameterType="String">
delete from webhooks
where
project_uuid = #{projectUuid,jdbcType=VARCHAR}
</delete>

</mapper>

+ 36
- 0
server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoTest.java View File

@@ -28,6 +28,8 @@ import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDbTester;
import org.sonar.db.organization.OrganizationDto;

@@ -47,8 +49,10 @@ public class WebhookDaoTest {
private final DbSession dbSession = dbTester.getSession();
private final WebhookDao underTest = dbClient.webhookDao();
private final WebhookDbTester webhookDbTester = dbTester.webhooks();
private final ComponentDbTester componentDbTester = dbTester.components();
private final OrganizationDbTester organizationDbTester = dbTester.organizations();


@Test
public void selectByUuid_returns_empty_if_uuid_does_not_exist() {
assertThat(underTest.selectByUuid(dbSession, "missing")).isEmpty();
@@ -116,10 +120,42 @@ public class WebhookDaoTest {
assertThat(new Date(reloaded.getUpdatedAt())).isInSameMinuteWindowAs(new Date(system2.now()));
}

@Test
public void cleanWebhooksOfAProject() {

OrganizationDto organization = organizationDbTester.insert();
ComponentDto componentDto = componentDbTester.insertPrivateProject(organization);
webhookDbTester.insertWebhook(componentDto);
webhookDbTester.insertWebhook(componentDto);
webhookDbTester.insertWebhook(componentDto);
webhookDbTester.insertWebhook(componentDto);

underTest.cleanWebhooks(dbSession, componentDto);

Optional<WebhookDto> reloaded = underTest.selectByUuid(dbSession, componentDto.uuid());
assertThat(reloaded).isEmpty();
}

@Test
public void cleanWebhooksOfAnOrganization() {

OrganizationDto organization = organizationDbTester.insert();
webhookDbTester.insertWebhook(organization);
webhookDbTester.insertWebhook(organization);
webhookDbTester.insertWebhook(organization);
webhookDbTester.insertWebhook(organization);

underTest.cleanWebhooks(dbSession, organization);

Optional<WebhookDto> reloaded = underTest.selectByUuid(dbSession, organization.getUuid());
assertThat(reloaded).isEmpty();
}

@Test
public void delete() {

OrganizationDto organization = organizationDbTester.insert();

WebhookDto dto = webhookDbTester.insertWebhook(organization);

underTest.delete(dbSession, dto.getUuid());

+ 1
- 0
server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java View File

@@ -62,6 +62,7 @@ public class ComponentCleanerService {
checkArgument(!hasNotProjectScope(project) && !isNotDeletable(project) && project.getMainBranchProjectUuid() == null, "Only projects can be deleted");
dbClient.purgeDao().deleteProject(dbSession, project.uuid());
dbClient.userDao().cleanHomepage(dbSession, project);
dbClient.webhookDao().cleanWebhooks(dbSession, project);
projectIndexers.commitAndIndex(dbSession, singletonList(project), ProjectIndexer.Cause.PROJECT_DELETION);
}


+ 2
- 0
server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java View File

@@ -115,6 +115,7 @@ public class DeleteAction implements OrganizationsWsAction {

private void deleteProjects(DbSession dbSession, OrganizationDto organization) {
List<ComponentDto> roots = dbClient.componentDao().selectAllRootsByOrganization(dbSession, organization.getUuid());
roots.forEach(project -> dbClient.webhookDao().cleanWebhooks(dbSession, project));
componentCleanerService.delete(dbSession, roots);
}

@@ -151,6 +152,7 @@ public class DeleteAction implements OrganizationsWsAction {
dbClient.organizationMemberDao().deleteByOrganizationUuid(dbSession, organization.getUuid());
dbClient.organizationDao().deleteByUuid(dbSession, organization.getUuid());
dbClient.userDao().cleanHomepage(dbSession, organization);
dbClient.webhookDao().cleanWebhooks(dbSession, organization);
userIndexer.commitAndIndexByLogins(dbSession, logins);
}


Loading…
Cancel
Save