From 99c9cd5ee295df37d4380abda6eec2705570caea Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 30 Jan 2023 12:33:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=83=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB?= =?UTF-8?q?=D0=B8=D0=BA=D1=82=D0=B0=D1=85=20MR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mergerequest/ConflictResolveMrNotify.java | 30 +++++++++++ .../repository/MergeRequestRepository.java | 4 +- .../context/service/MergeRequestsService.java | 4 +- .../bot/gitlab/context/utils/Icons.java | 1 + .../impl/MergeRequestsServiceImpl.java | 38 ++++++++++++-- .../core/service/parser/ProjectParser.java | 9 ++-- .../data/impl/MergeRequestRepositoryImpl.java | 9 +++- .../data/jpa/MergeRequestJpaRepository.java | 8 ++- .../notify/ConflictPrNotifyGenerator.java | 27 ++++++++-- .../ConflictResolvePrNotifyGenerator.java | 50 +++++++++++++++++++ .../unit/command/DisableNotifyMrUnit.java | 2 +- .../unit/command/EnableProjectNotify.java | 3 ++ 12 files changed, 166 insertions(+), 19 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictResolveMrNotify.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ConflictResolvePrNotifyGenerator.java diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictResolveMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictResolveMrNotify.java new file mode 100644 index 0000000..548ae5c --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictResolveMrNotify.java @@ -0,0 +1,30 @@ +package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class ConflictResolveMrNotify extends MrNotify { + + public static final String TYPE = "ConflictResolveMrNotify"; + + private final String sourceBranch; + + @Builder + private ConflictResolveMrNotify( + Long mrId, + String name, + String url, + String projectKey, + String sourceBranch + ) { + super(mrId, projectKey, name, url); + this.sourceBranch = sourceBranch; + } + + @Override + public String getType() { + return TYPE; + } + +} 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 d82fd7c..bf7d90f 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 @@ -28,6 +28,8 @@ public interface MergeRequestRepository { Set findAllIds(); - void disableNotify(Long mrId); + void notification(boolean enable, Long mrId); + + void notificationByProjectId(boolean enable, Set projectIds); } 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 2569a06..66de696 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 @@ -38,6 +38,8 @@ public interface MergeRequestsService { Set getAllIds(); - void disableNotify(@NonNull Long mrId); + void notification(boolean enable, @NonNull Long mrId); + + void notificationByProjectId(boolean enable, @NonNull Set projectIds); } 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 index 1cd7116..b037072 100644 --- 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 @@ -16,6 +16,7 @@ public class Icons { public static final String TASK = "\uD83D\uDCBC"; public static final String ARROW = " ➜ "; public static final String DANGEROUS = "⚠️"; + public static final String GREEN_CIRCLE = "\uD83D\uDFE2"; public static final String PEN = "✏️"; public static final String ASSIGNEE = "\uD83C\uDFA9"; public static final String BUILD = "⚙️"; 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 8a94a5d..16d1add 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 @@ -12,6 +12,7 @@ import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Project; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictMrNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictResolveMrNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.NewMrForAssignee; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.NewMrForReview; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.StatusMrNotify; @@ -179,7 +180,8 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { if (isChangedMr) { notifyAboutStatus(oldMergeRequest, mergeRequest, project); - notifyAboutConflict(oldMergeRequest, mergeRequest, project); + notifyAboutNewConflict(oldMergeRequest, mergeRequest, project); + notifyAboutResolveConflict(oldMergeRequest, mergeRequest, project); notifyAboutUpdate(oldMergeRequest, mergeRequest, project); } @@ -195,14 +197,15 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } //TODO [05.12.2022|uPagge]: Добавить уведомление, если происходит удаление + private void notifyAssignee(AssigneeChanged assigneeChanged, MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { switch (assigneeChanged) { case BECOME -> sendNotifyNewAssignee(mergeRequest, project.getName(), Optional.ofNullable(oldMergeRequest.getAssignee()).map(Person::getName).orElse(null)); } } - //TODO [05.12.2022|uPagge]: Добавить уведомление, если происходит удаление ревьювера + private void notifyReviewer(ReviewerChanged reviewerChanged, MergeRequest mergeRequest, Project project) { switch (reviewerChanged) { case BECOME -> sendNotifyNewMrReview(mergeRequest, project.getName()); @@ -268,8 +271,14 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { @Override @Transactional - public void disableNotify(@NonNull Long mrId) { - repository.disableNotify(mrId); + public void notification(boolean enable, @NonNull Long mrId) { + repository.notification(enable, mrId); + } + + @Override + @Transactional + public void notificationByProjectId(boolean enable, @NonNull Set projectIds) { + repository.notificationByProjectId(enable, projectIds); } private void notifyAboutUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { @@ -317,7 +326,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } } - protected void notifyAboutConflict(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { + protected void notifyAboutNewConflict(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { final Long gitlabUserId = personInformation.getId(); if ( !oldMergeRequest.isConflict() // У старого MR не было конфликта @@ -336,6 +345,25 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } } + private void notifyAboutResolveConflict(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { + final Long gitlabUserId = personInformation.getId(); + if ( + oldMergeRequest.isConflict() // У старого MR был конфликт + && !mergeRequest.isConflict() // А у нового нет + && gitlabUserId.equals(oldMergeRequest.getAuthor().getId()) // и MR создан пользователем бота + ) { + notifyService.send( + ConflictResolveMrNotify.builder() + .mrId(oldMergeRequest.getId()) + .sourceBranch(oldMergeRequest.getSourceBranch()) + .name(mergeRequest.getTitle()) + .url(mergeRequest.getWebUrl()) + .projectKey(project.getName()) + .build() + ); + } + } + protected void notifyAboutStatus(MergeRequest oldMergeRequest, MergeRequest newMergeRequest, Project project) { final MergeRequestState oldStatus = oldMergeRequest.getState(); final MergeRequestState newStatus = newMergeRequest.getState(); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/ProjectParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/ProjectParser.java index 086017c..8eb9acf 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/ProjectParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/ProjectParser.java @@ -3,6 +3,7 @@ package dev.struchkov.bot.gitlab.core.service.parser; import dev.struchkov.bot.gitlab.context.domain.ExistContainer; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Project; +import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.PersonService; import dev.struchkov.bot.gitlab.context.service.ProjectService; import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; @@ -39,8 +40,8 @@ public class ProjectParser { public static final String OWNER = "&owned=true"; public static final String PRIVATE = "&visibility=private"; - public static final String PUBLIC_OWNER = "&visibility=public&owned=true"; + private final MergeRequestsService mergeRequestsService; private final ProjectService projectService; private final PersonService personService; @@ -99,8 +100,10 @@ public class ProjectParser { final Project newProject = conversionService.convert(projectJson, Project.class); projectService.create(newProject); } else { - projectService.notification(true, singleton(projectJson.getId())); - projectService.processing(true, singleton(projectJson.getId())); + final Set projectId = singleton(projectJson.getId()); + projectService.notification(true, projectId); + projectService.processing(true, projectId); + mergeRequestsService.notificationByProjectId(true, projectId); } } 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 b0ffc72..645cb30 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 @@ -71,8 +71,13 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository { } @Override - public void disableNotify(Long mrId) { - jpaRepository.disableNotify(mrId); + public void notification(boolean enable, Long mrId) { + jpaRepository.disableNotify(enable, mrId); + } + + @Override + public void notificationByProjectId(boolean enable, Set projectIds) { + jpaRepository.notificationByProjectId(enable, projectIds); } } 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 ea89ab2..c085488 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 @@ -29,7 +29,11 @@ public interface MergeRequestJpaRepository extends JpaRepositoryImplementation findAllIds(); @Modifying - @Query("UPDATE MergeRequest mr SET mr.notification = false WHERE mr.id = :mrId") - void disableNotify(@Param("mrId") Long mrId); + @Query("UPDATE MergeRequest mr SET mr.notification = :enable WHERE mr.id = :mrId") + void disableNotify(@Param("enable") boolean enable, @Param("mrId") Long mrId); + + @Modifying + @Query("UPDATE MergeRequest mr SET mr.notification = :enable WHERE mr.projectId in :projectIds") + void notificationByProjectId(@Param("enable") boolean enable, @Param("projectIds") Set projectIds); } 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 index 43653e9..98043dc 100644 --- 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 @@ -5,8 +5,15 @@ 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.bot.gitlab.telegram.utils.Const.BUTTON_ARG_CONFIRMATION; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_DISABLE_NOTIFY_MR_ID; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_VALUE_FALSE; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; 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; import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; @Component @@ -15,12 +22,24 @@ public class ConflictPrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(ConflictResolveMrNotify notify) { + + final StringBuilder builder = new StringBuilder(Icons.GREEN_CIRCLE).append(" *Merge request conflict resolved!*") + .append(Icons.HR) + .append(escapeMarkdown(notify.getTitle())) + .append(Icons.HR) + .append(Icons.PROJECT).append(": ").append(escapeMarkdown(notify.getProjectName())).append("\n") + .append(Icons.TREE).append(": ").append(escapeMarkdown(notify.getSourceBranch())); + + final String notifyMessage = builder.toString(); + return boxAnswer( + notifyMessage, + inlineKeyBoard( + simpleLine( + simpleButton(Icons.VIEW, DELETE_MESSAGE), + urlButton(Icons.LINK, notify.getUrl()), + simpleButton(Icons.DISABLE_NOTIFY, "[" + BUTTON_ARG_DISABLE_NOTIFY_MR_ID + ":" + notify.getMrId() + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_FALSE + "]") + ) + ) + ); + } + + @Override + public String getNotifyType() { + return ConflictResolveMrNotify.TYPE; + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java index 05ef5c2..2ac9d58 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java @@ -79,7 +79,7 @@ public class DisableNotifyMrUnit { .orElseThrow(); if (confirmation) { - mergeRequestsService.disableNotify(mrId); + mergeRequestsService.notification(false, mrId); scheduledExecutorService.schedule(() -> telegramSending.deleteMessage(mail.getPersonId(), clickButton.getMessageId()), 5, TimeUnit.SECONDS); return replaceBoxAnswer(SUCCESSFULLY_DISABLED); } else { diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/EnableProjectNotify.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/EnableProjectNotify.java index 811c301..2db429b 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/EnableProjectNotify.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/EnableProjectNotify.java @@ -2,6 +2,7 @@ package dev.struchkov.bot.gitlab.telegram.unit.command; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.ProjectService; import dev.struchkov.bot.gitlab.context.utils.Icons; import dev.struchkov.bot.gitlab.telegram.utils.UnitName; @@ -28,6 +29,7 @@ import static dev.struchkov.godfather.main.domain.BoxAnswer.replaceBoxAnswer; public class EnableProjectNotify { private final ProjectService projectService; + private final MergeRequestsService mergeRequestsService; private final AppSettingService settingService; private final PersonInformation personInformation; @@ -59,6 +61,7 @@ public class EnableProjectNotify { final Set setProjectId = Set.of(projectId); projectService.processing(true, setProjectId); projectService.notification(true, setProjectId); + mergeRequestsService.notificationByProjectId(true, setProjectId); return replaceBoxAnswer(mail.getId(), Icons.GOOD + " you will now receive notifications!"); } )