From d2cd4b62f5158a6cccf76c2880b733803ad45bc9 Mon Sep 17 00:00:00 2001 From: upagge Date: Thu, 10 Sep 2020 06:18:18 +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=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC?= =?UTF-8?q?=D1=8B=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bitbucketbot/domain/change/Change.java | 2 + .../change/comment/AnswerCommentChange.java | 15 +++ .../domain/change/comment/CommentChange.java | 10 ++ .../change/pullrequest/ConflictPrChange.java | 11 ++ .../change/pullrequest/NewPrChange.java | 15 +++ .../domain/change/pullrequest/PrChange.java | 4 +- .../change/pullrequest/ReviewersPrChange.java | 43 +++++++ .../change/pullrequest/StatusPrChange.java | 12 ++ .../change/pullrequest/UpdatePrChange.java | 13 ++ .../domain/change/task/TaskCloseChange.java | 36 ++++++ .../domain/change/task/TaskNewChange.java | 38 ++++++ .../bot/bitbucketbot/domain/entity/Task.java | 3 - .../repository/TaskRepository.java | 10 +- .../repository/impl/TaskRepositoryImpl.java | 24 +--- .../repository/jpa/TaskRepositoryJpa.java | 4 + .../scheduler/SchedulerChangeParsing.java | 59 +-------- .../bot/bitbucketbot/service/TaskService.java | 15 +-- .../service/impl/CommentServiceImpl.java | 36 +++++- .../service/impl/TaskServiceImpl.java | 60 ++++++--- .../service/parser/CommentAndTaskParser.java | 38 +----- .../bot/bitbucketbot/utils/Message.java | 121 +----------------- 21 files changed, 289 insertions(+), 280 deletions(-) create mode 100644 src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskCloseChange.java create mode 100644 src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskNewChange.java diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/Change.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/Change.java index 5838cb3..edc686b 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/Change.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/Change.java @@ -24,4 +24,6 @@ public abstract class Change { this.telegramIds = telegramIds; } + public abstract String generateMessage(); + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/AnswerCommentChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/AnswerCommentChange.java index 95a8a08..3faa7b4 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/AnswerCommentChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/AnswerCommentChange.java @@ -6,9 +6,12 @@ import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.Answer; import org.sadtech.bot.bitbucketbot.domain.change.Change; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; @Getter @EqualsAndHashCode(callSuper = true) @@ -31,4 +34,16 @@ public class AnswerCommentChange extends Change { this.answers = answers; } + @Override + public String generateMessage() { + final String answerText = answers.stream() + .map(answer -> answer.getAuthorName() + ": " + answer.getMessage().substring(0, Math.min(answer.getMessage().length(), 500))) + .collect(Collectors.joining("\n\n")); + return MessageFormat.format( + "{0} *Новые ответы на ваш комментарий* | [ПР]({1}){2}" + + "{3}{4}" + + "{5}", + Smile.BELL, url, Smile.HR, youMessage.substring(0, Math.min(youMessage.length(), 180)), Smile.HR, answerText + ); + } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/CommentChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/CommentChange.java index 49dc160..94b8707 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/CommentChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/comment/CommentChange.java @@ -5,7 +5,9 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.Change; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.Set; @Getter @@ -29,6 +31,14 @@ public class CommentChange extends Change { this.url = url; } + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Новое упоминание* | [ПР]({1}){2}" + + "{3}: {4}", + Smile.BELL, url, Smile.HR, authorName, message.replaceAll("@[\\w]+", "") + ); + } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ConflictPrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ConflictPrChange.java index 28bd291..a662df1 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ConflictPrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ConflictPrChange.java @@ -3,7 +3,9 @@ package org.sadtech.bot.bitbucketbot.domain.change.pullrequest; import lombok.Builder; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.Set; @Getter @@ -18,4 +20,13 @@ public class ConflictPrChange extends PrChange { super(ChangeType.CONFLICT_PR, telegramIds, name, url); } + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Внимание конфликт в ПР*" + + "{1}" + + "[{2}]({3})\n\n", + Smile.DANGEROUS, Smile.HR, title, url + ); + } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/NewPrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/NewPrChange.java index a10e2df..f12bfc7 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/NewPrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/NewPrChange.java @@ -4,7 +4,9 @@ import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.Set; @Getter @@ -26,4 +28,17 @@ public class NewPrChange extends PrChange { this.author = author; } + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Новый Pull Request*\n" + + "[{1}]({2})" + + "{3}\n" + + "{4}" + + "{5}: {6}\n\n", + Smile.FUN, title, url, Smile.HR, + (description != null && !"".equals(description)) ? description + Smile.HR : "", + Smile.AUTHOR, author + ); + } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/PrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/PrChange.java index a5a0f8f..ccb8c2e 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/PrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/PrChange.java @@ -11,8 +11,8 @@ import java.util.Set; @EqualsAndHashCode(callSuper = true) public abstract class PrChange extends Change { - private final String title; - private final String url; + protected final String title; + protected final String url; protected PrChange(ChangeType type, Set telegramIds, String title, String url) { super(type, telegramIds); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java index d68e055..cb9c72e 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java @@ -5,9 +5,16 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; +import org.sadtech.bot.bitbucketbot.utils.Smile; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; + +import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.DELETED; +import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.NEW; +import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.OLD; @Getter @EqualsAndHashCode(callSuper = true) @@ -25,4 +32,40 @@ public class ReviewersPrChange extends PrChange { this.reviewerChanges = reviewerChanges; } + @Override + public String generateMessage() { + StringBuilder stringBuilder = new StringBuilder(); + final Map> changes = reviewerChanges.stream() + .collect(Collectors.groupingBy(ReviewerChange::getType)); + if (changes.containsKey(OLD)) { + stringBuilder.append(Smile.BR).append("Изменили свое решение:").append(Smile.BR); + changes.get(OLD).forEach( + change -> stringBuilder + .append(Smile.AUTHOR).append(change.getName()).append(": ") + .append(change.getOldStatus().getValue()).append(" -> ") + .append(change.getStatus().getValue()) + .append(Smile.BR) + ); + } + if (changes.containsKey(NEW)) { + stringBuilder.append(Smile.BR).append("Новые ревьюверы:").append(Smile.BR); + changes.get(NEW).forEach( + change -> stringBuilder + .append(change.getName()).append(" (").append(change.getStatus().getValue()).append(")") + .append(Smile.BR) + ); + } + if (changes.containsKey(DELETED)) { + stringBuilder.append(Smile.BR).append("Не выдержали ревью:").append(Smile.BR) + .append( + changes.get(DELETED).stream() + .map(ReviewerChange::getName).collect(Collectors.joining(",")) + ); + } + final String createMessage = stringBuilder.toString(); + return Smile.PEN + " *Изменения ревьюверов вашего ПР*" + + Smile.HR + + "[" + title + "](" + url + ")\n" + + createMessage; + } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/StatusPrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/StatusPrChange.java index 7254bed..d647666 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/StatusPrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/StatusPrChange.java @@ -5,7 +5,9 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.Set; @Getter @@ -27,4 +29,14 @@ public class StatusPrChange extends PrChange { this.newStatus = newStatus; } + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Изменился статус вашего ПР*{1}" + + "[{2}]({3})\n" + + "{4} -> {5}\n\n", + Smile.PEN, Smile.HR, title, url, oldStatus.name(), newStatus.name() + ); + } + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/UpdatePrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/UpdatePrChange.java index 8e52fb9..cb485f3 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/UpdatePrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/UpdatePrChange.java @@ -4,7 +4,9 @@ import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; +import java.text.MessageFormat; import java.util.Set; @Getter @@ -22,4 +24,15 @@ public class UpdatePrChange extends PrChange { this.author = author; } + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Обновление Pull Request*\n" + + "[{1}]({2})" + + "{3}" + + "{4}: {5}\n\n", + Smile.UPDATE, title, url, Smile.HR, Smile.AUTHOR, author + ); + } + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskCloseChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskCloseChange.java new file mode 100644 index 0000000..14120eb --- /dev/null +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskCloseChange.java @@ -0,0 +1,36 @@ +package org.sadtech.bot.bitbucketbot.domain.change.task; + +import lombok.Builder; +import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; + +import java.text.MessageFormat; +import java.util.Set; + +/** + * // TODO: 10.09.2020 Добавить описание. + * + * @author upagge 10.09.2020 + */ +public class TaskCloseChange extends TaskChange { + + @Builder + protected TaskCloseChange( + Set telegramIds, + String authorName, + String url, + String messageTask + ) { + super(ChangeType.RESOLVED_TASK, telegramIds, authorName, url, messageTask); + } + + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} Задача выполнена* | [ПР]({1}){2}" + + "{3}: {4}", + Smile.TASK, url, Smile.HR, authorName, messageTask + ); + } + +} diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskNewChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskNewChange.java new file mode 100644 index 0000000..1816696 --- /dev/null +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/task/TaskNewChange.java @@ -0,0 +1,38 @@ +package org.sadtech.bot.bitbucketbot.domain.change.task; + +import lombok.Builder; +import lombok.Getter; +import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; +import org.sadtech.bot.bitbucketbot.utils.Smile; + +import java.text.MessageFormat; +import java.util.Set; + +/** + * // TODO: 10.09.2020 Добавить описание. + * + * @author upagge 10.09.2020 + */ +@Getter +public class TaskNewChange extends TaskChange { + + @Builder + protected TaskNewChange( + Set telegramIds, + String authorName, + String url, + String messageTask + ) { + super(ChangeType.NEW_TASK, telegramIds, authorName, url, messageTask); + } + + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Назначена новая задача* | [ПР]({1}){2}" + + "{3}: {4}", + Smile.TASK, url, Smile.HR, authorName, messageTask + ); + } + +} diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java index 07083d8..ff63775 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java @@ -10,8 +10,6 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinTable; import javax.persistence.OneToMany; @@ -32,7 +30,6 @@ public class Task { @Id @Column(name = "id") @EqualsAndHashCode.Include - @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; /** diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/TaskRepository.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/TaskRepository.java index 782fb48..9e5672b 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/TaskRepository.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/TaskRepository.java @@ -1,17 +1,11 @@ package org.sadtech.bot.bitbucketbot.repository; -import lombok.NonNull; +import org.sadtech.basic.context.repository.SimpleManagerRepository; import org.sadtech.bot.bitbucketbot.domain.entity.Task; import java.util.Optional; -public interface TaskRepository { - - Task save(@NonNull Task task); - - void deleteById(@NonNull Long id); - - Optional findById(@NonNull Long id); +public interface TaskRepository extends SimpleManagerRepository { Optional findFirstByOrderByIdDesc(); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/TaskRepositoryImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/TaskRepositoryImpl.java index e1a1788..93cb383 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/TaskRepositoryImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/TaskRepositoryImpl.java @@ -1,7 +1,6 @@ package org.sadtech.bot.bitbucketbot.repository.impl; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import org.sadtech.basic.database.repository.manager.AbstractSimpleManagerRepository; import org.sadtech.bot.bitbucketbot.domain.entity.Task; import org.sadtech.bot.bitbucketbot.repository.TaskRepository; import org.sadtech.bot.bitbucketbot.repository.jpa.TaskRepositoryJpa; @@ -10,29 +9,18 @@ import org.springframework.stereotype.Repository; import java.util.Optional; @Repository -@RequiredArgsConstructor -public class TaskRepositoryImpl implements TaskRepository { +public class TaskRepositoryImpl extends AbstractSimpleManagerRepository implements TaskRepository { private final TaskRepositoryJpa taskRepositoryJpa; - @Override - public Task save(@NonNull Task task) { - return taskRepositoryJpa.save(task); - } - - @Override - public void deleteById(@NonNull Long id) { - taskRepositoryJpa.deleteById(id); - } - - @Override - public Optional findById(@NonNull Long id) { - return taskRepositoryJpa.findById(id); + public TaskRepositoryImpl(TaskRepositoryJpa taskRepositoryJpa) { + super(taskRepositoryJpa); + this.taskRepositoryJpa = taskRepositoryJpa; } @Override public Optional findFirstByOrderByIdDesc() { - return Optional.empty(); + return taskRepositoryJpa.findFirstByOrderByIdDesc(); } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/TaskRepositoryJpa.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/TaskRepositoryJpa.java index 5c1a76e..92409d1 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/TaskRepositoryJpa.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/TaskRepositoryJpa.java @@ -3,6 +3,10 @@ package org.sadtech.bot.bitbucketbot.repository.jpa; import org.sadtech.bot.bitbucketbot.domain.entity.Task; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface TaskRepositoryJpa extends JpaRepository { + Optional findFirstByOrderByIdDesc(); + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerChangeParsing.java b/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerChangeParsing.java index 08f46df..5a94453 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerChangeParsing.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerChangeParsing.java @@ -1,21 +1,10 @@ package org.sadtech.bot.bitbucketbot.scheduler; -import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.sadtech.bot.bitbucketbot.domain.MessageSend; import org.sadtech.bot.bitbucketbot.domain.change.Change; -import org.sadtech.bot.bitbucketbot.domain.change.comment.AnswerCommentChange; -import org.sadtech.bot.bitbucketbot.domain.change.comment.CommentChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ConflictPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ReviewersPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.StatusPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.UpdatePrChange; -import org.sadtech.bot.bitbucketbot.domain.change.task.TaskChange; -import org.sadtech.bot.bitbucketbot.exception.NotFoundException; import org.sadtech.bot.bitbucketbot.service.ChangeService; import org.sadtech.bot.bitbucketbot.service.MessageSendService; -import org.sadtech.bot.bitbucketbot.utils.Message; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -45,7 +34,7 @@ public class SchedulerChangeParsing { .filter(change -> change.getTelegramIds() != null && !change.getTelegramIds().isEmpty()) .collect(Collectors.toList()); for (Change change : newChange) { - final String message = generateMessage(change); + final String message = change.generateMessage(); change.getTelegramIds().forEach( telegramId -> messageSendService.add( MessageSend.builder() @@ -57,50 +46,4 @@ public class SchedulerChangeParsing { } } - /** - * Создает сообщение, которое необходимо отправить в зависимости от типа изменения. - * - * @param change Объект изменения - * @return Текстовое сообщение - */ - private String generateMessage(@NonNull Change change) { - String message; - switch (change.getType()) { - case NEW_PR: - message = Message.generate(((NewPrChange) change)); - break; - case REVIEWERS: - message = Message.generate(((ReviewersPrChange) change)); - break; - case STATUS_PR: - message = Message.generate(((StatusPrChange) change)); - break; - case UPDATE_PR: - message = Message.generate(((UpdatePrChange) change)); - break; - case CONFLICT_PR: - message = Message.generate(((ConflictPrChange) change)); - break; - case NEW_COMMENT: - message = Message.generate(((CommentChange) change)); - break; - case NEW_ANSWERS_COMMENT: - message = Message.generate(((AnswerCommentChange) change)); - break; - case NEW_TASK: - case OPEN_TASK: - message = Message.generateNewTask(((TaskChange) change)); - break; - case DELETED_TASK: - message = Message.generateDeleteTask(((TaskChange) change)); - break; - case RESOLVED_TASK: - message = Message.generateResolveTask(((TaskChange) change)); - break; - default: - throw new NotFoundException("Нет обработчика для типа " + change.getType().name()); - } - return message; - } - } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/TaskService.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/TaskService.java index d05efed..7332fea 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/TaskService.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/TaskService.java @@ -1,20 +1,9 @@ package org.sadtech.bot.bitbucketbot.service; -import lombok.NonNull; +import org.sadtech.basic.context.service.SimpleManagerService; import org.sadtech.bot.bitbucketbot.domain.entity.Task; -import java.util.Collection; -import java.util.List; - -public interface TaskService { - - Task create(@NonNull Task task); - - void deleteById(@NonNull Long id); - - Task update(@NonNull Task task); - - List createAll(@NonNull Collection tasks); +public interface TaskService extends SimpleManagerService { Long getLastTaskId(); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java index 90e0935..97a5c91 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java @@ -3,31 +3,37 @@ package org.sadtech.bot.bitbucketbot.service.impl; import lombok.NonNull; import org.sadtech.basic.core.service.AbstractSimpleManagerService; import org.sadtech.bot.bitbucketbot.config.InitProperty; +import org.sadtech.bot.bitbucketbot.domain.change.comment.CommentChange; import org.sadtech.bot.bitbucketbot.domain.entity.Comment; import org.sadtech.bot.bitbucketbot.exception.NotFoundException; import org.sadtech.bot.bitbucketbot.repository.CommentRepository; +import org.sadtech.bot.bitbucketbot.service.ChangeService; import org.sadtech.bot.bitbucketbot.service.CommentService; import org.sadtech.bot.bitbucketbot.service.PersonService; -import org.sadtech.bot.bitbucketbot.service.PullRequestsService; import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Service public class CommentServiceImpl extends AbstractSimpleManagerService implements CommentService { + private static final Pattern PATTERN = Pattern.compile("@[\\w]+"); + private final CommentRepository commentRepository; private final PersonService personService; - private final PullRequestsService pullRequestsService; + private final ChangeService changeService; private final InitProperty initProperty; - public CommentServiceImpl(CommentRepository commentRepository, PersonService personService, PullRequestsService pullRequestsService, InitProperty initProperty) { + public CommentServiceImpl(CommentRepository commentRepository, PersonService personService, ChangeService changeService, InitProperty initProperty) { super(commentRepository); this.personService = personService; this.commentRepository = commentRepository; - this.pullRequestsService = pullRequestsService; + this.changeService = changeService; this.initProperty = initProperty; } @@ -47,7 +53,27 @@ public class CommentServiceImpl extends AbstractSimpleManagerService recipientsLogins = new HashSet<>(); + while (matcher.find()) { + final String login = matcher.group(0).replace("@", ""); + recipientsLogins.add(login); + } + final Set recipientsIds = personService.getAllTelegramIdByLogin(recipientsLogins); + changeService.save( + CommentChange.builder() + .authorName(comment.getAuthor()) + .url(comment.getUrl()) + .telegramIds(recipientsIds) + .message(comment.getMessage()) + .build() + ); } @Override diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java index a22e2a4..5938d8c 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java @@ -1,35 +1,60 @@ package org.sadtech.bot.bitbucketbot.service.impl; import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import org.sadtech.basic.core.service.AbstractSimpleManagerService; +import org.sadtech.basic.core.util.Assert; +import org.sadtech.bot.bitbucketbot.domain.change.task.TaskNewChange; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; import org.sadtech.bot.bitbucketbot.domain.entity.Task; -import org.sadtech.bot.bitbucketbot.exception.CreateException; import org.sadtech.bot.bitbucketbot.exception.NotFoundException; import org.sadtech.bot.bitbucketbot.repository.TaskRepository; +import org.sadtech.bot.bitbucketbot.service.ChangeService; +import org.sadtech.bot.bitbucketbot.service.PersonService; +import org.sadtech.bot.bitbucketbot.service.PullRequestsService; import org.sadtech.bot.bitbucketbot.service.TaskService; import org.springframework.stereotype.Service; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; +import java.util.Collections; @Service -@RequiredArgsConstructor -public class TaskServiceImpl implements TaskService { +public class TaskServiceImpl extends AbstractSimpleManagerService implements TaskService { private final TaskRepository taskRepository; - @Override - public Task create(@NonNull Task task) { - if (task.getId() == null) { - return taskRepository.save(task); - } - throw new CreateException("При создании объекта не должно быть идентификатора"); + private final PullRequestsService pullRequestsService; + private final ChangeService changeService; + private final PersonService personService; + + public TaskServiceImpl(TaskRepository taskRepository, PullRequestsService pullRequestsService, ChangeService changeService, PersonService personService) { + super(taskRepository); + this.taskRepository = taskRepository; + this.pullRequestsService = pullRequestsService; + this.changeService = changeService; + this.personService = personService; } @Override - public void deleteById(@NonNull Long id) { - taskRepository.deleteById(id); + public Task create(@NonNull Task task) { + Assert.isNotNull(task.getId(), "При создании объекта должен быть установлен идентификатор"); + final Task newTask = taskRepository.save(task); + + final PullRequest pullRequest = pullRequestsService.getById(task.getPullRequestId()) + .orElseThrow(() -> new NotFoundException("ПР не найден")); + + changeService.save( + TaskNewChange.builder() + .authorName(task.getAuthor()) + .messageTask(task.getDescription()) + .url(task.getUrl()) + .telegramIds( + personService.getAllTelegramIdByLogin( + Collections.singleton(pullRequest.getAuthorLogin()) + ) + ) + .build() + ); + + return newTask; } @Override @@ -39,11 +64,6 @@ public class TaskServiceImpl implements TaskService { return taskRepository.save(oldTask); } - @Override - public List createAll(@NonNull Collection tasks) { - return tasks.stream().map(this::create).collect(Collectors.toList()); - } - @Override public Long getLastTaskId() { return taskRepository.findFirstByOrderByIdDesc().map(Task::getId).orElse(0L); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java index 47ecfb8..48b5c3b 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java @@ -9,7 +9,6 @@ import org.sadtech.bot.bitbucketbot.config.properties.BitbucketProperty; import org.sadtech.bot.bitbucketbot.config.properties.CommentSchedulerProperty; import org.sadtech.bot.bitbucketbot.domain.Answer; import org.sadtech.bot.bitbucketbot.domain.change.comment.AnswerCommentChange; -import org.sadtech.bot.bitbucketbot.domain.change.comment.CommentChange; import org.sadtech.bot.bitbucketbot.domain.entity.Comment; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; import org.sadtech.bot.bitbucketbot.domain.entity.Task; @@ -30,12 +29,9 @@ import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -46,8 +42,6 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class CommentAndTaskParser { - private static final Pattern PATTERN = Pattern.compile("@[\\w]+"); - private final CommentService commentService; private final PullRequestsService pullRequestsService; private final PersonService personService; @@ -68,8 +62,8 @@ public class CommentAndTaskParser { executorScanner.registration(dataScans); final List resultScans = executorScanner.getResult(); if (!resultScans.isEmpty()) { - processingComments(resultScans); - processingTasks(resultScans); + commentService.createAll(getCommentsByResultScan(resultScans)); + taskService.createAll(getTaskByResultScan(resultScans)); count = 0; } } while (count++ < commentSchedulerProperty.getNoCommentCount()); @@ -83,16 +77,6 @@ public class CommentAndTaskParser { return commentStartId; } - private void processingComments(List resultScans) { - final List newComments = commentService.createAll(getCommentsByResultScan(resultScans)); - newComments.forEach(this::notificationPersonal); - } - - private void processingTasks(List resultScans) { - final List newTasks = taskService.createAll(getTaskByResultScan(resultScans)); - newTasks.forEach(changeService::createTask); - } - private List generatingLinksToPossibleComments(@NonNull Long commentId) { List commentUrls = new ArrayList<>(); for (int i = 0; i < 5; i++) { @@ -141,24 +125,6 @@ public class CommentAndTaskParser { .replace("{commentId}", String.valueOf(commentId)); } - private void notificationPersonal(@NonNull Comment comment) { - Matcher matcher = PATTERN.matcher(comment.getMessage()); - Set recipientsLogins = new HashSet<>(); - while (matcher.find()) { - final String login = matcher.group(0).replace("@", ""); - recipientsLogins.add(login); - } - final Set recipientsIds = personService.getAllTelegramIdByLogin(recipientsLogins); - changeService.createCommentChange( - CommentChange.builder() - .authorName(comment.getAuthor()) - .url(comment.getUrl()) - .telegramIds(recipientsIds) - .message(comment.getMessage()) - .build() - ); - } - public void scanOldComment() { @NonNull final List comments = commentService.getAllBetweenDate( LocalDateTime.now().minusDays(10), LocalDateTime.now() diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/utils/Message.java b/src/main/java/org/sadtech/bot/bitbucketbot/utils/Message.java index 691e86b..294ada5 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/utils/Message.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/utils/Message.java @@ -1,112 +1,28 @@ package org.sadtech.bot.bitbucketbot.utils; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import lombok.NonNull; -import org.sadtech.bot.bitbucketbot.domain.change.comment.AnswerCommentChange; -import org.sadtech.bot.bitbucketbot.domain.change.comment.CommentChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ConflictPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ReviewersPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.StatusPrChange; -import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.UpdatePrChange; -import org.sadtech.bot.bitbucketbot.domain.change.task.TaskChange; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; -import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; import org.sadtech.bot.bitbucketbot.dto.bitbucket.CommentJson; import java.time.LocalDate; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.DELETED; -import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.NEW; -import static org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange.Type.OLD; - /** * Генерирует сообщения для отправки. * * @author upagge [07.02.2020] */ -public class Message { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class Message { private static final UpdateDataComparator COMPARATOR = new UpdateDataComparator(); private static final Integer PR_COUNT = 4; private static final String DONATION_LINK = "https://www.tinkoff.ru/sl/1T9s4esiMf"; private static final String HELP_LINK = "https://nuzhnapomosh.ru/about/"; - private Message() { - throw new IllegalStateException("Утилитарный класс"); - } - - @NonNull - public static String generate(NewPrChange newPrChange) { - String message = Smile.FUN + " *Новый Pull Request*" + Smile.BR + - link(newPrChange.getTitle(), newPrChange.getUrl()) + - Smile.HR; - if (newPrChange.getDescription() != null && !"".equals(newPrChange.getDescription())) { - message += newPrChange.getDescription() + Smile.HR; - } - message += Smile.AUTHOR + ": " + newPrChange.getAuthor() + Smile.TWO_BR; - return message; - } - - public static String generate(@NonNull StatusPrChange change) { - return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR + - link(change.getTitle(), change.getUrl()) + Smile.BR + - change.getOldStatus().name() + " -> " + change.getNewStatus().name() + - Smile.TWO_BR; - } - - @NonNull - public static String generate(@NonNull ReviewersPrChange reviewersChange) { - StringBuilder stringBuilder = new StringBuilder(); - final Map> changes = reviewersChange.getReviewerChanges().stream() - .collect(Collectors.groupingBy(ReviewerChange::getType)); - if (changes.containsKey(OLD)) { - stringBuilder.append(Smile.BR).append("Изменили свое решение:").append(Smile.BR); - changes.get(OLD).forEach( - change -> stringBuilder - .append(Smile.AUTHOR).append(change.getName()).append(": ") - .append(change.getOldStatus().getValue()).append(" -> ") - .append(change.getStatus().getValue()) - .append(Smile.BR) - ); - } - if (changes.containsKey(NEW)) { - stringBuilder.append(Smile.BR).append("Новые ревьюверы:").append(Smile.BR); - changes.get(NEW).forEach( - change -> stringBuilder - .append(change.getName()).append(" (").append(change.getStatus().getValue()).append(")") - .append(Smile.BR) - ); - } - if (changes.containsKey(DELETED)) { - stringBuilder.append(Smile.BR).append("Не выдержали ревью:").append(Smile.BR) - .append( - changes.get(DELETED).stream() - .map(ReviewerChange::getName).collect(Collectors.joining(",")) - ); - } - final String createMessage = stringBuilder.toString(); - return Smile.PEN + " *Изменения ревьюверов вашего ПР*" + - Smile.HR + - link(reviewersChange.getTitle(), reviewersChange.getUrl()) + Smile.BR + - createMessage; - } - - public static String generate(@NonNull UpdatePrChange change) { - return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BR + - link(change.getTitle(), change.getUrl()) + - Smile.HR + - Smile.AUTHOR + ": " + change.getAuthor() + - Smile.TWO_BR; - } - - public static String generate(@NonNull ConflictPrChange change) { - return Smile.DANGEROUS + "*Внимание конфликт в ПР*" + Smile.HR + - link(change.getTitle(), change.getUrl()) + Smile.TWO_BR; - } - @NonNull public static String goodMorningStatistic(List pullRequestsReviews, List pullRequestsNeedWork) { StringBuilder message = new StringBuilder().append(Smile.SUN).append(" Доброе утро ").append(Smile.SUN).append(Smile.HR); @@ -150,35 +66,6 @@ public class Message { comment.getAuthor().getName() + ": " + comment.getText().replaceAll("@[\\w]+", ""); } - public static String generate(@NonNull CommentChange commentChange) { - return Smile.BELL + " *Новое упоминание* | " + link("ПР", commentChange.getUrl()) + Smile.HR + - commentChange.getAuthorName() + ": " + commentChange.getMessage().replaceAll("@[\\w]+", ""); - } - - public static String generate(@NonNull AnswerCommentChange answerCommentChange) { - final StringBuilder message = new StringBuilder(); - message.append(Smile.BELL).append("Новые ответы на ваш комментарий").append(" | ").append(link("ПР", answerCommentChange.getUrl())).append(Smile.HR) - .append(answerCommentChange.getYouMessage(), 0, Math.min(answerCommentChange.getYouMessage().length(), 180)).append(Smile.HR); - answerCommentChange.getAnswers().forEach(answer -> message.append(answer.getAuthorName()).append(": ") - .append(answer.getMessage(), 0, Math.min(answer.getMessage().length(), 500)).append(Smile.TWO_BR)); - return message.toString(); - } - - public static String generateNewTask(@NonNull TaskChange newTaskChange) { - return Smile.TASK + "*Назначена новая задача* | " + link("ПР", newTaskChange.getUrl()) + Smile.HR + - newTaskChange.getAuthorName() + ": " + newTaskChange.getMessageTask(); - } - - public static String generateDeleteTask(@NonNull TaskChange deletedTaskChange) { - return Smile.TASK + "*Задача была удалена* | " + link("ПР", deletedTaskChange.getUrl()) + Smile.HR + - deletedTaskChange.getAuthorName() + ": " + deletedTaskChange.getMessageTask(); - } - - public static String generateResolveTask(@NonNull TaskChange deletedTaskChange) { - return Smile.TASK + "*Задача выполнена* | " + link("ПР", deletedTaskChange.getUrl()) + Smile.HR + - deletedTaskChange.getAuthorName() + ": " + deletedTaskChange.getMessageTask(); - } - private static String needWorkPr(@NonNull List pullRequestsNeedWork) { final StringBuilder message = new StringBuilder(); pullRequestsNeedWork.stream()