diff --git a/bot-app/Dockerfile b/bot-app/Dockerfile index 2ffcf9c..2a35ece 100644 --- a/bot-app/Dockerfile +++ b/bot-app/Dockerfile @@ -1,6 +1,24 @@ FROM openjdk:8-jdk-alpine -MAINTAINER uPagge -RUN addgroup -S bitbucketbot && adduser -S bitbucketbot -G bitbucketbot -USER bitbucketbot:bitbucketbot -COPY target/bitbucketbot.jar app.jar -ENTRYPOINT ["java", "-jar", "", "app.jar"] \ No newline at end of file +MAINTAINER uPagge +RUN apk add --no-cache bash && \ + addgroup -S gitlabbot && \ + adduser -S gitlabbot -G gitlabbot && \ + mkdir -p /bot && \ + chown -R gitlabbot:gitlabbot /bot +WORKDIR /bot +USER gitlabbot:gitlabbot +COPY target/gitlab-notification.jar app.jar +VOLUME logs +ENV TELEGRAM_PERSON_ID=TELEGRAM_PERSON_ID DATASOURCE_URL=DATASOURCE_URL \ + DATASOURCE_PASSWORD=DATASOURCE_PASSWORD DATASOURCE_USERNAME=DATASOURCE_USERNAME \ + GITLAB_PERSONAL_TOKEN=GITLAB_PERSONAL_TOKEN TELEGRAM_BOT_TOKEN=TELEGRAM_BOT_TOKEN \ + TELEGRAM_BOT_USERNAME=TELEGRAM_BOT_USERNAME GITLAB_URL=GITLAB_URL +ENTRYPOINT java -DTELEGRAM_BOT_USERNAME=${TELEGRAM_BOT_USERNAME} \ + -DTELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN \ + -DTELEGRAM_PERSON_ID=$TELEGRAM_PERSON_ID \ + -DDATASOURCE_URL=$DATASOURCE_URL \ + -DDATASOURCE_PASSWORD=$DATASOURCE_PASSWORD \ + -DDATASOURCE_USERNAME=$DATASOURCE_USERNAME \ + -DGITLAB_PERSONAL_TOKEN=$GITLAB_PERSONAL_TOKEN \ + -DGITLAB_URL=$GITLAB_URL \ + -jar app.jar \ No newline at end of file diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/PullRequestParserScheduler.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/PullRequestParserScheduler.java deleted file mode 100644 index eb7b752..0000000 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/scheduler/PullRequestParserScheduler.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.sadtech.bot.gitlab.app.scheduler; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -/** - * Позволяет задать время парсинга для ПРов. - * - * @author upagge 06.09.2020 - */ -@Slf4j -@Service -@RequiredArgsConstructor -public class PullRequestParserScheduler { - -// private final PullRequestParser pullRequestParser; -// -// @Scheduled(cron = "0 */1 * * * *") -// public void parsingOldPullRequest() { -// pullRequestParser.parsingOldPullRequest(); -// } -// -// @Scheduled(cron = "0 */1 * * * *") -// public void parsingNewPullRequest() { -// pullRequestParser.parsingNewPullRequest(); -// } - -} 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 cc42b15..2daee25 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 @@ -30,4 +30,9 @@ public class SchedulerService { mergeRequestParser.parsingNewMergeRequest(); } + @Scheduled(cron = "*/30 * * * * *") + public void oldMergeRequest() { + mergeRequestParser.parsingOldMergeRequest(); + } + } diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/MergeRequestParser.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/MergeRequestParser.java index ec8b415..80dfe36 100644 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/MergeRequestParser.java +++ b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/MergeRequestParser.java @@ -2,6 +2,7 @@ package org.sadtech.bot.gitlab.app.service.parser; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.sadtech.bot.gitlab.context.domain.IdAndStatusPr; import org.sadtech.bot.gitlab.context.domain.MergeRequestState; import org.sadtech.bot.gitlab.context.domain.entity.MergeRequest; import org.sadtech.bot.gitlab.context.domain.entity.Project; @@ -11,6 +12,7 @@ import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty; import org.sadtech.bot.gitlab.core.config.properties.PersonProperty; import org.sadtech.bot.gitlab.sdk.domain.MergeRequestJson; import org.sadtech.haiti.context.domain.ExistsContainer; +import org.sadtech.haiti.context.exception.NotFoundException; import org.sadtech.haiti.context.page.Sheet; import org.sadtech.haiti.core.page.PaginationImpl; import org.sadtech.haiti.utils.network.HttpHeader; @@ -45,8 +47,19 @@ public class MergeRequestParser { private final ConversionService conversionService; private final PersonProperty personProperty; - public void parsingOldPullRequest() { -// processingOldPullRequests(gitlabProperty.getUrlPullRequestOpen(), gitlabProperty.getUrlPullRequestClose()); + public void parsingOldMergeRequest() { + final Set existIds = mergeRequestsService.getAllId(OLD_STATUSES); + + for (IdAndStatusPr existId : existIds) { + final MergeRequest mergeRequest = HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPullRequest(), existId.getProjectId(), existId.getTwoId())) + .header(ACCEPT) + .header(AUTHORIZATION, BEARER + personProperty.getToken()) + .execute(MergeRequestJson.class) + .map(json -> conversionService.convert(json, MergeRequest.class)) + .orElseThrow(() -> new NotFoundException("МержРеквест не найден, возможно удален")); + mergeRequestsService.update(mergeRequest); + } + } public void parsingNewMergeRequest() { @@ -88,51 +101,4 @@ public class MergeRequestParser { } -// private Set getExistsPullRequestIds(String bitbucketUrl) { -// Optional sheetJson = Utils.urlToJson(url, token, PullRequestSheetJson.class); -// Set ids = new HashSet<>(); -// while (sheetJson.isPresent() && sheetJson.get().hasContent()) { -// final PullRequestSheetJson jsonSheet = sheetJson.get(); -// final List existsPr = getExistsPr(jsonSheet.getValues()); -// -// ids.addAll( -// pullRequestsService.updateAll(existsPr).stream() -// .map(PullRequest::getId) -// .collect(Collectors.toSet()) -// ); -// -// if (jsonSheet.getNextPageStart() != null) { -// sheetJson = Utils.urlToJson(url + jsonSheet.getNextPageStart(), token, PullRequestSheetJson.class); -// } else { -// break; -// } -// } -// return ids; -// } -// -// public void processingOldPullRequests(@NonNull String urlPullRequestOpen, @NonNull String urlPullRequestClose) { -// final Set existsId = pullRequestsService.getAllId(OLD_STATUSES).stream() -// .map(IdAndStatusPr::getId) -// .collect(Collectors.toSet()); -// final Set openId = getExistsPullRequestIds(urlPullRequestOpen); -// final Set closeId = getExistsPullRequestIds(urlPullRequestClose); -// final Set newNotExistsId = existsId.stream() -// .filter(id -> !openId.contains(id) && !closeId.contains(id)) -// .collect(Collectors.toSet()); -// log.info("Открыты: " + Arrays.toString(openId.toArray())); -// log.info("Закрыты: " + Arrays.toString(closeId.toArray())); -// log.info("Не найдены: " + Arrays.toString(newNotExistsId.toArray())); -// if (!newNotExistsId.isEmpty() && !openId.isEmpty()) { -// pullRequestsService.deleteAllById(newNotExistsId); -// } -// } -// -// private List getExistsPr(@NonNull List pullRequestJsons) { -// return pullRequestJsons.stream() -// .filter(json -> pullRequestsService.exists(bitbucketIdAndPullRequestId(json))) -// .map(json -> conversionService.convert(json, PullRequest.class)) -// .collect(Collectors.toList()); -// } -// - } diff --git a/bot-app/src/main/resources/application.yaml b/bot-app/src/main/resources/application.yaml index 7709810..629cea9 100644 --- a/bot-app/src/main/resources/application.yaml +++ b/bot-app/src/main/resources/application.yaml @@ -36,7 +36,7 @@ gitlab-bot: url-pull-request-open: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests?state=opened url-pull-request-close: ${GITLAB_URL} url-pull-request-comment: ${GITLAB_URL} - url-pull-request: ${GITLAB_URL} + url-pull-request: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests/{1} user-url: ${GITLAB_URL}/api/v4/user users-url: ${GITLAB_URL}/api/v4/users teamcity: diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/IdAndStatusPr.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/IdAndStatusPr.java index d75787b..e812b39 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/IdAndStatusPr.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/IdAndStatusPr.java @@ -10,6 +10,8 @@ import lombok.Setter; public class IdAndStatusPr { private Long id; + private Long twoId; + private Long projectId; private MergeRequestState status; } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/MergeRequestRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/MergeRequestRepository.java index cfdfae3..fe6ec71 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/MergeRequestRepository.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/MergeRequestRepository.java @@ -13,7 +13,7 @@ import java.util.Set; public interface MergeRequestRepository extends SimpleManagerRepository, FilterOperation { - Set findAllIdByStatusIn(Set statuses); + Set findAllIdByStateIn(Set states); Optional findMiniInfoById(@NonNull Long id); 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 0c5b1f5..378d82a 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 @@ -85,7 +85,8 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService new NotFoundException("МержРеквест не найден")); // forgottenNotification(oldMergeRequest); @@ -163,7 +164,7 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService getAllId(Set statuses) { - return null; + return mergeRequestRepository.findAllIdByStateIn(statuses); } @Override @@ -191,15 +192,4 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService new UpdateException("ПР с таким id не существует")); - return null; - } - } diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java index e68eed4..bcb674c 100644 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java @@ -27,7 +27,7 @@ public class MergeRequestRepositoryImpl extends FilterManagerRepository findAllIdByStatusIn(Set statuses) { + public Set findAllIdByStateIn(Set statuses) { return repositoryJpa.findAllIdByStateIn(statuses); } diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/MergeRequestJpaRepository.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/MergeRequestJpaRepository.java index 17325a3..919781a 100644 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/MergeRequestJpaRepository.java +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/MergeRequestJpaRepository.java @@ -21,8 +21,8 @@ public interface MergeRequestJpaRepository extends JpaRepositoryImplementation id); - @Query("SELECT new org.sadtech.bot.gitlab.context.domain.IdAndStatusPr(p.id, p.state) FROM MergeRequest p WHERE p.state IN :statuses") - Set findAllIdByStateIn(@Param("statuses") Set statuses); + @Query("SELECT new org.sadtech.bot.gitlab.context.domain.IdAndStatusPr(p.id, p.twoId, p.projectId, p.state) FROM MergeRequest p WHERE p.state IN :states") + Set findAllIdByStateIn(@Param("states") Set states); @Query("SELECT p.id from MergeRequest p") Set findAllIds();