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/AssigneeChanged.java index de57c81..32bce74 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/AssigneeChanged.java @@ -4,8 +4,7 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Person; import lombok.Getter; import lombok.RequiredArgsConstructor; -import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Checker.checkNull; +import java.util.Optional; @Getter @RequiredArgsConstructor @@ -18,18 +17,18 @@ public enum AssigneeChanged { private final boolean changed; - public static AssigneeChanged valueOf(Long gitlabUserId, Person oldAssignee, Person newAssignee) { - if (checkNull(oldAssignee) && checkNotNull(newAssignee) && gitlabUserId.equals(newAssignee.getId())) { + public static AssigneeChanged valueOf(Long gitlabUserId, Optional oldAssignee, Optional newAssignee) { + if (oldAssignee.isEmpty() && newAssignee.isPresent() && gitlabUserId.equals(newAssignee.get().getId())) { return AssigneeChanged.BECOME; } - if (checkNotNull(oldAssignee) && checkNull(newAssignee) && gitlabUserId.equals(oldAssignee.getId())) { + if (oldAssignee.isPresent() && newAssignee.isEmpty() && gitlabUserId.equals(oldAssignee.get().getId())) { return AssigneeChanged.DELETED; } - if (checkNotNull(oldAssignee) && checkNotNull(newAssignee) && !oldAssignee.getId().equals(newAssignee.getId())) { - if (gitlabUserId.equals(oldAssignee.getId())) { + if (oldAssignee.isPresent() && newAssignee.isPresent() && !oldAssignee.get().getId().equals(newAssignee.get().getId())) { + if (gitlabUserId.equals(oldAssignee.get().getId())) { return AssigneeChanged.DELETED; } - if (gitlabUserId.equals(newAssignee.getId())) { + if (gitlabUserId.equals(newAssignee.get().getId())) { return AssigneeChanged.BECOME; } return AssigneeChanged.NOT_AFFECT_USER; 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 173603b..1bedbdf 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 @@ -81,10 +81,11 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { private boolean isBotUserAssigneeAndNotAuthor(MergeRequest mergeRequest) { final Long gitlabUserId = personInformation.getId(); - final Person assignee = mergeRequest.getAssignee(); + final Optional optAssignee = getAssignee(mergeRequest); final Person author = mergeRequest.getAuthor(); - if (checkNotNull(assignee)) { + if (optAssignee.isPresent()) { + final Person assignee = optAssignee.get(); if (gitlabUserId.equals(assignee.getId()) && !isAuthorSameAssignee(author, assignee)) { return true; } @@ -116,20 +117,26 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } private void sendNotifyNewMrReview(MergeRequest mergeRequest, String projectName) { - notifyService.send( - NewMrForReview.builder() - .mrId(mergeRequest.getId()) - .projectName(projectName) - .labels(mergeRequest.getLabels()) - .author(mergeRequest.getAuthor().getName()) - .description(mergeRequest.getDescription()) - .title(mergeRequest.getTitle()) - .url(mergeRequest.getWebUrl()) - .targetBranch(mergeRequest.getTargetBranch()) - .sourceBranch(mergeRequest.getSourceBranch()) - .assignee(mergeRequest.getAssignee().getName()) - .build() - ); + final NewMrForReview.NewMrForReviewBuilder builder = NewMrForReview.builder() + .mrId(mergeRequest.getId()) + .projectName(projectName) + .labels(mergeRequest.getLabels()) + .author(mergeRequest.getAuthor().getName()) + .description(mergeRequest.getDescription()) + .title(mergeRequest.getTitle()) + .url(mergeRequest.getWebUrl()) + .targetBranch(mergeRequest.getTargetBranch()) + .sourceBranch(mergeRequest.getSourceBranch()); + + getAssignee(mergeRequest) + .map(Person::getName) + .ifPresent(builder::assignee); + + notifyService.send(builder.build()); + } + + private Optional getAssignee(MergeRequest mergeRequest) { + return Optional.ofNullable(mergeRequest.getAssignee()); } private void sendNotifyNewAssignee(MergeRequest mergeRequest, String projectName, String oldAssigneeName) { @@ -148,9 +155,9 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { if (checkNotNull(oldAssigneeName)) { builder.oldAssigneeName(oldAssigneeName); - if (checkNotNull(mergeRequest.getAssignee())) { - builder.newAssigneeName(mergeRequest.getAssignee().getName()); - } + getAssignee(mergeRequest) + .map(Person::getName) + .ifPresent(builder::newAssigneeName); } notifyService.send(builder.build()); @@ -165,7 +172,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { mergeRequest.setNotification(oldMergeRequest.isNotification()); final Long gitlabUserId = personInformation.getId(); - final AssigneeChanged assigneeChanged = AssigneeChanged.valueOf(gitlabUserId, oldMergeRequest.getAssignee(), mergeRequest.getAssignee()); + final AssigneeChanged assigneeChanged = AssigneeChanged.valueOf(gitlabUserId, getAssignee(oldMergeRequest), getAssignee(mergeRequest)); final ReviewerChanged reviewerChanged = ReviewerChanged.valueOf(gitlabUserId, oldMergeRequest.getReviewers(), mergeRequest.getReviewers()); mergeRequest.setUserAssignee(assigneeChanged.getNewStatus(oldMergeRequest.isUserAssignee())); @@ -201,8 +208,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { 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)); + 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/service/impl/PipelineServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java index 3c5d17b..739eab4 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java @@ -101,8 +101,9 @@ public class PipelineServiceImpl implements PipelineService { private boolean isNeedNotifyNewPipeline(@NonNull Pipeline pipeline) { final Person personPipelineCreator = pipeline.getPerson(); return notificationStatus.contains(pipeline.getStatus()) // Пайплайн имеет статус необходимый для уведомления - && checkNotNull(personPipelineCreator) // Создатель пайплайна не null - && personInformation.getId().equals(personPipelineCreator.getId()); // Пользователь приложения является инициатором пайплайна + && checkNotNull(personPipelineCreator) // Создатель пайплайна не null + && personInformation.getId().equals(personPipelineCreator.getId()) // Пользователь приложения является инициатором пайплайна + && LocalDateTime.now().minusDays(1).isBefore(pipeline.getCreated()); // Пайплан был создан не более 24 часов назад } @Override @@ -128,7 +129,7 @@ public class PipelineServiceImpl implements PipelineService { @Override public void cleanOld() { log.debug("Старт очистки старых пайплайнов"); - repository.deleteByCreatedBefore(LocalDateTime.now().minusDays(1L)); + repository.deleteByCreatedBefore(LocalDateTime.now().minusDays(7L)); log.debug("Конец очистки старых пайплайнов"); } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java index 7db61fc..41f9ee1 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java @@ -35,6 +35,7 @@ import static dev.struchkov.haiti.utils.Checker.checkFalse; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResult; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResults; +import static java.util.stream.Collectors.toMap; /** * Парсер пайплайнов. @@ -79,7 +80,7 @@ public class PipelineParser { final Set projectIds = projectService.getAllIdByProcessingEnable(); final Map pipelineProjectMap = getPipelineShortJsons(projectIds).stream() - .collect(Collectors.toMap(PipelineShortJson::getId, PipelineShortJson::getProjectId)); + .collect(toMap(PipelineShortJson::getId, PipelineShortJson::getProjectId)); if (checkNotEmpty(pipelineProjectMap)) { final ExistContainer existContainer = pipelineService.existsById(pipelineProjectMap.keySet()); @@ -99,14 +100,16 @@ public class PipelineParser { log.debug("Конец обработки новых пайплайнов"); } - private List getNewPipelines(Map pipelineProjectMap, Set idsNotFound) { + private List getNewPipelines(Map pipelineIdAndProjectId, Set idsNotFound) { final List>> tasks = idsNotFound.stream() - .map(pipelineId -> new GetPipelineTask( - gitlabProperty.getPipelineUrl(), - pipelineProjectMap.get(pipelineId), - pipelineId, - personProperty.getToken() - )) + .map( + pipelineId -> GetPipelineTask.builder() + .pipelineId(pipelineId) + .projectId(pipelineIdAndProjectId.get(pipelineId)) + .urlPipeline(gitlabProperty.getPipelineUrl()) + .gitlabToken(personProperty.getToken()) + .build() + ) .map(forkJoinPool::submit) .collect(Collectors.toList()); @@ -141,12 +144,13 @@ public class PipelineParser { final List>> tasks = pipelines.stream() .map( - pipeline -> new GetPipelineTask( - gitlabProperty.getPipelineUrl(), - pipeline.getProjectId(), - pipeline.getId(), - personProperty.getToken() - ) + pipeline -> + GetPipelineTask.builder() + .projectId(pipeline.getProjectId()) + .pipelineId(pipeline.getId()) + .urlPipeline(gitlabProperty.getPipelineUrl()) + .gitlabToken(gitlabProperty.getPipelineUrl()) + .build() ) .map(forkJoinPool::submit) .collect(Collectors.toList()); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java index 8e43d22..5da19dc 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java @@ -3,7 +3,10 @@ package dev.struchkov.bot.gitlab.core.service.parser.forktask; import dev.struchkov.bot.gitlab.core.utils.HttpParse; import dev.struchkov.bot.gitlab.core.utils.StringUtils; import dev.struchkov.bot.gitlab.sdk.domain.PipelineJson; -import lombok.RequiredArgsConstructor; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -15,13 +18,15 @@ import static dev.struchkov.bot.gitlab.core.utils.HttpParse.ACCEPT; @Slf4j -@RequiredArgsConstructor +@Builder +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class GetPipelineTask extends RecursiveTask> { - private final String urlPipeline; - private final long projectId; - private final long pipelineId; - private final String gitlabToken; + private String urlPipeline; + private long projectId; + private long pipelineId; + private String gitlabToken; @Override @SneakyThrows diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index 1638eca..4c2c48b 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -19,7 +19,8 @@ spring: logging: level: - "dev.struchkov": ${LOG_LEVEL:info} + "dev.struchkov": ${LOG_LEVEL:INFO} + "dev.struchkov.bot.gitlab": ${APP_LOG_LEVEL:INFO} telegram: bot: @@ -39,9 +40,9 @@ gitlab-bot: version: 1.0.0 cron: scan: - general: "0 */1 * * * *" - new-project: "0 0 */1 * * *" - new-merge-request: "0 */15 * * * *" + general: ${CRON_GENERAL:0 */1 * * * *} + new-project: ${CRON_NEW_PROJECTS:0 0 */1 * * *} + new-merge-request: ${CRON_NEW_MR:0 */15 * * * *} person: telegram-id: ${TELEGRAM_PERSON_ID} token: ${GITLAB_PERSONAL_TOKEN} 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 d616bba..4b02579 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 @@ -9,11 +9,11 @@ import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_CONFIRMAT 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.simple.domain.BoxAnswer.boxAnswer; 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.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN; import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; @Component @@ -30,16 +30,18 @@ public class ConflictPrNotifyGenerator implements NotifyBoxAnswerGenerator> notes) { diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java index 7ba36ef..87b2871 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/PipelineNotifyGenerator.java @@ -11,11 +11,11 @@ import org.springframework.stereotype.Service; import java.util.Optional; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; -import static dev.struchkov.godfather.simple.domain.BoxAnswer.boxAnswer; 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.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN; @Service @RequiredArgsConstructor @@ -42,15 +42,16 @@ public class PipelineNotifyGenerator implements NotifyBoxAnswerGeneratorbuilder() .triggerCheck( mail -> { - final List forwardMails = mail.getForwardMail(); - if (Checker.checkNotNull(forwardMails) && forwardMails.size() == 1) { - final Mail forwardMail = forwardMails.get(0); - final boolean isLink = Attachments.findFirstLink(forwardMail.getAttachments()) - .isPresent(); + final Mail replay = mail.getReplayMail(); + if (checkNotNull(replay)) { + final boolean isLink = Attachments.findFirstLink(replay.getAttachments()).isPresent(); if (isLink) { final boolean isAccess = personInformation.getTelegramId().equals(mail.getFromPersonId()); final boolean firstStart = settingService.isFirstStart(); @@ -56,7 +53,7 @@ public class AnswerNoteUnit implements PersonUnitConfiguration { ) .answer( mail -> { - final String noteUrl = Attachments.findFirstLink(mail.getForwardMail().get(0).getAttachments()) + final String noteUrl = Attachments.findFirstLink(mail.getReplayMail().getAttachments()) .map(LinkAttachment::getUrl) .orElseThrow(); final Matcher matcher = NOTE_LINK.matcher(noteUrl);