From 232624ec8ddcc002d322c90b981bcbe0d67a2b49 Mon Sep 17 00:00:00 2001 From: upagge Date: Sun, 11 Oct 2020 10:45:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AD=D0=BA=D1=80=D0=B0=D0=BD=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bot/vcs/core/domain/notify/Notify.java | 19 +++++++++++++++++++ .../core/domain/notify/SimpleTextNotify.java | 2 +- .../notify/comment/AnswerCommentNotify.java | 10 +++++++--- .../domain/notify/comment/CommentNotify.java | 2 +- .../notify/pullrequest/NewPrNotify.java | 6 +++--- .../notify/pullrequest/StatusPrNotify.java | 6 +++--- .../domain/notify/task/TaskCloseNotify.java | 2 +- .../domain/notify/task/TaskNewNotify.java | 2 +- .../core/service/impl/CommentServiceImpl.java | 12 ++++++------ .../core/service/impl/TaskServiceImpl.java | 10 ++++++---- .../core/domain/TeamcityBuildNotify.java | 2 +- 11 files changed, 49 insertions(+), 24 deletions(-) diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/Notify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/Notify.java index 4ac50eb..179bbca 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/Notify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/Notify.java @@ -1,15 +1,22 @@ package org.sadtech.bot.vcs.core.domain.notify; import lombok.Getter; +import lombok.NonNull; import lombok.Setter; import org.sadtech.bot.vcs.core.domain.EntityType; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; @Getter @Setter public abstract class Notify { + public static final Set FORBIDDEN_SYMBOLS = Stream.of( + '\\', '+', '`', '[', ']', '\"', '~', '*', '#', '=', '-', '_', '>', '<', '!' + ).collect(Collectors.toSet()); + protected EntityType entityType; protected Set recipients; @@ -20,4 +27,16 @@ public abstract class Notify { public abstract String generateMessage(); + public static String escapeMarkdown(@NonNull String s) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (FORBIDDEN_SYMBOLS.contains(c)) { + sb.append('\\'); + } + sb.append(c); + } + return sb.toString(); + } + } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/SimpleTextNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/SimpleTextNotify.java index a226645..9f75980 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/SimpleTextNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/SimpleTextNotify.java @@ -24,7 +24,7 @@ public class SimpleTextNotify extends Notify { @Override public String generateMessage() { - return message; + return escapeMarkdown(message); } } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/AnswerCommentNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/AnswerCommentNotify.java index 391427c..b8aee09 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/AnswerCommentNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/AnswerCommentNotify.java @@ -41,9 +41,13 @@ public class AnswerCommentNotify extends Notify { .collect(Collectors.joining("\n\n")); return MessageFormat.format( "{0} *Новые ответы на ваш комментарий* | [ПР]({1}){2}" + - "{3}{4}" + - "{5}", - Smile.BELL, url, Smile.HR, youMessage.substring(0, Math.min(youMessage.length(), 180)), Smile.HR, answerText + "{3}{2}" + + "{4}", + Smile.BELL, + url, + Smile.HR, + escapeMarkdown(youMessage.substring(0, Math.min(youMessage.length(), 180))), + escapeMarkdown(answerText) ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/CommentNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/CommentNotify.java index 42aded1..29f2e36 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/CommentNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/comment/CommentNotify.java @@ -36,7 +36,7 @@ public class CommentNotify extends Notify { return MessageFormat.format( "{0} *Новое упоминание* | [ПР]({1}){2}" + "{3}: {4}", - Smile.BELL, url, Smile.HR, authorName, message.replaceAll("@[\\w]+", "") + Smile.BELL, url, Smile.HR, authorName, escapeMarkdown(message.replaceAll("@[\\w]+", "")) ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/NewPrNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/NewPrNotify.java index 1f40cb3..b2ae276 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/NewPrNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/NewPrNotify.java @@ -32,9 +32,9 @@ public class NewPrNotify extends PrNotify { return MessageFormat.format( "{0} *Новый Pull Request*{1}" + "[{2}]({3})" + - "{4}{5}{6}: {7}\n\n", - Smile.FUN, Smile.HR, title, url, Smile.HR, - (description != null && !"".equals(description)) ? description + Smile.HR : "", + "{1}{4}{5}: {6}\n\n", + Smile.FUN, Smile.HR, title, url, + (description != null && !"".equals(description)) ? escapeMarkdown(description) + Smile.HR : "", Smile.AUTHOR, author ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/StatusPrNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/StatusPrNotify.java index 18fabf0..687bc10 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/StatusPrNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/pullrequest/StatusPrNotify.java @@ -32,9 +32,9 @@ public class StatusPrNotify extends PrNotify { public String generateMessage() { return MessageFormat.format( "{0} *Изменился статус вашего ПР*{1}" + - "[{2}]({3}){4}" + - "{5} -> {6}\n\n", - Smile.PEN, Smile.HR, title, url, Smile.HR, oldStatus.name(), newStatus.name() + "[{2}]({3}){1}" + + "{4} -> {5}\n\n", + Smile.PEN, Smile.HR, title, url, oldStatus.name(), newStatus.name() ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskCloseNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskCloseNotify.java index ec63528..78d315a 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskCloseNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskCloseNotify.java @@ -28,7 +28,7 @@ public class TaskCloseNotify extends TaskNotify { return MessageFormat.format( "{0} *Задача выполнена* | [ПР]({1}){2}" + "{3}: {4}", - Smile.TASK, url, Smile.HR, authorName, messageTask + Smile.TASK, url, Smile.HR, authorName, escapeMarkdown(messageTask) ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskNewNotify.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskNewNotify.java index e765b68..618c26a 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskNewNotify.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/domain/notify/task/TaskNewNotify.java @@ -30,7 +30,7 @@ public class TaskNewNotify extends TaskNotify { return MessageFormat.format( "{0} *Назначена новая задача* | [ПР]({1}){2}" + "{3}: {4}", - Smile.TASK, url, Smile.HR, authorName, messageTask + Smile.TASK, url, Smile.HR, authorName, escapeMarkdown(messageTask) ); } diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/CommentServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/CommentServiceImpl.java index 01a00e2..3ec0dcd 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/CommentServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/CommentServiceImpl.java @@ -134,12 +134,12 @@ public class CommentServiceImpl extends AbstractSimpleManagerService newAnswerIds = newComment.getAnswers(); if (!oldAnswerIds.equals(newAnswerIds)) { final Set existsNewAnswersIds = commentRepository.existsById(newAnswerIds); - if (!existsNewAnswersIds.isEmpty()) { - final List newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream() - .filter(comment -> !oldAnswerIds.contains(comment.getId())) - .collect(Collectors.toList()); - oldComment.getAnswers().clear(); - oldComment.setAnswers(existsNewAnswersIds); + final List newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream() + .filter(comment -> !oldAnswerIds.contains(comment.getId())) + .collect(Collectors.toList()); + oldComment.getAnswers().clear(); + oldComment.setAnswers(existsNewAnswersIds); + if (!newAnswers.isEmpty()) { notifyService.send( AnswerCommentNotify.builder() .recipients(Collections.singleton(newComment.getAuthor())) diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/TaskServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/TaskServiceImpl.java index f09a4f5..04d900f 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/TaskServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/TaskServiceImpl.java @@ -126,10 +126,12 @@ public class TaskServiceImpl extends AbstractSimpleManagerService im final Set newAnswerIds = task.getAnswers(); if (!oldAnswerIds.equals(newAnswerIds)) { final Set existsNewAnswersIds = commentService.existsById(newAnswerIds); - if (!existsNewAnswersIds.isEmpty()) { - final List newAnswers = commentService.getAllById(existsNewAnswersIds).stream() - .filter(comment -> !oldAnswerIds.contains(comment.getId())) - .collect(Collectors.toList()); + final List newAnswers = commentService.getAllById(existsNewAnswersIds).stream() + .filter(comment -> !oldAnswerIds.contains(comment.getId())) + .collect(Collectors.toList()); + oldTask.getAnswers().clear(); + oldTask.setAnswers(existsNewAnswersIds); + if (!newAnswers.isEmpty()) { oldTask.getAnswers().clear(); oldTask.setAnswers(existsNewAnswersIds); notifyService.send( diff --git a/teamcity/teamcity-core/src/main/java/org/sadtech/bot/vcs/teamcity/core/domain/TeamcityBuildNotify.java b/teamcity/teamcity-core/src/main/java/org/sadtech/bot/vcs/teamcity/core/domain/TeamcityBuildNotify.java index b8507f3..926720a 100644 --- a/teamcity/teamcity-core/src/main/java/org/sadtech/bot/vcs/teamcity/core/domain/TeamcityBuildNotify.java +++ b/teamcity/teamcity-core/src/main/java/org/sadtech/bot/vcs/teamcity/core/domain/TeamcityBuildNotify.java @@ -36,7 +36,7 @@ public class TeamcityBuildNotify extends Notify { Smile.BUILD, buildShort.getId(), buildShort.getNumber(), - buildShort.getProjectId(), + escapeMarkdown(buildShort.getProjectId()), Smile.HR, buildShort.getBuildTypeId(), buildShort.getUrl(),