diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/config/InitProperty.java b/src/main/java/org/sadtech/bot/bitbucketbot/config/InitProperty.java index 32f320c..76f3116 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/config/InitProperty.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/config/InitProperty.java @@ -12,5 +12,6 @@ import org.springframework.stereotype.Component; public class InitProperty { private Long startCommentId; + private boolean use = false; } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Comment.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Comment.java index babb059..1d1214c 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Comment.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Comment.java @@ -30,6 +30,9 @@ public class Comment { @EqualsAndHashCode.Include private Long id; + @Column(name = "url_api") + private String urlApi; + @Column(name = "url") private String url; diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequestMini.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequestMini.java new file mode 100644 index 0000000..d7d0aef --- /dev/null +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequestMini.java @@ -0,0 +1,47 @@ +package org.sadtech.bot.bitbucketbot.domain.entity; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * // TODO: 12.09.2020 Добавить описание. + * + * @author upagge 12.09.2020 + */ +@Getter +@Setter +@Entity +@Table(name = "pull_request") +@EqualsAndHashCode(onlyExplicitlyIncluded = true) +public class PullRequestMini { + + /** + * Идентификатор + */ + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + @EqualsAndHashCode.Include + private Long id; + + /** + * Адрес ПР + */ + @Column(name = "url") + private String url; + + /** + * Автор ПР + */ + @Column(name = "author_login") + private String authorLogin; + +} diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java index ff63775..bfcf021 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Task.java @@ -5,7 +5,6 @@ import lombok.Getter; import lombok.Setter; import org.sadtech.bot.bitbucketbot.domain.TaskStatus; -import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; @@ -15,6 +14,7 @@ import javax.persistence.JoinTable; import javax.persistence.OneToMany; import javax.persistence.Table; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; @Entity @@ -63,8 +63,11 @@ public class Task { @Column(name = "author_login") private String author; + @Column(name = "responsible_login") + private String responsible; + @JoinTable - @OneToMany(cascade = CascadeType.ALL) - private List comments; + @OneToMany + private List comments = new ArrayList<>(); } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/PullRequestsRepository.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/PullRequestsRepository.java index 7a3e12d..d59c711 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/PullRequestsRepository.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/PullRequestsRepository.java @@ -1,13 +1,16 @@ package org.sadtech.bot.bitbucketbot.repository; +import lombok.NonNull; import org.sadtech.basic.context.repository.SimpleManagerRepository; import org.sadtech.basic.context.repository.simple.FilterOperation; import org.sadtech.bot.bitbucketbot.domain.IdAndStatusPr; import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus; import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequestMini; import java.util.List; +import java.util.Optional; import java.util.Set; public interface PullRequestsRepository extends SimpleManagerRepository, FilterOperation { @@ -18,4 +21,6 @@ public interface PullRequestsRepository extends SimpleManagerRepository findAllIdByStatusIn(Set statuses); + Optional findMiniInfoById(@NonNull Long id); + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/PullRequestsRepositoryImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/PullRequestsRepositoryImpl.java index 271c515..05f050d 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/PullRequestsRepositoryImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/impl/PullRequestsRepositoryImpl.java @@ -1,25 +1,31 @@ package org.sadtech.bot.bitbucketbot.repository.impl; +import lombok.NonNull; import org.sadtech.basic.database.repository.manager.FilterManagerRepository; import org.sadtech.bot.bitbucketbot.domain.IdAndStatusPr; import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus; import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequestMini; import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository; +import org.sadtech.bot.bitbucketbot.repository.jpa.PullRequestMiniRepositoryJpa; import org.sadtech.bot.bitbucketbot.repository.jpa.PullRequestsRepositoryJpa; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; import java.util.Set; @Repository public class PullRequestsRepositoryImpl extends FilterManagerRepository implements PullRequestsRepository { private final PullRequestsRepositoryJpa repositoryJpa; + private final PullRequestMiniRepositoryJpa pullRequestMiniRepositoryJpa; - public PullRequestsRepositoryImpl(PullRequestsRepositoryJpa jpaRepository) { + public PullRequestsRepositoryImpl(PullRequestsRepositoryJpa jpaRepository, PullRequestMiniRepositoryJpa pullRequestMiniRepositoryJpa) { super(jpaRepository); repositoryJpa = jpaRepository; + this.pullRequestMiniRepositoryJpa = pullRequestMiniRepositoryJpa; } @Override @@ -37,4 +43,9 @@ public class PullRequestsRepositoryImpl extends FilterManagerRepository findMiniInfoById(@NonNull Long id) { + return pullRequestMiniRepositoryJpa.findById(id); + } + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestMiniRepositoryJpa.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestMiniRepositoryJpa.java new file mode 100644 index 0000000..5bfb1e4 --- /dev/null +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestMiniRepositoryJpa.java @@ -0,0 +1,12 @@ +package org.sadtech.bot.bitbucketbot.repository.jpa; + +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequestMini; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * // TODO: 12.09.2020 Добавить описание. + * + * @author upagge 12.09.2020 + */ +public interface PullRequestMiniRepositoryJpa extends JpaRepository { +} diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestsRepositoryJpa.java b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestsRepositoryJpa.java index 2282041..8eb2ada 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestsRepositoryJpa.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/repository/jpa/PullRequestsRepositoryJpa.java @@ -39,6 +39,9 @@ public interface PullRequestsRepositoryJpa extends JpaRepositoryImplementation

findAllIds(); + @Query("SELECT p.authorLogin from PullRequest p WHERE p.id = :id") + Optional findAuthorById(@Param("id") Long id); + // @Query("SELECT p FROM PullRequest p WHERE p.authorLogin = :login AND p.createDate BETWEEN :dateFrom AND :dateTo") // List findAllByAuthorAndDateBetween(@Param("login") String login, @Param("dateFrom") LocalDateTime dateFrom, @Param("dateTo") LocalDateTime dateTo); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/PullRequestsService.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/PullRequestsService.java index 30aa382..3045153 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/PullRequestsService.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/PullRequestsService.java @@ -7,9 +7,11 @@ import org.sadtech.bot.bitbucketbot.domain.IdAndStatusPr; import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus; import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequestMini; import org.sadtech.bot.bitbucketbot.domain.filter.PullRequestFilter; import java.util.List; +import java.util.Optional; import java.util.Set; public interface PullRequestsService extends SimpleManagerService, FilterService { @@ -27,4 +29,6 @@ public interface PullRequestsService extends SimpleManagerService getAllId(Set statuses); + Optional getMiniInfo(@NonNull Long pullRequestId); + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToComment.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToComment.java index 958e875..b91e056 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToComment.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToComment.java @@ -21,7 +21,7 @@ public class ResultScanToComment implements Converter { comment.setAuthor(commentJson.getAuthor().getName()); comment.setPullRequestId(resultScan.getPullRequestId()); comment.setMessage(commentJson.getText()); - comment.setUrl(resultScan.getUrlComment()); + comment.setUrlApi(resultScan.getCommentApiUrl()); comment.setBitbucketVersion(commentJson.getVersion()); comment.setAnswers( commentJson.getComments().stream() diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToTaskConvert.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToTaskConvert.java index 27964aa..7fe7689 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToTaskConvert.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/converter/ResultScanToTaskConvert.java @@ -1,7 +1,10 @@ package org.sadtech.bot.bitbucketbot.service.converter; +import org.sadtech.basic.context.exception.ConvertException; +import org.sadtech.bot.bitbucketbot.domain.TaskStatus; import org.sadtech.bot.bitbucketbot.domain.entity.Task; import org.sadtech.bot.bitbucketbot.dto.bitbucket.CommentJson; +import org.sadtech.bot.bitbucketbot.dto.bitbucket.CommentState; import org.sadtech.bot.bitbucketbot.service.executor.ResultScan; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @@ -19,7 +22,20 @@ public class ResultScanToTaskConvert implements Converter { task.setCreateDate(json.getCreatedDate()); task.setBitbucketVersion(json.getVersion()); task.setPullRequestId(resultScan.getPullRequestId()); + task.setStatus(convertState(json.getState())); + task.setUrlApi(resultScan.getCommentApiUrl()); return task; } + private TaskStatus convertState(CommentState state) { + switch (state) { + case RESOLVED: + return TaskStatus.RESOLVED; + case OPEN: + return TaskStatus.OPEN; + default: + throw new ConvertException("Неподдерживаемый тип задачи"); + } + } + } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/executor/ResultScan.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/executor/ResultScan.java index 4e1b26c..7945cc4 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/executor/ResultScan.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/executor/ResultScan.java @@ -8,7 +8,7 @@ import org.sadtech.bot.bitbucketbot.dto.bitbucket.CommentJson; @RequiredArgsConstructor public class ResultScan { - private final String urlComment; + private final String commentApiUrl; private final Long pullRequestId; private final CommentJson commentJson; diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java index 97a5c91..a263699 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/CommentServiceImpl.java @@ -2,7 +2,6 @@ package org.sadtech.bot.bitbucketbot.service.impl; import lombok.NonNull; import org.sadtech.basic.core.service.AbstractSimpleManagerService; -import org.sadtech.bot.bitbucketbot.config.InitProperty; import org.sadtech.bot.bitbucketbot.domain.change.comment.CommentChange; import org.sadtech.bot.bitbucketbot.domain.entity.Comment; import org.sadtech.bot.bitbucketbot.exception.NotFoundException; @@ -27,23 +26,17 @@ public class CommentServiceImpl extends AbstractSimpleManagerService getMiniInfo(@NonNull Long pullRequestId) { + return pullRequestsRepository.findMiniInfoById(pullRequestId); + } + @Override public Sheet getAll(@NonNull PullRequestFilter filter, Pagination pagination) { return filterService.getAll(filter, pagination); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java index 5938d8c..7628961 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/TaskServiceImpl.java @@ -36,6 +36,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService im @Override public Task create(@NonNull Task task) { Assert.isNotNull(task.getId(), "При создании объекта должен быть установлен идентификатор"); + task.getComments().clear(); final Task newTask = taskRepository.save(task); final PullRequest pullRequest = pullRequestsService.getById(task.getPullRequestId()) diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java index 48b5c3b..ca539cb 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/CommentAndTaskParser.java @@ -11,9 +11,11 @@ import org.sadtech.bot.bitbucketbot.domain.Answer; import org.sadtech.bot.bitbucketbot.domain.change.comment.AnswerCommentChange; import org.sadtech.bot.bitbucketbot.domain.entity.Comment; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequestMini; import org.sadtech.bot.bitbucketbot.domain.entity.Task; import org.sadtech.bot.bitbucketbot.dto.bitbucket.CommentJson; import org.sadtech.bot.bitbucketbot.dto.bitbucket.Severity; +import org.sadtech.bot.bitbucketbot.exception.NotFoundException; import org.sadtech.bot.bitbucketbot.service.ChangeService; import org.sadtech.bot.bitbucketbot.service.CommentService; import org.sadtech.bot.bitbucketbot.service.PersonService; @@ -26,6 +28,7 @@ import org.sadtech.bot.bitbucketbot.service.impl.ExecutorScanner; import org.springframework.core.convert.ConversionService; import org.springframework.stereotype.Component; +import java.text.MessageFormat; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; @@ -54,6 +57,8 @@ public class CommentAndTaskParser { private final CommentSchedulerProperty commentSchedulerProperty; private final InitProperty initProperty; + private boolean initStart = false; + public void scanNewCommentAndTask() { long commentId = getLastIdCommentOrTask() + 1; int count = 0; @@ -62,8 +67,13 @@ public class CommentAndTaskParser { executorScanner.registration(dataScans); final List resultScans = executorScanner.getResult(); if (!resultScans.isEmpty()) { - commentService.createAll(getCommentsByResultScan(resultScans)); - taskService.createAll(getTaskByResultScan(resultScans)); + final long commentMax = commentService.createAll(getCommentsByResultScan(resultScans)).stream() + .mapToLong(Comment::getId) + .max().orElse(0L); + final long taskMax = taskService.createAll(getTaskByResultScan(resultScans)).stream() + .mapToLong(Task::getId) + .max().orElse(0L); + commentId = Long.max(commentMax, taskMax) + 1; count = 0; } } while (count++ < commentSchedulerProperty.getNoCommentCount()); @@ -71,8 +81,9 @@ public class CommentAndTaskParser { private long getLastIdCommentOrTask() { Long commentStartId = Long.max(commentService.getLastCommentId(), taskService.getLastTaskId()); - if (commentStartId == 0L && initProperty != null) { + if (initProperty != null && !initStart && (commentStartId == 0L || initProperty.isUse())) { commentStartId = initProperty.getStartCommentId(); + initStart = true; } return commentStartId; } @@ -107,6 +118,13 @@ public class CommentAndTaskParser { return resultScans.stream() .filter(resultScan -> Severity.NORMAL.equals(resultScan.getCommentJson().getSeverity())) .map(resultScan -> conversionService.convert(resultScan, Comment.class)) + .peek( + comment -> { + final PullRequestMini pullRequestMini = pullRequestsService.getMiniInfo(comment.getPullRequestId()) + .orElseThrow(() -> new NotFoundException("Автор ПР не найден")); + comment.setUrl(generateUrl(comment.getId(), pullRequestMini.getUrl())); + } + ) .collect(Collectors.toList()); } @@ -114,9 +132,21 @@ public class CommentAndTaskParser { return resultScans.stream() .filter(commentJson -> Severity.BLOCKER.equals(commentJson.getCommentJson().getSeverity())) .map(resultScan -> conversionService.convert(resultScan, Task.class)) + .peek( + task -> { + final PullRequestMini pullRequestMini = pullRequestsService.getMiniInfo(task.getPullRequestId()) + .orElseThrow(() -> new NotFoundException("Автор ПР не найден")); + task.setResponsible(pullRequestMini.getAuthorLogin()); + task.setUrl(generateUrl(task.getId(), pullRequestMini.getUrl())); + } + ) .collect(Collectors.toList()); } + private String generateUrl(@NonNull Long id, @NonNull String pullRequestUrl) { + return MessageFormat.format("{0}/overview?commentId={1}", pullRequestUrl, id).replaceAll(" ", ""); + } + private String getCommentUrl(long commentId, PullRequest pullRequest) { return bitbucketProperty.getUrlPullRequestComment() .replace("{projectKey}", pullRequest.getProjectKey()) diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 830512f..d255a75 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -23,7 +23,8 @@ bitbucketbot: no-comment-count: 20 comment-count: 100 init: - start-comment-id: 7623 + start-comment-id: 7796 + use: false server-send: url: http://188.225.35.149:8080/api/send bitbucket: diff --git a/src/main/resources/liquibase/v.2.0.0/2020-09-06-create-table.xml b/src/main/resources/liquibase/v.2.0.0/2020-09-06-create-table.xml index 5488bab..a0df8e0 100644 --- a/src/main/resources/liquibase/v.2.0.0/2020-09-06-create-table.xml +++ b/src/main/resources/liquibase/v.2.0.0/2020-09-06-create-table.xml @@ -91,7 +91,12 @@ - + + + + + + @@ -139,15 +144,19 @@ - + - + + + +