From 246e4235c19c4379075b48ff9385890b08e8fedb Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 15 Jan 2023 16:46:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20=D0=B3=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8E=20=D1=83=D0=B2=D0=B5?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20Not?= =?UTF-8?q?ifyBoxAnswerGenerator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 7 +- .../gitlab/context/domain/PipelineStatus.java | 30 +- .../context/domain/entity/AppSetting.java | 3 + .../context/domain/entity/Discussion.java | 2 +- .../domain/notify/NewProjectNotify.java | 42 ++- .../gitlab/context/domain/notify/Notify.java | 2 +- .../domain/notify/SimpleTextNotify.java | 19 - .../notify/comment/AnswerCommentNotify.java | 52 --- .../notify/comment/NewCommentNotify.java | 64 ++-- .../notify/mergerequest/ConflictPrNotify.java | 12 +- .../mergerequest/ForgottenSmartPrNotify.java | 36 -- .../notify/mergerequest/NewPrNotify.java | 33 +- .../notify/mergerequest/StatusPrNotify.java | 12 +- .../notify/mergerequest/UpdatePrNotify.java | 25 +- .../notify/pipeline/PipelineNotify.java | 58 +-- .../notify/task/DiscussionNewNotify.java | 28 +- .../domain/notify/task/TaskCloseNotify.java | 16 +- .../repository/DiscussionRepository.java | 2 + .../repository/MergeRequestRepository.java | 2 + .../repository/PipelineRepository.java | 2 + .../context/repository/ProjectRepository.java | 2 + .../context/service/AppSettingService.java | 4 + .../context/service/DiscussionService.java | 2 + .../context/service/MergeRequestsService.java | 2 + .../gitlab/context/service/NotifyService.java | 4 - .../context/service/PipelineService.java | 2 + .../context/service/ProjectService.java | 4 + .../bot/gitlab/context/utils/Icons.java | 31 ++ .../gitlab/context/utils/MessageUtils.java | 55 --- .../bot/gitlab/context/utils/Smile.java | 13 - .../service/impl/AppSettingServiceImpl.java | 20 +- .../service/impl/DiscussionServiceImpl.java | 5 + .../impl/MergeRequestsServiceImpl.java | 6 + .../core/service/impl/NotifyServiceImpl.java | 26 +- .../service/impl/PipelineServiceImpl.java | 14 +- .../core/service/impl/ProjectServiceImpl.java | 6 + .../GetAllDiscussionForMergeRequestTask.java | 2 +- .../GetAllMergeRequestForProjectTask.java | 2 +- .../parser/forktask/GetPipelineShortTask.java | 2 +- .../parser/forktask/GetPipelineTask.java | 2 +- .../forktask/GetSingleMergeRequestTask.java | 2 +- .../data/impl/DiscussionRepositoryImpl.java | 5 + .../data/impl/MergeRequestRepositoryImpl.java | 5 + .../data/impl/PipelineRepositoryImpl.java | 5 + .../data/impl/ProjectRepositoryImpl.java | 5 + .../data/jpa/DiscussionJpaRepository.java | 5 + .../data/jpa/MergeRequestJpaRepository.java | 3 + .../data/jpa/PipelineJpaRepository.java | 4 + .../gitlab/data/jpa/ProjectJpaRepository.java | 5 + .../bot/gitlab/config/AppConfig.java | 27 +- .../gitlab/scheduler/SchedulerService.java | 23 +- .../v.2.0.0/2022-12-03-create-tables.xml | 7 +- pom.xml | 38 ++ .../telegram/config/TelegramBotConfig.java | 25 +- .../service/MessageSendTelegramService.java | 33 +- .../gitlab/telegram/service/StartNotify.java | 26 +- .../notify/ConflictPrNotifyGenerator.java | 31 ++ .../notify/DiscussionNewNotifyGenerator.java | 48 +++ .../notify/NewCommentNotifyGenerator.java | 41 +++ .../service/notify/NewPrNotifyGenerator.java | 45 +++ .../notify/NewProjectNotifyGenerator.java | 45 +++ .../notify/NotifyBoxAnswerGenerator.java | 18 + .../notify/PipelineNotifyGenerator.java | 60 +++ .../notify/StatusPrNotifyGenerator.java | 33 ++ .../notify/TaskCloseNotifyGenerate.java | 31 ++ .../notify/UpdatePrNotifyGenerator.java | 44 +++ .../bot/gitlab/telegram/unit/CommandUnit.java | 110 ++++++ .../bot/gitlab/telegram/unit/MenuConfig.java | 69 +++- .../bot/gitlab/telegram/unit/UnitConfig.java | 265 -------------- .../telegram/unit/flow/InitSettingFlow.java | 345 ++++++++++++++++++ .../bot/gitlab/telegram/utils/Keys.java | 13 + .../bot/gitlab/telegram/utils/UnitName.java | 13 +- 72 files changed, 1354 insertions(+), 726 deletions(-) delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/SimpleTextNotify.java delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/AnswerCommentNotify.java delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ForgottenSmartPrNotify.java create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/MessageUtils.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ConflictPrNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/DiscussionNewNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewCommentNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewPrNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewProjectNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NotifyBoxAnswerGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/StatusPrNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/TaskCloseNotifyGenerate.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/UpdatePrNotifyGenerator.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java delete mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/UnitConfig.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/flow/InitSettingFlow.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Keys.java diff --git a/.drone.yml b/.drone.yml index 884171f..edd8920 100644 --- a/.drone.yml +++ b/.drone.yml @@ -97,4 +97,9 @@ steps: commands: - echo "$DOCKER_REGISTRY_TOKEN" | docker login docker.io --username $DOCKER_REGISTRY_USER --password-stdin - docker buildx create --use - - docker buildx build --push --platform linux/amd64,linux/arm64/v8 -t "$DOCKER_IMAGE_NAME:latest" -t "$DOCKER_IMAGE_NAME:$DRONE_TAG" . \ No newline at end of file + - docker buildx build --push --platform linux/amd64,linux/arm64/v8 -t "$DOCKER_IMAGE_NAME:latest" -t "$DOCKER_IMAGE_NAME:$DRONE_TAG" . +--- +kind: signature +hmac: 1bb00c9edf1c37a2f36c92ea6c1cb9023630b05550b4dc3148c6f5b30cad4af6 + +... diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/PipelineStatus.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/PipelineStatus.java index 39458f5..65a3df1 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/PipelineStatus.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/PipelineStatus.java @@ -1,20 +1,28 @@ package dev.struchkov.bot.gitlab.context.domain; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + /** * @author upagge 17.01.2021 */ +@Getter +@RequiredArgsConstructor public enum PipelineStatus { - CREATED, - WAITING_FOR_RESOURCE, - PREPARING, - PENDING, - RUNNING, - SUCCESS, - FAILED, - CANCELED, - SKIPPED, - MANUAL, - SCHEDULED + CREATED("\uD83C\uDD95"), + WAITING_FOR_RESOURCE("\uD83D\uDCA2"), + PREPARING("♿️"), + PENDING("⚠️"), + RUNNING("\uD83D\uDD04"), + SUCCESS("✅"), + FAILED("❌"), + CANCELED("\uD83D\uDEAB"), + SKIPPED("\uD83D\uDD18"), + MANUAL("\uD83D\uDD79"), + SCHEDULED("\uD83D\uDD52"), + NULL("\uD83C\uDD95"); + + private final String icon; } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/AppSetting.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/AppSetting.java index cf10d74..c58bb70 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/AppSetting.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/AppSetting.java @@ -26,4 +26,7 @@ public class AppSetting { @Column(name = "first_start") private boolean firstStart; + @Column(name = "enable_notify") + private boolean enableNotify; + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java index 91ad68a..882078f 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/Discussion.java @@ -38,7 +38,7 @@ public class Discussion { @Column(name = "resolved") private Boolean resolved; - @ManyToOne + @ManyToOne(optional = false) @JoinTable( name = "discussion_merge_request", joinColumns = @JoinColumn(name = "discussion_id"), diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/NewProjectNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/NewProjectNotify.java index 436578d..661be63 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/NewProjectNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/NewProjectNotify.java @@ -1,35 +1,37 @@ package dev.struchkov.bot.gitlab.context.domain.notify; -import dev.struchkov.bot.gitlab.context.utils.Smile; -import dev.struchkov.haiti.utils.Strings; import lombok.Builder; - -import java.text.MessageFormat; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; +import lombok.Getter; /** * @author upagge 15.01.2021 */ -public record NewProjectNotify( - String projectName, - String projectUrl, - String projectDescription, - String authorName -) implements Notify { +@Getter +public final class NewProjectNotify implements Notify { + + public static final String TYPE = "NewProjectNotify"; + + private final String projectName; + private final String projectUrl; + private final String projectDescription; + private final String authorName; @Builder - public NewProjectNotify { + public NewProjectNotify( + String projectName, + String projectUrl, + String projectDescription, + String authorName + ) { + this.projectName = projectName; + this.projectUrl = projectUrl; + this.projectDescription = projectDescription; + this.authorName = authorName; } @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *New project*{1}[{2}]({3}){1}{4}{5}: {6}", - Smile.FUN.getValue(), Smile.HR.getValue(), projectName, projectUrl, - (projectDescription != null && !"".equals(projectDescription)) ? escapeMarkdown(projectDescription) + Smile.HR : Strings.EMPTY, - Smile.AUTHOR.getValue(), authorName - ); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/Notify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/Notify.java index 6c1bb37..2a5ae01 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/Notify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/Notify.java @@ -2,6 +2,6 @@ package dev.struchkov.bot.gitlab.context.domain.notify; public interface Notify { - String generateMessage(); + String getType(); } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/SimpleTextNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/SimpleTextNotify.java deleted file mode 100644 index 4c20a25..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/SimpleTextNotify.java +++ /dev/null @@ -1,19 +0,0 @@ -package dev.struchkov.bot.gitlab.context.domain.notify; - -import lombok.Builder; - -/** - * @author upagge 20.09.2020 - */ -public record SimpleTextNotify(String message) implements Notify { - - @Builder - public SimpleTextNotify { - } - - @Override - public String generateMessage() { - return message; - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/AnswerCommentNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/AnswerCommentNotify.java deleted file mode 100644 index df275a0..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/AnswerCommentNotify.java +++ /dev/null @@ -1,52 +0,0 @@ -package dev.struchkov.bot.gitlab.context.domain.notify.comment; - -import dev.struchkov.bot.gitlab.context.domain.Answer; -import dev.struchkov.bot.gitlab.context.domain.notify.Notify; -import dev.struchkov.bot.gitlab.context.utils.Smile; -import dev.struchkov.haiti.utils.Strings; -import lombok.Builder; -import lombok.Getter; - -import java.text.MessageFormat; -import java.util.List; -import java.util.stream.Collectors; - -import static dev.struchkov.haiti.utils.Strings.TWO_NEW_LINE; -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; - -@Getter -public class AnswerCommentNotify implements Notify { - - private final String youMessage; - private final String url; - private final List answers; - - @Builder - protected AnswerCommentNotify( - String youMessage, - String url, - List answers - ) { - this.youMessage = youMessage; - this.url = url; - 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(TWO_NEW_LINE)); - return MessageFormat.format( - "{0} *Новые ответы* на [комментарий]({1}){2}" + - "{3}{2}" + - "{4}", - Smile.COMMENT, - url, - Smile.HR, - escapeMarkdown(Strings.cutoff(youMessage, 180)), - escapeMarkdown(answerText) - ); - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java index 6385b1f..a0ffd7f 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java @@ -1,41 +1,45 @@ package dev.struchkov.bot.gitlab.context.domain.notify.comment; import dev.struchkov.bot.gitlab.context.domain.notify.Notify; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; +import lombok.Getter; -import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; +@Getter +public final class NewCommentNotify implements Notify { + + public static final String TYPE = "NewCommentNotify"; + + private final String url; + private final String discussionMessage; + private final String discussionAuthor; + private final String previousMessage; + private final String previousAuthor; + private final String authorName; + private final String message; + + @Builder + public NewCommentNotify( + String url, + String discussionMessage, + String discussionAuthor, + String previousMessage, + String previousAuthor, + String authorName, + String message + ) { + this.url = url; + this.discussionMessage = discussionMessage; + this.discussionAuthor = discussionAuthor; + this.previousMessage = previousMessage; + this.previousAuthor = previousAuthor; + this.authorName = authorName; + this.message = message; + } -@Builder -public record NewCommentNotify( - String url, - String discussionMessage, - String discussionAuthor, - String previousMessage, - String previousAuthor, - String authorName, - String message -) implements Notify { @Override - public String generateMessage() { - - final StringBuilder builder = new StringBuilder(Smile.COMMENT.getValue()).append(" [New answer in discussion](").append(url).append(")\n--- --- --- ---"); - - if (checkNotNull(discussionMessage)) { - builder.append("\n-- -- discussion first message -- --\n") - .append("*").append(discussionAuthor).append("*: ").append(escapeMarkdown(discussionMessage)); - } - - if (checkNotNull(previousMessage)) { - builder.append("\n-- -- -- previous message -- -- --\n") - .append("*").append(previousAuthor).append("*: ").append(escapeMarkdown(previousMessage)); - } - - builder.append("\n-- -- -- --- new answer --- -- -- --\n") - .append("*").append(authorName).append("*: ").append(escapeMarkdown(message)); - return builder.toString(); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictPrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictPrNotify.java index 5132563..a959495 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictPrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictPrNotify.java @@ -1,14 +1,13 @@ package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; import lombok.Getter; -import java.text.MessageFormat; - @Getter public class ConflictPrNotify extends PrNotify { + public static final String TYPE = "ConflictPrNotify"; + private final String sourceBranch; @Builder @@ -23,11 +22,8 @@ public class ConflictPrNotify extends PrNotify { } @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *Attention! MergeRequest conflict | {4}*{1}[{2}]({3})", - Smile.DANGEROUS.getValue(), Smile.HR.getValue(), title, url, projectName, sourceBranch - ); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ForgottenSmartPrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ForgottenSmartPrNotify.java deleted file mode 100644 index 805d7ed..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ForgottenSmartPrNotify.java +++ /dev/null @@ -1,36 +0,0 @@ -package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; - -import dev.struchkov.bot.gitlab.context.utils.Smile; -import lombok.Builder; -import lombok.Getter; - -import java.text.MessageFormat; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -@Getter -//TODO [28.01.2022]: Решить доработать и оставить или удалить -public class ForgottenSmartPrNotify extends PrNotify { - - @Builder - protected ForgottenSmartPrNotify( - String title, - String url, - String projectName, - String repositorySlug - ) { - super(projectName, title, url); - } - - @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *MergeRequest Review Reminder | {4}*{3}[{1}]({2})", - Smile.SMART.getValue(), title, url, Smile.HR.getValue(), projectName - ); - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewPrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewPrNotify.java index f8c0ca3..7ccb2d2 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewPrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewPrNotify.java @@ -1,19 +1,15 @@ package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; -import dev.struchkov.bot.gitlab.context.utils.Smile; -import dev.struchkov.haiti.utils.Strings; import lombok.Builder; import lombok.Getter; -import java.text.MessageFormat; import java.util.Set; -import java.util.stream.Collectors; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; @Getter public class NewPrNotify extends PrNotify { + public static final String TYPE = "NewPrNotify"; + private final String description; private final String author; private final String targetBranch; @@ -40,29 +36,8 @@ public class NewPrNotify extends PrNotify { } @Override - public String generateMessage() { - String labelText = labels.stream() - .map(label -> "#" + label) - .collect(Collectors.joining(" ")); - if (!labelText.isEmpty()) { - labelText = "\n\n" + labelText; - } - return MessageFormat.format( - "{0} *New merge request for review | {1}*{2}[{3}]({4}){5}{2}{9}: {10} {12} {11}\n{7}: {8}", - Smile.FUN.getValue(), - projectName, - Smile.HR.getValue(), - title, - url, - labelText, - (description != null && !"".equals(description)) ? escapeMarkdown(description) + Smile.HR : Strings.EMPTY, - Smile.AUTHOR.getValue(), - author, - Smile.TREE.getValue(), - sourceBranch, - targetBranch, - Smile.ARROW.getValue() - ); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusPrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusPrNotify.java index 9b123d7..6e689c0 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusPrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusPrNotify.java @@ -1,15 +1,14 @@ package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; import lombok.Getter; -import java.text.MessageFormat; - @Getter public class StatusPrNotify extends PrNotify { + public static final String TYPE = "StatusPrNotify"; + private final MergeRequestState oldStatus; private final MergeRequestState newStatus; @@ -27,11 +26,8 @@ public class StatusPrNotify extends PrNotify { } @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *MergeRequest status changed | {7}*{1}[{2}]({3}){1}{4} {5} {6}", - Smile.PEN.getValue(), Smile.HR.getValue(), title, url, oldStatus.name(), Smile.ARROW.getValue(), newStatus.name(), projectName - ); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdatePrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdatePrNotify.java index be6149e..2fa9881 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdatePrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdatePrNotify.java @@ -1,12 +1,13 @@ package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; import lombok.Getter; @Getter public class UpdatePrNotify extends PrNotify { + public static final String TYPE = "UpdatePrNotify"; + private final String author; private final Long allTasks; private final Long allResolvedTasks; @@ -32,26 +33,10 @@ public class UpdatePrNotify extends PrNotify { this.personResolvedTasks = personResolvedTasks; } + @Override - public String generateMessage() { - final StringBuilder builder = new StringBuilder(Smile.UPDATE.getValue()).append(" *MergeRequest update | ").append(projectName).append("*") - .append(Smile.HR.getValue()) - .append("[").append(title).append("](").append(url).append(")"); - - - if (allTasks > 0) { - builder.append(Smile.HR.getValue()) - .append("All tasks: ").append(allResolvedTasks).append("/").append(allTasks); - - if (personTasks > 0) { - builder.append("\nYour tasks: ").append(personResolvedTasks).append("/").append(personTasks); - } - } - - builder.append(Smile.HR.getValue()) - .append(Smile.AUTHOR.getValue()).append(": ").append(author); - - return builder.toString(); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java index 95f986b..ea20bb4 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java @@ -1,43 +1,47 @@ package dev.struchkov.bot.gitlab.context.domain.notify.pipeline; +import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.notify.Notify; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; - -import java.text.MessageFormat; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; +import lombok.Getter; /** * @author upagge 17.01.2021 */ //TODO [16.12.2022|uPagge]: Нужно реализовать заполнение projectName -public record PipelineNotify( - Long pipelineId, - String projectName, - String refName, - String oldStatus, - String newStatus, - String webUrl -) implements Notify { + +@Getter +public final class PipelineNotify implements Notify { + + public static final String TYPE = "PipelineNotify"; + + private final Long projectId; + private final Long pipelineId; + private final String refName; + private final PipelineStatus oldStatus; + private final PipelineStatus newStatus; + private final String webUrl; @Builder - public PipelineNotify { + public PipelineNotify( + Long projectId, + Long pipelineId, + String refName, + PipelineStatus oldStatus, + PipelineStatus newStatus, + String webUrl + ) { + this.projectId = projectId; + this.pipelineId = pipelineId; + this.refName = refName; + this.oldStatus = oldStatus; + this.newStatus = newStatus; + this.webUrl = webUrl; } @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *Pipeline {1,number,#}* | {2}{3}[{4}]({5}){3}{6} {7} {8}", - Smile.BUILD, - pipelineId, - escapeMarkdown(projectName), - Smile.HR, - refName, - webUrl, - oldStatus, - Smile.ARROW, - newStatus - ); + public String getType() { + return TYPE; } + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java index 69c0877..9caef45 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java @@ -1,16 +1,11 @@ package dev.struchkov.bot.gitlab.context.domain.notify.task; -import dev.struchkov.bot.gitlab.context.utils.Smile; import dev.struchkov.haiti.utils.Pair; import lombok.Builder; import lombok.Getter; import lombok.Singular; import java.util.List; -import java.util.stream.Collectors; - -import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; /** * @author upagge 10.09.2020 @@ -18,6 +13,8 @@ import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; @Getter public class DiscussionNewNotify extends TaskNotify { + public static final String TYPE = "DiscussionNewNotify"; + private final String mrName; private final List> notes; @@ -35,25 +32,8 @@ public class DiscussionNewNotify extends TaskNotify { } @Override - public String generateMessage() { - final StringBuilder builder = new StringBuilder(Smile.TASK.getValue()).append(" [New discussion](").append(url).append(")") - .append(Smile.HR.getValue()) - .append(escapeMarkdown(mrName)) - .append(Smile.HR.getValue()) - .append("*").append(authorName).append("*: ").append(escapeMarkdown(messageTask)); - - if (checkNotNull(notes)) { - builder.append("\n-- -- -- -- comments -- -- -- --\n") - .append(convertNotes(notes)); - } - - return builder.toString(); - } - - private String convertNotes(List> notes) { - return notes.stream() - .map(note -> "*" + note.getKey() + "*: " + note.getValue()) - .collect(Collectors.joining("\n")); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskCloseNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskCloseNotify.java index 776d5b1..f32d7f9 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskCloseNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskCloseNotify.java @@ -1,17 +1,16 @@ package dev.struchkov.bot.gitlab.context.domain.notify.task; -import dev.struchkov.bot.gitlab.context.utils.Smile; import lombok.Builder; - -import java.text.MessageFormat; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; +import lombok.Getter; /** * @author upagge 10.09.2020 */ +@Getter public class TaskCloseNotify extends TaskNotify { + public static final String TYPE = "TaskCloseNotify"; + private final Long personTasks; private final Long personResolvedTasks; @@ -29,11 +28,8 @@ public class TaskCloseNotify extends TaskNotify { } @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *Closed [task]({1}){2}*{3}*: {4}", - Smile.TASK.getValue(), url, Smile.HR.getValue(), authorName, escapeMarkdown(messageTask), personTasks, personResolvedTasks - ); + public String getType() { + return TYPE; } } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java index 2d06dd0..5a26d07 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/DiscussionRepository.java @@ -24,4 +24,6 @@ public interface DiscussionRepository { List findAllById(Set discussionIds); + Set findAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java index 75f589b..57a6e3a 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java @@ -26,4 +26,6 @@ public interface MergeRequestRepository { void deleteByStates(Set states); + Set findAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java index 2b05e1a..de53495 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java @@ -23,4 +23,6 @@ public interface PipelineRepository { void deleteByCreatedBefore(LocalDateTime date); + Set findAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/ProjectRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/ProjectRepository.java index cd1c327..202b136 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/ProjectRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/ProjectRepository.java @@ -25,4 +25,6 @@ public interface ProjectRepository { Set findAllIds(); + Optional findProjectNameById(Long projectId); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/AppSettingService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/AppSettingService.java index f42eb44..7120614 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/AppSettingService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/AppSettingService.java @@ -21,4 +21,8 @@ public interface AppSettingService { */ void disableFirstStart(); + boolean isEnableAllNotify(); + + void turnOnAllNotify(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java index e0a0bfb..ea2ac74 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/DiscussionService.java @@ -37,4 +37,6 @@ public interface DiscussionService { List getAll(); + Set getAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java index 36cdd0e..aceaa4b 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java @@ -36,4 +36,6 @@ public interface MergeRequestsService { void cleanOld(); + Set getAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/NotifyService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/NotifyService.java index 1a55373..25ce24a 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/NotifyService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/NotifyService.java @@ -12,8 +12,4 @@ public interface NotifyService { void send(T notify); - void enableAllNotify(); - - void disableAllNotify(); - } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java index b5bbe39..1c28198 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java @@ -29,4 +29,6 @@ public interface PipelineService { void cleanOld(); + Set getAllIds(); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/ProjectService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/ProjectService.java index e3c1a13..ec21e35 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/ProjectService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/ProjectService.java @@ -5,6 +5,7 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Project; import lombok.NonNull; import java.util.List; +import java.util.Optional; import java.util.Set; /** @@ -25,4 +26,7 @@ public interface ProjectService { ExistContainer existsById(Set projectIds); Set getAllIds(); + + Optional getProjectNameById(Long projectId); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java new file mode 100644 index 0000000..49de493 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java @@ -0,0 +1,31 @@ +package dev.struchkov.bot.gitlab.context.utils; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +public class Icons { + + public static final String HR = "\n-- -- -- -- --\n"; + + public static final String FUN = "\uD83C\uDF89"; + public static final String VIEW = "\uD83D\uDC40"; + public static final String TREE = "\uD83C\uDF33"; + public static final String AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB️"; + public static final String UPDATE = "\uD83D\uDD04"; + public static final String COMMENT = "\uD83D\uDCAC"; + public static final String TASK = "\uD83D\uDCBC"; + public static final String ARROW = " ➜ "; + public static final String DANGEROUS = "⚠️"; + public static final String PEN = "✏️"; + public static final String BUILD = "\uD83D\uDEE0"; + public static final String LINK = "\uD83D\uDD17"; + + private Icons() { + utilityClass(); + } + + public static String link(String title, String url) { + return "[" + escapeMarkdown(title) + "](" + url + ")"; + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/MessageUtils.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/MessageUtils.java deleted file mode 100644 index eb8f536..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/MessageUtils.java +++ /dev/null @@ -1,55 +0,0 @@ -package dev.struchkov.bot.gitlab.context.utils; - -import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.NonNull; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -/** - * Генерирует сообщения для отправки. - * - * @author upagge [07.02.2020] - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class MessageUtils { - - public static Optional mergeRequestForReview(@NonNull List mergeRequestsReviews) { - if (!mergeRequestsReviews.isEmpty()) { - return Optional.of( - mergeRequestsReviews.stream() - .map(MessageUtils::generateStringItemMergeRequestReview) - .collect(Collectors.joining("\n")) - ); - } - return Optional.empty(); - } - - public static Optional mergeRequestForNeedWork(@NonNull List mergeRequestNeedWork) { - if (!mergeRequestNeedWork.isEmpty()) { - return Optional.of( - mergeRequestNeedWork.stream() - .map(MessageUtils::generateStringItemMergeRequestNeedWork) - .collect(Collectors.joining("\n")) - ); - } - return Optional.empty(); - } - - private static String generateStringItemMergeRequestNeedWork(MergeRequest mergeRequest) { - return "-- " + link(mergeRequest.getTitle(), mergeRequest.getWebUrl()); - } - - private static String generateStringItemMergeRequestReview(MergeRequest mergeRequest) { - return Smile.statusPr(mergeRequest.getUpdatedDate()) + " " + - link(mergeRequest.getTitle(), mergeRequest.getWebUrl()); - } - - private static String link(String name, String url) { - return "[" + name + "](" + url + ")"; - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Smile.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Smile.java index 721ddda..cf963eb 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Smile.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Smile.java @@ -3,10 +3,6 @@ package dev.struchkov.bot.gitlab.context.utils; import lombok.AllArgsConstructor; import lombok.Getter; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.Period; - @AllArgsConstructor public enum Smile { @@ -47,15 +43,6 @@ public enum Smile { @Getter private final String value; - public static Smile statusPr(LocalDateTime updateDate) { - int periodDay = Period.between(updateDate.toLocalDate(), LocalDate.now()).getDays(); - if (periodDay < 5) { - return Smile.valueOf("DAY_" + periodDay); - } else { - return Smile.DAY_5; - } - } - @Override public String toString() { return value; diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/AppSettingServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/AppSettingServiceImpl.java index d9ce143..c6ccff6 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/AppSettingServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/AppSettingServiceImpl.java @@ -5,8 +5,8 @@ import dev.struchkov.bot.gitlab.context.repository.AppSettingRepository; import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.haiti.context.exception.NotFoundException; import lombok.RequiredArgsConstructor; -import org.springframework.context.MessageSource; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.function.Supplier; @@ -25,18 +25,30 @@ public class AppSettingServiceImpl implements AppSettingService { public static final Supplier NOT_FOUND_SETTINGS = notFoundException("Ошибка, невозможно найти настройки приложения, проверьте базу данных."); private final AppSettingRepository appSettingRepository; - private final MessageSource messageSource; - @Override + @Transactional(readOnly = true) public boolean isFirstStart() { return getAppSetting().isFirstStart(); } @Override + @Transactional public void disableFirstStart() { final AppSetting appSetting = getAppSetting(); appSetting.setFirstStart(false); - appSettingRepository.save(appSetting); + } + + @Override + @Transactional(readOnly = true) + public boolean isEnableAllNotify() { + return getAppSetting().isEnableNotify(); + } + + @Override + @Transactional + public void turnOnAllNotify() { + final AppSetting appSetting = getAppSetting(); + appSetting.setEnableNotify(true); } private AppSetting getAppSetting() { diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java index 079a5d6..718b396 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DiscussionServiceImpl.java @@ -300,6 +300,11 @@ public class DiscussionServiceImpl implements DiscussionService { return repository.findAll(); } + @Override + public Set getAllIds() { + return repository.findAllIds(); + } + /** * Уведомляет пользователя, если его никнейм упоминается в комментарии. */ diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java index c255fff..5d2834b 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java @@ -250,6 +250,12 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { log.debug("Конец очистки старых MR"); } + @Override + @Transactional(readOnly = true) + public Set getAllIds() { + return repository.findAllIds(); + } + private void notifyAboutUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { final Long botUserGitlabId = personInformation.getId(); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java index 7c0830a..336ad0b 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java @@ -1,33 +1,31 @@ package dev.struchkov.bot.gitlab.core.service.impl; import dev.struchkov.bot.gitlab.context.domain.notify.Notify; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.bot.gitlab.context.service.MessageSendService; import dev.struchkov.bot.gitlab.context.service.NotifyService; -import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @Service -@RequiredArgsConstructor public class NotifyServiceImpl implements NotifyService { - private boolean enableAllNotify = true; private final MessageSendService messageSendService; + private final AppSettingService settingService; + + public NotifyServiceImpl( + @Lazy MessageSendService messageSendService, + AppSettingService settingService + ) { + this.messageSendService = messageSendService; + this.settingService = settingService; + } @Override public void send(T notify) { - if (enableAllNotify) { + if (settingService.isEnableAllNotify()) { messageSendService.send(notify); } } - @Override - public void enableAllNotify() { - enableAllNotify = true; - } - - @Override - public void disableAllNotify() { - enableAllNotify = false; - } - } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java index ab3e872..3c5d17b 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java @@ -49,7 +49,7 @@ public class PipelineServiceImpl implements PipelineService { @Transactional public Pipeline create(@NonNull Pipeline pipeline) { final Pipeline newPipeline = repository.save(pipeline); - notifyNewPipeline(pipeline, "n/a"); + notifyNewPipeline(pipeline, PipelineStatus.NULL); return newPipeline; } @@ -61,11 +61,12 @@ public class PipelineServiceImpl implements PipelineService { .collect(Collectors.toList()); } - private void notifyNewPipeline(Pipeline pipeline, String oldStatus) { + private void notifyNewPipeline(Pipeline pipeline, PipelineStatus oldStatus) { if (isNeedNotifyNewPipeline(pipeline)) { notifyService.send( PipelineNotify.builder() - .newStatus(pipeline.getStatus().name()) + .projectId(pipeline.getProjectId()) + .newStatus(pipeline.getStatus()) .pipelineId(pipeline.getId()) .refName(pipeline.getRef()) .webUrl(pipeline.getWebUrl()) @@ -83,7 +84,7 @@ public class PipelineServiceImpl implements PipelineService { pipeline.setProjectId(oldPipeline.getProjectId()); if (!oldPipeline.getUpdated().equals(pipeline.getUpdated())) { - notifyNewPipeline(pipeline, oldPipeline.getStatus().name()); + notifyNewPipeline(pipeline, oldPipeline.getStatus()); return repository.save(pipeline); } @@ -131,4 +132,9 @@ public class PipelineServiceImpl implements PipelineService { log.debug("Конец очистки старых пайплайнов"); } + @Override + public Set getAllIds() { + return repository.findAllIds(); + } + } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/ProjectServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/ProjectServiceImpl.java index 2750f10..601d266 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/ProjectServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/ProjectServiceImpl.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -92,6 +93,11 @@ public class ProjectServiceImpl implements ProjectService { return repository.findAllIds(); } + @Override + public Optional getProjectNameById(@NonNull Long projectId) { + return repository.findProjectNameById(projectId); + } + private void notifyAboutNewProject(Project newProject, String authorName) { notifyService.send( NewProjectNotify.builder() diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java index d821542..f929d18 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java @@ -29,7 +29,7 @@ public class GetAllDiscussionForMergeRequestTask extends RecursiveTask compute() { - Thread.sleep(100); + Thread.sleep(200); final List jsons = getDiscussionJson(); if (checkNotEmpty(jsons) && jsons.size() == PAGE_COUNT) { final var newTask = new GetAllDiscussionForMergeRequestTask(discussionsUrl, projectId, mergeRequestTwoId, personalGitlabToken, page + 1); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java index 2f1c99b..41fe93a 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java @@ -30,7 +30,7 @@ public class GetAllMergeRequestForProjectTask extends RecursiveTask compute() { - Thread.sleep(100); + Thread.sleep(200); final List mergeRequestJsons = getMergeRequestJsons(); if (checkNotEmpty(mergeRequestJsons) && mergeRequestJsons.size() == PAGE_COUNT) { final GetAllMergeRequestForProjectTask newTask = new GetAllMergeRequestForProjectTask(projectId, pageNumber + 1, urlMrOpen, gitlabToken); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineShortTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineShortTask.java index 65bb1e3..456e528 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineShortTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineShortTask.java @@ -31,7 +31,7 @@ public class GetPipelineShortTask extends RecursiveTask> @Override @SneakyThrows protected List compute() { - Thread.sleep(100); + Thread.sleep(200); final List jsons = getPipelineJsons(); if (jsons.size() == PAGE_COUNT) { final GetPipelineShortTask newTask = new GetPipelineShortTask(urlPipelines, projectId, pageNumber + 1, lastUpdate, gitlabToken); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java index c7a1241..13ef2b4 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java @@ -25,7 +25,7 @@ public class GetPipelineTask extends RecursiveTask> { @Override @SneakyThrows protected Optional compute() { - Thread.sleep(100); + Thread.sleep(200); return HttpParse.request(MessageFormat.format(urlPipeline, projectId, pipelineId)) .header(ACCEPT) .header(StringUtils.H_PRIVATE_TOKEN, gitlabToken) diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetSingleMergeRequestTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetSingleMergeRequestTask.java index 45bf499..5ff1448 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetSingleMergeRequestTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetSingleMergeRequestTask.java @@ -25,7 +25,7 @@ public class GetSingleMergeRequestTask extends RecursiveTask compute() { - Thread.sleep(100); + Thread.sleep(200); final String mrUrl = MessageFormat.format(urlMr, projectId, mrTwoId); return HttpParse.request(mrUrl) .header(ACCEPT) diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java index f2fa5c5..73d0751 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/DiscussionRepositoryImpl.java @@ -47,4 +47,9 @@ public class DiscussionRepositoryImpl implements DiscussionRepository { return jpaRepository.findAllById(discussionIds); } + @Override + public Set findAllIds() { + return jpaRepository.findAllIds(); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java index 15326c3..55b83c6 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java @@ -65,4 +65,9 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository { jpaRepository.deleteAllByStateIn(states); } + @Override + public Set findAllIds() { + return jpaRepository.findAllIds(); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java index 224972d..4cd145e 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java @@ -48,4 +48,9 @@ public class PipelineRepositoryImpl implements PipelineRepository { jpaRepository.deleteAllByCreatedBefore(date); } + @Override + public Set findAllIds() { + return jpaRepository.findAllIds(); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/ProjectRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/ProjectRepositoryImpl.java index e48418a..8063285 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/ProjectRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/ProjectRepositoryImpl.java @@ -51,4 +51,9 @@ public class ProjectRepositoryImpl implements ProjectRepository { return jpaRepository.findAllIds(); } + @Override + public Optional findProjectNameById(Long projectId) { + return jpaRepository.findProjectNameById(projectId); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java index 20f1b16..e79862d 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DiscussionJpaRepository.java @@ -2,8 +2,10 @@ package dev.struchkov.bot.gitlab.data.jpa; import dev.struchkov.bot.gitlab.context.domain.entity.Discussion; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.List; +import java.util.Set; /** * @author upagge 11.02.2021 @@ -15,4 +17,7 @@ public interface DiscussionJpaRepository extends JpaRepository findAllByMergeRequestId(Long mergeRequestId); + @Query("SELECT d.id FROM Discussion d") + Set findAllIds(); + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java index 9e9e8d3..217e8c0 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java @@ -24,4 +24,7 @@ public interface MergeRequestJpaRepository extends JpaRepositoryImplementation states); + @Query("SELECT mr.id FROM MergeRequest mr") + Set findAllIds(); + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java index f6d6991..f5cf4f0 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java @@ -2,6 +2,7 @@ package dev.struchkov.bot.gitlab.data.jpa; import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; import java.time.LocalDateTime; @@ -17,4 +18,7 @@ public interface PipelineJpaRepository extends JpaRepositoryImplementation findAllIds(); + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/ProjectJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/ProjectJpaRepository.java index eda1c9b..c368c27 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/ProjectJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/ProjectJpaRepository.java @@ -3,7 +3,9 @@ package dev.struchkov.bot.gitlab.data.jpa; import dev.struchkov.bot.gitlab.context.domain.entity.Project; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import java.util.Optional; import java.util.Set; /** @@ -14,4 +16,7 @@ public interface ProjectJpaRepository extends JpaRepository { @Query("SELECT p.id FROM Project p") Set findAllIds(); + @Query("SELECT p.name FROM Project p WHERE p.id = :projectId") + Optional findProjectNameById(@Param("projectId") Long projectId); + } diff --git a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/config/AppConfig.java b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/config/AppConfig.java index f2ae9d8..15f2007 100644 --- a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/config/AppConfig.java +++ b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/config/AppConfig.java @@ -10,13 +10,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import java.util.Arrays; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException; import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; @@ -30,20 +26,15 @@ import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; @EnableScheduling public class AppConfig { - /** - * Отвечает за работу шедулеров в паралельном режиме - */ - @Bean - public TaskScheduler taskScheduler() { - ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); - taskScheduler.setPoolSize(12); - return taskScheduler; - } - - @Bean - public ExecutorService executorService() { - return Executors.newFixedThreadPool(3); - } +// /** +// * Отвечает за работу шедулеров в паралельном режиме +// */ +// @Bean +// public TaskScheduler taskScheduler() { +// ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); +// taskScheduler.setPoolSize(12); +// return taskScheduler; +// } @Bean public ConversionService conversionService(Converter... converters) { diff --git a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java index a1740d9..f02c3da 100644 --- a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java +++ b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java @@ -1,5 +1,6 @@ package dev.struchkov.bot.gitlab.scheduler; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.PipelineService; import dev.struchkov.bot.gitlab.core.service.parser.DiscussionParser; @@ -18,6 +19,8 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class SchedulerService { + private final AppSettingService settingService; + private final PipelineParser pipelineParser; private final MergeRequestParser mergeRequestParser; private final DiscussionParser discussionParser; @@ -28,14 +31,18 @@ public class SchedulerService { @Scheduled(cron = "0 */1 * * * *") public void newMergeRequest() { log.debug("Запуск процесса обновления данных"); - mergeRequestParser.parsingOldMergeRequest(); - mergeRequestParser.parsingNewMergeRequest(); - pipelineParser.scanOldPipeline(); - pipelineParser.scanNewPipeline(); - discussionParser.scanOldDiscussions(); - discussionParser.scanNewDiscussion(); - mergeRequestsService.cleanOld(); - pipelineService.cleanOld(); + if (!settingService.isFirstStart()) { + mergeRequestParser.parsingOldMergeRequest(); + mergeRequestParser.parsingNewMergeRequest(); + pipelineParser.scanOldPipeline(); + pipelineParser.scanNewPipeline(); + discussionParser.scanOldDiscussions(); + discussionParser.scanNewDiscussion(); + mergeRequestsService.cleanOld(); + pipelineService.cleanOld(); + } else { + log.warn("Процесс обновления данных не был выполнен, так как пользователь не выполнил первичную настройку."); + } log.debug("Конец процесса обновления данных"); } diff --git a/gitlab-app/src/main/resources/liquibase/v.2.0.0/2022-12-03-create-tables.xml b/gitlab-app/src/main/resources/liquibase/v.2.0.0/2022-12-03-create-tables.xml index 985a77d..b4b6068 100644 --- a/gitlab-app/src/main/resources/liquibase/v.2.0.0/2022-12-03-create-tables.xml +++ b/gitlab-app/src/main/resources/liquibase/v.2.0.0/2022-12-03-create-tables.xml @@ -8,7 +8,12 @@ - + + + + + + diff --git a/pom.xml b/pom.xml index cf7caba..f463193 100644 --- a/pom.xml +++ b/pom.xml @@ -258,6 +258,44 @@ + + + struchkov-nexus-release + https://nexus.struchkov.dev/repository/maven-releases/ + + true + always + fail + + + false + + + + struchkov-nexus-snapshot + https://nexus.struchkov.dev/repository/maven-snapshots/ + + false + + + true + always + warn + + + + + + + struchkov-nexus-release + https://nexus.struchkov.dev/repository/maven-releases/ + + + struchkov-nexus-snapshot + https://nexus.struchkov.dev/repository/maven-snapshots/ + + + uPagge diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java index 91e7604..4b3a53b 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java @@ -1,17 +1,20 @@ package dev.struchkov.bot.gitlab.telegram.config; import dev.struchkov.bot.gitlab.telegram.service.ReplaceUrlLocalhost; +import dev.struchkov.bot.gitlab.telegram.unit.CommandUnit; import dev.struchkov.bot.gitlab.telegram.unit.MenuConfig; -import dev.struchkov.bot.gitlab.telegram.unit.UnitConfig; +import dev.struchkov.bot.gitlab.telegram.unit.flow.InitSettingFlow; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.context.service.EventHandler; import dev.struchkov.godfather.simple.context.service.PersonSettingService; import dev.struchkov.godfather.simple.context.service.UnitPointerService; import dev.struchkov.godfather.simple.core.provider.StoryLineHandler; import dev.struchkov.godfather.simple.core.service.PersonSettingServiceImpl; +import dev.struchkov.godfather.simple.core.service.StorylineContextMapImpl; import dev.struchkov.godfather.simple.core.service.StorylineMailService; import dev.struchkov.godfather.simple.core.service.StorylineService; import dev.struchkov.godfather.simple.core.service.UnitPointerServiceImpl; +import dev.struchkov.godfather.simple.data.StorylineContext; import dev.struchkov.godfather.simple.data.repository.impl.PersonSettingLocalRepository; import dev.struchkov.godfather.simple.data.repository.impl.StorylineMapRepository; import dev.struchkov.godfather.simple.data.repository.impl.UnitPointLocalRepository; @@ -25,6 +28,7 @@ import dev.struchkov.godfather.telegram.simple.core.MailAutoresponderTelegram; import dev.struchkov.godfather.telegram.simple.core.TelegramConnectBot; import dev.struchkov.godfather.telegram.simple.core.service.SenderMapRepository; import dev.struchkov.godfather.telegram.simple.sender.TelegramSender; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -32,6 +36,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * @author upagge [30.01.2020] @@ -40,6 +46,16 @@ import java.util.List; @EnableScheduling public class TelegramBotConfig { + @Bean("messageExecutorService") + public ExecutorService executorService() { + return Executors.newFixedThreadPool(3); + } + + @Bean + public StorylineContext storylineContext() { + return new StorylineContextMapImpl(); + } + @Bean public UnitPointerService unitPointerService() { return new UnitPointerServiceImpl(new UnitPointLocalRepository()); @@ -55,9 +71,10 @@ public class TelegramBotConfig { UnitPointerService unitPointerService, MenuConfig menuConfig, - UnitConfig unitConfig + InitSettingFlow unitConfig, + CommandUnit commandUnit ) { - final List config = List.of(menuConfig, unitConfig); + final List config = List.of(menuConfig, unitConfig, commandUnit); return new StorylineMailService( unitPointerService, @@ -68,6 +85,7 @@ public class TelegramBotConfig { @Bean public MailAutoresponderTelegram messageAutoresponderTelegram( + @Qualifier("messageExecutorService") ExecutorService executorService, TelegramSending sending, PersonSettingService personSettingService, @@ -76,6 +94,7 @@ public class TelegramBotConfig { final MailAutoresponderTelegram autoresponder = new MailAutoresponderTelegram( sending, personSettingService, mailStorylineService ); + autoresponder.setExecutorService(executorService); return autoresponder; } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/MessageSendTelegramService.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/MessageSendTelegramService.java index b3c7761..6083b07 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/MessageSendTelegramService.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/MessageSendTelegramService.java @@ -3,13 +3,16 @@ package dev.struchkov.bot.gitlab.telegram.service; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.domain.notify.Notify; import dev.struchkov.bot.gitlab.context.service.MessageSendService; +import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator; import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import lombok.NonNull; -import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; /** * Отправляет сообщение в телеграмм. @@ -17,18 +20,36 @@ import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; * @author upagge 17.09.2020 */ @Service -@RequiredArgsConstructor public class MessageSendTelegramService implements MessageSendService { + private final Map generatorMap; private final TelegramSending sending; private final PersonInformation personInformation; + public MessageSendTelegramService( + List generators, + TelegramSending sending, + PersonInformation personInformation + ) { + this.generatorMap = generators.stream().collect(Collectors.toMap(NotifyBoxAnswerGenerator::getNotifyType, n -> n)); + this.sending = sending; + this.personInformation = personInformation; + } + @Override public void send(@NonNull Notify notify) { - final BoxAnswer boxAnswer = boxAnswer(notify.generateMessage()); - boxAnswer.setRecipientIfNull(personInformation.getTelegramId()); - sending.send(boxAnswer); + getGenerator(notify.getType()) + .map(generator -> { + final BoxAnswer answer = generator.generate(notify); + answer.setRecipientIfNull(personInformation.getTelegramId()); + return answer; + }) + .ifPresent(sending::send); + } + + private Optional getGenerator(String notifyType) { + return Optional.ofNullable(generatorMap.get(notifyType)); } } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/StartNotify.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/StartNotify.java index 9de610a..e4f15f9 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/StartNotify.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/StartNotify.java @@ -1,9 +1,10 @@ package dev.struchkov.bot.gitlab.telegram.service; -import dev.struchkov.bot.gitlab.context.domain.notify.SimpleTextNotify; import dev.struchkov.bot.gitlab.context.service.AppSettingService; -import dev.struchkov.bot.gitlab.context.service.NotifyService; import dev.struchkov.bot.gitlab.core.config.properties.AppProperty; +import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -16,20 +17,25 @@ import javax.annotation.PostConstruct; @RequiredArgsConstructor public class StartNotify { - private final NotifyService notifyService; + private final TelegramSending sending; private final AppProperty appProperty; private final AppSettingService settingService; + private final PersonProperty personProperty; @PostConstruct public void sendStartNotification() { if (!settingService.isFirstStart()) { - notifyService.send( - SimpleTextNotify.builder() - .message("Hello. I wish you a productive day :)" + - "\n-- -- -- -- --\n" + - "Version " + appProperty.getVersion() + " | Developer: [uPagge](https://mark.struchkov.dev)") - .build() - ); + final BoxAnswer boxAnswer = BoxAnswer.builder() + .recipientPersonId(personProperty.getTelegramId()) + .message( + new StringBuilder() + .append("Hello. I wish you a productive day :)") + .append("\n-- -- -- -- --\n") + .append("Version ").append(appProperty.getVersion()).append(" | Developer: [uPagge](https://mark.struchkov.dev)") + .toString() + ).build(); + + sending.send(boxAnswer); } } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ConflictPrNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ConflictPrNotifyGenerator.java new file mode 100644 index 0000000..3376bba --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ConflictPrNotifyGenerator.java @@ -0,0 +1,31 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictPrNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class ConflictPrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(ConflictPrNotify notify) { + + final StringBuilder builder = new StringBuilder(Icons.DANGEROUS).append(" *Attention! MergeRequest conflict | ").append(escapeMarkdown(notify.getProjectName())).append("*") + .append(Icons.HR) + .append(link(notify.getTitle(), notify.getUrl())); + + final String notifyMessage = builder.toString(); + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return ConflictPrNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/DiscussionNewNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/DiscussionNewNotifyGenerator.java new file mode 100644 index 0000000..2acf68f --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/DiscussionNewNotifyGenerator.java @@ -0,0 +1,48 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.haiti.utils.Pair; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class DiscussionNewNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(DiscussionNewNotify notify) { + final StringBuilder builder = new StringBuilder(Icons.TASK).append(" [New discussion](").append(notify.getUrl()).append(")") + .append(Icons.HR) + .append(escapeMarkdown(notify.getMrName())) + .append(Icons.HR) + .append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessageTask())); + + final List> notes = notify.getNotes(); + if (checkNotNull(notes)) { + builder.append("\n-- -- -- -- comments -- -- -- --\n") + .append(convertNotes(notes)); + } + + final String notifyMessage = builder.toString(); + return boxAnswer(notifyMessage); + } + + private String convertNotes(List> notes) { + return notes.stream() + .map(note -> "*" + note.getKey() + "*: " + note.getValue()) + .collect(Collectors.joining("\n")); + } + + @Override + public String getNotifyType() { + return DiscussionNewNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewCommentNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewCommentNotifyGenerator.java new file mode 100644 index 0000000..fe8a313 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewCommentNotifyGenerator.java @@ -0,0 +1,41 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify; +import dev.struchkov.bot.gitlab.context.utils.Smile; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(NewCommentNotify notify) { + final StringBuilder builder = new StringBuilder(Smile.COMMENT.getValue()).append(" [New answer in discussion](").append(notify.getUrl()).append(")\n--- --- --- ---"); + + if (checkNotNull(notify.getDiscussionMessage())) { + builder.append("\n-- -- discussion first message -- --\n") + .append("*").append(notify.getDiscussionAuthor()).append("*: ").append(escapeMarkdown(notify.getDiscussionMessage())); + } + + if (checkNotNull(notify.getPreviousMessage())) { + builder.append("\n-- -- -- previous message -- -- --\n") + .append("*").append(notify.getPreviousAuthor()).append("*: ").append(escapeMarkdown(notify.getPreviousMessage())); + } + + builder.append("\n-- -- -- --- new answer --- -- -- --\n") + .append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessage())); + + final String messageNotify = builder.toString(); + return boxAnswer(messageNotify); + } + + @Override + public String getNotifyType() { + return NewCommentNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewPrNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewPrNotifyGenerator.java new file mode 100644 index 0000000..1cdde46 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewPrNotifyGenerator.java @@ -0,0 +1,45 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.NewPrNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import java.util.stream.Collectors; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class NewPrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(NewPrNotify notify) { + final String labelText = notify.getLabels().stream() + .map(label -> "#" + label) + .collect(Collectors.joining(" ")); + + + final StringBuilder builder = new StringBuilder(Icons.FUN).append(" *New merge request for review | ").append(escapeMarkdown(notify.getProjectName())).append("*") + .append(Icons.HR) + .append(link(notify.getType(), notify.getUrl())); + + if (!labelText.isEmpty()) { + builder.append("\n\n").append(labelText); + } + + builder.append(Icons.HR) + .append(Icons.TREE).append(": ").append(notify.getSourceBranch()).append(Icons.ARROW).append(notify.getTargetBranch()).append("\n") + .append(Icons.AUTHOR).append(": ").append(notify.getAuthor()); + + final String notifyMessage = builder.toString(); + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return NewPrNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewProjectNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewProjectNotifyGenerator.java new file mode 100644 index 0000000..daeadb2 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewProjectNotifyGenerator.java @@ -0,0 +1,45 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.NewProjectNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.haiti.utils.Strings; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; + +@Component +public class NewProjectNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(NewProjectNotify notify) { + final Optional optDescription = Optional.ofNullable(notify.getProjectDescription()) + .map(Strings::escapeMarkdown); + + final StringBuilder builder = new StringBuilder(Icons.FUN).append("*New project*") + .append(Icons.HR) + .append(link(notify.getProjectName(), notify.getProjectUrl())) + .append(Icons.HR); + + if (optDescription.isPresent() || !Strings.EMPTY.equals(optDescription.get())) { + final String description = optDescription.get(); + builder.append(description) + .append(Icons.HR); + } + + builder.append(Icons.AUTHOR).append(": ").append(notify.getAuthorName()); + + final String notifyMessage = builder.toString(); + + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return NewProjectNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NotifyBoxAnswerGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NotifyBoxAnswerGenerator.java new file mode 100644 index 0000000..ac06aa9 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NotifyBoxAnswerGenerator.java @@ -0,0 +1,18 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.godfather.main.domain.BoxAnswer; + +import java.util.List; +import java.util.stream.Collectors; + +public interface NotifyBoxAnswerGenerator { + + BoxAnswer generate(T notify); + + default List generate(List notify) { + return notify.stream().map(this::generate).collect(Collectors.toList()); + } + + String getNotifyType(); + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java new file mode 100644 index 0000000..5bd1f94 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java @@ -0,0 +1,60 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.pipeline.PipelineNotify; +import dev.struchkov.bot.gitlab.context.service.ProjectService; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.haiti.utils.Strings; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; +import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; +import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; +import static dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton.urlButton; + +@Service +@RequiredArgsConstructor +public class PipelineNotifyGenerator implements NotifyBoxAnswerGenerator { + + private final ProjectService projectService; + + @Override + public BoxAnswer generate(PipelineNotify notify) { + final Optional optProjectName = projectService.getProjectNameById(notify.getProjectId()) + .map(Strings::escapeMarkdown); + + final StringBuilder builder = new StringBuilder(Icons.BUILD).append(" *Pipeline ").append(notify.getPipelineId()).append("*"); + + if (optProjectName.isPresent()) { + final String projectName = optProjectName.get(); + builder.append(" | ").append(projectName); + } + + final String notifyMessage = builder + .append(Icons.HR) + .append(Icons.TREE).append(": ").append(notify.getRefName()) + .append(Icons.HR) + .append(notify.getOldStatus().getIcon()).append(" ").append(notify.getOldStatus()).append(Icons.ARROW).append(notify.getNewStatus().getIcon()).append(" ").append(notify.getNewStatus()) + .toString(); + + return boxAnswer( + notifyMessage, + inlineKeyBoard( + simpleLine( + simpleButton(Icons.VIEW, "DELETE_MESSAGE"), + urlButton(Icons.LINK, notify.getWebUrl()) + ) + ) + ); + } + + @Override + public String getNotifyType() { + return PipelineNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/StatusPrNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/StatusPrNotifyGenerator.java new file mode 100644 index 0000000..05a2c01 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/StatusPrNotifyGenerator.java @@ -0,0 +1,33 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.StatusPrNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; + +@Component +public class StatusPrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(StatusPrNotify notify) { + + final StringBuilder builder = new StringBuilder(Icons.PEN).append(" *MergeRequest status changed | ").append(notify.getProjectName()).append("*") + .append(Icons.HR) + .append(link(notify.getTitle(), notify.getUrl())) + .append(Icons.HR) + .append(notify.getOldStatus().name()).append(Icons.ARROW).append(notify.getNewStatus().name()); + + final String notifyMessage = builder.toString(); + + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return StatusPrNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/TaskCloseNotifyGenerate.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/TaskCloseNotifyGenerate.java new file mode 100644 index 0000000..be6d42a --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/TaskCloseNotifyGenerate.java @@ -0,0 +1,31 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class TaskCloseNotifyGenerate implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(TaskCloseNotify notify) { + + final StringBuilder builder = new StringBuilder(Icons.TASK).append(" *Closed ").append(link("task", notify.getUrl())) + .append(Icons.HR) + .append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessageTask())); + + final String notifyMessage = builder.toString(); + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return TaskCloseNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/UpdatePrNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/UpdatePrNotifyGenerator.java new file mode 100644 index 0000000..eec36b3 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/UpdatePrNotifyGenerator.java @@ -0,0 +1,44 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.UpdatePrNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.bot.gitlab.context.utils.Smile; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import org.springframework.stereotype.Component; + +import static dev.struchkov.bot.gitlab.context.utils.Icons.link; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +public class UpdatePrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(UpdatePrNotify notify) { + + final StringBuilder builder = new StringBuilder(Icons.UPDATE).append(" *MergeRequest update | ").append(escapeMarkdown(notify.getProjectName())).append("*") + .append(Smile.HR.getValue()) + .append(link(notify.getTitle(), notify.getUrl())); + + if (notify.getAllTasks() > 0) { + builder.append(Smile.HR.getValue()) + .append("All tasks: ").append(notify.getAllResolvedTasks()).append("/").append(notify.getAllTasks()); + + if (notify.getPersonTasks() > 0) { + builder.append("\nYour tasks: ").append(notify.getPersonResolvedTasks()).append("/").append(notify.getPersonTasks()); + } + } + + builder.append(Icons.HR) + .append(Icons.AUTHOR).append(": ").append(notify.getAuthor()); + + final String notifyMessage = builder.toString(); + return boxAnswer(notifyMessage); + } + + @Override + public String getNotifyType() { + return UpdatePrNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java new file mode 100644 index 0000000..cab416b --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java @@ -0,0 +1,110 @@ +package dev.struchkov.bot.gitlab.telegram.unit; + +import dev.struchkov.bot.gitlab.context.domain.PersonInformation; +import dev.struchkov.bot.gitlab.context.domain.entity.Note; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.bot.gitlab.context.service.DiscussionService; +import dev.struchkov.bot.gitlab.context.service.NoteService; +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.godfather.main.domain.annotation.Unit; +import dev.struchkov.godfather.main.domain.content.Attachment; +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.core.unit.AnswerText; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import dev.struchkov.haiti.utils.Checker; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.text.MessageFormat; +import java.util.List; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ANSWER_NOTE; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; + +@Component +@RequiredArgsConstructor +public class CommandUnit { + + private static final Pattern NOTE_LINK = Pattern.compile("#note_\\d+$"); + + private final PersonInformation personInformation; + private final AppSettingService settingService; + private final NoteService noteService; + private final DiscussionService discussionService; + private final TelegramSending telegramSending; + + @Unit(value = ANSWER_NOTE, main = true) + public AnswerText answerNote() { + return AnswerText.builder() + .triggerCheck( + mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + final boolean isFirstStart = settingService.isFirstStart(); + if (!isFirstStart) { + final List forwardMails = mail.getForwardMail(); + if (Checker.checkNotNull(forwardMails) && forwardMails.size() == 1) { + final Mail forwardMail = forwardMails.get(0); + return Attachments.findFirstLink(forwardMail.getAttachments()).isPresent(); + } + } + } + return false; + } + ) + .answer( + mail -> { + final List attachments = mail.getForwardMail().get(0).getAttachments(); + for (Attachment attachment : attachments) { + if (TelegramAttachmentType.LINK.name().equals(attachment.getType())) { + final String url = ((LinkAttachment) attachment).getUrl(); + final Matcher matcher = NOTE_LINK.matcher(url); + if (matcher.find()) { + final String noteText = url.substring(matcher.start(), matcher.end()); + final Long noteId = Long.valueOf(noteText.replaceAll("#note_", "")); + final Note note = noteService.getByIdOrThrow(noteId); + final String discussionId = note.getDiscussion().getId(); + discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), mail.getText())); + return BoxAnswer.builder().build(); + } + } + } + return boxAnswer("Error"); + } + ) + .build(); + } + + @Unit(value = "DELETE_MESSAGE", main = true) + public AnswerText deleteMessage() { + return AnswerText.builder() + .triggerCheck(mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + final boolean isFirstStart = settingService.isFirstStart(); + if (!isFirstStart) { + final Optional optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments()); + if (optButtonClick.isPresent()) { + final ButtonClickAttachment buttonClick = optButtonClick.get(); + final String rawData = buttonClick.getRawCallBackData(); + return rawData.equals("DELETE_MESSAGE"); + } + } + } + return false; + }) + .answer(mail -> { + final ButtonClickAttachment clickButton = Attachments.findFirstButtonClick(mail.getAttachments()).orElseThrow(); + telegramSending.deleteMessage(mail.getPersonId(), clickButton.getMessageId()); + }) + .build(); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/MenuConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/MenuConfig.java index d32df5e..4dfc3e3 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/MenuConfig.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/MenuConfig.java @@ -2,15 +2,22 @@ package dev.struchkov.bot.gitlab.telegram.unit; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.NoteService; +import dev.struchkov.bot.gitlab.context.utils.Smile; import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.service.parser.ProjectParser; +import dev.struchkov.bot.gitlab.telegram.utils.Keys; import dev.struchkov.bot.gitlab.telegram.utils.UnitName; +import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.annotation.Unit; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.core.unit.AnswerText; import dev.struchkov.godfather.simple.core.unit.MainUnit; +import dev.struchkov.godfather.simple.data.StorylineContext; +import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import dev.struchkov.haiti.utils.Checker; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -19,6 +26,7 @@ import java.text.MessageFormat; import java.util.List; import java.util.stream.Collectors; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ACCESS_ERROR; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ADD_NEW_PROJECT; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.GENERAL_MENU; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.GET_ASSIGNEE_MERGE_REQUEST; @@ -26,6 +34,7 @@ import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.GET_TASKS; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.SETTINGS; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.TEXT_ADD_NEW_PROJECT; import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.godfather.main.domain.BoxAnswer.replaceBoxAnswer; import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; @@ -40,13 +49,32 @@ import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.cl @RequiredArgsConstructor public class MenuConfig { + private final StorylineContext context; + private final TelegramSending sending; + private final ProjectParser projectParser; private final GitlabProperty gitlabProperty; private final PersonInformation personInformation; private final NoteService noteService; private final MergeRequestsService mergeRequestsService; + private final AppSettingService settingService; - @Unit(GENERAL_MENU) + @Unit(value = ACCESS_ERROR, main = true) + public AnswerText accessError() { + return AnswerText.builder() + .triggerCheck(mail -> !personInformation.getTelegramId().equals(mail.getPersonId())) + .answer(message -> { + final String messageText = new StringBuilder("\uD83D\uDEA8 *Попытка несанкционированного доступа к боту*") + .append(Smile.HR.getValue()) + .append("\uD83E\uDDB9\u200D♂️: ").append(message.getPersonId()).append("\n") + .append("\uD83D\uDCAC: ").append(message.getText()) + .toString(); + return BoxAnswer.builder().recipientPersonId(personInformation.getTelegramId()).message(messageText).build(); + }) + .build(); + } + + @Unit(value = GENERAL_MENU, main = true) public AnswerText menu( @Unit(SETTINGS) MainUnit settings, @Unit(TEXT_ADD_NEW_PROJECT) MainUnit textAddNewProject, @@ -54,17 +82,34 @@ public class MenuConfig { @Unit(GET_ASSIGNEE_MERGE_REQUEST) MainUnit getAssigneeMergeRequest ) { return AnswerText.builder() - .answer(boxAnswer( - "This is the bot menu, select a new item", - inlineKeyBoard( - simpleLine(simpleButton("Add project", TEXT_ADD_NEW_PROJECT)), - simpleLine( - simpleButton("My tasks", GET_TASKS), - simpleButton("Merge Request", GET_ASSIGNEE_MERGE_REQUEST) - ), - simpleLine(simpleButton("Settings", SETTINGS)) - ) - ) + .priority(5) + .triggerCheck(mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + final boolean firstStart = settingService.isFirstStart(); + return !firstStart; + } + return false; + }) + .answer(mail -> { + final String messageText = "This is the bot menu, select a new item"; + final InlineKeyBoard generalMenuKeyBoard = inlineKeyBoard( + simpleLine(simpleButton("Add project", TEXT_ADD_NEW_PROJECT)), + simpleLine( + simpleButton("My tasks", GET_TASKS), + simpleButton("Merge Request", GET_ASSIGNEE_MERGE_REQUEST) + ), + simpleLine(simpleButton("Settings", SETTINGS)) + ); + final String personId = mail.getPersonId(); + final var initSettingFinish = context.removeKey(personId, Keys.INIT_SETTING_FINISH); + if (initSettingFinish.isPresent()) { + context.removeKey(personId, Keys.INIT_SETTING_PRIVATE_PROJECT_MESSAGE_ID).ifPresent(messageId -> sending.deleteMessage(personId, messageId)); + context.removeKey(personId, Keys.INIT_SETTING_PUBLIC_PROJECT_MESSAGE_ID).ifPresent(messageId -> sending.deleteMessage(personId, messageId)); + return replaceBoxAnswer(messageText, generalMenuKeyBoard); + } + return boxAnswer(messageText, generalMenuKeyBoard); + } ) .next(settings) .next(textAddNewProject) diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/UnitConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/UnitConfig.java deleted file mode 100644 index a89b4be..0000000 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/UnitConfig.java +++ /dev/null @@ -1,265 +0,0 @@ -package dev.struchkov.bot.gitlab.telegram.unit; - -import dev.struchkov.bot.gitlab.context.domain.PersonInformation; -import dev.struchkov.bot.gitlab.context.domain.entity.Note; -import dev.struchkov.bot.gitlab.context.service.AppSettingService; -import dev.struchkov.bot.gitlab.context.service.DiscussionService; -import dev.struchkov.bot.gitlab.context.service.NoteService; -import dev.struchkov.bot.gitlab.context.service.NotifyService; -import dev.struchkov.bot.gitlab.context.utils.Smile; -import dev.struchkov.bot.gitlab.core.service.parser.ProjectParser; -import dev.struchkov.godfather.main.core.unit.UnitActiveType; -import dev.struchkov.godfather.main.domain.BoxAnswer; -import dev.struchkov.godfather.main.domain.annotation.Unit; -import dev.struchkov.godfather.main.domain.content.Attachment; -import dev.struchkov.godfather.main.domain.content.Mail; -import dev.struchkov.godfather.simple.core.unit.AnswerCheck; -import dev.struchkov.godfather.simple.core.unit.AnswerText; -import dev.struchkov.godfather.simple.core.unit.MainUnit; -import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; -import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; -import dev.struchkov.godfather.telegram.main.core.util.Attachments; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -import java.text.MessageFormat; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ACCESS_ERROR; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ANSWER_NOTE; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.AUTHORIZATION; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_FIRST_START; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_MENU_OR_ANSWER; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSER_PRIVATE_PROJECT; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSE_OWNER_PROJECT; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.END_SETTING; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.GENERAL_MENU; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.PARSER_PRIVATE_PROJECT; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.PARSE_OWNER_PROJECT; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.TEXT_PARSER_PRIVATE_PROJECT; -import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.TEXT_PARSE_OWNER_PROJECT; -import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; -import static dev.struchkov.godfather.main.domain.BoxAnswer.replaceBoxAnswer; -import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; -import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; -import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [30.01.2020] - */ -@Component -@RequiredArgsConstructor -public class UnitConfig { - - private static final Pattern NOTE_LINK = Pattern.compile("#note_\\d+$"); - - private final PersonInformation personInformation; - - private final AppSettingService settingService; - private final NoteService noteService; - private final DiscussionService discussionService; - private final NotifyService notifyService; - - private final ProjectParser projectParser; - - @Unit(value = AUTHORIZATION, main = true) - public AnswerCheck auth( - @Unit(CHECK_FIRST_START) MainUnit checkFirstStart, - @Unit(ACCESS_ERROR) MainUnit accessError - ) { - return AnswerCheck.builder() - .check(mail -> personInformation.getTelegramId().equals(mail.getPersonId())) - .unitTrue(checkFirstStart) - .unitFalse(accessError) - .build(); - } - - @Unit(value = ACCESS_ERROR) - public AnswerText accessError() { - return AnswerText.builder() - .answer(message -> { - final String messageText = new StringBuilder("\uD83D\uDEA8 *Попытка несанкционированного доступа к боту*") - .append(Smile.HR.getValue()) - .append("\uD83E\uDDB9\u200D♂️: ").append(message.getPersonId()).append("\n") - .append("\uD83D\uDCAC: ").append(message.getText()) - .toString(); - return BoxAnswer.builder().recipientPersonId(personInformation.getTelegramId()).message(messageText).build(); - }) - .build(); - } - - @Unit(value = CHECK_FIRST_START) - public AnswerCheck checkFirstStart( - @Unit(TEXT_PARSER_PRIVATE_PROJECT) MainUnit textParserPrivateProject, - @Unit(CHECK_MENU_OR_ANSWER) MainUnit checkMenuOrAnswer - ) { - return AnswerCheck.builder() - .check(message -> settingService.isFirstStart()) - .unitFalse(checkMenuOrAnswer) - .unitTrue(textParserPrivateProject) - .build(); - } - - @Unit(value = CHECK_MENU_OR_ANSWER) - public AnswerCheck checkMenuOrAnswer( - @Unit(GENERAL_MENU) MainUnit menu, - @Unit(ANSWER_NOTE) MainUnit answerNote - ) { - return AnswerCheck.builder() - .check( - mail -> { - final List forwardMails = mail.getForwardMail(); - if (forwardMails != null && forwardMails.size() == 1) { - final Mail forwardMail = forwardMails.get(0); - return Attachments.findFirstLink(forwardMail.getAttachments()).isPresent(); - } - return false; - } - ) - .unitTrue(answerNote) - .unitFalse(menu) - .build(); - } - - @Unit(ANSWER_NOTE) - public AnswerText answerNote() { - return AnswerText.builder() - .answer( - mail -> { - final List attachments = mail.getForwardMail().get(0).getAttachments(); - for (Attachment attachment : attachments) { - if (TelegramAttachmentType.LINK.name().equals(attachment.getType())) { - final String url = ((LinkAttachment) attachment).getUrl(); - final Matcher matcher = NOTE_LINK.matcher(url); - if (matcher.find()) { - final String noteText = url.substring(matcher.start(), matcher.end()); - final Long noteId = Long.valueOf(noteText.replaceAll("#note_", "")); - final Note note = noteService.getByIdOrThrow(noteId); - final String discussionId = note.getDiscussion().getId(); - discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), mail.getText())); - return BoxAnswer.builder().build(); - } - } - } - return boxAnswer("Error"); - } - ) - .build(); - } - - @Unit(TEXT_PARSER_PRIVATE_PROJECT) - public AnswerText textParserPrivateProject( - @Unit(CHECK_PARSER_PRIVATE_PROJECT) MainUnit checkParserPrivateProject - ) { - return AnswerText.builder() - .answer(() -> boxAnswer( - "Start tracking private projects?", - inlineKeyBoard( - simpleLine( - simpleButton("Yes", "YES"), - simpleButton("No", "NO") - ) - ) - ) - ) - .activeType(UnitActiveType.AFTER) - .next(checkParserPrivateProject) - .build(); - } - - @Unit(CHECK_PARSER_PRIVATE_PROJECT) - public AnswerCheck checkParserPrivateProject( - @Unit(PARSER_PRIVATE_PROJECT) AnswerText parserPrivateProject, - @Unit(TEXT_PARSE_OWNER_PROJECT) MainUnit textParseOwnerProject - ) { - return AnswerCheck.builder() - .check(mail -> "YES".equalsIgnoreCase(mail.getText())) - .intermediateAnswerIfTrue(replaceBoxAnswer("Scanning of private projects has begun. Wait...")) - .unitTrue(parserPrivateProject) - .unitFalse(textParseOwnerProject) - .build(); - } - - @Unit(PARSER_PRIVATE_PROJECT) - public AnswerText parserPrivateProject( - @Unit(TEXT_PARSE_OWNER_PROJECT) MainUnit textParseOwnerProject - ) { - return AnswerText.builder() - .answer(() -> { - notifyService.disableAllNotify(); - projectParser.parseAllPrivateProject(); - return replaceBoxAnswer("Projects have been successfully added to tracking"); - }) - .next(textParseOwnerProject) - .build(); - } - - @Unit(TEXT_PARSE_OWNER_PROJECT) - public AnswerText textParseOwnerProject( - @Unit(CHECK_PARSE_OWNER_PROJECT) MainUnit checkParseOwnerProject - ) { - return AnswerText.builder() - .answer( - boxAnswer( - "Start tracking public projects that you own?", - inlineKeyBoard( - simpleLine( - simpleButton("Yes", "YES"), - simpleButton("No", "NO") - ) - ) - ) - ) - .activeType(UnitActiveType.AFTER) - .next(checkParseOwnerProject) - .build(); - } - - @Unit(CHECK_PARSE_OWNER_PROJECT) - public AnswerCheck checkParseOwnerProject( - @Unit(PARSE_OWNER_PROJECT) MainUnit parseOwnerProject, - @Unit(END_SETTING) MainUnit endSetting - ) { - return AnswerCheck.builder() - .check(message -> "YES".equalsIgnoreCase(message.getText())) - .intermediateAnswerIfTrue(replaceBoxAnswer("Scanning of public projects has begun. Wait...")) - .unitTrue(parseOwnerProject) - .unitFalse(endSetting) - .build(); - } - - @Unit(PARSE_OWNER_PROJECT) - public AnswerText parseOwnerProject( - @Unit(END_SETTING) MainUnit endSetting - ) { - return AnswerText.builder() - .answer(() -> { - projectParser.parseAllProjectOwner(); - return replaceBoxAnswer("Projects have been successfully added to tracking"); - }) - .next(endSetting) - .build(); - } - - @Unit(END_SETTING) - public AnswerText endSetting() { - return AnswerText.builder() - .answer( - () -> { - settingService.disableFirstStart(); - notifyService.enableAllNotify(); - return replaceBoxAnswer(""" - Configuration completed successfully - Developer: [uPagge](https://mark.struchkov.dev) - """); - } - ) - .activeType(UnitActiveType.AFTER) - .build(); - } - -} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/flow/InitSettingFlow.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/flow/InitSettingFlow.java new file mode 100644 index 0000000..a1b39de --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/flow/InitSettingFlow.java @@ -0,0 +1,345 @@ +package dev.struchkov.bot.gitlab.telegram.unit.flow; + +import dev.struchkov.bot.gitlab.context.domain.PersonInformation; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.bot.gitlab.context.service.DiscussionService; +import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; +import dev.struchkov.bot.gitlab.context.service.PipelineService; +import dev.struchkov.bot.gitlab.context.service.ProjectService; +import dev.struchkov.bot.gitlab.core.service.parser.DiscussionParser; +import dev.struchkov.bot.gitlab.core.service.parser.MergeRequestParser; +import dev.struchkov.bot.gitlab.core.service.parser.PipelineParser; +import dev.struchkov.bot.gitlab.core.service.parser.ProjectParser; +import dev.struchkov.bot.gitlab.telegram.utils.Keys; +import dev.struchkov.godfather.main.domain.annotation.Unit; +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.core.unit.AnswerText; +import dev.struchkov.godfather.simple.core.unit.MainUnit; +import dev.struchkov.godfather.simple.data.StorylineContext; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSER_PRIVATE_PROJECT_NO; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSER_PRIVATE_PROJECT_YES; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSE_OWNER_PROJECT_NO; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.CHECK_PARSE_OWNER_PROJECT_YES; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.END_SETTING; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.FIRST_START; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.TEXT_PARSER_PRIVATE_PROJECT; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.TEXT_PARSE_OWNER_PROJECT; +import static dev.struchkov.godfather.main.core.unit.UnitActiveType.AFTER; +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.godfather.main.domain.BoxAnswer.replaceBoxAnswer; +import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; +import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; +import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; +import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.clickButtonRaw; +import static java.text.MessageFormat.format; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [30.01.2020] + */ +@Component +@RequiredArgsConstructor +public class InitSettingFlow { + + private final TelegramSending sending; + private final StorylineContext context; + + private final PersonInformation personInformation; + + private final AppSettingService settingService; + + private final ProjectParser projectParser; + private final MergeRequestParser mergeRequestParser; + private final PipelineParser pipelineParser; + private final DiscussionParser discussionParser; + + private final ProjectService projectService; + private final MergeRequestsService mergeRequestsService; + private final PipelineService pipelineService; + private final DiscussionService discussionService; + + @Unit(value = FIRST_START, main = true) + public AnswerText firstStart( + @Unit(value = TEXT_PARSER_PRIVATE_PROJECT) MainUnit textParserPrivateProject + ) { + return AnswerText.builder() + .triggerCheck(mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + return settingService.isFirstStart(); + } + return false; + }) + .answer( + boxAnswer( + """ + Hello! + This bot will help you keep your finger on the pulse of all your GitLab projects. + Press start to start initial setup 👇 + """, + inlineKeyBoard(simpleButton("start", TEXT_PARSER_PRIVATE_PROJECT)) + ) + ) + .next(textParserPrivateProject) + .build(); + } + + @Unit(value = TEXT_PARSER_PRIVATE_PROJECT) + public AnswerText textParserPrivateProject( + @Unit(CHECK_PARSER_PRIVATE_PROJECT_YES) MainUnit checkParserPrivateProject + ) { + return AnswerText.builder() + .answer(() -> replaceBoxAnswer( + "Start tracking private projects?", + inlineKeyBoard( + simpleLine( + simpleButton("Yes", "YES"), + simpleButton("No", "NO") + ) + ) + ) + ) + .next(checkParserPrivateProject) + .build(); + } + + @Unit(CHECK_PARSER_PRIVATE_PROJECT_YES) + public AnswerText checkParserPrivateProjectYes( + @Unit(TEXT_PARSE_OWNER_PROJECT) MainUnit textParseOwnerProject + ) { + final String step1 = """ + -- -- -- -- -- + 🔘 Scanning of private projects has begun. + ⌛ Wait... + -- -- -- -- -- + """; + + final String step2 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🔘 Scanning merge requests in found projects. + ⌛ Wait... + -- -- -- -- -- + """; + final String step3 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🔘 Scanning pipelines in found merge requests. + ⌛ Wait... + -- -- -- -- -- + """; + + final String step4 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🟢 Pipelines have been successfully added. Found: {2} + 🔘 Scanning threads in merge requests. + ⌛ Wait... + -- -- -- -- -- + """; + + final String finalAnswer = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🟢 Pipelines have been successfully added. Found: {2} + 🟢 Threads have been successfully added. Found: {3} + -- -- -- -- -- + """; + + return AnswerText.builder() + .triggerCheck(clickButtonRaw("YES")) + .answer(mail -> { + final String personId = mail.getPersonId(); + final Integer messageId = Attachments.findFirstButtonClick(mail.getAttachments()) + .map(ButtonClickAttachment::getMessageId) + .orElseThrow(); + sending.replaceMessage(personId, messageId, boxAnswer(step1)); + + projectParser.parseAllPrivateProject(); + final int projectCount = projectService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step2, projectCount))); + + mergeRequestParser.parsingNewMergeRequest(); + final int mrCount = mergeRequestsService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step3, projectCount, mrCount))); + + pipelineParser.scanNewPipeline(); + final int pipelineCount = pipelineService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step4, projectCount, mrCount, pipelineCount))); + + discussionParser.scanNewDiscussion(); + final int discussionCount = discussionService.getAllIds().size(); + + context.save(mail.getPersonId(), Keys.INIT_SETTING_PRIVATE_PROJECT_MESSAGE_ID, messageId); + return replaceBoxAnswer(format(finalAnswer, pipelineCount, mrCount, pipelineCount, discussionCount)); + }) + .next(textParseOwnerProject) + .build(); + } + + @Unit(CHECK_PARSER_PRIVATE_PROJECT_NO) + public AnswerText checkParserPrivateProjectNo( + @Unit(TEXT_PARSE_OWNER_PROJECT) MainUnit textParseOwnerProject + ) { + return AnswerText.builder() + .triggerPhrase("NO") + .answer(mail -> { + final Integer messageId = Attachments.findFirstButtonClick(mail.getAttachments()) + .map(ButtonClickAttachment::getMessageId) + .orElseThrow(); + + context.save(mail.getPersonId(), Keys.INIT_SETTING_PRIVATE_PROJECT_MESSAGE_ID, messageId); + replaceBoxAnswer("Okay, I won't scan private projects."); + }) + .next(textParseOwnerProject) + .build(); + } + + @Unit(TEXT_PARSE_OWNER_PROJECT) + public AnswerText textParseOwnerProject( + @Unit(CHECK_PARSE_OWNER_PROJECT_YES) MainUnit checkParseOwnerProjectYes, + @Unit(CHECK_PARSE_OWNER_PROJECT_NO) MainUnit checkParseOwnerProjectNo + ) { + return AnswerText.builder() + .answer( + boxAnswer( + "Start tracking public projects that you own?", + inlineKeyBoard( + simpleLine( + simpleButton("Yes", "YES"), + simpleButton("No", "NO") + ) + ) + ) + ) + .activeType(AFTER) + .next(checkParseOwnerProjectYes) + .next(checkParseOwnerProjectNo) + .build(); + } + + @Unit(CHECK_PARSE_OWNER_PROJECT_YES) + public AnswerText checkParseOwnerProjectYes( + @Unit(END_SETTING) MainUnit endSetting + ) { + final String step1 = """ + -- -- -- -- -- + 🔘 Scanning of public projects has begun. + ⌛ Wait... + -- -- -- -- -- + """; + + final String step2 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🔘 Scanning merge requests in found projects. + ⌛ Wait... + -- -- -- -- -- + """; + final String step3 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🔘 Scanning pipelines in found merge requests. + ⌛ Wait... + -- -- -- -- -- + """; + + final String step4 = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🟢 Pipelines have been successfully added. Found: {2} + 🔘 Scanning threads in merge requests. + ⌛ Wait... + -- -- -- -- -- + """; + + final String finalAnswer = """ + -- -- -- -- -- + 🟢 Projects have been successfully added to tracking. Found: {0} + 🟢 Merge requests have been successfully added. Found: {1} + 🟢 Pipelines have been successfully added. Found: {2} + 🟢 Threads have been successfully added. Found: {3} + -- -- -- -- -- + """; + + return AnswerText.builder() + .triggerCheck(clickButtonRaw("YES")) + .answer(mail -> { + final String personId = mail.getPersonId(); + final Integer messageId = Attachments.findFirstButtonClick(mail.getAttachments()) + .map(ButtonClickAttachment::getMessageId) + .orElseThrow(); + sending.replaceMessage(personId, messageId, boxAnswer(step1)); + + projectParser.parseAllProjectOwner(); + final int projectCount = projectService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step2, projectCount))); + + mergeRequestParser.parsingNewMergeRequest(); + final int mrCount = mergeRequestsService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step3, projectCount, mrCount))); + + pipelineParser.scanNewPipeline(); + final int pipelineCount = pipelineService.getAllIds().size(); + sending.replaceMessage(personId, messageId, boxAnswer(format(step4, projectCount, mrCount, pipelineCount))); + + discussionParser.scanNewDiscussion(); + final int discussionCount = discussionService.getAllIds().size(); + + context.save(mail.getPersonId(), Keys.INIT_SETTING_PUBLIC_PROJECT_MESSAGE_ID, messageId); + return replaceBoxAnswer(format(finalAnswer, pipelineCount, mrCount, pipelineCount, discussionCount)); + }) + .next(endSetting) + .build(); + } + + @Unit(CHECK_PARSE_OWNER_PROJECT_NO) + public AnswerText checkParseOwnerProjectNo( + @Unit(END_SETTING) MainUnit endSetting + ) { + return AnswerText.builder() + .triggerCheck(clickButtonRaw("NO")) + .answer(mail -> { + final Integer messageId = Attachments.findFirstButtonClick(mail.getAttachments()) + .map(ButtonClickAttachment::getMessageId) + .orElseThrow(); + context.save(mail.getPersonId(), Keys.INIT_SETTING_PUBLIC_PROJECT_MESSAGE_ID, messageId); + return replaceBoxAnswer("Okay, I won't scan public projects."); + }) + .next(endSetting) + .build(); + } + + @Unit(END_SETTING) + public AnswerText endSetting() { + return AnswerText.builder() + .activeType(AFTER) + .answer( + mail -> { + context.save(mail.getPersonId(), Keys.INIT_SETTING_FINISH, Boolean.TRUE); + settingService.isEnableAllNotify(); + settingService.disableFirstStart(); + return boxAnswer(""" + Configuration completed successfully + Developer: [uPagge](https://mark.struchkov.dev) + """, + inlineKeyBoard(simpleButton("open menu", "INIT_SETTING_OPEN_MENU")) + ); + } + ) + .build(); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Keys.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Keys.java new file mode 100644 index 0000000..42fb112 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Keys.java @@ -0,0 +1,13 @@ +package dev.struchkov.bot.gitlab.telegram.utils; + +import dev.struchkov.godfather.main.domain.ContextKey; +import lombok.experimental.UtilityClass; + +@UtilityClass +public class Keys { + + public static final ContextKey INIT_SETTING_FINISH = ContextKey.of("INIT_SETTING_FINISH", Boolean.class); + public static final ContextKey INIT_SETTING_PRIVATE_PROJECT_MESSAGE_ID = ContextKey.of("INIT_SETTING_PRIVATE_PROJECT_MESSAGE_ID", Integer.class); + public static final ContextKey INIT_SETTING_PUBLIC_PROJECT_MESSAGE_ID = ContextKey.of("INIT_SETTING_PUBLIC_PROJECT_MESSAGE_ID", Integer.class); + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java index 797e1b8..4b815c2 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java @@ -10,18 +10,17 @@ public final class UnitName { public static final String SETTINGS = "settings"; public static final String GET_TASKS = "getTasks"; public static final String GET_ASSIGNEE_MERGE_REQUEST = "getAssigneeMergeRequest"; - public static final String CHECK_FIRST_START = "checkFirstStart"; - public static final String CHECK_MENU_OR_ANSWER = "checkMenuOrAnswer"; + public static final String FIRST_START = "checkFirstStart"; public static final String ANSWER_NOTE = "answerNote"; public static final String TEXT_PARSER_PRIVATE_PROJECT = "textParserPrivateProject"; - public static final String CHECK_PARSER_PRIVATE_PROJECT = "checkParserPrivateProject"; - public static final String PARSER_PRIVATE_PROJECT = "parserPrivateProject"; + public static final String CHECK_PARSER_PRIVATE_PROJECT_YES = "checkParserPrivateProject"; public static final String TEXT_PARSE_OWNER_PROJECT = "textParseOwnerProject"; - public static final String CHECK_PARSE_OWNER_PROJECT = "checkParseOwnerProject"; - public static final String PARSE_OWNER_PROJECT = "parseOwnerProject"; + public static final String CHECK_PARSE_OWNER_PROJECT_YES = "checkParseOwnerProject"; public static final String END_SETTING = "endSetting"; - public static final String AUTHORIZATION = "AUTHORIZATION"; public static final String ACCESS_ERROR = "ACCESS_ERROR"; + public static final String CHECK_PARSER_PRIVATE_PROJECT_NO = "CHECK_PARSER_PRIVATE_PROJECT_NO"; + public static final String CHECK_PARSE_OWNER_PROJECT_NO = "CHECK_PARSE_OWNER_PROJECT_NO"; + public static final String REPLACE_GENERAL_MENU = "REPLACE_GENERAL_MENU"; private UnitName() { utilityClass();