diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java index 348e502..e7cc89d 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPullRequest.java @@ -132,22 +132,72 @@ public class SchedulerPullRequest { private List processingUpdate(Map newPullRequests, Set pullRequests) { List updatePullRequest = new ArrayList<>(); for (PullRequest pullRequest : pullRequests) { - final PullRequest newPullRequest = newPullRequests.get(pullRequest.getId()); - final User author = pullRequest.getAuthor(); - StringBuilder stringBuilder = new StringBuilder(); - if (author.getTelegramId() != null) { - changeStatusPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); - changeReviewersPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); - final String message = stringBuilder.toString(); - if (!Message.EMPTY.equalsIgnoreCase(message)) { - updatePullRequest.add(newPullRequest); - messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build()); - } + PullRequest newPullRequest = newPullRequests.get(pullRequest.getId()); + @NonNull boolean author = processingAuthor(pullRequest, newPullRequest); + @NonNull boolean reviewer = processingReviewer(pullRequest, newPullRequest); + if (author || reviewer) { + updatePullRequest.add(newPullRequest); } } return updatePullRequest; } + private boolean processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) { + StringBuilder stringBuilder = new StringBuilder(); + changeVersionPr(pullRequest, newPullRequest).ifPresent(stringBuilder::append); + String message = stringBuilder.toString(); + if (!Message.EMPTY.equalsIgnoreCase(message)) { + newPullRequest.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.updatePullRequest( + newPullRequest.getName(), + newPullRequest.getUrl(), + newPullRequest.getAuthor().getLogin())) + .build()) + ); + return true; + } + return false; + } + + @NonNull + private boolean processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) { + final User author = pullRequest.getAuthor(); + StringBuilder stringBuilder = new StringBuilder(); + if (author.getTelegramId() != null) { + changeStatusPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); + changeReviewersPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append); + final String message = stringBuilder.toString(); + if (!Message.EMPTY.equalsIgnoreCase(message)) { + messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build()); + return true; + } + } + return false; + } + + @NonNull + private Optional changeVersionPr(PullRequest pullRequest, PullRequest newPullRequest) { + Integer oldVersion = pullRequest.getVersion(); + Integer newVersion = newPullRequest.getVersion(); + if (!oldVersion.equals(newVersion)) { + return Optional.of( + Message.updatePullRequest( + newPullRequest.getName(), + newPullRequest.getUrl(), + newPullRequest.getAuthor().getLogin() + ) + ); + } + return Optional.empty(); + } + @NonNull private Optional changeReviewersPR(PullRequest pullRequest, PullRequest newPullRequest) { final Map oldReviewers = pullRequest.getReviewers().stream() diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Message.java b/src/main/java/com/tsc/bitbucketbot/utils/Message.java index ccd6c01..ef0c4bd 100644 --- a/src/main/java/com/tsc/bitbucketbot/utils/Message.java +++ b/src/main/java/com/tsc/bitbucketbot/utils/Message.java @@ -24,6 +24,8 @@ public class Message { 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_UPDATE = "\uD83D\uDD04"; + private static final String HR = "\n -- -- -- -- --\n"; private Message() { throw new IllegalStateException("Утилитарный класс"); @@ -33,19 +35,16 @@ public class Message { 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"; + HR + + SMILE_AUTHOR + ": " + pullRequest.getAuthor().getLogin() + + TWO_BREAK; } @NonNull public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) { - return "✏️ *Изменился статус вашего ПР*" + BREAK + + return SMILE_PEN + " *Изменился статус вашего ПР*" + HR + "[" + name + "](" + url + ")" + BREAK + oldStatus.name() + " -> " + newStatus.name() + - BREAK + "-- -- -- --" + BREAK + - "\uD83D\uDCCC: #pullRequest #change" + TWO_BREAK; } @@ -84,13 +83,20 @@ public class Message { if (!EMPTY.equalsIgnoreCase(createMessage)) { return Optional.of( SMILE_PEN + " *Изменения ревьюверов вашего ПР*" + BREAK + + HR + "[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")" + BREAK + createMessage - + "\n-- -- -- -- --" ); } return Optional.empty(); } - + @NonNull + public static String updatePullRequest(String pullRequestName, String prUrl, String author) { + return SMILE_UPDATE + " *Обновление Pull Request*" + BREAK + + "[" + pullRequestName + "](" + prUrl + ")" + + HR + + SMILE_AUTHOR + ": " + author + + TWO_BREAK; + } } diff --git a/src/main/resources/liquibase/change-log.xml b/src/main/resources/liquibase/change-log.xml index fd3c82a..a84e0ec 100644 --- a/src/main/resources/liquibase/change-log.xml +++ b/src/main/resources/liquibase/change-log.xml @@ -4,6 +4,6 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> - + \ No newline at end of file diff --git a/src/main/resources/liquibase/change-set/v1.2.0.xml b/src/main/resources/liquibase/change-set/v1.2.0.xml index 84e6784..3cba5c3 100644 --- a/src/main/resources/liquibase/change-set/v1.2.0.xml +++ b/src/main/resources/liquibase/change-set/v1.2.0.xml @@ -4,9 +4,15 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> - + + + + + + + \ No newline at end of file