diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java index 882078f..21516bb 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java @@ -38,7 +38,7 @@ public class Discussion { @Column(name = "resolved") private Boolean resolved; - @ManyToOne(optional = false) + @ManyToOne(optional = false, cascade = CascadeType.REMOVE) @JoinTable( name = "discussion_merge_request", joinColumns = @JoinColumn(name = "discussion_id"), diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java index 5a26d07..1ff65f6 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java @@ -26,4 +26,8 @@ public interface DiscussionRepository { Set findAllIds(); + void deleteById(String id); + + void cleanOld(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java index ea2ac74..5e1eda4 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java @@ -39,4 +39,8 @@ public interface DiscussionService { Set getAllIds(); + void deleteById(@NonNull String discussionId); + + void cleanOld(); + } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java index 718b396..aff089b 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java @@ -305,6 +305,18 @@ public class DiscussionServiceImpl implements DiscussionService { return repository.findAllIds(); } + @Override + public void deleteById(@NonNull String discussionId) { + repository.deleteById(discussionId); + } + + @Override + public void cleanOld() { + log.debug("Старт очистки старых дискуссий"); + repository.cleanOld(); + log.debug("Конец очистки старых дискуссий"); + } + /** * Уведомляет пользователя, если его никнейм упоминается в комментарии. */ diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java index 14eb805..575b6fa 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java @@ -170,12 +170,16 @@ public class DiscussionParser { final List newDiscussions = new ArrayList<>(); for (Discussion discussion : discussions) { - getOldDiscussionJson(discussion) - .map(json -> { - final Discussion newDiscussion = conversionService.convert(json, Discussion.class); - newDiscussion.getNotes().forEach(createNoteLink(discussion.getMergeRequest())); - return newDiscussion; - }).ifPresent(newDiscussions::add); + if (checkNotNull(discussion.getMergeRequest())) { + getOldDiscussionJson(discussion) + .map(json -> { + final Discussion newDiscussion = conversionService.convert(json, Discussion.class); + newDiscussion.getNotes().forEach(createNoteLink(discussion.getMergeRequest())); + return newDiscussion; + }).ifPresent(newDiscussions::add); + } else { + discussionService.deleteById(discussion.getId()); + } } if (checkNotEmpty(newDiscussions)) { diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java index 73d0751..658ea19 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java @@ -52,4 +52,14 @@ public class DiscussionRepositoryImpl implements DiscussionRepository { return jpaRepository.findAllIds(); } + @Override + public void deleteById(String id) { + jpaRepository.deleteById(id); + } + + @Override + public void cleanOld() { + jpaRepository.removeAllByMergeRequestIsNull(); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java index e79862d..e0d1e70 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java @@ -20,4 +20,6 @@ public interface DiscussionJpaRepository extends JpaRepository findAllIds(); + void removeAllByMergeRequestIsNull(); + } diff --git a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java index f02c3da..a4a93ee 100644 --- a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java +++ b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java @@ -1,6 +1,7 @@ package dev.struchkov.bot.gitlab.scheduler; import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.bot.gitlab.context.service.DiscussionService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.PipelineService; import dev.struchkov.bot.gitlab.core.service.parser.DiscussionParser; @@ -27,6 +28,7 @@ public class SchedulerService { private final PipelineService pipelineService; private final MergeRequestsService mergeRequestsService; + private final DiscussionService discussionService; @Scheduled(cron = "0 */1 * * * *") public void newMergeRequest() { @@ -39,6 +41,7 @@ public class SchedulerService { discussionParser.scanOldDiscussions(); discussionParser.scanNewDiscussion(); mergeRequestsService.cleanOld(); + discussionService.cleanOld(); pipelineService.cleanOld(); } else { log.warn("Процесс обновления данных не был выполнен, так как пользователь не выполнил первичную настройку.");