From 167843d8b2192ff55d52af73d2d6371607a8bb37 Mon Sep 17 00:00:00 2001 From: uPagge Date: Sun, 17 Jan 2021 19:57:27 +0300 Subject: [PATCH] release-0.0.1 --- .../app/scheduler/SchedulerService.java | 9 +- bot-app/src/main/resources/application.yaml | 9 +- .../v.1.0.0/2021-01-14-create-tables.xml | 23 ++- .../src/main/resources/messages_en.properties | 2 +- .../src/main/resources/messages_ru.properties | 2 +- .../bot/gitlab/context/domain/TaskStatus.java | 8 - .../context/domain/entity/MergeRequest.java | 12 ++ .../gitlab/context/domain/entity/Note.java | 17 +- .../gitlab/context/domain/entity/Task.java | 23 ++- .../context/repository/CommentRepository.java | 27 --- .../context/repository/NoteRepository.java | 13 ++ .../context/repository/TaskRepository.java | 12 -- .../gitlab/context/service/NoteService.java | 14 -- .../gitlab/context/service/TaskService.java | 14 -- .../config/properties/GitlabProperty.java | 2 + .../service/convert/CommentToTaskConvert.java | 33 ---- .../core/service/convert/NoteJsonConvert.java | 36 ++++ .../convert/NoteJsonToTaskConvert.java | 41 ++++ .../service/convert/TaskToCommentConvert.java | 32 ---- .../impl/MergeRequestsServiceImpl.java | 14 +- .../core/service/impl/NoteServiceImpl.java | 141 -------------- .../core/service/impl/TaskServiceImpl.java | 176 ------------------ .../impl/note/AbstractNoteService.java | 56 ++++++ .../service/impl/note/NoteServiceImpl.java | 73 ++++++++ .../service/impl/note/TaskServiceImpl.java | 89 +++++++++ .../core/service/parser/NoteParser.java | 82 ++++++-- .../data/impl/CommentRepositoryImpl.java | 49 ----- .../gitlab/data/impl/NoteRepositoryImpl.java | 24 +++ .../gitlab/data/impl/TaskRepositoryImpl.java | 24 +-- .../gitlab/data/jpa/CommentRepositoryJpa.java | 15 -- .../gitlab/data/jpa/TaskRepositoryJpa.java | 14 -- .../bot/gitlab/sdk/domain/NoteJson.java | 10 +- 32 files changed, 492 insertions(+), 604 deletions(-) delete mode 100644 bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/TaskStatus.java delete mode 100644 bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/CommentRepository.java create mode 100644 bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/NoteRepository.java delete mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/CommentToTaskConvert.java create mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonConvert.java create mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonToTaskConvert.java delete mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/TaskToCommentConvert.java delete mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/NoteServiceImpl.java delete mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/TaskServiceImpl.java create mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/AbstractNoteService.java create mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/NoteServiceImpl.java create mode 100644 bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/TaskServiceImpl.java delete mode 100644 bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/CommentRepositoryImpl.java create mode 100644 bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/NoteRepositoryImpl.java diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/SchedulerService.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/SchedulerService.java index 9290679..43abbdd 100644 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/SchedulerService.java +++ b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/SchedulerService.java @@ -3,7 +3,7 @@ package org.sadtech.bot.gitlab.app.scheduler; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.sadtech.bot.gitlab.core.service.parser.MergeRequestParser; -import org.sadtech.bot.gitlab.core.service.parser.ProjectParser; +import org.sadtech.bot.gitlab.core.service.parser.NoteParser; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -17,8 +17,8 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class SchedulerService { - private final ProjectParser projectParser; private final MergeRequestParser mergeRequestParser; + private final NoteParser noteParser; @Scheduled(cron = "*/30 * * * * *") public void newMergeRequest() { @@ -30,4 +30,9 @@ public class SchedulerService { mergeRequestParser.parsingOldMergeRequest(); } + @Scheduled(cron = "*/30 * * * * *") + public void newNoteParser() { + noteParser.scanNewCommentAndTask(); + } + } diff --git a/bot-app/src/main/resources/application.yaml b/bot-app/src/main/resources/application.yaml index 2cc649c..4282d5f 100644 --- a/bot-app/src/main/resources/application.yaml +++ b/bot-app/src/main/resources/application.yaml @@ -32,14 +32,15 @@ gitlab-bot: telegram-id: ${TELEGRAM_PERSON_ID} token: ${GITLAB_PERSONAL_TOKEN} gitlab: - url-project: ${GITLAB_URL}/api/v4/projects?page={0}&per_page=100 - url-pull-request-open: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests?state=opened&page={1}&per_page=100 + url-project: ${GITLAB_URL}/api/v4/projects?page={0, number, integer}&per_page=100 + url-pull-request-open: ${GITLAB_URL}/api/v4/projects/{0, number, integer}/merge_requests?state=opened&page={1, number, integer}&per_page=100 url-pull-request-close: ${GITLAB_URL} - url-pull-request-comment: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests/{1}/notes - url-pull-request: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests/{1}?page={2}&per_page=100 + url-pull-request-comment: ${GITLAB_URL}/api/v4/projects/{0, number, integer}/merge_requests/{1}/notes + url-pull-request: ${GITLAB_URL}/api/v4/projects/{0, number, integer}/merge_requests/{1, number, integer}?page={2, number, integer}&per_page=100 url-merge-request-add: ${GITLAB_URL}/api/v4/projects/{0}%2F{1} user-url: ${GITLAB_URL}/api/v4/user users-url: ${GITLAB_URL}/api/v4/users + url-note: "{0}#note_{1,number,#}" teamcity: token: ${TEAMCITY_ADMIN_TOKEN} project-url: ${TEAMCITY_URL}/app/rest/projects diff --git a/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml b/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml index c8cb826..41ba28e 100644 --- a/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml +++ b/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml @@ -94,6 +94,9 @@ + + + @@ -136,13 +139,31 @@ - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bot-app/src/main/resources/messages_en.properties b/bot-app/src/main/resources/messages_en.properties index 7120c37..18056c6 100644 --- a/bot-app/src/main/resources/messages_en.properties +++ b/bot-app/src/main/resources/messages_en.properties @@ -15,7 +15,7 @@ menu.add_project_success=Project added successfully ui.menu.setting.text=This is the settings menu ui.menu.setting.language=Language settings ui.menu.setting.language.text=Choose your language -notify.pr.new={0} *New PullRequest | {1}*{2}[{3}]({4}){5}{2}{9}: {10} {12} {11}\n{7}: {8} +notify.pr.new={0} *New MergeRequest | {1}*{2}[{3}]({4}){5}{2}{9}: {10} {12} {11}\n{7}: {8} notify.pr.forgotten={0} *MergeRequest Review Reminder | {4}*{3}[{1}]({2}) notify.pr.conflict={0} *Attention! MergeRequest conflict | {4}*{1}[{2}]({3}) notify.pr.smart={0} *MergeRequest Reminder | {6}*{3}[{1}]({2}){3}{4} изменил свое решение на {5} diff --git a/bot-app/src/main/resources/messages_ru.properties b/bot-app/src/main/resources/messages_ru.properties index e5ae604..2ae90af 100644 --- a/bot-app/src/main/resources/messages_ru.properties +++ b/bot-app/src/main/resources/messages_ru.properties @@ -15,7 +15,7 @@ menu.add_project_success=Проект успешно добавлен ui.menu.setting.text=Это меню настроек ui.menu.setting.language=Настройки языка ui.menu.setting.language.text=Выберете язык -notify.pr.new={0} *Новый PullRequest | {1}*{2}[{3}]({4}){5}{2}{9}: {10} {12} {11}\n{7}: {8} +notify.pr.new={0} *Новый MergeRequest | {1}*{2}[{3}]({4}){5}{2}{9}: {10} {12} {11}\n{7}: {8} notify.pr.forgotten={0} *Напоминание о просмотре PullRequest | {4}*{3}[{1}]({2}) notify.pr.conflict={0} *Внимание! Конфликт в MergeRequest | {4}*{1}[{2}]({3}) notify.pr.smart={0} *Напоминание о MergeRequest | {6}*{3}[{1}]({2}){3}{4} изменил свое решение на {5} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/TaskStatus.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/TaskStatus.java deleted file mode 100644 index 0548132..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/TaskStatus.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.sadtech.bot.gitlab.context.domain; - -public enum TaskStatus { - - OPEN, - RESOLVED - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java index c450869..27d6dc4 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java @@ -12,12 +12,17 @@ import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; +import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.Table; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -79,9 +84,16 @@ public class MergeRequest implements BasicEntity { @Column(name = "source_branch") private String sourceBranch; + @Column(name = "notification") + private Boolean notification; + @ElementCollection @CollectionTable(name = "merge_request_label", joinColumns = @JoinColumn(name = "merge_request_id")) @Column(name = "label") private Set labels = new HashSet<>(); + @JoinTable + @OneToMany(fetch = FetchType.LAZY) + private List notes = new ArrayList<>(); + } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Note.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Note.java index ea892a3..7c9aba0 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Note.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Note.java @@ -23,8 +23,8 @@ import java.time.LocalDateTime; @Table(name = "note") @EqualsAndHashCode(onlyExplicitlyIncluded = true) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@DiscriminatorColumn(name = "type") -@DiscriminatorValue("null") +@DiscriminatorColumn(name = "resolvable") +@DiscriminatorValue("false") public class Note implements BasicEntity { @Id @@ -56,17 +56,10 @@ public class Note implements BasicEntity { @Column(name = "noteable_type") private String noteableType; - @Column(name = "resolveable") - private Boolean resolveable; - - @Column(name = "resolved") - private Boolean resolved; - - @ManyToOne - @JoinColumn(name = "resolved_id") - private Person resolvedBy; - @Column(name = "noteable_iid") private Long noteableIid; + @Column(name = "web_url") + private String webUrl; + } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Task.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Task.java index f8de498..53d9982 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Task.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Task.java @@ -1,12 +1,31 @@ package org.sadtech.bot.gitlab.context.domain.entity; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; -//@Entity +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity @Getter @Setter -//@Table(name = "task") +@DiscriminatorValue("true") +@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true) public class Task extends Note { + @Column(name = "resolved") + private Boolean resolved; + + @ManyToOne + @JoinColumn(name = "resolved_id") + private Person resolvedBy; + + @ManyToOne + @JoinColumn(name = "responsible_id") + private Person responsible; + } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/CommentRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/CommentRepository.java deleted file mode 100644 index be5f4c4..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/CommentRepository.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.sadtech.bot.gitlab.context.repository; - -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.haiti.context.repository.SimpleManagerRepository; - -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -/** - * // TODO: 08.09.2020 Добавить описание. - * - * @author upagge 08.09.2020 - */ -public interface CommentRepository extends SimpleManagerRepository { - - Optional findFirstByOrderByIdDesc(); - - List findByCreateDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - - List findAllById(@NonNull Set ids); - - Set existsById(Set ids); - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/NoteRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/NoteRepository.java new file mode 100644 index 0000000..a90d452 --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/NoteRepository.java @@ -0,0 +1,13 @@ +package org.sadtech.bot.gitlab.context.repository; + +import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.haiti.context.repository.SimpleManagerRepository; + +/** + * // TODO: 08.09.2020 Добавить описание. + * + * @author upagge 08.09.2020 + */ +public interface NoteRepository extends SimpleManagerRepository { + +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/TaskRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/TaskRepository.java index e30d30e..0809a1e 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/TaskRepository.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/TaskRepository.java @@ -1,20 +1,8 @@ package org.sadtech.bot.gitlab.context.repository; -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.TaskStatus; import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.sadtech.haiti.context.repository.SimpleManagerRepository; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; - public interface TaskRepository extends SimpleManagerRepository { - Optional findFirstByOrderByIdDesc(); - - List findByCreateDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo); - - List findAllByResponsibleAndStatus(@NonNull String responsibleLogin, @NonNull TaskStatus status); - } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/NoteService.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/NoteService.java index fb2079d..3df8f46 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/NoteService.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/NoteService.java @@ -1,22 +1,8 @@ package org.sadtech.bot.gitlab.context.service; -import lombok.NonNull; import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.sadtech.haiti.context.service.SimpleManagerService; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Set; - public interface NoteService extends SimpleManagerService { - Long getLastCommentId(); - - List getAllBetweenDate(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - - List getAllById(@NonNull Set ids); - - Note convert(@NonNull Task task); - } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/TaskService.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/TaskService.java index 2f6fdfb..a783432 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/TaskService.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/TaskService.java @@ -1,22 +1,8 @@ package org.sadtech.bot.gitlab.context.service; -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.TaskStatus; -import org.sadtech.bot.gitlab.context.domain.entity.Note; import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.sadtech.haiti.context.service.SimpleManagerService; -import java.time.LocalDateTime; -import java.util.List; - public interface TaskService extends SimpleManagerService { - Long getLastTaskId(); - - Task convert(@NonNull Note note); - - List getAllBetweenDate(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - - List getAllByResponsibleAndStatus(@NonNull String login, @NonNull TaskStatus open); - } diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java index aaa4645..837e283 100644 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java @@ -44,4 +44,6 @@ public class GitlabProperty { private String urlMergeRequestAdd; + private String urlNote; + } diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/CommentToTaskConvert.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/CommentToTaskConvert.java deleted file mode 100644 index 1ffa0fb..0000000 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/CommentToTaskConvert.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.sadtech.bot.gitlab.core.service.convert; - -import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.bot.gitlab.context.domain.entity.Task; -import org.springframework.core.convert.converter.Converter; -import org.springframework.stereotype.Component; - -/** - * // TODO: 12.09.2020 Добавить описание. - * - * @author upagge 12.09.2020 - */ -@Component -public class CommentToTaskConvert implements Converter { - - @Override - public Task convert(Note source) { - final Task task = new Task(); -// task.setId(source.getId()); -// task.setUrl(source.getUrl()); -// task.setUrlApi(source.getUrlApi()); -// task.setResponsible(source.getResponsible()); -// task.setStatus(TaskStatus.OPEN); -// task.setPullRequestId(source.getPullRequestId()); -// task.setBitbucketVersion(source.getBitbucketVersion()); -// task.setCreateDate(source.getCreateDate()); -// task.setDescription(source.getMessage()); -// task.setAuthor(source.getAuthor()); -// task.setAnswers(source.getAnswers()); - return task; - } - -} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonConvert.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonConvert.java new file mode 100644 index 0000000..327ad90 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonConvert.java @@ -0,0 +1,36 @@ +package org.sadtech.bot.gitlab.core.service.convert; + +import lombok.RequiredArgsConstructor; +import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.bot.gitlab.sdk.domain.NoteJson; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +/** + * // TODO: 12.09.2020 Добавить описание. + * + * @author upagge 12.09.2020 + */ +@Component +@RequiredArgsConstructor +public class NoteJsonConvert implements Converter { + + private final PersonJsonConverter personConverter; + + @Override + public Note convert(NoteJson source) { + final Note note = new Note(); + note.setAuthor(personConverter.convert(source.getAuthor())); + note.setId(source.getId()); + note.setBody(source.getBody()); + note.setType(source.getType()); + note.setNoteableType(source.getNoteableType()); + note.setCreated(source.getCreated()); + note.setUpdated(source.getUpdated()); + note.setNoteableId(source.getNoteableId()); + note.setNoteableIid(source.getNoteableIid()); + note.setSystem(source.isSystem()); + return note; + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonToTaskConvert.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonToTaskConvert.java new file mode 100644 index 0000000..96178a9 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/NoteJsonToTaskConvert.java @@ -0,0 +1,41 @@ +package org.sadtech.bot.gitlab.core.service.convert; + +import lombok.RequiredArgsConstructor; +import org.sadtech.bot.gitlab.context.domain.entity.Task; +import org.sadtech.bot.gitlab.sdk.domain.NoteJson; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +/** + * // TODO: 12.09.2020 Добавить описание. + * + * @author upagge 12.09.2020 + */ +@Component +@RequiredArgsConstructor +public class NoteJsonToTaskConvert implements Converter { + + private final PersonJsonConverter personConverter; + + @Override + public Task convert(NoteJson source) { + final Task task = new Task(); + task.setAuthor(personConverter.convert(source.getAuthor())); + task.setId(source.getId()); + task.setBody(source.getBody()); + task.setType(source.getType()); + task.setNoteableType(source.getNoteableType()); + task.setCreated(source.getCreated()); + task.setUpdated(source.getUpdated()); + task.setNoteableId(source.getNoteableId()); + task.setNoteableIid(source.getNoteableIid()); + task.setSystem(source.isSystem()); + + task.setResolved(source.getResolved()); + if (source.getResolvedBy() != null) { + task.setResolvedBy(personConverter.convert(source.getResolvedBy())); + } + return task; + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/TaskToCommentConvert.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/TaskToCommentConvert.java deleted file mode 100644 index e94e7b6..0000000 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/TaskToCommentConvert.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.sadtech.bot.gitlab.core.service.convert; - -import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.bot.gitlab.context.domain.entity.Task; -import org.springframework.core.convert.converter.Converter; -import org.springframework.stereotype.Component; - -/** - * // TODO: 12.09.2020 Добавить описание. - * - * @author upagge 12.09.2020 - */ -@Component -public class TaskToCommentConvert implements Converter { - - @Override - public Note convert(Task source) { - final Note note = new Note(); -// note.setId(source.getId()); -// note.setUrl(source.getUrl()); -// note.setUrlApi(source.getUrlApi()); -// note.setPullRequestId(source.getPullRequestId()); -// note.setBitbucketVersion(source.getBitbucketVersion()); -// note.setCreateDate(source.getCreateDate()); -// note.setMessage(source.getDescription()); -// note.setResponsible(source.getResponsible()); -// note.setAuthor(source.getAuthor()); -// note.setAnswers(source.getAnswers()); - return note; - } - -} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java index 911fbe2..674e18d 100644 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java @@ -62,6 +62,8 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService new NotFoundException("Проект не найден")); - notifyStatus(oldMergeRequest, mergeRequest, project); - notifyConflict(oldMergeRequest, mergeRequest, project); - notifyUpdate(oldMergeRequest, mergeRequest, project); + if (Boolean.TRUE.equals(oldMergeRequest.getNotification())) { + notifyStatus(oldMergeRequest, mergeRequest, project); + notifyConflict(oldMergeRequest, mergeRequest, project); + notifyUpdate(oldMergeRequest, mergeRequest, project); + } return mergeRequestRepository.save(mergeRequest); } diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/NoteServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/NoteServiceImpl.java deleted file mode 100644 index cad894a..0000000 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/NoteServiceImpl.java +++ /dev/null @@ -1,141 +0,0 @@ -package org.sadtech.bot.gitlab.core.service.impl; - -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.bot.gitlab.context.domain.entity.Task; -import org.sadtech.bot.gitlab.context.domain.notify.comment.CommentNotify; -import org.sadtech.bot.gitlab.context.exception.NotFoundException; -import org.sadtech.bot.gitlab.context.repository.CommentRepository; -import org.sadtech.bot.gitlab.context.service.NoteService; -import org.sadtech.bot.gitlab.context.service.NotifyService; -import org.sadtech.bot.gitlab.context.service.TaskService; -import org.sadtech.haiti.context.domain.ExistsContainer; -import org.sadtech.haiti.core.service.AbstractSimpleManagerService; -import org.springframework.context.annotation.Lazy; -import org.springframework.core.convert.ConversionService; -import org.springframework.stereotype.Service; - -import java.time.LocalDateTime; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -@Service -public class NoteServiceImpl extends AbstractSimpleManagerService implements NoteService { - - private static final Pattern PATTERN = Pattern.compile("@[\\w]+"); - - private final CommentRepository commentRepository; - private final NotifyService notifyService; - private final TaskService taskService; - - private final ConversionService conversionService; - - public NoteServiceImpl( - CommentRepository commentRepository, - NotifyService notifyService, - @Lazy TaskService taskService, - ConversionService conversionService - ) { - super(commentRepository); - this.commentRepository = commentRepository; - this.notifyService = notifyService; - this.taskService = taskService; - this.conversionService = conversionService; - } - - @Override - public Long getLastCommentId() { - return commentRepository.findFirstByOrderByIdDesc().map(Note::getId).orElse(0L); - } - - @Override - public List getAllBetweenDate(@NonNull LocalDateTime dateFrom, LocalDateTime dateTo) { - return commentRepository.findByCreateDateBetween(dateFrom, dateTo); - } - - @Override - public Note create(@NonNull Note note) { - final Note newNote = commentRepository.save(note); - notificationPersonal(note); - return newNote; - } - - private void notificationPersonal(@NonNull Note note) { - Matcher matcher = PATTERN.matcher(note.getBody()); - Set recipientsLogins = new HashSet<>(); - while (matcher.find()) { - final String login = matcher.group(0).replace("@", ""); - recipientsLogins.add(login); - } - notifyService.send( - CommentNotify.builder() -// .authorName(note.getAuthor()) -// .url(note.getUrl()) -// .message(note.getMessage()) - .build() - ); - } - - @Override - public Note update(Note note) { - final Note oldNote = commentRepository.findById(note.getId()) - .orElseThrow(() -> new NotFoundException("Комментарий не найден")); - - updateAnswer(oldNote, note); - - return commentRepository.save(oldNote); - } - - @Override - public List getAllById(@NonNull Set ids) { - return commentRepository.findAllById(ids); - } - - @Override - public Note convert(@NonNull Task task) { - taskService.deleteById(task.getId()); - final Note note = conversionService.convert(task, Note.class); - return commentRepository.save(note); - } - - @Override - public Set existsById(@NonNull Set ids) { - return commentRepository.existsById(ids); - } - - private void updateAnswer(Note oldNote, Note newNote) { -// final Set oldAnswerIds = oldNote.getAnswers(); -// final Set newAnswerIds = newNote.getAnswers(); -// if (!oldAnswerIds.equals(newAnswerIds)) { -// final Set existsNewAnswersIds = commentRepository.existsById(newAnswerIds); -// final List newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream() -// .filter(comment -> !oldAnswerIds.contains(comment.getId())) -// .collect(Collectors.toList()); -// oldNote.getAnswers().clear(); -// oldNote.setAnswers(existsNewAnswersIds); -// if (!newAnswers.isEmpty()) { -// notifyService.send( -// AnswerCommentNotify.builder() -// .url(oldNote.getUrl()) -// .youMessage(newNote.getMessage()) -// .answers( -// newAnswers.stream() -// .map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage())) -// .collect(Collectors.toList()) -// ) -// .build() -// ); -// } -// } - } - - @Override - public ExistsContainer existsById(@NonNull Collection collection) { - return null; - } - -} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/TaskServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/TaskServiceImpl.java deleted file mode 100644 index 74cb158..0000000 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/TaskServiceImpl.java +++ /dev/null @@ -1,176 +0,0 @@ -package org.sadtech.bot.gitlab.core.service.impl; - -//@Service -//public class TaskServiceImpl extends AbstractSimpleManagerService implements TaskService { -// -// private static final Pattern PATTERN = Pattern.compile("@[\\w]+"); -// -// private final TaskRepository taskRepository; -// -// private final MergeRequestsService mergeRequestsService; -// private final NotifyService notifyService; -// private final NoteService noteService; -// -// private final ConversionService conversionService; -// -// public TaskServiceImpl( -// TaskRepository taskRepository, -// MergeRequestsService mergeRequestsService, -// NotifyService notifyService, -// NoteService noteService, -// ConversionService conversionService -// ) { -// super(taskRepository); -// this.taskRepository = taskRepository; -// this.mergeRequestsService = mergeRequestsService; -// this.notifyService = notifyService; -// this.noteService = noteService; -// this.conversionService = conversionService; -// } -// -// @Override -// public Task create(@NonNull Task task) { -// Assert.isNotNull(task.getId(), "При создании объекта должен быть установлен идентификатор"); -// task.getAnswers().clear(); -// final Task newTask = taskRepository.save(task); -// notifyNewTask(task); -// notificationPersonal(task); -// return newTask; -// } -// -// @Override -// public Task update(@NonNull Task task) { -// final Task oldTask = taskRepository.findById(task.getId()) -// .orElseThrow(() -> new NotFoundException("Задача не найдена")); -// -// if (!task.getBitbucketVersion().equals(oldTask.getBitbucketVersion())) { -// oldTask.setDescription(task.getDescription()); -// oldTask.setBitbucketVersion(task.getBitbucketVersion()); -// } -// updateAnswer(oldTask, task); -// updateStatus(oldTask, task); -// oldTask.setStatus(task.getStatus()); -// return taskRepository.save(oldTask); -// } -// -// private void updateStatus(Task oldTask, Task task) { -// final TaskStatus oldStatus = oldTask.getStatus(); -// final TaskStatus newStatus = task.getStatus(); -// if (!oldStatus.equals(newStatus)) { -// switch (newStatus) { -// case OPEN: -// notifyService.send( -// TaskNewNotify.builder() -// .messageTask(task.getDescription()) -// .authorName(oldTask.getAuthor()) -// .url(oldTask.getUrl()) -// .build() -// ); -// break; -// case RESOLVED: -// notifyService.send( -// TaskCloseNotify.builder() -// .messageTask(oldTask.getDescription()) -// .authorName(oldTask.getAuthor()) -// .url(oldTask.getUrl()) -// .build() -// ); -// break; -// default: -// throw new NotFoundException("Обработчика типа не существует"); -// } -// oldTask.setStatus(newStatus); -// } -// } -// -// private void updateAnswer(Task oldTask, Task task) { -//// final Set oldAnswerIds = oldTask.getAnswers(); -//// final Set newAnswerIds = task.getAnswers(); -//// if (!oldAnswerIds.equals(newAnswerIds)) { -//// final Set existsNewAnswersIds = noteService.existsById(newAnswerIds); -//// final List newAnswers = noteService.getAllById(existsNewAnswersIds).stream() -//// .filter(comment -> !oldAnswerIds.contains(comment.getId())) -//// .collect(Collectors.toList()); -//// oldTask.getAnswers().clear(); -//// oldTask.setAnswers(existsNewAnswersIds); -//// if (!newAnswers.isEmpty()) { -//// notifyService.send( -//// AnswerCommentNotify.builder() -//// .url(oldTask.getUrl()) -//// .youMessage(oldTask.getDescription()) -//// .answers( -//// newAnswers.stream() -//// .map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage())) -//// .collect(Collectors.toList()) -//// ) -//// .build() -//// ); -//// } -//// } -// } -// -// @Override -// public Long getLastTaskId() { -// return taskRepository.findFirstByOrderByIdDesc().map(Task::getId).orElse(0L); -// } -// -// @Override -// public Task convert(@NonNull Note note) { -// noteService.deleteById(note.getId()); -// final Task task = conversionService.convert(note, Task.class); -// final Task newTask = taskRepository.save(task); -// notifyNewTask(newTask); -// return newTask; -// } -// -// @Override -// public List getAllBetweenDate(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo) { -// return taskRepository.findByCreateDateBetween(dateFrom, dateTo); -// } -// -// @Override -// public List getAllByResponsibleAndStatus(@NonNull String login, @NonNull TaskStatus status) { -// return taskRepository.findAllByResponsibleAndStatus(login, status); -// } -// -// private void notifyNewTask(Task task) { -// final MergeRequest mergeRequest = mergeRequestsService.getById(task.getPullRequestId()) -// .orElseThrow(() -> new NotFoundException("ПР не найден")); -// -// notifyService.send( -// TaskNewNotify.builder() -// .authorName(task.getAuthor()) -// .messageTask(task.getDescription()) -// .url(task.getUrl()) -//// .recipients(Collections.singleton(mergeRequest.getAuthor().getId())) -// .build() -// ); -// } -// -// private void notificationPersonal(@NonNull Task task) { -// Matcher matcher = PATTERN.matcher(task.getDescription()); -// Set recipientsLogins = new HashSet<>(); -// while (matcher.find()) { -// final String login = matcher.group(0).replace("@", ""); -// recipientsLogins.add(login); -// } -// notifyService.send( -// CommentNotify.builder() -// .authorName(task.getAuthor()) -// .url(task.getUrl()) -// .message(task.getDescription()) -// .build() -// ); -// } -// -// @Override -// public void deleteById(@NonNull Long id) { -// super.deleteById(id); -// } -// -// @Override -// public ExistsContainer existsById(@NonNull Collection collection) { -// return null; -// } -// -//} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/AbstractNoteService.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/AbstractNoteService.java new file mode 100644 index 0000000..52083e0 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/AbstractNoteService.java @@ -0,0 +1,56 @@ +package org.sadtech.bot.gitlab.core.service.impl.note; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PersonInformation; +import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.bot.gitlab.context.domain.notify.comment.CommentNotify; +import org.sadtech.bot.gitlab.context.service.NotifyService; +import org.sadtech.haiti.context.repository.SimpleManagerRepository; +import org.sadtech.haiti.core.service.AbstractSimpleManagerService; + +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public abstract class AbstractNoteService extends AbstractSimpleManagerService { + + protected static final Pattern PATTERN = Pattern.compile("@[\\w]+"); + + private final NotifyService notifyService; + private final PersonInformation personInformation; + + protected AbstractNoteService( + SimpleManagerRepository repository, + NotifyService notifyService, + PersonInformation personInformation + ) { + super(repository); + this.notifyService = notifyService; + this.personInformation = personInformation; + } + + protected void notificationPersonal(@NonNull Note note) { + Matcher matcher = PATTERN.matcher(note.getBody()); + Set recipientsLogins = new HashSet<>(); + while (matcher.find()) { + final String login = matcher.group(0).replace("@", ""); + recipientsLogins.add(login); + } + if (recipientsLogins.contains(personInformation.getUsername())) { + notifyService.send( + CommentNotify.builder() + .authorName(note.getAuthor().getName()) + .message(note.getBody()) + .url(note.getWebUrl()) + .build() + ); + } + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/NoteServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/NoteServiceImpl.java new file mode 100644 index 0000000..fc86857 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/NoteServiceImpl.java @@ -0,0 +1,73 @@ +package org.sadtech.bot.gitlab.core.service.impl.note; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PersonInformation; +import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.bot.gitlab.context.exception.NotFoundException; +import org.sadtech.bot.gitlab.context.repository.NoteRepository; +import org.sadtech.bot.gitlab.context.service.NoteService; +import org.sadtech.bot.gitlab.context.service.NotifyService; +import org.springframework.stereotype.Service; + +@Service +public class NoteServiceImpl extends AbstractNoteService implements NoteService { + + private final NoteRepository noteRepository; + + public NoteServiceImpl( + NoteRepository noteRepository, + NotifyService notifyService, + PersonInformation personInformation + ) { + super(noteRepository, notifyService, personInformation); + this.noteRepository = noteRepository; + } + + @Override + public Note create(@NonNull Note note) { + final Note newNote = noteRepository.save(note); + notificationPersonal(note); + return newNote; + } + + @Override + public Note update(@NonNull Note note) { + final Note oldNote = noteRepository.findById(note.getId()) + .orElseThrow(() -> new NotFoundException("Комментарий не найден")); + + if (!oldNote.getUpdated().equals(note.getUpdated())) { + note.setWebUrl(oldNote.getWebUrl()); + return noteRepository.save(oldNote); + } +// updateAnswer(oldNote, note); + + return oldNote; + } + + private void updateAnswer(Note oldNote, Note newNote) { +// final Set oldAnswerIds = oldNote.getAnswers(); +// final Set newAnswerIds = newNote.getAnswers(); +// if (!oldAnswerIds.equals(newAnswerIds)) { +// final Set existsNewAnswersIds = commentRepository.existsById(newAnswerIds); +// final List newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream() +// .filter(comment -> !oldAnswerIds.contains(comment.getId())) +// .collect(Collectors.toList()); +// oldNote.getAnswers().clear(); +// oldNote.setAnswers(existsNewAnswersIds); +// if (!newAnswers.isEmpty()) { +// notifyService.send( +// AnswerCommentNotify.builder() +// .url(oldNote.getUrl()) +// .youMessage(newNote.getMessage()) +// .answers( +// newAnswers.stream() +// .map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage())) +// .collect(Collectors.toList()) +// ) +// .build() +// ); +// } +// } + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/TaskServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/TaskServiceImpl.java new file mode 100644 index 0000000..aad8405 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/note/TaskServiceImpl.java @@ -0,0 +1,89 @@ +package org.sadtech.bot.gitlab.core.service.impl.note; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PersonInformation; +import org.sadtech.bot.gitlab.context.domain.entity.Task; +import org.sadtech.bot.gitlab.context.domain.notify.task.TaskCloseNotify; +import org.sadtech.bot.gitlab.context.domain.notify.task.TaskNewNotify; +import org.sadtech.bot.gitlab.context.repository.TaskRepository; +import org.sadtech.bot.gitlab.context.service.NotifyService; +import org.sadtech.bot.gitlab.context.service.TaskService; +import org.sadtech.haiti.context.exception.NotFoundException; +import org.springframework.stereotype.Service; + +@Service +public class TaskServiceImpl extends AbstractNoteService implements TaskService { + + private final TaskRepository taskRepository; + private final NotifyService notifyService; + private final PersonInformation personInformation; + + public TaskServiceImpl( + TaskRepository taskRepository, + NotifyService notifyService, + PersonInformation personInformation) { + super(taskRepository, notifyService, personInformation); + this.taskRepository = taskRepository; + this.notifyService = notifyService; + this.personInformation = personInformation; + } + + @Override + public Task create(@NonNull Task task) { + final Task newTask = taskRepository.save(task); + notifyNewTask(task); + notificationPersonal(task); + return newTask; + } + + @Override + public Task update(@NonNull Task task) { + final Task oldTask = taskRepository.findById(task.getId()) + .orElseThrow(() -> new NotFoundException("Задача не найдена")); + + if (oldTask.getUpdated().equals(task.getUpdated())) { + + task.setWebUrl(oldTask.getWebUrl()); + task.setResponsible(oldTask.getResponsible()); + + notifyUpdateStatus(oldTask, task); + + return taskRepository.save(oldTask); + } + return oldTask; + } + + private void notifyUpdateStatus(Task oldTask, Task task) { + if ( + personInformation.getId().equals(oldTask.getAuthor().getId()) + && !personInformation.getId().equals(oldTask.getResolvedBy().getId()) + + ) { + final boolean oldStatus = oldTask.getResolved(); + final boolean newStatus = task.getResolved(); + if (!oldStatus && newStatus) { + notifyService.send( + TaskCloseNotify.builder() + .authorName(task.getAuthor().getName()) + .messageTask(task.getBody()) + .url(task.getWebUrl()) + .build() + ); + } + } + } + + private void notifyNewTask(Task task) { + if (personInformation.getId().equals(task.getResponsible().getId()) + && !personInformation.getId().equals(task.getAuthor().getId())) { + notifyService.send( + TaskNewNotify.builder() + .authorName(task.getAuthor().getName()) + .messageTask(task.getBody()) + .url(task.getWebUrl()) + .build() + ); + } + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/NoteParser.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/NoteParser.java index 6c0e00c..940d80e 100644 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/NoteParser.java +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/NoteParser.java @@ -3,11 +3,11 @@ package org.sadtech.bot.gitlab.core.service.parser; import lombok.NonNull; import org.sadtech.bot.gitlab.context.domain.entity.MergeRequest; import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.sadtech.bot.gitlab.context.service.MergeRequestsService; import org.sadtech.bot.gitlab.context.service.NoteService; -import org.sadtech.bot.gitlab.context.service.ProjectService; +import org.sadtech.bot.gitlab.context.service.TaskService; import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty; -import org.sadtech.bot.gitlab.core.config.properties.InitProperty; import org.sadtech.bot.gitlab.core.config.properties.PersonProperty; import org.sadtech.bot.gitlab.sdk.domain.NoteJson; import org.sadtech.haiti.context.domain.ExistsContainer; @@ -35,50 +35,40 @@ public class NoteParser { public static final int COUNT = 100; - private final ProjectService projectService; private final MergeRequestsService mergeRequestsService; private final ConversionService conversionService; private final GitlabProperty gitlabProperty; - private final InitProperty initProperty; private final PersonProperty personProperty; private final NoteService noteService; + private final TaskService taskService; public NoteParser( - ProjectService projectService, MergeRequestsService mergeRequestsService, + MergeRequestsService mergeRequestsService, ConversionService conversionService, GitlabProperty gitlabProperty, - InitProperty initProperty, PersonProperty personProperty, - NoteService noteService + NoteService noteService, + TaskService taskService ) { - this.projectService = projectService; this.mergeRequestsService = mergeRequestsService; this.conversionService = conversionService; this.gitlabProperty = gitlabProperty; - this.initProperty = initProperty; this.personProperty = personProperty; this.noteService = noteService; + this.taskService = taskService; } public void scanNewCommentAndTask() { - int page = 1; + int page = 0; Sheet mergeRequestSheet = mergeRequestsService.getAll(PaginationImpl.of(page, COUNT)); while (mergeRequestSheet.hasContent()) { final List mergeRequests = mergeRequestSheet.getContent(); for (MergeRequest mergeRequest : mergeRequests) { - final List noteJsons = HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPullRequestComment(), mergeRequest.getProjectId(), mergeRequest.getTwoId())) - .header(ACCEPT) - .header(AUTHORIZATION, BEARER + personProperty.getToken()) - .executeList(NoteJson.class) - .stream() - .filter(noteJson -> !noteJson.isSystem()) - .collect(Collectors.toList()); - createNewComment(noteJsons); - createNewTask(noteJsons); + processingMergeRequest(mergeRequest); } @@ -87,7 +77,56 @@ public class NoteParser { } - private void createNewComment(List noteJsons) { + private void processingMergeRequest(MergeRequest mergeRequest) { + int page = 1; + List noteJsons = getNoteJson(mergeRequest, page); + + while (!noteJsons.isEmpty()) { + + createNewComment(noteJsons, mergeRequest); + createNewTask(noteJsons, mergeRequest); + + noteJsons = getNoteJson(mergeRequest, ++page); + } + } + + private List getNoteJson(MergeRequest mergeRequest, int page) { + return HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPullRequestComment(), mergeRequest.getProjectId(), mergeRequest.getTwoId(), page)) + .header(ACCEPT) + .header(AUTHORIZATION, BEARER + personProperty.getToken()) + .executeList(NoteJson.class) + .stream() + .filter(noteJson -> !noteJson.isSystem()) + .collect(Collectors.toList()); + } + + private void createNewTask(List noteJsons, MergeRequest mergeRequest) { + final List newJsons = noteJsons.stream() + .filter(json -> json.getType() != null) + .collect(Collectors.toList()); + + final Set jsonIds = newJsons.stream() + .map(NoteJson::getId) + .collect(Collectors.toSet()); + + final ExistsContainer existsContainer = taskService.existsById(jsonIds); + + if (!existsContainer.isAllFound()) { + final List newNotes = newJsons.stream() + .filter(json -> existsContainer.getIdNoFound().contains(json.getId())) + .map(json -> conversionService.convert(json, Task.class)) + .peek(task -> { + task.setWebUrl(MessageFormat.format(gitlabProperty.getUrlNote(), mergeRequest.getWebUrl(), task.getId())); + task.setResponsible(mergeRequest.getAuthor()); + } + ) + .collect(Collectors.toList()); + + taskService.createAll(newNotes); + } + } + + private void createNewComment(List noteJsons, MergeRequest mergeRequest) { final List newJsons = noteJsons.stream() .filter(json -> json.getType() == null) .collect(Collectors.toList()); @@ -102,6 +141,9 @@ public class NoteParser { final List newNotes = newJsons.stream() .filter(json -> existsContainer.getIdNoFound().contains(json.getId())) .map(json -> conversionService.convert(json, Note.class)) + .peek(note -> note.setWebUrl( + MessageFormat.format(gitlabProperty.getUrlNote(), mergeRequest.getWebUrl(), note.getId())) + ) .collect(Collectors.toList()); noteService.createAll(newNotes); diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/CommentRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/CommentRepositoryImpl.java deleted file mode 100644 index eed6766..0000000 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/CommentRepositoryImpl.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.sadtech.bot.gitlab.data.impl; - -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.entity.Note; -import org.sadtech.bot.gitlab.context.repository.CommentRepository; -import org.sadtech.bot.gitlab.data.jpa.CommentRepositoryJpa; -import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository; - -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -/** - * // TODO: 08.09.2020 Добавить описание. - * - * @author upagge 08.09.2020 - */ -//@Repository -public class CommentRepositoryImpl extends AbstractSimpleManagerRepository implements CommentRepository { - - private final CommentRepositoryJpa repositoryJpa; - - public CommentRepositoryImpl(CommentRepositoryJpa repositoryJpa) { - super(repositoryJpa); - this.repositoryJpa = repositoryJpa; - } - - @Override - public Optional findFirstByOrderByIdDesc() { - return repositoryJpa.findFirstByOrderByIdDesc(); - } - - @Override - public List findByCreateDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo) { - return repositoryJpa.findByCreateDateBetween(dateFrom, dateTo); - } - - @Override - public List findAllById(@NonNull Set ids) { - return repositoryJpa.findAllById(ids); - } - - @Override - public Set existsById(Set ids) { - return repositoryJpa.existsAllById(ids); - } - -} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/NoteRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/NoteRepositoryImpl.java new file mode 100644 index 0000000..862b488 --- /dev/null +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/NoteRepositoryImpl.java @@ -0,0 +1,24 @@ +package org.sadtech.bot.gitlab.data.impl; + +import org.sadtech.bot.gitlab.context.domain.entity.Note; +import org.sadtech.bot.gitlab.context.repository.NoteRepository; +import org.sadtech.bot.gitlab.data.jpa.CommentRepositoryJpa; +import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository; +import org.springframework.stereotype.Repository; + +/** + * // TODO: 08.09.2020 Добавить описание. + * + * @author upagge 08.09.2020 + */ +@Repository +public class NoteRepositoryImpl extends AbstractSimpleManagerRepository implements NoteRepository { + + private final CommentRepositoryJpa repositoryJpa; + + public NoteRepositoryImpl(CommentRepositoryJpa repositoryJpa) { + super(repositoryJpa); + this.repositoryJpa = repositoryJpa; + } + +} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/TaskRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/TaskRepositoryImpl.java index 0221ec2..78b5468 100644 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/TaskRepositoryImpl.java +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/TaskRepositoryImpl.java @@ -1,17 +1,12 @@ package org.sadtech.bot.gitlab.data.impl; -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.TaskStatus; import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.sadtech.bot.gitlab.context.repository.TaskRepository; import org.sadtech.bot.gitlab.data.jpa.TaskRepositoryJpa; import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository; +import org.springframework.stereotype.Repository; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; - -//@Repository +@Repository public class TaskRepositoryImpl extends AbstractSimpleManagerRepository implements TaskRepository { private final TaskRepositoryJpa taskRepositoryJpa; @@ -21,19 +16,4 @@ public class TaskRepositoryImpl extends AbstractSimpleManagerRepository findFirstByOrderByIdDesc() { - return taskRepositoryJpa.findFirstByOrderByIdDesc(); - } - - @Override - public List findByCreateDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo) { - return taskRepositoryJpa.findByCreateDateBetween(dateFrom, dateTo); - } - - @Override - public List findAllByResponsibleAndStatus(@NonNull String responsibleLogin, @NonNull TaskStatus status) { - return taskRepositoryJpa.findAllByResponsibleAndStatus(responsibleLogin, status); - } - } diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/CommentRepositoryJpa.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/CommentRepositoryJpa.java index 86c9676..c77046f 100644 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/CommentRepositoryJpa.java +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/CommentRepositoryJpa.java @@ -1,23 +1,8 @@ package org.sadtech.bot.gitlab.data.jpa; -import lombok.NonNull; import org.sadtech.bot.gitlab.context.domain.entity.Note; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.NoRepositoryBean; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -@NoRepositoryBean public interface CommentRepositoryJpa extends JpaRepository { - Optional findFirstByOrderByIdDesc(); - - List findByCreateDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - - // @Query("SELECT c.id FROM Comment c WHERE c.id IN :ids") - Set existsAllById(@NonNull Set ids); - } diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/TaskRepositoryJpa.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/TaskRepositoryJpa.java index 6fe2d17..8e9ce37 100644 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/TaskRepositoryJpa.java +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/TaskRepositoryJpa.java @@ -1,22 +1,8 @@ package org.sadtech.bot.gitlab.data.jpa; -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.TaskStatus; import org.sadtech.bot.gitlab.context.domain.entity.Task; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.NoRepositoryBean; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; - -@NoRepositoryBean public interface TaskRepositoryJpa extends JpaRepository { - Optional findFirstByOrderByIdDesc(); - - List findByCreateDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - - List findAllByResponsibleAndStatus(String login, TaskStatus taskStatus); - } diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/NoteJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/NoteJson.java index 62c2b03..16d3ebc 100644 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/NoteJson.java +++ b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/NoteJson.java @@ -1,6 +1,10 @@ package org.sadtech.bot.gitlab.sdk.domain; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import lombok.Data; import java.time.LocalDateTime; @@ -12,9 +16,13 @@ public class NoteJson { private String type; private String body; + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonProperty("created_at") private LocalDateTime created; + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonProperty("updated_at") private LocalDateTime updated; @@ -27,7 +35,7 @@ public class NoteJson { @JsonProperty("noteable_type") private String noteableType; - private Boolean resolveable; + private Boolean resolvable; private Boolean resolved;