From 07b5687cf2629c0c565a56c024d04644c35ee38b Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Tue, 3 Sep 2024 23:51:44 +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=BE=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=B5=20=D0=BE=D0=B1=20=D0=BE=D1=82=D1=81=D1=83?= =?UTF-8?q?=D1=82=D1=81=D1=82=D0=B2=D0=B8=D0=B8=20=D1=80=D0=B5=D0=B2=D1=8C?= =?UTF-8?q?=D1=8E=D0=B2=D0=B5=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/mr/ConflictMrGroupNotify.java | 36 +++++++++++ ...y.java => NewMergeRequestGroupNotify.java} | 4 +- .../group/mr/NoReviewerGroupNotify.java | 41 ++++++++++++ .../group/pipeline/PipelineGroupNotify.java | 4 +- .../core/handler/MergeRequestHandler.java | 62 ++++++++++++------ gitlab-app/src/main/resources/application.yml | 4 +- .../NewMergeRequestGroupNotifyGenerator.java | 8 +-- .../group/NoReviewerGroupNotifyGenerator.java | 64 +++++++++++++++++++ 8 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/ConflictMrGroupNotify.java rename bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/{NewMergeRequestGroupPersonalNotify.java => NewMergeRequestGroupNotify.java} (81%) create mode 100644 bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NoReviewerGroupNotify.java create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NoReviewerGroupNotifyGenerator.java diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/ConflictMrGroupNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/ConflictMrGroupNotify.java new file mode 100644 index 0000000..d16de72 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/ConflictMrGroupNotify.java @@ -0,0 +1,36 @@ +package dev.struchkov.bot.gitlab.context.domain.notify.group.mr; + +import dev.struchkov.bot.gitlab.context.domain.notify.GroupNotify; +import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; +import lombok.Getter; + +import static dev.struchkov.bot.gitlab.context.domain.notify.group.mr.ConflictMrGroupNotifyFields.CLASS_NAME; + +@Getter +@FieldNames +public class ConflictMrGroupNotify implements GroupNotify { + + public static final String TYPE = CLASS_NAME; + + protected final String sourceBranch; + protected final String projectName; + protected final String title; + protected final String url; + protected final String milestone; + protected final String authorTelegramUsername; + + public ConflictMrGroupNotify(String sourceBranch, String projectName, String title, String url, String milestone, String authorTelegramUsername) { + this.sourceBranch = sourceBranch; + this.projectName = projectName; + this.title = title; + this.url = url; + this.milestone = milestone; + this.authorTelegramUsername = authorTelegramUsername; + } + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupPersonalNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupNotify.java similarity index 81% rename from bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupPersonalNotify.java rename to bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupNotify.java index e783bb2..4bda777 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupPersonalNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NewMergeRequestGroupNotify.java @@ -12,7 +12,7 @@ import static dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRe @Getter @FieldNames -public class NewMergeRequestGroupPersonalNotify implements GroupNotify { +public class NewMergeRequestGroupNotify implements GroupNotify { public static final String TYPE = CLASS_NAME; @@ -29,7 +29,7 @@ public class NewMergeRequestGroupPersonalNotify implements GroupNotify { protected final Person assignee; @Builder - public NewMergeRequestGroupPersonalNotify(Long mrId, String projectName, String title, String url, String milestone, String description, String author, String targetBranch, String sourceBranch, Map reviewerTelegramUsernames, Person assignee) { + public NewMergeRequestGroupNotify(Long mrId, String projectName, String title, String url, String milestone, String description, String author, String targetBranch, String sourceBranch, Map reviewerTelegramUsernames, Person assignee) { this.mrId = mrId; this.projectName = projectName; this.title = title; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NoReviewerGroupNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NoReviewerGroupNotify.java new file mode 100644 index 0000000..88a8e39 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/mr/NoReviewerGroupNotify.java @@ -0,0 +1,41 @@ +package dev.struchkov.bot.gitlab.context.domain.notify.group.mr; + +import dev.struchkov.bot.gitlab.context.domain.entity.Person; +import dev.struchkov.bot.gitlab.context.domain.notify.GroupNotify; +import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; +import lombok.Builder; +import lombok.Getter; + +@Getter +@FieldNames +public class NoReviewerGroupNotify implements GroupNotify { + + public static final String TYPE = "NoReviewersGroupNotify"; + + protected final String projectName; + protected final String title; + protected final String url; + protected final String milestone; + protected final String targetBranch; + protected final String sourceBranch; + protected final Person assignee; + protected final String ownerTelegramUsername; + + @Builder + public NoReviewerGroupNotify(String projectName, String title, String url, String milestone, String targetBranch, String sourceBranch, Person assignee, String ownerTelegramUsername) { + this.projectName = projectName; + this.title = title; + this.url = url; + this.milestone = milestone; + this.targetBranch = targetBranch; + this.sourceBranch = sourceBranch; + this.assignee = assignee; + this.ownerTelegramUsername = ownerTelegramUsername; + } + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/pipeline/PipelineGroupNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/pipeline/PipelineGroupNotify.java index a440f8e..6add782 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/pipeline/PipelineGroupNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/group/pipeline/PipelineGroupNotify.java @@ -6,11 +6,13 @@ import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; import lombok.Builder; import lombok.Getter; +import static dev.struchkov.bot.gitlab.context.domain.notify.group.pipeline.PipelineGroupNotifyFields.CLASS_NAME; + @Getter @FieldNames public class PipelineGroupNotify implements GroupNotify { - public static final String TYPE = "PipelineGroupNotify"; + public static final String TYPE = CLASS_NAME; private final String projectName; private final String refName; 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 56de03e..a9f691c 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 @@ -11,7 +11,8 @@ 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.group.mr.NewMergeRequestGroupPersonalNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NoReviewerGroupNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictMrPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictResolveMrPersonalNotify; @@ -56,32 +57,53 @@ public class MergeRequestHandler { final boolean userReviewer = mergeRequest.isUserReviewer(); final boolean userAssignee = mergeRequest.isUserAssignee(); - if (!mergeRequest.isConflict()) { + if (mergeRequest.isConflict()) { + + } else { if (mergeRequest.isNotification()) { if (userReviewer || userAssignee) { if (userReviewer) sendNotifyNewMrReview(mergeRequest, projectName); if (userAssignee) sendNotifyNewAssignee(mergeRequest, projectName, null); } } - final Map reviewerTelegramUsernames = personService.getTelegramUsernamesByPersonIds(mergeRequest.getReviewers().stream().map(Person::getId).collect(Collectors.toSet())); - if (!reviewerTelegramUsernames.isEmpty()) { - notifyService.send( - NewMergeRequestGroupPersonalNotify.builder() - .mrId(mergeRequest.getId()) - .title(mergeRequest.getTitle()) - .url(mergeRequest.getWebUrl()) - .author(mergeRequest.getAuthor().getName()) - .reviewerTelegramUsernames(reviewerTelegramUsernames) - .milestone(mergeRequest.getMilestone()) - .projectName(projectName) - .targetBranch(mergeRequest.getTargetBranch()) - .sourceBranch(mergeRequest.getSourceBranch()) - .description(mergeRequest.getDescription()) - .build() - ); + + if (checkNotEmpty(mergeRequest.getReviewers())) { + final Map reviewerTelegramUsernames = personService.getTelegramUsernamesByPersonIds(mergeRequest.getReviewers().stream().map(Person::getId).collect(Collectors.toSet())); + if (!reviewerTelegramUsernames.isEmpty()) { + notifyService.send( + NewMergeRequestGroupNotify.builder() + .mrId(mergeRequest.getId()) + .title(mergeRequest.getTitle()) + .url(mergeRequest.getWebUrl()) + .author(mergeRequest.getAuthor().getName()) + .reviewerTelegramUsernames(reviewerTelegramUsernames) + .milestone(mergeRequest.getMilestone()) + .projectName(projectName) + .targetBranch(mergeRequest.getTargetBranch()) + .sourceBranch(mergeRequest.getSourceBranch()) + .description(mergeRequest.getDescription()) + .build() + ); + } + } else { + final Optional optAuthorUserName = personService.getTelegramUsernamesByPersonIds(mergeRequest.getAuthor().getId()); + if (optAuthorUserName.isPresent()) { + final String authorUsername = optAuthorUserName.get(); + notifyService.send( + NoReviewerGroupNotify.builder() + .ownerTelegramUsername(authorUsername) + .title(mergeRequest.getTitle()) + .url(mergeRequest.getWebUrl()) + .milestone(mergeRequest.getMilestone()) + .projectName(projectName) + .targetBranch(mergeRequest.getTargetBranch()) + .sourceBranch(mergeRequest.getSourceBranch()) + .build() + ); + } } - } else { - //TODO [03.09.2024|uPagge]: Уведомление о конфликте + + } } diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index 6ff5903..c720660 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -46,8 +46,8 @@ gitlab-bot: person: telegram-id: ${TELEGRAM_PERSON_ID} group-notify: - chat-id: "-1002233809566" - thread-id: "2" + chat-id: ${TELEGRAM_CHAT_ID:} + thread-id: ${TELEGRAM_THREAD_ID:} gitlab-sdk: access-token: ${GITLAB_PERSONAL_TOKEN} base-url: ${GITLAB_URL} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NewMergeRequestGroupNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NewMergeRequestGroupNotifyGenerator.java index d828fd6..a9e5470 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NewMergeRequestGroupNotifyGenerator.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NewMergeRequestGroupNotifyGenerator.java @@ -1,6 +1,6 @@ package dev.struchkov.bot.gitlab.telegram.service.notify.group; -import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupPersonalNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupNotify; import dev.struchkov.bot.gitlab.context.utils.Icons; import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator; import dev.struchkov.godfather.simple.domain.BoxAnswer; @@ -16,10 +16,10 @@ import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; @Component @RequiredArgsConstructor -public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGenerator { +public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGenerator { @Override - public BoxAnswer generate(NewMergeRequestGroupPersonalNotify notify) { + public BoxAnswer generate(NewMergeRequestGroupNotify notify) { final String reviewerUsernames = String.join(", ", notify.getReviewerTelegramUsernames().values()); final StringBuilder builder = new StringBuilder(Icons.FUN) .append("*You are reviewers:* ").append(reviewerUsernames) @@ -59,7 +59,7 @@ public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGener @Override public String getNotifyType() { - return NewMergeRequestGroupPersonalNotify.TYPE; + return NewMergeRequestGroupNotify.TYPE; } } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NoReviewerGroupNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NoReviewerGroupNotifyGenerator.java new file mode 100644 index 0000000..4800edb --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/group/NoReviewerGroupNotifyGenerator.java @@ -0,0 +1,64 @@ +package dev.struchkov.bot.gitlab.telegram.service.notify.group; + +import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NoReviewerGroupNotify; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator; +import dev.struchkov.godfather.simple.domain.BoxAnswer; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +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.UrlButton.urlButton; +import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Component +@RequiredArgsConstructor +public class NoReviewerGroupNotifyGenerator implements NotifyBoxAnswerGenerator { + + @Override + public BoxAnswer generate(NoReviewerGroupNotify notify) { + final StringBuilder builder = new StringBuilder(Icons.DANGEROUS) + .append(notify.getOwnerTelegramUsername()) + .append(", *your merge request does not have reviewers*") + .append(Icons.HR) + .append(escapeMarkdown(notify.getTitle())); + + 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(Icons.TREE).append(": ").append(escapeMarkdown(notify.getSourceBranch())).append(Icons.ARROW).append(escapeMarkdown(notify.getTargetBranch())).append("\n"); + + if (checkNotNull(notify.getAssignee())) { + builder.append(Icons.ASSIGNEE).append(": ").append(notify.getAssignee()); + } + + final String notifyMessage = builder.toString(); + + return BoxAnswer.builder() + .message(notifyMessage) + .keyBoard(inlineKeyBoard( + keyBoardLine( + urlButton(Icons.LINK, notify.getUrl()) + ) + )) + .payload(ENABLE_MARKDOWN) + .build(); + } + + @Override + public String getNotifyType() { + return NoReviewerGroupNotify.TYPE; + } + +}