From e844942973d67e961d954a45b53b5f08774d41a9 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 26 Aug 2024 20:21:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B0=D0=BF=D1=80=D1=83=D0=B2=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/changed/ApprovalChanged.java | 51 ++++++++++++ .../domain/{ => changed}/AssigneeChanged.java | 2 +- .../domain/{ => changed}/ReviewerChanged.java | 2 +- .../domain/event/UpdateMergeRequestEvent.java | 8 +- .../mergerequest/ApprovalChangedMrNotify.java | 41 +++++++++ .../bot/gitlab/context/utils/Icons.java | 1 + .../core/handler/MergeRequestHandler.java | 76 ++++++++++++----- .../gitlab/core/parser/DiscussionParser.java | 4 +- .../core/parser/MergeRequestParser.java | 8 +- .../service/MergeRequestsServiceImpl.java | 10 +-- gitlab-app/src/main/resources/application.yml | 2 +- .../ApprovalChangedMrNotifyGenerator.java | 83 +++++++++++++++++++ 12 files changed, 253 insertions(+), 35 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ApprovalChanged.java rename bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/{ => changed}/AssigneeChanged.java (96%) rename bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/{ => changed}/ReviewerChanged.java (96%) create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ApprovalChangedMrNotify.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ApprovalChangedMrNotifyGenerator.java diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ApprovalChanged.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ApprovalChanged.java new file mode 100644 index 0000000..7fb6344 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ApprovalChanged.java @@ -0,0 +1,51 @@ +package dev.struchkov.bot.gitlab.context.domain.changed; + +import dev.struchkov.bot.gitlab.context.domain.entity.Person; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class ApprovalChanged { + + private Set newApproval; + private Set dontApprove; + + public static Optional approvalChanged(List oldApproval, List newApproval) { + // Если списки одинаковы, возвращаем пустой Optional + if (Objects.equals(oldApproval, newApproval)) { + return Optional.empty(); + } + + // Преобразуем списки в множества для удобства вычислений + Set oldApprovalSet = new HashSet<>(oldApproval); + Set newApprovalSet = new HashSet<>(newApproval); + + // Вычисляем новые одобрения: те, кто есть в новом списке, но нет в старом + final Set newApprovalSetOnly = new HashSet<>(newApprovalSet); + newApprovalSetOnly.removeAll(oldApprovalSet); + + // Вычисляем тех, кто больше не одобряет: те, кто есть в старом списке, но нет в новом + final Set dontApproveSet = new HashSet<>(oldApprovalSet); + dontApproveSet.removeAll(newApprovalSet); + + if (!newApprovalSetOnly.isEmpty() || !dontApproveSet.isEmpty()) { + return Optional.of(new ApprovalChanged(newApprovalSetOnly, dontApproveSet)); + } + return Optional.empty(); + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/AssigneeChanged.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/AssigneeChanged.java similarity index 96% rename from bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/AssigneeChanged.java rename to bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/AssigneeChanged.java index 32bce74..d0c9a96 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/AssigneeChanged.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/AssigneeChanged.java @@ -1,4 +1,4 @@ -package dev.struchkov.bot.gitlab.context.domain; +package dev.struchkov.bot.gitlab.context.domain.changed; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import lombok.Getter; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/ReviewerChanged.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ReviewerChanged.java similarity index 96% rename from bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/ReviewerChanged.java rename to bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ReviewerChanged.java index 3980ff8..4735271 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/ReviewerChanged.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/changed/ReviewerChanged.java @@ -1,4 +1,4 @@ -package dev.struchkov.bot.gitlab.context.domain; +package dev.struchkov.bot.gitlab.context.domain.changed; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import lombok.Getter; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/event/UpdateMergeRequestEvent.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/event/UpdateMergeRequestEvent.java index c18d485..cb6538c 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/event/UpdateMergeRequestEvent.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/event/UpdateMergeRequestEvent.java @@ -1,7 +1,8 @@ package dev.struchkov.bot.gitlab.context.domain.event; -import dev.struchkov.bot.gitlab.context.domain.AssigneeChanged; -import dev.struchkov.bot.gitlab.context.domain.ReviewerChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ApprovalChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.AssigneeChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ReviewerChanged; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import lombok.AllArgsConstructor; import lombok.Builder; @@ -9,6 +10,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import java.util.Optional; + @Getter @Setter @Builder @@ -21,5 +24,6 @@ public class UpdateMergeRequestEvent { private AssigneeChanged assigneeChanged; private ReviewerChanged reviewerChanged; + private Optional optApprovalChanged; } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ApprovalChangedMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ApprovalChangedMrNotify.java new file mode 100644 index 0000000..01488c4 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ApprovalChangedMrNotify.java @@ -0,0 +1,41 @@ +package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; + +import dev.struchkov.bot.gitlab.context.domain.entity.Person; +import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; +import lombok.Builder; +import lombok.Getter; + +import java.util.Set; + +import static dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrNotifyFields.CLASS_NAME; + +@Getter +@FieldNames +public class ApprovalChangedMrNotify extends MrNotify { + + public static final String TYPE = CLASS_NAME; + + private final Set newApproval; + private final Set dontApproval; + + @Builder + public ApprovalChangedMrNotify( + Long mrId, + String projectName, + String title, + String url, + String milestone, + Set newApproval, + Set dontApproval + ) { + super(mrId, projectName, title, url, milestone); + this.newApproval = newApproval; + this.dontApproval = dontApproval; + } + + @Override + public String getType() { + return TYPE; + } + +} 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 86d0d05..08c7027 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 @@ -19,6 +19,7 @@ public class Icons { public static final String GREEN_CIRCLE = "\uD83D\uDFE2"; public static final String PEN = "✏️"; public static final String ASSIGNEE = "\uD83C\uDFA9"; + public static final String APPROVAL = "\uD83D\uDC4D"; public static final String BUILD = "⚙️"; public static final String LINK = "\uD83D\uDD17"; public static final String REVIEWER = "\uD83D\uDD0E"; diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/handler/MergeRequestHandler.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/handler/MergeRequestHandler.java index ded4587..4b3ab16 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/handler/MergeRequestHandler.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/handler/MergeRequestHandler.java @@ -1,15 +1,17 @@ package dev.struchkov.bot.gitlab.core.handler; -import dev.struchkov.bot.gitlab.context.domain.AssigneeChanged; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; -import dev.struchkov.bot.gitlab.context.domain.ReviewerChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ApprovalChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.AssigneeChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ReviewerChanged; 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.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Project; import dev.struchkov.bot.gitlab.context.domain.event.NewMergeRequestEvent; import dev.struchkov.bot.gitlab.context.domain.event.UpdateMergeRequestEvent; +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrNotify; 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; @@ -24,9 +26,11 @@ import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import java.util.List; -import java.util.Objects; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static java.lang.Boolean.TRUE; @@ -62,29 +66,56 @@ public class MergeRequestHandler { final MergeRequest newMergeRequest = event.getNewMergeRequest(); final AssigneeChanged assigneeChanged = event.getAssigneeChanged(); final ReviewerChanged reviewerChanged = event.getReviewerChanged(); + final Optional optApprovalChanged = event.getOptApprovalChanged(); final boolean isChangedMr = !oldMergeRequest.getUpdatedDate().equals(newMergeRequest.getUpdatedDate()) || oldMergeRequest.isConflict() != newMergeRequest.isConflict(); - final boolean isChangedLinkedEntity = reviewerChanged.isChanged() || assigneeChanged.isChanged(); - final boolean isMilestone = !Objects.equals(oldMergeRequest.getMilestone(), newMergeRequest.getMilestone()); - if (isChangedMr || isChangedLinkedEntity || isMilestone) { + if (oldMergeRequest.isNotification()) { + final Project project = projectService.getByIdOrThrow(newMergeRequest.getProjectId()); - if (oldMergeRequest.isNotification()) { - final Project project = projectService.getByIdOrThrow(newMergeRequest.getProjectId()); - - if (isChangedMr) { - notifyAboutStatus(oldMergeRequest, newMergeRequest, project); - notifyAboutNewConflict(oldMergeRequest, newMergeRequest, project); - notifyAboutResolveConflict(oldMergeRequest, newMergeRequest, project); - notifyAboutUpdate(oldMergeRequest, newMergeRequest, project); - } - - if (isChangedLinkedEntity) { - notifyReviewer(reviewerChanged, newMergeRequest, project); - notifyAssignee(assigneeChanged, oldMergeRequest, newMergeRequest, project); - } + if (isChangedMr) { + notifyAboutStatus(oldMergeRequest, newMergeRequest, project); + notifyAboutNewConflict(oldMergeRequest, newMergeRequest, project); + notifyAboutResolveConflict(oldMergeRequest, newMergeRequest, project); + notifyAboutUpdate(oldMergeRequest, newMergeRequest, project); } + if (reviewerChanged.isChanged()) { + notifyReviewer(reviewerChanged, newMergeRequest, project); + } + + if (assigneeChanged.isChanged()) { + notifyAssignee(assigneeChanged, oldMergeRequest, newMergeRequest, project); + } + + if (optApprovalChanged.isPresent()) { + final ApprovalChanged approvalChanged = optApprovalChanged.get(); + notifyApproval(newMergeRequest, project, approvalChanged); + } + } + + } + + private void notifyApproval(MergeRequest mergeRequest, Project project, ApprovalChanged approvalChanged) { + final Set newApproval = approvalChanged.getNewApproval().stream() + .filter(approval -> !personInformation.getId().equals(approval.getId())) + .collect(Collectors.toSet()); + final Set dontApproval = approvalChanged.getDontApprove().stream() + .filter(approval -> !personInformation.getId().equals(approval.getId())) + .collect(Collectors.toSet()); + + if (checkNotEmpty(newApproval) || checkNotEmpty(dontApproval)) { + notifyService.send( + ApprovalChangedMrNotify.builder() + .mrId(mergeRequest.getId()) + .milestone(mergeRequest.getMilestone()) + .projectName(project.getName()) + .title(mergeRequest.getTitle()) + .url(mergeRequest.getWebUrl()) + .newApproval(newApproval) + .dontApproval(dontApproval) + .build() + ); } } @@ -257,8 +288,9 @@ public class MergeRequestHandler { private void notifyAssignee(AssigneeChanged assigneeChanged, MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { switch (assigneeChanged) { - case BECOME -> - sendNotifyNewAssignee(mergeRequest, project.getName(), getAssignee(oldMergeRequest).map(Person::getName).orElse(null)); + case BECOME -> sendNotifyNewAssignee( + mergeRequest, project.getName(), getAssignee(oldMergeRequest).map(Person::getName).orElse(null) + ); } } //TODO [05.12.2022|uPagge]: Добавить уведомление, если происходит удаление ревьювера diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/DiscussionParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/DiscussionParser.java index 33554a9..76d9e27 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/DiscussionParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/DiscussionParser.java @@ -65,11 +65,11 @@ public class DiscussionParser { private void processingNewDiscussion(MergeRequestForDiscussion mergeRequest) { int page = 1; - final List discussionJson = gitlabSdkManager.getDiscussionForMergeRequest(mergeRequest.getProjectId(), mergeRequest.getTwoId(), page); + final List discussionJson = gitlabSdkManager.getDiscussionForMergeRequest(mergeRequest.getProjectId(), mergeRequest.getTwoId(), page, PAGE_COUNT); if (checkNotEmpty(discussionJson)) { while (discussionJson.size() == PAGE_COUNT) { - discussionJson.addAll(gitlabSdkManager.getDiscussionForMergeRequest(mergeRequest.getProjectId(), mergeRequest.getTwoId(), ++page)); + discussionJson.addAll(gitlabSdkManager.getDiscussionForMergeRequest(mergeRequest.getProjectId(), mergeRequest.getTwoId(), ++page, PAGE_COUNT)); } createNewDiscussion(discussionJson, mergeRequest); } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/MergeRequestParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/MergeRequestParser.java index 777a338..ab6309c 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/MergeRequestParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/parser/MergeRequestParser.java @@ -104,7 +104,7 @@ public class MergeRequestParser { newMergeRequests.stream() .flatMap(mergeRequest -> Stream.of(mergeRequest.getAssignee(), mergeRequest.getAuthor())), newMergeRequests.stream() - .flatMap(mergeRequest -> mergeRequest.getReviewers().stream()) + .flatMap(mergeRequest -> Stream.concat(mergeRequest.getReviewers().stream(), mergeRequest.getApprovals().stream())) ).distinct() .filter(Objects::nonNull) .collect(Collectors.toMap(Person::getId, p -> p)); @@ -122,6 +122,12 @@ public class MergeRequestParser { .map(reviewer -> personMap.get(reviewer.getId())) .collect(toList()) ); + + newMergeRequest.setApprovals( + newMergeRequest.getApprovals().stream() + .map(approval -> personMap.get(approval.getId())) + .collect(toList()) + ); } } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/MergeRequestsServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/MergeRequestsServiceImpl.java index e9304c8..edcd653 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/MergeRequestsServiceImpl.java @@ -1,11 +1,12 @@ package dev.struchkov.bot.gitlab.core.service; -import dev.struchkov.bot.gitlab.context.domain.AssigneeChanged; import dev.struchkov.bot.gitlab.context.domain.ExistContainer; import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; -import dev.struchkov.bot.gitlab.context.domain.ReviewerChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ApprovalChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.AssigneeChanged; +import dev.struchkov.bot.gitlab.context.domain.changed.ReviewerChanged; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion; import dev.struchkov.bot.gitlab.context.domain.entity.Person; @@ -104,19 +105,18 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { final MergeRequest oldMergeRequest = repository.findById(mergeRequest.getId()) .orElseThrow(notFoundException("MergeRequest не найден")); - final MergeRequest cloneOldMergeRequest = oldMergeRequest.toBuilder().build(); - mergeRequest.setNotification(oldMergeRequest.isNotification()); final Long gitlabUserId = personInformation.getId(); final AssigneeChanged assigneeChanged = AssigneeChanged.valueOf(gitlabUserId, getAssignee(oldMergeRequest), getAssignee(mergeRequest)); final ReviewerChanged reviewerChanged = ReviewerChanged.valueOf(gitlabUserId, oldMergeRequest.getReviewers(), mergeRequest.getReviewers()); + final Optional optApprovalChanged = ApprovalChanged.approvalChanged(oldMergeRequest.getApprovals(), mergeRequest.getApprovals()); mergeRequest.setUserAssignee(assigneeChanged.getNewStatus(oldMergeRequest.isUserAssignee())); mergeRequest.setUserReviewer(reviewerChanged.getNewStatus(oldMergeRequest.isUserReviewer())); eventPublisher.publishEvent( - updateMergeRequest(cloneOldMergeRequest, mergeRequest, assigneeChanged, reviewerChanged) + updateMergeRequest(oldMergeRequest, mergeRequest, assigneeChanged, reviewerChanged, optApprovalChanged) ); return repository.save(mergeRequest); diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index 4543a0b..94cd7ef 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -8,7 +8,7 @@ spring: change-log: classpath:liquibase/changelog.xml jpa: show-sql: false -# open-in-view: false + open-in-view: false hibernate: ddl-auto: none properties: diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ApprovalChangedMrNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ApprovalChangedMrNotifyGenerator.java new file mode 100644 index 0000000..4218d32 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/ApprovalChangedMrNotifyGenerator.java @@ -0,0 +1,83 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify; + +import dev.struchkov.bot.gitlab.context.domain.entity.Person; +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.simple.domain.BoxAnswer; +import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.Set; + +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.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; +import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine; +import static dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton.simpleButton; +import static dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton.urlButton; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; +import static java.util.stream.Collectors.joining; + +@Component +@RequiredArgsConstructor +public class ApprovalChangedMrNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(ApprovalChangedMrNotify notify) { + final StringBuilder builder = new StringBuilder(Icons.APPROVAL).append(" *Approvals changed*") + .append(Icons.HR) + .append(escapeMarkdown(notify.getTitle())); + + if (checkNotNull(notify.getProjectName()) || checkNotNull(notify.getMilestone())) { + builder + .append(Icons.HR); + + if (checkNotNull(notify.getProjectName())) { + builder.append(Icons.PROJECT).append(": ").append(escapeMarkdown(notify.getProjectName())).append("\n"); + } + + if (checkNotNull(notify.getMilestone())) { + builder.append(Icons.MILESTONE).append(": ").append(notify.getMilestone()).append("\n"); + } + + builder + .append("-- -- -- -- --\n"); + } + + final Set newApprovals = notify.getNewApproval(); + if (checkNotEmpty(newApprovals)) { + builder.append(Icons.YES).append(" : ").append(newApprovals.stream().map(Person::getName).collect(joining(", "))) + .append("\n\n"); + } + + final Set dontApproval = notify.getDontApproval(); + if (checkNotEmpty(dontApproval)) { + builder.append(Icons.NO).append(" : ").append(dontApproval.stream().map(Person::getName).collect(joining(", "))); + } + + return BoxAnswer.builder() + .message(builder.toString()) + .keyBoard( + inlineKeyBoard( + keyBoardLine( + 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 + "]") + ) + ) + ) + .payload(BoxAnswerPayload.ENABLE_MARKDOWN) + .build(); + } + + @Override + public String getNotifyType() { + return ApprovalChangedMrNotify.TYPE; + } + +}