From d7fcbf549ab6207b984dc2bde3bec932d8547f50 Mon Sep 17 00:00:00 2001 From: upagge Date: Mon, 2 Mar 2020 11:53:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=89=D0=B8=D0=B9=20=D1=80=D0=B5?= =?UTF-8?q?=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/SchedulerPullRequest.java | 40 ++++++------------- .../service/PullRequestsService.java | 2 - .../service/impl/MessageSendServiceImpl.java | 3 ++ .../service/impl/PullRequestsServiceImpl.java | 8 ---- .../com/tsc/bitbucketbot/utils/Message.java | 16 ++++++-- 5 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java index ea11c12..348e502 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java @@ -24,7 +24,6 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -201,7 +200,7 @@ public class SchedulerPullRequest { .map(test -> conversionService.convert(test.getValue(), PullRequest.class)) .collect(Collectors.toList()); final List newPullRequests = pullRequestsService.addAll(newPullRequest); - sendNotification(newPullRequests); + sendNotificationNewPullRequest(newPullRequests); if (pullRequestBitbucketSheet.getNextPageStart() != null) { sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen() + pullRequestBitbucketSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class); } else { @@ -211,35 +210,22 @@ public class SchedulerPullRequest { } } - private void sendNotification(@NonNull List newPullRequests) { + private void sendNotificationNewPullRequest(@NonNull List newPullRequests) { if (!newPullRequests.isEmpty()) { - Map map = new HashMap<>(); newPullRequests.forEach( - pullRequest -> pullRequest.getReviewers().forEach( - reviewer -> test(pullRequest, reviewer, map) - ) + pullRequest -> pullRequest.getReviewers().stream() + .map(reviewer -> userService.getByLogin(reviewer.getUser())) + .filter(Optional::isPresent) + .map(Optional::get) + .filter(user -> user.getTelegramId() != null) + .forEach(user -> messageSendService.add( + MessageSend.builder() + .telegramId(user.getTelegramId()) + .message(Message.newPullRequest(pullRequest)) + .build() + )) ); - map.forEach((key, value) -> messageSendService.add(MessageSend.builder().telegramId(key).message(value.toString()).build())); } } - private void test(PullRequest pullRequest, Reviewer reviewer, Map map) { - userService.getByLogin(reviewer.getUser()).ifPresent( - user -> { - final Long telegramId = user.getTelegramId(); - if (telegramId != null) { - if (!map.containsKey(telegramId)) { - map.put(telegramId, new StringBuilder()); - } - map.get(telegramId).append("\uD83C\uDF89 *Новый Pull Request*\n") - .append("[").append(pullRequest.getName()).append("](").append(pullRequest.getUrl()).append(")\n") - .append("\uD83D\uDC68\u200D\uD83D\uDCBB️: ").append(pullRequest.getAuthor().getLogin()) - .append("\n-- -- -- -- --\n") - .append("\uD83D\uDCCC: ").append("#").append(pullRequest.getAuthor().getLogin()).append(" #pullRequest") - .append("\n\n"); - } - } - ); - } - } diff --git a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java index 243e9fa..203fb6a 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java +++ b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java @@ -29,6 +29,4 @@ public interface PullRequestsService { void deleteAll(@NonNull Set id); - Optional update(PullRequest pullRequest); - } diff --git a/src/main/java/com/tsc/bitbucketbot/service/impl/MessageSendServiceImpl.java b/src/main/java/com/tsc/bitbucketbot/service/impl/MessageSendServiceImpl.java index c5c5abd..d39016f 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/impl/MessageSendServiceImpl.java +++ b/src/main/java/com/tsc/bitbucketbot/service/impl/MessageSendServiceImpl.java @@ -17,6 +17,9 @@ public class MessageSendServiceImpl implements MessageSendService { @Override public void add(@NonNull MessageSend messageSend) { + messageSend.setMessage( + messageSend.getMessage().replace("localhost", "192.168.236.164:7990") + ); messageSendRepository.add(messageSend); } 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 e25ee29..e229037 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java +++ b/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java @@ -49,14 +49,6 @@ public class PullRequestsServiceImpl implements PullRequestsService { return pullRequestsRepository.saveAll(updatePullRequests); } - @Override - public Optional update(PullRequest pullRequest) { - if (pullRequestsRepository.existsById(pullRequest.getId())) { - pullRequestsRepository.save(pullRequest); - } - return Optional.empty(); - } - @Override public Optional getIdByBitbucketIdAndReposId(@NonNull Long bitbucketId, @NonNull Long repositoryId) { return pullRequestsRepository.findIdByBitbucketIdAndRepositoryId(bitbucketId, repositoryId); diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Message.java b/src/main/java/com/tsc/bitbucketbot/utils/Message.java index 61a91e6..ccd6c01 100644 --- a/src/main/java/com/tsc/bitbucketbot/utils/Message.java +++ b/src/main/java/com/tsc/bitbucketbot/utils/Message.java @@ -13,7 +13,7 @@ import java.util.stream.Collectors; import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*; /** - * TODO: Добавить описание класса. + * Генерирует сообщения для отправки. * * @author upagge [07.02.2020] */ @@ -29,10 +29,20 @@ public class Message { throw new IllegalStateException("Утилитарный класс"); } + @NonNull + public static String newPullRequest(PullRequest pullRequest) { + return "\uD83C\uDF89 *Новый Pull Request*\n" + + "[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")\n" + + "\uD83D\uDC68\u200D\uD83D\uDCBB️: " + pullRequest.getAuthor().getLogin() + + "\n-- -- -- -- --\n" + + "\uD83D\uDCCC: " + "#" + pullRequest.getAuthor().getLogin() + " #pullRequest" + + "\n\n"; + } + @NonNull public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) { return "✏️ *Изменился статус вашего ПР*" + BREAK + - "[" + name + "](" + url.replace("localhost", "192.168.236.164") + ")" + BREAK + + "[" + name + "](" + url + ")" + BREAK + oldStatus.name() + " -> " + newStatus.name() + BREAK + "-- -- -- --" + BREAK + "\uD83D\uDCCC: #pullRequest #change" + @@ -74,7 +84,7 @@ public class Message { if (!EMPTY.equalsIgnoreCase(createMessage)) { return Optional.of( SMILE_PEN + " *Изменения ревьюверов вашего ПР*" + BREAK + - "[" + pullRequest.getName() + "](" + pullRequest.getUrl().replace("localhost", "192.168.236.164") + ")" + BREAK + + "[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")" + BREAK + createMessage + "\n-- -- -- -- --" );