From 7839fceaa474e7e96733fa8398a9fd6aa1472cca Mon Sep 17 00:00:00 2001 From: upagge Date: Tue, 8 Sep 2020 21:28:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=D1=8E=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=85=D0=B0=D0=BD=D0=B8=D0=B7=D0=BC=20=D1=81=20?= =?UTF-8?q?=D0=9F=D0=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../change/pullrequest/ReviewersPrChange.java | 6 +- .../domain/entity/PullRequest.java | 7 +- .../bitbucketbot/domain/entity/Reviewer.java | 5 +- .../scheduler/SchedulerPushMessageSend.java | 1 + .../bitbucketbot/service/ChangeService.java | 3 +- .../service/impl/ChangeServiceImpl.java | 39 ++--------- .../service/impl/PullRequestsServiceImpl.java | 65 +++++++++++++++---- .../parser/PullRequestBitbucketParser.java | 5 +- 8 files changed, 73 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java index 7660731..d68e055 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/change/pullrequest/ReviewersPrChange.java @@ -6,8 +6,8 @@ import lombok.Getter; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; -import java.util.Collections; import java.util.List; +import java.util.Set; @Getter @EqualsAndHashCode(callSuper = true) @@ -17,11 +17,11 @@ public class ReviewersPrChange extends PrChange { @Builder private ReviewersPrChange( - Long telegramId, + Set telegramIds, String title, String url, List reviewerChanges) { - super(ChangeType.REVIEWERS, Collections.singleton(telegramId), title, url); + super(ChangeType.REVIEWERS, telegramIds, title, url); this.reviewerChanges = reviewerChanges; } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequest.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequest.java index 6ae8f61..a67b551 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequest.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/PullRequest.java @@ -14,10 +14,10 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; /** @@ -123,9 +123,8 @@ public class PullRequest { /** * Ревьюверы */ - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) - @JoinColumn(name = "pull_request_id") - private List reviewers; + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "pullRequest") + private List reviewers = new ArrayList<>(); public void setReviewers(List reviewers) { reviewers.forEach(reviewer -> reviewer.setPullRequest(this)); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Reviewer.java b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Reviewer.java index 1bc7b58..ed277f1 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Reviewer.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/domain/entity/Reviewer.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.Setter; import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; @@ -13,6 +14,7 @@ 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 javax.persistence.Table; @@ -50,7 +52,8 @@ public class Reviewer { @Column(name = "status") private ReviewerStatus status; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, optional = false) + @JoinColumn(name = "pull_request_id") private PullRequest pullRequest; } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerPushMessageSend.java b/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerPushMessageSend.java index 080c8bb..05f720a 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerPushMessageSend.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/scheduler/SchedulerPushMessageSend.java @@ -63,6 +63,7 @@ public class SchedulerPushMessageSend { final String json = objectMapper.writeValueAsString(pushMessage); // sendMessage(json); System.out.println(json); + System.out.println("\n\n"); } catch (JsonProcessingException e) { log.error(e.getMessage()); } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/ChangeService.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/ChangeService.java index 8a031ff..70d6994 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/ChangeService.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/ChangeService.java @@ -5,6 +5,7 @@ import org.sadtech.bot.bitbucketbot.domain.change.Change; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.UpdatePrChange; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; import java.util.List; @@ -20,7 +21,7 @@ public interface ChangeService { UpdatePrChange createUpdatePr(@NonNull PullRequest oldPullRequest, @NonNull PullRequest newPullRequest); - Change createReviewersPr(@NonNull PullRequest oldPullRequest, @NonNull PullRequest newPullRequest); + Change createReviewersPr(String prName, String prUrl, String authorLogin, List reviewerChanges); /** * Позволяет получить новые изменения. diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/ChangeServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/ChangeServiceImpl.java index 710f288..d240288 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/ChangeServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/ChangeServiceImpl.java @@ -2,7 +2,6 @@ package org.sadtech.bot.bitbucketbot.service.impl; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; import org.sadtech.bot.bitbucketbot.domain.change.Change; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ReviewersPrChange; @@ -15,9 +14,8 @@ import org.sadtech.bot.bitbucketbot.service.ChangeService; import org.sadtech.bot.bitbucketbot.service.PersonService; import org.springframework.stereotype.Service; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -54,37 +52,14 @@ public class ChangeServiceImpl implements ChangeService { } @Override - public Change createReviewersPr(@NonNull PullRequest oldPullRequest, @NonNull PullRequest newPullRequest) { - final Map oldReviewers = oldPullRequest.getReviewers().stream() - .collect(Collectors.toMap(Reviewer::getId, reviewer -> reviewer)); - final Map newReviewers = newPullRequest.getReviewers().stream() - .collect(Collectors.toMap(Reviewer::getId, reviewer -> reviewer)); - final List reviewerChanges = new ArrayList<>(); - for (Reviewer newReviewer : newReviewers.values()) { - if (oldReviewers.containsKey(newReviewer.getId())) { - final Reviewer oldReviewer = oldReviewers.get(newReviewer.getId()); - final ReviewerStatus oldStatus = oldReviewer.getStatus(); - final ReviewerStatus newStatus = newReviewer.getStatus(); - if (!oldStatus.equals(newStatus)) { - reviewerChanges.add(ReviewerChange.ofOld(oldReviewer.getUserLogin(), oldStatus, newStatus)); - } - } else { - reviewerChanges.add(ReviewerChange.ofNew(newReviewer.getUserLogin(), newReviewer.getStatus())); - } - } - final Set oldIds = oldReviewers.keySet(); - oldIds.removeAll(newReviewers.keySet()); - reviewerChanges.addAll( - oldReviewers.entrySet().stream() - .filter(e -> oldIds.contains(e.getKey())) - .map(e -> ReviewerChange.ofDeleted(e.getValue().getUserLogin())) - .collect(Collectors.toList()) - ); + public Change createReviewersPr(String prName, String prUrl, String authorLogin, List reviewerChanges) { return changeRepository.add( ReviewersPrChange.builder() - .title(newPullRequest.getTitle()) - .url(newPullRequest.getUrl()) - .telegramId(personService.getTelegramIdByLogin(newPullRequest.getAuthorLogin()).orElse(null)) + .title(prName) + .url(prUrl) + .telegramIds( + personService.getAllTelegramIdByLogin(Collections.singleton(authorLogin)) + ) .reviewerChanges(reviewerChanges) .build() ); diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java index 689089b..f2e96c0 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java @@ -13,7 +13,9 @@ 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.PullRequest_; +import org.sadtech.bot.bitbucketbot.domain.entity.Reviewer; import org.sadtech.bot.bitbucketbot.domain.filter.PullRequestFilter; +import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; import org.sadtech.bot.bitbucketbot.exception.UpdateException; import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository; import org.sadtech.bot.bitbucketbot.service.ChangeService; @@ -21,9 +23,12 @@ import org.sadtech.bot.bitbucketbot.service.PullRequestsService; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; @Service public class PullRequestsServiceImpl extends AbstractSimpleManagerService implements PullRequestsService { @@ -57,23 +62,55 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService oldReviewers = oldPullRequest.getReviewers().stream() + .collect(Collectors.toMap(Reviewer::getUserLogin, reviewer -> reviewer)); + final Map newReviewers = newPullRequest.getReviewers().stream() + .collect(Collectors.toMap(Reviewer::getUserLogin, reviewer -> reviewer)); + final List reviewerChanges = new ArrayList<>(); + for (Reviewer newReviewer : newReviewers.values()) { + if (oldReviewers.containsKey(newReviewer.getUserLogin())) { + final Reviewer oldReviewer = oldReviewers.get(newReviewer.getUserLogin()); + final ReviewerStatus oldStatus = oldReviewer.getStatus(); + final ReviewerStatus newStatus = newReviewer.getStatus(); + if (!oldStatus.equals(newStatus)) { + reviewerChanges.add(ReviewerChange.ofOld(oldReviewer.getUserLogin(), oldStatus, newStatus)); + oldReviewer.setStatus(newStatus); + } + } else { + reviewerChanges.add(ReviewerChange.ofNew(newReviewer.getUserLogin(), newReviewer.getStatus())); + newReviewer.setPullRequest(oldPullRequest); + oldPullRequest.getReviewers().add(newReviewer); + } + } + final Set oldIds = oldReviewers.keySet(); + oldIds.removeAll(newReviewers.keySet()); + reviewerChanges.addAll( + oldReviewers.entrySet().stream() + .filter(e -> oldIds.contains(e.getKey())) + .map(e -> ReviewerChange.ofDeleted(e.getValue().getUserLogin())) + .collect(Collectors.toList()) + ); + oldPullRequest.getReviewers() + .removeIf(reviewer -> oldIds.contains(reviewer.getUserLogin())); + if (!reviewerChanges.isEmpty()) { + changeService.createReviewersPr(newPullRequest.getTitle(), newPullRequest.getUrl(), newPullRequest.getAuthorLogin(), reviewerChanges); + } } @NonNull diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/parser/PullRequestBitbucketParser.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/parser/PullRequestBitbucketParser.java index 7f14da7..2900c70 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/parser/PullRequestBitbucketParser.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/parser/PullRequestBitbucketParser.java @@ -114,9 +114,8 @@ public class PullRequestBitbucketParser implements PullRequestParser { private List getExistsPr(@NonNull List pullRequestJsons) { return pullRequestJsons.stream() - .map(json -> pullRequestsService.getFirst(bitbucketIdAndPullRequestId(json))) - .filter(Optional::isPresent) - .map(Optional::get) + .filter(json -> pullRequestsService.exists(bitbucketIdAndPullRequestId(json))) + .map(json -> conversionService.convert(json, PullRequest.class)) .collect(Collectors.toList()); }