From 24d3edaedf856808aa03f56277dd3e9839750fe8 Mon Sep 17 00:00:00 2001 From: upagge Date: Wed, 4 Mar 2020 15:03:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=B8=20=D0=BD=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BE=20=D0=B4=D0=BE=D0=BD=D0=B0=D1=82=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/SchedulerPullRequest.java | 5 +- .../com/tsc/bitbucketbot/utils/Message.java | 108 ++++++------------ .../com/tsc/bitbucketbot/utils/Smile.java | 53 +++++++++ 3 files changed, 92 insertions(+), 74 deletions(-) create mode 100644 src/main/java/com/tsc/bitbucketbot/utils/Smile.java diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java index 0ddff3f..1039b46 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java @@ -17,6 +17,7 @@ import com.tsc.bitbucketbot.service.Utils; import com.tsc.bitbucketbot.service.converter.PullRequestJsonConverter; import com.tsc.bitbucketbot.utils.Message; import com.tsc.bitbucketbot.utils.Pair; +import com.tsc.bitbucketbot.utils.Smile; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.core.convert.ConversionService; @@ -158,7 +159,7 @@ public class SchedulerPullRequest { StringBuilder stringBuilder = new StringBuilder(); changeVersionPr(pullRequest, newPullRequest).ifPresent(stringBuilder::append); String message = stringBuilder.toString(); - if (!Message.EMPTY.equalsIgnoreCase(message)) { + if (!Smile.Constants.EMPTY.equalsIgnoreCase(message)) { newPullRequest.getReviewers().stream() .map(reviewer -> userService.getByLogin(reviewer.getUser())) .filter(Optional::isPresent) @@ -184,7 +185,7 @@ public class SchedulerPullRequest { changeStatusPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); changeReviewersPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); final String message = stringBuilder.toString(); - if (!Message.EMPTY.equalsIgnoreCase(message)) { + if (!Smile.Constants.EMPTY.equalsIgnoreCase(message)) { messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build()); } } diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Message.java b/src/main/java/com/tsc/bitbucketbot/utils/Message.java index 9752b86..bdbf75c 100644 --- a/src/main/java/com/tsc/bitbucketbot/utils/Message.java +++ b/src/main/java/com/tsc/bitbucketbot/utils/Message.java @@ -6,7 +6,6 @@ import com.tsc.bitbucketbot.domain.util.ReviewerChange; import lombok.NonNull; import java.time.LocalDate; -import java.time.Period; import java.util.List; import java.util.Map; import java.util.Optional; @@ -22,26 +21,6 @@ import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*; */ public class Message { - public static final String EMPTY = ""; - private static final String BREAK = "\n"; - private static final String TWO_BREAK = "\n\n"; - private static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB️"; - private static final String SMILE_PEN = "✏️"; - private static final String SMILE_FUN = "\uD83C\uDF89"; - private static final String SMILE_UPDATE = "\uD83D\uDD04"; - private static final String SMILE_SUN = "\uD83D\uDD06"; - private static final String SMILE_MIG = "\uD83D\uDE09"; - private static final String SMILE_BUY = "\uD83D\uDC4B"; - private static final String SMILE_FLOWER = "\uD83C\uDF40"; - private static final String SMILE_DAY_0 = "\uD83C\uDF15"; - private static final String SMILE_DAY_1 = "\uD83C\uDF16"; - private static final String SMILE_DAY_2 = "\uD83C\uDF17"; - private static final String SMILE_DAY_3 = "\uD83C\uDF18"; - private static final String SMILE_DAY_4 = "\uD83C\uDF11"; - private static final String SMILE_DAY_5 = "\uD83C\uDF1A"; - private static final String SMILE_MEGA_FUN = "\uD83D\uDE02"; - private static final String HR = "\n -- -- -- -- --\n"; - private static final UpdateDataComparator COMPARATOR = new UpdateDataComparator(); private static final Integer PR_COUNT = 4; @@ -51,19 +30,19 @@ public class Message { @NonNull public static String newPullRequest(PullRequest pullRequest) { - return SMILE_FUN + " *Новый Pull Request*" + BREAK + + return Smile.FUN + " *Новый Pull Request*" + Smile.BREAK + linkPr(pullRequest.getName(), pullRequest.getUrl()) + - HR + - SMILE_AUTHOR + ": " + pullRequest.getAuthor().getLogin() + - TWO_BREAK; + Smile.HR + + Smile.AUTHOR + ": " + pullRequest.getAuthor().getLogin() + + Smile.TWO_BREAK; } @NonNull public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) { - return SMILE_PEN + " *Изменился статус вашего ПР*" + HR + - linkPr(name, url) + BREAK + + return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR + + linkPr(name, url) + Smile.BREAK + oldStatus.name() + " -> " + newStatus.name() + - TWO_BREAK; + Smile.TWO_BREAK; } @NonNull @@ -72,25 +51,25 @@ public class Message { final Map> changes = reviewerChanges.stream() .collect(Collectors.groupingBy(ReviewerChange::getType)); if (changes.containsKey(OLD)) { - stringBuilder.append(BREAK).append("Изменили свое решение:").append(BREAK); + stringBuilder.append(Smile.BREAK).append("Изменили свое решение:").append(Smile.BREAK); changes.get(OLD).forEach( change -> stringBuilder - .append(SMILE_AUTHOR).append(change.getName()).append(": ") + .append(Smile.AUTHOR).append(change.getName()).append(": ") .append(change.getOldStatus().getValue()).append(" -> ") .append(change.getStatus().getValue()) - .append(BREAK) + .append(Smile.BREAK) ); } if (changes.containsKey(NEW)) { - stringBuilder.append(BREAK).append("Новые ревьюверы:").append(BREAK); + stringBuilder.append(Smile.BREAK).append("Новые ревьюверы:").append(Smile.BREAK); changes.get(NEW).forEach( change -> stringBuilder .append(change.getName()).append(" (").append(change.getStatus().getValue()).append(")") - .append(BREAK) + .append(Smile.BREAK) ); } if (changes.containsKey(DELETED)) { - stringBuilder.append(BREAK).append("Не выдержали ревью:").append(BREAK) + stringBuilder.append(Smile.BREAK).append("Не выдержали ревью:").append(Smile.BREAK) .append( changes.get(DELETED).stream() .map(ReviewerChange::getName).collect(Collectors.joining(",")) @@ -98,11 +77,11 @@ public class Message { } final String createMessage = stringBuilder.toString(); - if (!EMPTY.equalsIgnoreCase(createMessage)) { + if (!Smile.Constants.EMPTY.equalsIgnoreCase(createMessage)) { return Optional.of( - SMILE_PEN + " *Изменения ревьюверов вашего ПР*" + - HR + - linkPr(pullRequest.getName(), pullRequest.getUrl()) + BREAK + + Smile.PEN + " *Изменения ревьюверов вашего ПР*" + + Smile.HR + + linkPr(pullRequest.getName(), pullRequest.getUrl()) + Smile.BREAK + createMessage ); } @@ -111,19 +90,19 @@ public class Message { @NonNull public static String updatePullRequest(String pullRequestName, String prUrl, String author) { - return SMILE_UPDATE + " *Обновление Pull Request*" + BREAK + + return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BREAK + linkPr(pullRequestName, prUrl) + - HR + - SMILE_AUTHOR + ": " + author + - TWO_BREAK; + Smile.HR + + Smile.AUTHOR + ": " + author + + Smile.TWO_BREAK; } @NonNull public static String goodMorningStatistic(List pullRequests) { - StringBuilder message = new StringBuilder(SMILE_SUN).append(" Доброе утро ").append(SMILE_SUN).append(HR); + StringBuilder message = new StringBuilder().append(Smile.SUN).append(" Доброе утро ").append(Smile.SUN).append(Smile.HR); if (!pullRequests.isEmpty()) { - message.append("Сегодня тебя ждет проверка целых ").append(pullRequests.size()).append(" ПР!").append(TWO_BREAK) - .append("Топ старых ПР:").append(BREAK); + message.append("Сегодня тебя ждет проверка целых ").append(pullRequests.size()).append(" ПР!").append(Smile.TWO_BREAK) + .append("Топ старых ПР:").append(Smile.BREAK); List oldPr = pullRequests.stream() .sorted(COMPARATOR) .limit(PR_COUNT) @@ -133,7 +112,7 @@ public class Message { ); Set oldPrIds = oldPr.stream().map(PullRequest::getId).collect(Collectors.toSet()); if (pullRequests.size() > PR_COUNT) { - message.append(BREAK).append("Свежие ПР:").append(BREAK); + message.append(Smile.BREAK).append("Свежие ПР:").append(Smile.BREAK); pullRequests .stream() .filter(pullRequest -> !oldPrIds.contains(pullRequest.getId())) @@ -144,40 +123,25 @@ public class Message { ); } } else { - message.append("Ты либо самый лучший работник, либо тебе не доверяют проверку ПР ").append(SMILE_MEGA_FUN).append(TWO_BREAK) - .append("Поздравляю, у тебя ни одного ПР на проверку!").append(BREAK); + message.append("Ты либо самый лучший работник, либо тебе не доверяют проверку ПР ").append(Smile.MEGA_FUN).append(Smile.TWO_BREAK) + .append("Поздравляю, у тебя ни одного ПР на проверку!").append(Smile.BREAK); } if (dayX()) { - message.append(BREAK).append(SMILE_FUN).append(" Кстати, поздравляю, сегодня день З/П").append(BREAK); + message.append(Smile.BREAK).append(Smile.FUN).append(" Кстати, поздравляю, сегодня день З/П").append(Smile.BREAK) + .append(Smile.DANGEROUS).append("И раз такое дело, то напоминаю, что в виду независящих от разработчика условий бот работает на платном VDS. Поэтому всячески приветствуются ") + .append(linkPr("донаты на оплату сервера", "%donat_link%")).append(Smile.BREAK); } message - .append(BREAK) - .append("Удачного дня ").append(SMILE_FLOWER).append(TWO_BREAK); + .append(Smile.BREAK) + .append("Удачного дня ").append(Smile.FLOWER).append(Smile.TWO_BREAK); return message.toString(); } private static String topPr(PullRequest pullRequest) { - return selectSmile(pullRequest) + " " + + return Smile.statusPr(pullRequest.getUpdateDate()) + " " + linkPr(pullRequest.getName(), pullRequest.getUrl()) + - BREAK; - } - - private static String selectSmile(PullRequest pullRequest) { - switch (Period.between(LocalDate.now(), pullRequest.getUpdateDate()).getDays()) { - case 0: - return SMILE_DAY_0; - case 1: - return SMILE_DAY_1; - case 2: - return SMILE_DAY_2; - case 3: - return SMILE_DAY_3; - case 4: - return SMILE_DAY_4; - default: - return SMILE_DAY_5; - } + Smile.BREAK; } private static boolean dayX() { @@ -192,7 +156,7 @@ public class Message { @NonNull public static String goodWeekEnd() { - return "Ну вот и все! Веселых выходных " + SMILE_MIG + BREAK + - "До понедельника" + SMILE_BUY + TWO_BREAK; + return "Ну вот и все! Веселых выходных " + Smile.MIG + Smile.BREAK + + "До понедельника" + Smile.BUY + Smile.TWO_BREAK; } } diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Smile.java b/src/main/java/com/tsc/bitbucketbot/utils/Smile.java new file mode 100644 index 0000000..5de03d8 --- /dev/null +++ b/src/main/java/com/tsc/bitbucketbot/utils/Smile.java @@ -0,0 +1,53 @@ +package com.tsc.bitbucketbot.utils; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDate; +import java.time.Period; + +@AllArgsConstructor +public enum Smile { + + BREAK("\n"), + TWO_BREAK("\n\n"), + AUTHOR("\uD83D\uDC68\u200D\uD83D\uDCBB️"), + PEN("✏️"), + FUN("\uD83C\uDF89"), + UPDATE("\uD83D\uDD04"), + SUN("\uD83D\uDD06"), + MIG("\uD83D\uDE09"), + BUY("\uD83D\uDC4B"), + FLOWER("\uD83C\uDF40"), + DAY_0("\uD83C\uDF15"), + DAY_1("\uD83C\uDF16"), + DAY_2("\uD83C\uDF17"), + DAY_3("\uD83C\uDF18"), + DAY_4("\uD83C\uDF11"), + DAY_5("\uD83C\uDF1A"), + MEGA_FUN("\uD83D\uDE02"), + DANGEROUS("⚠️"), + HR("\n -- -- -- -- --\n"); + + @Getter + private String value; + + public static Smile statusPr(LocalDate updateDate) { + int periodDay = Period.between(LocalDate.now(), updateDate).getDays(); + if (periodDay < 5) { + return Smile.valueOf("DAY_" + periodDay); + } else { + return Smile.DAY_5; + } + } + + @Override + public String toString() { + return value; + } + + public static class Constants { + public static final String EMPTY = ""; + } + +}