From a5e706a524728eee8fcf4bfb8d531fe0247ff1c4 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Tue, 20 Dec 2022 21:01:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA?= =?UTF-8?q?=D0=B8=20=D1=81=D1=82=D0=B0=D1=80=D1=8B=D1=85=20mr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/MergeRequestRepository.java | 7 +-- .../gitlab/context/service/CleanService.java | 18 ------- .../context/service/MergeRequestsService.java | 7 +-- .../core/service/impl/CleanServiceImpl.java | 54 ------------------- .../impl/MergeRequestsServiceImpl.java | 26 +++++---- .../filter/MergeRequestFilterService.java | 43 --------------- .../data/impl/MergeRequestRepositoryImpl.java | 14 ++--- .../data/jpa/MergeRequestJpaRepository.java | 2 + .../gitlab/scheduler/SchedulerService.java | 6 +-- 9 files changed, 29 insertions(+), 148 deletions(-) delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java delete mode 100644 bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java delete mode 100644 bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/MergeRequestFilterService.java diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java index 68ddb29..8a73e52 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java @@ -3,10 +3,7 @@ package dev.struchkov.bot.gitlab.context.repository; import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import dev.struchkov.haiti.filter.Filter; import lombok.NonNull; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Optional; @@ -26,8 +23,8 @@ public interface MergeRequestRepository { void deleteByIds(Set mergeRequestIds); - Page filter(Filter filter, Pageable pageable); - List findAllByReviewerId(Long personId); + void deleteByStates(Set states); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java deleted file mode 100644 index 59bf0ae..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.struchkov.bot.gitlab.context.service; - -/** - *

Контракт очистки хранилища.

- *

Так как все что мы получаем от гитлаба сохраняется в БД, иногда нужно удалять устаревшие данные. Например, MR, которые уже были давно вмержены.

- * - * @author upagge 08.02.2021 - */ -public interface CleanService { - - /** - *

Удаляет старые MR.

- *

По умолчанию старыми считаются те, которые закрыты или вмержены

- */ - void cleanOldMergedRequests(); - - -} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java index 4433695..67823e0 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java @@ -4,10 +4,7 @@ import dev.struchkov.bot.gitlab.context.domain.ExistContainer; import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import dev.struchkov.bot.gitlab.context.domain.filter.MergeRequestFilter; import lombok.NonNull; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Set; @@ -30,8 +27,6 @@ public interface MergeRequestsService { List getAll(); - Page getAll(@NonNull MergeRequestFilter filter, Pageable pagination); - ExistContainer existsById(@NonNull Set mergeRequestIds); List createAll(List newMergeRequests); @@ -40,4 +35,6 @@ public interface MergeRequestsService { List getAllByReviewerId(@NonNull Long personId); + void cleanOld(); + } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java deleted file mode 100644 index 1d0cb56..0000000 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -package dev.struchkov.bot.gitlab.core.service.impl; - -import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import dev.struchkov.bot.gitlab.context.domain.filter.MergeRequestFilter; -import dev.struchkov.bot.gitlab.context.service.CleanService; -import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.stereotype.Service; - -import java.util.Set; -import java.util.stream.Collectors; - -import static dev.struchkov.bot.gitlab.context.domain.MergeRequestState.CLOSED; -import static dev.struchkov.bot.gitlab.context.domain.MergeRequestState.MERGED; - -/** - * Реализация сервиса очистки данных. - * - * @author upagge 08.02.2021 - */ -@Slf4j -@Service -@RequiredArgsConstructor -public class CleanServiceImpl implements CleanService { - - private static final int COUNT = 1000; - private static final MergeRequestFilter MR_CLEAN_FILTER = MergeRequestFilter.builder() - .states(Set.of(MERGED, CLOSED)) - .build(); - - private final MergeRequestsService mergeRequestsService; - - @Override - public void cleanOldMergedRequests() { - log.debug("Старт очистки старых MR"); - int page = 0; - Page mergeRequestSheet = mergeRequestsService.getAll(MR_CLEAN_FILTER, PageRequest.of(page, COUNT)); - - while (mergeRequestSheet.hasContent()) { - final Set ids = mergeRequestSheet.getContent().stream() - .map(MergeRequest::getId) - .collect(Collectors.toUnmodifiableSet()); - - mergeRequestsService.deleteAllById(ids); - - mergeRequestSheet = mergeRequestsService.getAll(MR_CLEAN_FILTER, PageRequest.of(++page, COUNT)); - } - log.debug("Конец очистки старых MR"); - } - -} diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java index a98c363..0bfd1f6 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java @@ -10,7 +10,6 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Discussion; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Project; -import dev.struchkov.bot.gitlab.context.domain.filter.MergeRequestFilter; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictPrNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.NewPrNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.StatusPrNotify; @@ -20,11 +19,9 @@ import dev.struchkov.bot.gitlab.context.service.DiscussionService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.NotifyService; import dev.struchkov.bot.gitlab.context.service.ProjectService; -import dev.struchkov.bot.gitlab.core.service.impl.filter.MergeRequestFilterService; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -32,18 +29,23 @@ import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import static dev.struchkov.bot.gitlab.context.domain.MergeRequestState.CLOSED; +import static dev.struchkov.bot.gitlab.context.domain.MergeRequestState.MERGED; import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static java.lang.Boolean.TRUE; +@Slf4j @Service @RequiredArgsConstructor public class MergeRequestsServiceImpl implements MergeRequestsService { - private final NotifyService notifyService; + public static final Set DELETE_STATES = Set.of(MERGED, CLOSED); + private final MergeRequestRepository repository; - private final MergeRequestFilterService filterService; + + private final NotifyService notifyService; private final ProjectService projectService; private final DiscussionService discussionService; @@ -214,11 +216,6 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { return repository.findAll(); } - @Override - public Page getAll(@NonNull MergeRequestFilter filter, Pageable pagination) { - return filterService.getAll(filter, pagination); - } - @Override public ExistContainer existsById(@NonNull Set mergeRequestIds) { final List existsEntity = repository.findAllById(mergeRequestIds); @@ -251,6 +248,13 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { return repository.findAllByReviewerId(personId); } + @Override + public void cleanOld() { + log.debug("Старт очистки старых MR"); + repository.deleteByStates(DELETE_STATES); + log.debug("Конец очистки старых MR"); + } + private void notifyAboutUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { final Long botUserGitlabId = personInformation.getId(); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/MergeRequestFilterService.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/MergeRequestFilterService.java deleted file mode 100644 index 58624c6..0000000 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/MergeRequestFilterService.java +++ /dev/null @@ -1,43 +0,0 @@ -package dev.struchkov.bot.gitlab.core.service.impl.filter; - -import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestFields; -import dev.struchkov.bot.gitlab.context.domain.filter.MergeRequestFilter; -import dev.struchkov.bot.gitlab.context.repository.MergeRequestRepository; -import dev.struchkov.haiti.filter.Filter; -import dev.struchkov.haiti.filter.FilterQuery; -import dev.struchkov.haiti.filter.criteria.CriteriaFilter; -import dev.struchkov.haiti.filter.criteria.CriteriaQuery; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class MergeRequestFilterService { - - private final MergeRequestRepository repository; - - public Page getAll(MergeRequestFilter filter, Pageable pagination) { - return repository.filter(createFilter(filter), pagination); - } - - private Filter createFilter(@NonNull MergeRequestFilter filter) { - return CriteriaFilter.create() - .and(convertFilter(filter)) - .or(convertFilterOr(filter)); - } - - private FilterQuery convertFilterOr(MergeRequestFilter filter) { - return CriteriaQuery.create() - .matchPhrase(MergeRequestFields.state, filter.getStates()); - } - - private FilterQuery convertFilter(@NonNull MergeRequestFilter filter) { - return CriteriaQuery.create() - .matchPhrase(MergeRequestFields.assignee, filter.getAssignee()); - } - -} diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java index 9324d6e..c1a2737 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java @@ -5,12 +5,8 @@ import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import dev.struchkov.bot.gitlab.context.repository.MergeRequestRepository; import dev.struchkov.bot.gitlab.data.jpa.MergeRequestJpaRepository; -import dev.struchkov.haiti.filter.Filter; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Repository; import java.util.List; @@ -53,14 +49,14 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository { jpaRepository.deleteAllByIdIn(mergeRequestIds); } - @Override - public Page filter(Filter filter, Pageable pageable) { - return jpaRepository.findAll(filter.>build(), pageable); - } - @Override public List findAllByReviewerId(Long personId) { return jpaRepository.findAllByReviewersIn(personId); } + @Override + public void deleteByStates(Set states) { + jpaRepository.deleteAllByStateIn(states); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java index ef4184c..9e9d0ba 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java @@ -34,4 +34,6 @@ public interface MergeRequestJpaRepository extends JpaRepositoryImplementation findAllByReviewersIn(@Param("reviewerId") Long reviewerId); + void deleteAllByStateIn(Set states); + } 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 63ab34f..a1740d9 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,6 @@ package dev.struchkov.bot.gitlab.scheduler; -import dev.struchkov.bot.gitlab.context.service.CleanService; +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; import dev.struchkov.bot.gitlab.core.service.parser.MergeRequestParser; @@ -20,10 +20,10 @@ public class SchedulerService { private final PipelineParser pipelineParser; private final MergeRequestParser mergeRequestParser; - private final CleanService cleanService; private final DiscussionParser discussionParser; private final PipelineService pipelineService; + private final MergeRequestsService mergeRequestsService; @Scheduled(cron = "0 */1 * * * *") public void newMergeRequest() { @@ -34,8 +34,8 @@ public class SchedulerService { pipelineParser.scanNewPipeline(); discussionParser.scanOldDiscussions(); discussionParser.scanNewDiscussion(); + mergeRequestsService.cleanOld(); pipelineService.cleanOld(); - cleanService.cleanOldMergedRequests(); log.debug("Конец процесса обновления данных"); }