From 7fe338820787f55f0dddc5f3ec79199d270b13a5 Mon Sep 17 00:00:00 2001 From: upagge Date: Tue, 7 Apr 2020 11:25:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/SchedulerPullRequest.java | 22 ++++++++++++++----- .../service/PullRequestsService.java | 4 ++++ .../service/impl/PullRequestsServiceImpl.java | 7 ++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java index 2b3b4b8..5da324f 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java @@ -8,6 +8,7 @@ import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.Reviewer; import com.tsc.bitbucketbot.domain.entity.User; import com.tsc.bitbucketbot.domain.util.ReviewerChange; +import com.tsc.bitbucketbot.dto.IdAndStatusPr; import com.tsc.bitbucketbot.dto.bitbucket.sheet.PullRequestSheetJson; import com.tsc.bitbucketbot.service.MessageSendService; import com.tsc.bitbucketbot.service.PullRequestsService; @@ -31,6 +32,11 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.tsc.bitbucketbot.domain.PullRequestStatus.DECLINED; +import static com.tsc.bitbucketbot.domain.PullRequestStatus.MERGED; +import static com.tsc.bitbucketbot.domain.PullRequestStatus.OPEN; /** * @author upagge [30.01.2020] @@ -39,6 +45,8 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class SchedulerPullRequest { + private static final Set STATUSES = Stream.of(MERGED, OPEN, DECLINED).collect(Collectors.toSet()); + private final PullRequestsService pullRequestsService; private final UserService userService; private final MessageSendService messageSendService; @@ -47,7 +55,9 @@ public class SchedulerPullRequest { @Scheduled(fixedRate = 30000) public void checkOldPullRequest() { - final Set existsId = pullRequestsService.getAllId(); + final Set existsId = pullRequestsService.getAllId(STATUSES).stream() + .map(IdAndStatusPr::getId) + .collect(Collectors.toSet()); final Set openId = checkOpenPullRequest(); final Set closeId = checkClosePullRequest(); final Set newNotExistsId = existsId.stream() @@ -119,17 +129,17 @@ public class SchedulerPullRequest { Optional sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen(), user.getToken(), PullRequestSheetJson.class); while (sheetJson.isPresent() && sheetJson.get().getValues() != null && !sheetJson.get().getValues().isEmpty()) { final PullRequestSheetJson jsonSheet = sheetJson.get(); - final Map existsJsonPr = jsonSheet.getValues().stream() + final Map existsPr = jsonSheet.getValues().stream() .filter(Objects::nonNull) .map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class)) .peek(pullRequest -> pullRequestsService.getIdByBitbucketIdAndReposId(pullRequest.getBitbucketId(), pullRequest.getRepositoryId()).ifPresent(pullRequest::setId)) .filter(pullRequest -> pullRequest.getId() != null) .collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest)); - final Set pullRequests = pullRequestsService.getAllById(existsJsonPr.keySet()); - if (!existsJsonPr.isEmpty() && !pullRequests.isEmpty()) { - processingUpdate(existsJsonPr, pullRequests); + final Set pullRequests = pullRequestsService.getAllById(existsPr.keySet()); + if (!existsPr.isEmpty() && !pullRequests.isEmpty()) { + processingUpdate(existsPr, pullRequests); ids.addAll( - pullRequestsService.updateAll(existsJsonPr.values()).stream() + pullRequestsService.updateAll(existsPr.values()).stream() .map(PullRequest::getId) .collect(Collectors.toSet()) ); diff --git a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java index e3adaae..8755468 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java +++ b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java @@ -1,8 +1,10 @@ package com.tsc.bitbucketbot.service; import com.tsc.bitbucketbot.domain.Pagination; +import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.entity.PullRequest; +import com.tsc.bitbucketbot.dto.IdAndStatusPr; import lombok.NonNull; import org.springframework.data.domain.Page; @@ -35,6 +37,8 @@ public interface PullRequestsService { Set getAllId(); + Set getAllId(Collection statuses); + Page getAll(@NonNull Pagination pagination); List getAllByAuthor(@NonNull String login, @NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); diff --git a/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java b/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java index 3a30eca..508b8cb 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java +++ b/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java @@ -1,8 +1,10 @@ package com.tsc.bitbucketbot.service.impl; import com.tsc.bitbucketbot.domain.Pagination; +import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.entity.PullRequest; +import com.tsc.bitbucketbot.dto.IdAndStatusPr; import com.tsc.bitbucketbot.repository.jpa.PullRequestsRepository; import com.tsc.bitbucketbot.service.PullRequestsService; import lombok.NonNull; @@ -76,6 +78,11 @@ public class PullRequestsServiceImpl implements PullRequestsService { return pullRequestsRepository.findAllIds(); } + @Override + public Set getAllId(Collection statuses) { + return pullRequestsRepository.findAllIdByStatusIn(statuses); + } + @Override public Page getAll(@NonNull Pagination pagination) { return pullRequestsRepository.findAll(PageRequest.of(pagination.getPage(), pagination.getSize()));