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 ca82524..8e6b417 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 @@ -4,6 +4,7 @@ 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.NoteParser; +import org.sadtech.bot.gitlab.core.service.parser.PipelineParser; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -17,6 +18,7 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class SchedulerService { + private final PipelineParser pipelineParser; private final MergeRequestParser mergeRequestParser; private final NoteParser noteParser; @@ -40,4 +42,14 @@ public class SchedulerService { noteParser.scanOldTask(); } + @Scheduled(cron = "*/30 * * * * *") + public void newPipeline() { + pipelineParser.scanNewPipeline(); + } + + @Scheduled(cron = "*/30 * * * * *") + public void oldPipeline() { + pipelineParser.scanOldPipeline(); + } + } diff --git a/bot-app/src/main/resources/application.yaml b/bot-app/src/main/resources/application.yaml index 509308b..c55905f 100644 --- a/bot-app/src/main/resources/application.yaml +++ b/bot-app/src/main/resources/application.yaml @@ -42,6 +42,8 @@ gitlab-bot: users-url: ${GITLAB_URL}/api/v4/users url-note: "{0}#note_{1,number,#}" url-note-api: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes/{2,number,#}" + url-pipelines: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines?&page={1,number,integer}&per_page=100" + url-pipeline: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines/{1,number,integer}" 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 2b5a011..df4999b 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 @@ -167,4 +167,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ 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 196db29..a2d95b6 100644 --- a/bot-app/src/main/resources/messages_en.properties +++ b/bot-app/src/main/resources/messages_en.properties @@ -24,4 +24,5 @@ notify.pr.update={0} *MergeRequest update | {6}*{3}[{1}]({2}){3}{4}: {5} notify.task.close={0} *Closed [task]({1}){2}*{3}*: {4} notify.task.new={0} *New [task]({1}) assigned{2}*{3}*: {4} notify.project.new={0} *New project*{1}[{2}]({3}){1}{4}{5}: {6} -notify.comment.bell={0} *New mention* | [MR]({1}){2}*{3}*: {4} \ No newline at end of file +notify.comment.bell={0} *New mention* | [MR]({1}){2}*{3}*: {4} +notify.pipeline={0} *Pipeline {1,number,#}* | {2}{3}[{4}]({5}){3}{6} {7} {8} \ No newline at end of file diff --git a/bot-app/src/main/resources/messages_ru.properties b/bot-app/src/main/resources/messages_ru.properties index 2523c33..fab8a37 100644 --- a/bot-app/src/main/resources/messages_ru.properties +++ b/bot-app/src/main/resources/messages_ru.properties @@ -24,4 +24,5 @@ notify.pr.update={0} *Обновление MergeRequest | {6}*{3}[{1}]({2}){3}{4 notify.task.close={0} *Закрыта* [задача]({1}){2}*{3}*: {4} notify.task.new={0} *Назначена новая* [задача]({1}){2}*{3}*: {4} notify.project.new={0} *Новый Проект*{1}[{2}]({3}){1}{4}{5}: {6} -notify.comment.bell={0} *Новое упоминание* | [MR]({1}){2}*{3}*: {4} \ No newline at end of file +notify.comment.bell={0} *Новое упоминание* | [MR]({1}){2}*{3}*: {4} +notify.pipeline={0} *Сборка {1,number,#}* | {2}{3}[{4}]({5}){3}{6} {7} {8} \ No newline at end of file diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/PipelineStatus.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/PipelineStatus.java new file mode 100644 index 0000000..18b9969 --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/PipelineStatus.java @@ -0,0 +1,22 @@ +package org.sadtech.bot.gitlab.context.domain; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public enum PipelineStatus { + + CREATED, + WAITING_FOR_RESOURCE, + PREPARING, + PENDING, + RUNNING, + SUCCESS, + FAILED, + CANCELED, + SKIPPED, + MANUAL, + SCHEDULED + +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Chat.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Chat.java deleted file mode 100644 index b2e2364..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Chat.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.sadtech.bot.gitlab.context.domain.entity; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.Setter; -import org.sadtech.haiti.context.domain.BasicEntity; - -import javax.persistence.Column; -import javax.persistence.Id; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -@Getter -@Setter -//@Entity -//@Table(name = "chat") -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -public class Chat implements BasicEntity { - - @Id - @Column(name = "key") - @EqualsAndHashCode.Include - private String id; - - @Column(name = "telegram_id") - private Long telegramId; - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Pipeline.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Pipeline.java new file mode 100644 index 0000000..87ff936 --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Pipeline.java @@ -0,0 +1,57 @@ +package org.sadtech.bot.gitlab.context.domain.entity; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.haiti.context.domain.BasicEntity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import java.time.LocalDateTime; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ + +@Entity +@Getter +@Setter +@Table(name = "pipeline") +@EqualsAndHashCode(onlyExplicitlyIncluded = true) +public class Pipeline implements BasicEntity { + + @Id + @Column(name = "id") + @EqualsAndHashCode.Include + private Long id; + + @Column(name = "created_date") + private LocalDateTime created; + + @Column(name = "updated_date") + private LocalDateTime updated; + + @Enumerated(EnumType.STRING) + @Column(name = "status") + private PipelineStatus status; + + @Column(name = "ref") + private String ref; + + @Column(name = "web_url") + private String webUrl; + + @ManyToOne + @JoinColumn(name = "project_id") + private Project project; + +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Reviewer.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Reviewer.java deleted file mode 100644 index 8c4a76e..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Reviewer.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.sadtech.bot.gitlab.context.domain.entity; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.Setter; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import java.time.LocalDateTime; - -/** - * Ревьювер пулреквеста. - * - * @author upagge [01.02.2020] - */ -//@Entity -@Getter -@Setter -//@Table(name = "reviewer") -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -public class Reviewer { - - /** - * Идентификатор - */ - @Id - @Column(name = "id") - @EqualsAndHashCode.Include - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - /** - * Пользователь - */ - @Column(name = "person_login") - private String personLogin; - - /** - * Статус - */ -// @Enumerated(EnumType.STRING) -// @Column(name = "status") -// private ReviewerStatus status; - - @Column(name = "date_change") - private LocalDateTime dateChange; - - @Column(name = "date_smart_notify") - private LocalDateTime dateSmartNotify; - - @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, optional = false) - @JoinColumn(name = "pull_request_id") - private MergeRequest mergeRequest; - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java new file mode 100644 index 0000000..4dbaf5b --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pipeline/PipelineNotify.java @@ -0,0 +1,49 @@ +package org.sadtech.bot.gitlab.context.domain.notify.pipeline; + +import lombok.Builder; +import org.sadtech.bot.gitlab.context.domain.notify.Notify; +import org.sadtech.bot.gitlab.context.service.AppSettingService; +import org.sadtech.bot.gitlab.context.utils.Smile; + +import java.text.MessageFormat; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public class PipelineNotify extends Notify { + + private final Long pipelineId; + private final String projectName; + private final String refName; + private final String newStatus; + private final String webUrl; + private String oldStatus = "n/a"; + + @Builder + public PipelineNotify(Long pipelineId, String projectName, String refName, String oldStatus, String newStatus, String webUrl) { + this.pipelineId = pipelineId; + this.projectName = projectName; + this.refName = refName; + this.oldStatus = oldStatus; + this.newStatus = newStatus; + this.webUrl = webUrl; + } + + @Override + public String generateMessage(AppSettingService appSettingService) { + return MessageFormat.format( + appSettingService.getMessage("notify.pipeline"), + Smile.BUILD, + pipelineId, + escapeMarkdown(projectName), + Smile.HR, + refName, + webUrl, + oldStatus, + Smile.ARROW, + newStatus + ); + } +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/SmartPrNotify.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/SmartPrNotify.java deleted file mode 100644 index b9a8709..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/SmartPrNotify.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.sadtech.bot.gitlab.context.domain.notify.pullrequest; - -import lombok.Builder; -import lombok.Getter; -import org.sadtech.bot.gitlab.context.domain.entity.Reviewer; -import org.sadtech.bot.gitlab.context.service.AppSettingService; -import org.sadtech.bot.gitlab.context.utils.Smile; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -@Getter -public class SmartPrNotify extends PrNotify { - - private final Reviewer reviewerTriggered; - - @Builder - protected SmartPrNotify( - String title, - String url, - String projectName, - String repositorySlug, - Reviewer reviewerTriggered - ) { - super(projectName, title, url); - this.reviewerTriggered = reviewerTriggered; - } - - @Override - public String generateMessage(AppSettingService settingService) { - return settingService.getMessage( - "notify.pr.smart", - Smile.SMART.getValue(), title, url, Smile.HR.getValue(), reviewerTriggered.getPersonLogin(), - projectName - ); - } - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/ChatRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/ChatRepository.java deleted file mode 100644 index 6c4faa6..0000000 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/ChatRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.sadtech.bot.gitlab.context.repository; - -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.entity.Chat; -import org.sadtech.haiti.context.repository.SimpleManagerRepository; - -import java.util.Set; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -public interface ChatRepository extends SimpleManagerRepository { - - Set findAllTelegramIdByKey(@NonNull Set keys); - -} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/PipelineRepository.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/PipelineRepository.java new file mode 100644 index 0000000..26780e9 --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/repository/PipelineRepository.java @@ -0,0 +1,20 @@ +package org.sadtech.bot.gitlab.context.repository; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.haiti.context.page.Pagination; +import org.sadtech.haiti.context.page.Sheet; +import org.sadtech.haiti.context.repository.SimpleManagerRepository; + +import java.util.Set; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public interface PipelineRepository extends SimpleManagerRepository { + + Sheet findAllByStatuses(@NonNull Set statuses, @NonNull Pagination pagination); +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/PipelineService.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/PipelineService.java new file mode 100644 index 0000000..2863c6c --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/service/PipelineService.java @@ -0,0 +1,21 @@ +package org.sadtech.bot.gitlab.context.service; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.haiti.context.page.Pagination; +import org.sadtech.haiti.context.page.Sheet; +import org.sadtech.haiti.context.service.SimpleManagerService; + +import java.util.Set; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public interface PipelineService extends SimpleManagerService { + + Sheet getAllByStatuses(@NonNull Set statuses, @NonNull Pagination pagination); + +} 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 d16db05..80cfb2a 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 @@ -48,4 +48,8 @@ public class GitlabProperty { private String urlNoteApi; + private String urlPipelines; + + private String urlPipeline; + } diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/PipelineJsonConverter.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/PipelineJsonConverter.java new file mode 100644 index 0000000..2de33e2 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/convert/PipelineJsonConverter.java @@ -0,0 +1,69 @@ +package org.sadtech.bot.gitlab.core.service.convert; + +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.bot.gitlab.sdk.domain.PipelineJson; +import org.sadtech.bot.gitlab.sdk.domain.PipelineStatusJson; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.CANCELED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.CREATED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.FAILED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.MANUAL; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.PENDING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.PREPARING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.RUNNING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.SCHEDULED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.SKIPPED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.SUCCESS; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.WAITING_FOR_RESOURCE; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +@Component +public class PipelineJsonConverter implements Converter { + + @Override + public Pipeline convert(PipelineJson source) { + final Pipeline pipeline = new Pipeline(); + pipeline.setId(source.getId()); + pipeline.setCreated(source.getCreated()); + pipeline.setUpdated(source.getUpdated()); + pipeline.setRef(source.getRef()); + pipeline.setWebUrl(source.getWebUrl()); + pipeline.setStatus(convertStatus(source.getStatus())); + return pipeline; + } + + private PipelineStatus convertStatus(PipelineStatusJson status) { + switch (status) { + case SKIPPED: + return SKIPPED; + case CANCELED: + return CANCELED; + case SUCCESS: + return SUCCESS; + case MANUAL: + return MANUAL; + case CREATED: + return CREATED; + case PENDING: + return PENDING; + case RUNNING: + return RUNNING; + case PREPARING: + return PREPARING; + case SCHEDULED: + return SCHEDULED; + case WAITING_FOR_RESOURCE: + return WAITING_FOR_RESOURCE; + default: + return FAILED; + } + } + +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/PipelineServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/PipelineServiceImpl.java new file mode 100644 index 0000000..4b2d5aa --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/PipelineServiceImpl.java @@ -0,0 +1,97 @@ +package org.sadtech.bot.gitlab.core.service.impl; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.bot.gitlab.context.domain.notify.pipeline.PipelineNotify; +import org.sadtech.bot.gitlab.context.repository.PipelineRepository; +import org.sadtech.bot.gitlab.context.service.NotifyService; +import org.sadtech.bot.gitlab.context.service.PipelineService; +import org.sadtech.haiti.context.exception.NotFoundException; +import org.sadtech.haiti.context.page.Pagination; +import org.sadtech.haiti.context.page.Sheet; +import org.sadtech.haiti.core.service.AbstractSimpleManagerService; +import org.springframework.stereotype.Service; + +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.CANCELED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.FAILED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.SKIPPED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.SUCCESS; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +@Service +public class PipelineServiceImpl extends AbstractSimpleManagerService implements PipelineService { + + private static final Set notificationStatus = Stream.of( + FAILED, SUCCESS, CANCELED, SKIPPED + ).collect(Collectors.toSet()); + + private final NotifyService notifyService; + private final PipelineRepository repository; + + public PipelineServiceImpl(NotifyService notifyService, PipelineRepository repository) { + super(repository); + this.notifyService = notifyService; + this.repository = repository; + } + + @Override + public Pipeline create(@NonNull Pipeline pipeline) { + final Pipeline newPipeline = repository.save(pipeline); + + if (notificationStatus.contains(pipeline.getStatus())) { + notifyService.send( + PipelineNotify.builder() + .newStatus(pipeline.getStatus().name()) + .pipelineId(pipeline.getId()) + .projectName(pipeline.getProject().getName()) + .refName(pipeline.getRef()) + .webUrl(pipeline.getWebUrl()) + .build() + ); + } + + return newPipeline; + } + + @Override + public Pipeline update(@NonNull Pipeline pipeline) { + final Pipeline oldPipeline = repository.findById(pipeline.getId()) + .orElseThrow(() -> new NotFoundException("Pipeline не найден")); + + if (!oldPipeline.getUpdated().equals(pipeline.getUpdated())) { + pipeline.setProject(oldPipeline.getProject()); + + if (notificationStatus.contains(pipeline.getStatus())) { + notifyService.send( + PipelineNotify.builder() + .pipelineId(pipeline.getId()) + .webUrl(pipeline.getWebUrl()) + .projectName(pipeline.getProject().getName()) + .refName(pipeline.getRef()) + .newStatus(pipeline.getStatus().name()) + .oldStatus(oldPipeline.getStatus().name()) + .build() + ); + + return repository.save(pipeline); + } + + } + + return oldPipeline; + } + + @Override + public Sheet getAllByStatuses(@NonNull Set statuses, @NonNull Pagination pagination) { + return repository.findAllByStatuses(statuses, pagination); + } +} diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/PipelineParser.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/PipelineParser.java new file mode 100644 index 0000000..a2b12a9 --- /dev/null +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/parser/PipelineParser.java @@ -0,0 +1,131 @@ +package org.sadtech.bot.gitlab.core.service.parser; + +import lombok.RequiredArgsConstructor; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.bot.gitlab.context.domain.entity.Project; +import org.sadtech.bot.gitlab.context.service.PipelineService; +import org.sadtech.bot.gitlab.context.service.ProjectService; +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.PipelineJson; +import org.sadtech.haiti.context.domain.ExistsContainer; +import org.sadtech.haiti.context.exception.ConvertException; +import org.sadtech.haiti.context.page.Sheet; +import org.sadtech.haiti.core.page.PaginationImpl; +import org.sadtech.haiti.utils.network.HttpParse; +import org.springframework.core.convert.ConversionService; +import org.springframework.stereotype.Service; + +import java.text.MessageFormat; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.CREATED; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.MANUAL; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.PENDING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.PREPARING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.RUNNING; +import static org.sadtech.bot.gitlab.context.domain.PipelineStatus.WAITING_FOR_RESOURCE; +import static org.sadtech.haiti.utils.network.HttpParse.ACCEPT; +import static org.sadtech.haiti.utils.network.HttpParse.AUTHORIZATION; +import static org.sadtech.haiti.utils.network.HttpParse.BEARER; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ + +@Service +@RequiredArgsConstructor +public class PipelineParser { + + public static final Integer COUNT = 100; + private static final Set oldStatus = Stream.of( + CREATED, WAITING_FOR_RESOURCE, PREPARING, PENDING, RUNNING, MANUAL + ).collect(Collectors.toSet()); + private final PipelineService pipelineService; + private final ProjectService projectService; + private final GitlabProperty gitlabProperty; + private final PersonProperty personProperty; + private final ConversionService conversionService; + + public void scanNewPipeline() { + int page = 0; + Sheet projectSheet = projectService.getAll(PaginationImpl.of(page, COUNT)); + + while (projectSheet.hasContent()) { + final List projects = projectSheet.getContent(); + + for (Project project : projects) { + processingProject(project); + } + + projectSheet = projectService.getAll(PaginationImpl.of(++page, COUNT)); + } + + } + + private void processingProject(Project project) { + int page = 1; + List pipelineJsons = getPipelineJsons(project.getId(), page); + + while (!pipelineJsons.isEmpty()) { + + final Set jsonIds = pipelineJsons.stream() + .map(PipelineJson::getId) + .collect(Collectors.toSet()); + + final ExistsContainer existsContainer = pipelineService.existsById(jsonIds); + + if (!existsContainer.isAllFound()) { + final List newPipelines = pipelineJsons.stream() + .filter(json -> existsContainer.getIdNoFound().contains(json.getId())) + .map(json -> conversionService.convert(json, Pipeline.class)) + .peek( + pipeline -> pipeline.setProject(project) + ).collect(Collectors.toList()); + + pipelineService.createAll(newPipelines); + } + + pipelineJsons = getPipelineJsons(project.getId(), ++page); + } + + } + + private List getPipelineJsons(Long projectId, int page) { + return HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPipelines(), projectId, page)) + .header(ACCEPT) + .header(HttpParse.AUTHORIZATION, HttpParse.BEARER + personProperty.getToken()) + .executeList(PipelineJson.class); + } + + public void scanOldPipeline() { + int page = 0; + Sheet pipelineSheet = pipelineService.getAllByStatuses(oldStatus, PaginationImpl.of(page, COUNT)); + + while (pipelineSheet.hasContent()) { + final List pipelines = pipelineSheet.getContent(); + + for (Pipeline pipeline : pipelines) { + final Pipeline newPipeline = HttpParse.request( + MessageFormat.format(gitlabProperty.getUrlPipeline(), pipeline.getProject().getId(), pipeline.getId()) + ) + .header(ACCEPT) + .header(AUTHORIZATION, BEARER + personProperty.getToken()) + .execute(PipelineJson.class) + .map(json -> conversionService.convert(json, Pipeline.class)) + .orElseThrow(() -> new ConvertException("Ошибка обновления Pipelines")); + + pipelineService.update(newPipeline); + } + + pipelineSheet = pipelineService.getAllByStatuses(oldStatus, PaginationImpl.of(++page, COUNT)); + } + } + +} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/ChatRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/ChatRepositoryImpl.java deleted file mode 100644 index e729b07..0000000 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/ChatRepositoryImpl.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.sadtech.bot.gitlab.data.impl; - -import lombok.NonNull; -import org.sadtech.bot.gitlab.context.domain.entity.Chat; -import org.sadtech.bot.gitlab.context.repository.ChatRepository; -import org.sadtech.bot.gitlab.data.jpa.ChatJpaRepository; -import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository; - -import java.util.Set; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -//@Repository -public class ChatRepositoryImpl extends AbstractSimpleManagerRepository implements ChatRepository { - - private final ChatJpaRepository jpaRepository; - - public ChatRepositoryImpl(ChatJpaRepository jpaRepository) { - super(jpaRepository); - this.jpaRepository = jpaRepository; - } - - @Override - public Set findAllTelegramIdByKey(@NonNull Set keys) { - return jpaRepository.findAllTelegramIdByKey(keys); - } - -} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/PipelineRepositoryImpl.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/PipelineRepositoryImpl.java new file mode 100644 index 0000000..396ea3c --- /dev/null +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/impl/PipelineRepositoryImpl.java @@ -0,0 +1,37 @@ +package org.sadtech.bot.gitlab.data.impl; + +import lombok.NonNull; +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.sadtech.bot.gitlab.context.repository.PipelineRepository; +import org.sadtech.bot.gitlab.data.jpa.PipelineJpaRepository; +import org.sadtech.haiti.context.page.Pagination; +import org.sadtech.haiti.context.page.Sheet; +import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository; +import org.sadtech.haiti.database.util.Converter; +import org.springframework.stereotype.Repository; + +import java.util.Set; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +@Repository +public class PipelineRepositoryImpl extends AbstractSimpleManagerRepository implements PipelineRepository { + + private final PipelineJpaRepository jpaRepository; + + public PipelineRepositoryImpl(PipelineJpaRepository jpaRepository) { + super(jpaRepository); + this.jpaRepository = jpaRepository; + } + + @Override + public Sheet findAllByStatuses(@NonNull Set statuses, @NonNull Pagination pagination) { + return Converter.page( + jpaRepository.findAllByStatusIn(statuses, Converter.pagination(pagination)) + ); + } +} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/ChatJpaRepository.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/ChatJpaRepository.java deleted file mode 100644 index 8d05127..0000000 --- a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/ChatJpaRepository.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.sadtech.bot.gitlab.data.jpa; - -import org.sadtech.bot.gitlab.context.domain.entity.Chat; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.NoRepositoryBean; -import org.springframework.data.repository.query.Param; - -import java.util.Set; - -/** - * // TODO: 11.10.2020 Добавить описание. - * - * @author upagge 11.10.2020 - */ -@NoRepositoryBean -public interface ChatJpaRepository extends JpaRepository { - -// @Query("SELECT c.telegramId FROM Chat c WHERE c.key IN :keys AND c.telegramId IS NOT NULL") - Set findAllTelegramIdByKey(@Param("keys") Set keys); - -} diff --git a/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/PipelineJpaRepository.java b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/PipelineJpaRepository.java new file mode 100644 index 0000000..5b29c6f --- /dev/null +++ b/bot-data/src/main/java/org/sadtech/bot/gitlab/data/jpa/PipelineJpaRepository.java @@ -0,0 +1,20 @@ +package org.sadtech.bot.gitlab.data.jpa; + +import org.sadtech.bot.gitlab.context.domain.PipelineStatus; +import org.sadtech.bot.gitlab.context.domain.entity.Pipeline; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Set; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public interface PipelineJpaRepository extends JpaRepository { + + Page findAllByStatusIn(Set statuses, Pageable pageable); + +} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/BitbucketUserRole.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/BitbucketUserRole.java deleted file mode 100644 index fdcb7f7..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/BitbucketUserRole.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -/** - * TODO: Добавить комментарий енума. - * - * @author upagge [31.01.2020] - */ -public enum BitbucketUserRole { - - AUTHOR, REVIEWER - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/CommentState.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/CommentState.java deleted file mode 100644 index 7dc75f2..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/CommentState.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -public enum CommentState { - - RESOLVED, - OPEN - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/FromRefJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/FromRefJson.java deleted file mode 100644 index 744d59d..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/FromRefJson.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [04.02.2020] - */ -@Data -public class FromRefJson { - - private RepositoryJson repository; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/GroupJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/GroupJson.java deleted file mode 100644 index c1f5bdd..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/GroupJson.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Data; - -/** - * // TODO: 14.01.2021 Добавить описание. - * - * @author upagge 14.01.2021 - */ -@Data -public class GroupJson { - - private Long id; - - @JsonProperty("web_url") - private String webUrl; - - private String name; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/LinkJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/LinkJson.java deleted file mode 100644 index 203bc32..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/LinkJson.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -import java.util.List; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [31.01.2020] - */ -@Data -public class LinkJson { - - private List self; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeResult.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeResult.java deleted file mode 100644 index 20fbc8e..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeResult.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -@Data -public class MergeResult { - - private Outcome outcome; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Outcome.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Outcome.java deleted file mode 100644 index a0b4997..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Outcome.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -public enum Outcome { - - CONFLICTED, - CLEAN - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineJson.java new file mode 100644 index 0000000..665ddef --- /dev/null +++ b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineJson.java @@ -0,0 +1,39 @@ +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; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +@Data +public class PipelineJson { + + private Long id; + + @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; + + private PipelineStatusJson status; + + private String ref; + + @JsonProperty("web_url") + private String webUrl; + +} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineStatusJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineStatusJson.java new file mode 100644 index 0000000..64117d5 --- /dev/null +++ b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/PipelineStatusJson.java @@ -0,0 +1,45 @@ +package org.sadtech.bot.gitlab.sdk.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * // TODO: 17.01.2021 Добавить описание. + * + * @author upagge 17.01.2021 + */ +public enum PipelineStatusJson { + + @JsonProperty("created") + CREATED, + + @JsonProperty("waiting_for_resource") + WAITING_FOR_RESOURCE, + + @JsonProperty("preparing") + PREPARING, + + @JsonProperty("pending") + PENDING, + + @JsonProperty("running") + RUNNING, + + @JsonProperty("success") + SUCCESS, + + @JsonProperty("failed") + FAILED, + + @JsonProperty("canceled") + CANCELED, + + @JsonProperty("skipped") + SKIPPED, + + @JsonProperty("manual") + MANUAL, + + @JsonProperty("scheduled") + SCHEDULED + +} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Properties.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Properties.java deleted file mode 100644 index 4e66a9e..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Properties.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -@Data -public class Properties { - - private MergeResult mergeResult; - private Integer resolvedTaskCount = 0; - private Integer commentCount = 0; - private Integer openTaskCount = 0; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/RepositoryJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/RepositoryJson.java deleted file mode 100644 index 49e41f3..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/RepositoryJson.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [04.02.2020] - */ -@Data -public class RepositoryJson { - - private Long id; - private String slug; - private ProjectJson project; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/SelfJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/SelfJson.java deleted file mode 100644 index 52534b2..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/SelfJson.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -import lombok.Data; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [31.01.2020] - */ -@Data -public class SelfJson { - - private String href; - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Severity.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Severity.java deleted file mode 100644 index 4b0f946..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/Severity.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -public enum Severity { - - NORMAL, - BLOCKER - -} diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/UserPullRequestStatus.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/UserPullRequestStatus.java deleted file mode 100644 index f1e1e8b..0000000 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/UserPullRequestStatus.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.sadtech.bot.gitlab.sdk.domain; - -/** - * TODO: Добавить комментарий енума. - * - * @author upagge [31.01.2020] - */ -public enum UserPullRequestStatus { - - UNAPPROVED, APPROVED, NEEDS_WORK - -}