diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java index 08d1e4f..eab26ca 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/DiscussionParser.java @@ -11,8 +11,8 @@ import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty; import dev.struchkov.bot.gitlab.sdk.domain.DiscussionJson; import dev.struchkov.haiti.utils.network.HttpParse; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.convert.ConversionService; import org.springframework.stereotype.Component; @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.ForkJoinPool; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -30,7 +31,6 @@ import java.util.stream.Stream; import static dev.struchkov.bot.gitlab.core.utils.StringUtils.H_PRIVATE_TOKEN; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Checker.checkNull; import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; /** @@ -40,16 +40,34 @@ import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; */ @Slf4j @Component -@RequiredArgsConstructor public class DiscussionParser { private final DiscussionService discussionService; + private final MergeRequestsService mergeRequestsService; private final ConversionService conversionService; + private final ForkJoinPool forkJoinPool; private final GitlabProperty gitlabProperty; + private final PersonProperty personProperty; + public DiscussionParser( + DiscussionService discussionService, + MergeRequestsService mergeRequestsService, + ConversionService conversionService, + @Qualifier("parserPool") ForkJoinPool forkJoinPool, + GitlabProperty gitlabProperty, + PersonProperty personProperty + ) { + this.discussionService = discussionService; + this.mergeRequestsService = mergeRequestsService; + this.conversionService = conversionService; + this.forkJoinPool = forkJoinPool; + this.gitlabProperty = gitlabProperty; + this.personProperty = personProperty; + } + /** * Поиск новых обсуждений */ @@ -148,23 +166,14 @@ public class DiscussionParser { log.debug("Старт обработки старых дискуссий"); final List discussions = discussionService.getAll(); - // Удаляем обсуждения, которые потеряли свои MR - //TODO [05.12.2022|uPagge]: Проверить целесообразность этого действия - discussions.stream() - .filter(discussion -> checkNull(discussion.getMergeRequest())) - .map(Discussion::getId) - .forEach(discussionService::deleteById); - final List newDiscussions = new ArrayList<>(); for (Discussion discussion : discussions) { - if (checkNotNull(discussion.getMergeRequest())) { - getOldDiscussionJson(discussion) - .map(json -> { - final Discussion newDiscussion = conversionService.convert(json, Discussion.class); - newDiscussion.getNotes().forEach(createNoteLink(discussion.getMergeRequest())); - return newDiscussion; - }).ifPresent(newDiscussions::add); - } + getOldDiscussionJson(discussion) + .map(json -> { + final Discussion newDiscussion = conversionService.convert(json, Discussion.class); + newDiscussion.getNotes().forEach(createNoteLink(discussion.getMergeRequest())); + return newDiscussion; + }).ifPresent(newDiscussions::add); } if (checkNotEmpty(newDiscussions)) { diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java new file mode 100644 index 0000000..660a73d --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllDiscussionForMergeRequestTask.java @@ -0,0 +1,49 @@ +package dev.struchkov.bot.gitlab.core.service.parser.forktask; + +import dev.struchkov.bot.gitlab.sdk.domain.DiscussionJson; +import dev.struchkov.haiti.utils.network.HttpParse; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; + +import java.text.MessageFormat; +import java.util.List; +import java.util.concurrent.RecursiveTask; + +import static dev.struchkov.bot.gitlab.core.utils.StringUtils.H_PRIVATE_TOKEN; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; +import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; + +@AllArgsConstructor +@RequiredArgsConstructor +public class GetAllDiscussionForMergeRequestTask extends RecursiveTask> { + + private static final int PAGE_COUNT = 100; + + private final String discussionsUrl; + private final long projectId; + private final long mergeRequestTwoId; + private final String personalGitlabToken; + private int page = 0; + + @Override + @SneakyThrows + protected List compute() { + Thread.sleep(100); + final List jsons = getDiscussionJson(); + if (checkNotEmpty(jsons) && jsons.size() == PAGE_COUNT) { + final var newTask = new GetAllDiscussionForMergeRequestTask(discussionsUrl, projectId, mergeRequestTwoId, personalGitlabToken, page + 1); + newTask.fork(); + jsons.addAll(newTask.join()); + } + return jsons; + } + + private List getDiscussionJson() { + return HttpParse.request(MessageFormat.format(discussionsUrl, projectId, mergeRequestTwoId, page, PAGE_COUNT)) + .header(ACCEPT) + .header(H_PRIVATE_TOKEN, personalGitlabToken) + .executeList(DiscussionJson.class); + } + +} diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java index 4013a45..fd77edb 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetAllMergeRequestForProjectTask.java @@ -12,6 +12,7 @@ import java.text.MessageFormat; import java.util.List; import java.util.concurrent.RecursiveTask; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; @Slf4j @@ -31,7 +32,7 @@ public class GetAllMergeRequestForProjectTask extends RecursiveTask compute() { Thread.sleep(100); final List mergeRequestJsons = getMergeRequestJsons(); - if (mergeRequestJsons.size() == PAGE_COUNT) { + if (checkNotEmpty(mergeRequestJsons) && mergeRequestJsons.size() == PAGE_COUNT) { final GetAllMergeRequestForProjectTask newTask = new GetAllMergeRequestForProjectTask(projectId, pageNumber + 1, urlMrOpen, gitlabToken); newTask.fork(); mergeRequestJsons.addAll(newTask.join()); diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index cc1d5c0..b69a3cd 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -35,8 +35,8 @@ gitlab-bot: gitlab: base-url: ${GITLAB_URL} replaceUrl: ${GITLAB_REPLACE_URL} - users-url: ${GITLAB_URL}/api/v4/users - user-url: ${GITLAB_URL}/api/v4/user + users-url: "${GITLAB_URL}/api/v4/users" + user-url: "${GITLAB_URL}/api/v4/user" projects-url: "${GITLAB_URL}/api/v4/projects?page={0, number, integer}&per_page=100" open-merge-requests-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests?state=opened&page={1, number, integer}&per_page={2, number, integer}" close-merge-requests-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests?state=closed&page={1, number, integer}&per_page=100"