From f8a6c36eea867571a51771ed24e6db62fb053083 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 7 Dec 2022 12:12:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B9=20?= =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 34 ++++++++- .../gitlab/context/domain/MessageSend.java | 3 +- .../context/domain/entity/Discussion.java | 10 +++ .../notify/comment/AnswerCommentNotify.java | 1 - .../domain/notify/comment/CommentNotify.java | 31 -------- .../notify/comment/NewCommentNotify.java | 43 +++++++++++ .../notify/task/DiscussionNewNotify.java | 59 ++++++++++++++ .../domain/notify/task/TaskNewNotify.java | 34 --------- .../bot/gitlab/context/utils/Smile.java | 4 +- .../service/impl/DiscussionServiceImpl.java | 76 +++++++++++++------ .../impl/MergeRequestsServiceImpl.java | 1 + gitlab-app/src/main/resources/application.yml | 2 +- pom.xml | 2 +- .../gitlab/telegram/service/StartNotify.java | 2 +- 14 files changed, 207 insertions(+), 95 deletions(-) delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/CommentNotify.java create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java delete mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskNewNotify.java diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index eeb5118..3e77a12 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,37 @@ stages: - build - deploy -build: +build-develop: + image: maven:3.8.6-eclipse-temurin-17 + stage: build + variables: + MAVEN_OPTS: "-Dmaven.repo.local=./.m2/repository" + only: + - develop + except: + - tags + script: + - 'mvn -U clean package' + artifacts: + paths: + - gitlab-app/target/gitlab-notification.jar + +docker-build-develop: + image: upagge/docker-buildx:latest + stage: deploy + only: + - develop + except: + - tags + services: + - docker:dind + before_script: + - echo "$CI_REGISTRY_PASSWORD" | docker login $CI_REGISTRY --username $CI_REGISTRY_USER --password-stdin + script: + - docker buildx create --use + - docker buildx build --push --platform linux/amd64,linux/arm64/v8 -t "$CI_REGISTRY_IMAGE:develop" . + +build-release: image: maven:3.8.6-eclipse-temurin-17 stage: build variables: @@ -17,7 +47,7 @@ build: paths: - gitlab-app/target/gitlab-notification.jar -docker-build: +docker-build-release: image: upagge/docker-buildx:latest stage: deploy only: diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/MessageSend.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/MessageSend.java index c7e9898..bb9319d 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/MessageSend.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/MessageSend.java @@ -12,9 +12,10 @@ import lombok.Setter; @Builder @NoArgsConstructor @AllArgsConstructor -@EqualsAndHashCode(of = "id") +@EqualsAndHashCode(onlyExplicitlyIncluded = true) public class MessageSend { + @EqualsAndHashCode.Include private Long id; private Long telegramId; private String message; 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 4ca9352..5eda83a 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 @@ -15,6 +15,7 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import java.util.List; +import java.util.Optional; /** * @author upagge 11.02.2021 @@ -65,4 +66,13 @@ public class Discussion { return this.notes.get(0); } + public Optional getPrevLastNote() { + final int size = notes.size(); + if (size > 2) { + return Optional.of(notes.get(size - 2)); + } + return Optional.empty(); + } + + } 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 index 951e988..df275a0 100644 --- 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 @@ -2,7 +2,6 @@ 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.service.AppSettingService; import dev.struchkov.bot.gitlab.context.utils.Smile; import dev.struchkov.haiti.utils.Strings; import lombok.Builder; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/CommentNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/CommentNotify.java deleted file mode 100644 index 1f22e22..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/CommentNotify.java +++ /dev/null @@ -1,31 +0,0 @@ -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 java.text.MessageFormat; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; - -public record CommentNotify( - String url, - String authorName, - String message -) implements Notify { - - @Builder - public CommentNotify { - } - - @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *New mention* | [MR]({1}){2}*{3}*: {4}", - Smile.COMMENT.getValue(), url, Smile.HR.getValue(), authorName, escapeMarkdown(message) - ); - } - -} - - 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 new file mode 100644 index 0000000..6385b1f --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/comment/NewCommentNotify.java @@ -0,0 +1,43 @@ +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 static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@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(); + } + +} + + 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 new file mode 100644 index 0000000..69c0877 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/DiscussionNewNotify.java @@ -0,0 +1,59 @@ +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 + */ +@Getter +public class DiscussionNewNotify extends TaskNotify { + + private final String mrName; + private final List> notes; + + @Builder + public DiscussionNewNotify( + String mrName, + String authorName, + String url, + String discussionMessage, + @Singular List> notes + ) { + super(authorName, url, discussionMessage); + this.mrName = mrName; + this.notes = notes; + } + + @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")); + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskNewNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskNewNotify.java deleted file mode 100644 index 8066f57..0000000 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/task/TaskNewNotify.java +++ /dev/null @@ -1,34 +0,0 @@ -package dev.struchkov.bot.gitlab.context.domain.notify.task; - -import dev.struchkov.bot.gitlab.context.utils.Smile; -import lombok.Builder; -import lombok.Getter; - -import java.text.MessageFormat; - -import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; - -/** - * @author upagge 10.09.2020 - */ -@Getter -public class TaskNewNotify extends TaskNotify { - - @Builder - protected TaskNewNotify( - String authorName, - String url, - String messageTask - ) { - super(authorName, url, messageTask); - } - - @Override - public String generateMessage() { - return MessageFormat.format( - "{0} *New [task]({1}) assigned{2}*{3}*: {4}", - Smile.TASK.getValue(), url, Smile.HR.getValue(), authorName, escapeMarkdown(messageTask) - ); - } - -} 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 f29556b..721ddda 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 @@ -33,7 +33,9 @@ public enum Smile { DANGEROUS("⚠️"), COMMENT("\uD83D\uDCAC"), ARROW("➜"), - HR("\n -- -- -- -- --\n"), + SHORT_HR("\n-- -- --\n"), + HR("\n-- -- -- -- --\n"), + HR2("\n-- -- -- -- -- -- -- -- -- --\n"), FAILURE("❌"), SUCCESS("✅"), BUILD("♻️"), 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 e8241c7..46277c5 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 @@ -6,15 +6,16 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Discussion; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import dev.struchkov.bot.gitlab.context.domain.entity.Note; import dev.struchkov.bot.gitlab.context.domain.entity.Person; -import dev.struchkov.bot.gitlab.context.domain.notify.comment.CommentNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify; import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify; -import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskNewNotify; import dev.struchkov.bot.gitlab.context.repository.DiscussionRepository; import dev.struchkov.bot.gitlab.context.service.DiscussionService; import dev.struchkov.bot.gitlab.context.service.NotifyService; import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty; import dev.struchkov.bot.gitlab.core.utils.StringUtils; +import dev.struchkov.haiti.utils.Pair; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -33,6 +34,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -67,8 +69,11 @@ public class DiscussionServiceImpl implements DiscussionService { public Discussion create(@NonNull Discussion discussion) { final List notes = discussion.getNotes(); - notes.forEach(this::notificationPersonal); - notes.forEach(note -> notifyNewNote(note, discussion)); + if (isNeedNotifyNewNote(discussion)) { + notifyNewDiscussion(discussion); + } else { + notes.forEach(this::notificationPersonal); + } final boolean resolved = discussion.getNotes().stream() .allMatch(note -> note.isResolvable() && note.getResolved()); @@ -81,24 +86,37 @@ public class DiscussionServiceImpl implements DiscussionService { /** *

Уведомляет пользователя, если появился новый комментарий

*/ - private void notifyNewNote(Note note, Discussion discussion) { - if (isNeedNotifyNewNote(note, discussion)) { - notifyService.send( - TaskNewNotify.builder() - .authorName(note.getAuthor().getName()) - .messageTask(note.getBody()) - .url(note.getWebUrl()) - .build() - ); + private void notifyNewDiscussion(Discussion discussion) { + final Note firstNote = discussion.getFirstNote(); + final List notes = discussion.getNotes(); + + + final MergeRequest mergeRequest = discussion.getMergeRequest(); + final DiscussionNewNotify.DiscussionNewNotifyBuilder notifyBuilder = DiscussionNewNotify.builder() + .mrName(mergeRequest.getTitle()) + .authorName(firstNote.getAuthor().getName()) + .discussionMessage(firstNote.getBody()) + .url(firstNote.getWebUrl()); + + if (notes.size() > 1) { + for (int i = 1; i < notes.size(); i++) { + final Note note = notes.get(i); + notifyBuilder.note( + new Pair<>(note.getAuthor().getName(), note.getBody()) + ); + } } + + notifyService.send(notifyBuilder.build()); } - private boolean isNeedNotifyNewNote(Note note, Discussion discussion) { + private boolean isNeedNotifyNewNote(Discussion discussion) { + final Note firstNote = discussion.getFirstNote(); final Long gitlabUserId = personInformation.getId(); - return note.isResolvable() // Тип комментария требует решения (Задачи) - && gitlabUserId.equals(discussion.getResponsible().getId()) // Создатель дискуссии пользователь приложения - && !gitlabUserId.equals(note.getAuthor().getId()) // Создатель комментария не пользователь системы - && FALSE.equals(note.getResolved()); // Комментарий не отмечен как решенный + return firstNote.isResolvable() // Тип комментария требует решения (Задачи) + && gitlabUserId.equals(discussion.getResponsible().getId()) // Ответственный за дискуссию пользователь + && !gitlabUserId.equals(firstNote.getAuthor().getId()) // Создатель комментария не пользователь системы + && FALSE.equals(firstNote.getResolved()); // Комментарий не отмечен как решенный } @Override @@ -160,7 +178,7 @@ public class DiscussionServiceImpl implements DiscussionService { } else { if (userParticipatedInDiscussion) { - notifyNewAnswer(newNote); + notifyNewAnswer(discussion, newNote); } else { notificationPersonal(newNote); } @@ -169,11 +187,25 @@ public class DiscussionServiceImpl implements DiscussionService { } - private void notifyNewAnswer(Note note) { + private void notifyNewAnswer(Discussion discussion, Note note) { if (!personInformation.getId().equals(note.getAuthor().getId())) { + final Note firstNote = discussion.getFirstNote(); + final Optional prevLastNote = discussion.getPrevLastNote(); + + + final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder(); + + if (prevLastNote.isPresent()) { + final Note prevNote = prevLastNote.get(); + notifyBuilder.previousMessage(prevNote.getBody()); + notifyBuilder.previousAuthor(prevNote.getAuthor().getName()); + } + notifyService.send( - CommentNotify.builder() + notifyBuilder .url(note.getWebUrl()) + .discussionMessage(firstNote.getBody()) + .discussionAuthor(firstNote.getAuthor().getName()) .message(note.getBody()) .authorName(note.getAuthor().getName()) .build() @@ -287,7 +319,7 @@ public class DiscussionServiceImpl implements DiscussionService { } if (recipientsLogins.contains(personInformation.getUsername())) { notifyService.send( - CommentNotify.builder() + NewCommentNotify.builder() .authorName(note.getAuthor().getName()) .message(note.getBody()) .url(note.getWebUrl()) 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 0fb8cf5..72c1747 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 @@ -241,6 +241,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } @Override + @Transactional public void deleteAllById(@NonNull Set mergeRequestIds) { repository.deleteByIds(mergeRequestIds); } diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index 45657eb..4e6527a 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: liquibase: change-log: classpath:liquibase/changelog.xml jpa: - show-sql: true + show-sql: false hibernate: ddl-auto: none database-platform: org.hibernate.dialect.PostgreSQLDialect diff --git a/pom.xml b/pom.xml index cc958f8..61dfeee 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ UTF-8 UTF-8 - 0.0.38 + 0.0.39 2.2 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 0173756..9de610a 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 @@ -25,7 +25,7 @@ public class StartNotify { if (!settingService.isFirstStart()) { notifyService.send( SimpleTextNotify.builder() - .message("Привет. Желаю продуктивного дня :)" + + .message("Hello. I wish you a productive day :)" + "\n-- -- -- -- --\n" + "Version " + appProperty.getVersion() + " | Developer: [uPagge](https://mark.struchkov.dev)") .build()