Полностью работает механизм с ПР

This commit is contained in:
upagge 2020-09-08 21:28:08 +03:00
parent 2f1e929422
commit 7839fceaa4
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
8 changed files with 73 additions and 58 deletions

View File

@ -6,8 +6,8 @@ import lombok.Getter;
import org.sadtech.bot.bitbucketbot.domain.change.ChangeType; import org.sadtech.bot.bitbucketbot.domain.change.ChangeType;
import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange; import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
@Getter @Getter
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ -17,11 +17,11 @@ public class ReviewersPrChange extends PrChange {
@Builder @Builder
private ReviewersPrChange( private ReviewersPrChange(
Long telegramId, Set<Long> telegramIds,
String title, String title,
String url, String url,
List<ReviewerChange> reviewerChanges) { List<ReviewerChange> reviewerChanges) {
super(ChangeType.REVIEWERS, Collections.singleton(telegramId), title, url); super(ChangeType.REVIEWERS, telegramIds, title, url);
this.reviewerChanges = reviewerChanges; this.reviewerChanges = reviewerChanges;
} }

View File

@ -14,10 +14,10 @@ import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -123,9 +123,8 @@ public class PullRequest {
/** /**
* Ревьюверы * Ревьюверы
*/ */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "pullRequest")
@JoinColumn(name = "pull_request_id") private List<Reviewer> reviewers = new ArrayList<>();
private List<Reviewer> reviewers;
public void setReviewers(List<Reviewer> reviewers) { public void setReviewers(List<Reviewer> reviewers) {
reviewers.forEach(reviewer -> reviewer.setPullRequest(this)); reviewers.forEach(reviewer -> reviewer.setPullRequest(this));

View File

@ -5,6 +5,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus;
import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
@ -13,6 +14,7 @@ import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@ -50,7 +52,8 @@ public class Reviewer {
@Column(name = "status") @Column(name = "status")
private ReviewerStatus 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; private PullRequest pullRequest;
} }

View File

@ -63,6 +63,7 @@ public class SchedulerPushMessageSend {
final String json = objectMapper.writeValueAsString(pushMessage); final String json = objectMapper.writeValueAsString(pushMessage);
// sendMessage(json); // sendMessage(json);
System.out.println(json); System.out.println(json);
System.out.println("\n\n");
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }

View File

@ -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.NewPrChange;
import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.UpdatePrChange; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.UpdatePrChange;
import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest;
import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange;
import java.util.List; import java.util.List;
@ -20,7 +21,7 @@ public interface ChangeService {
UpdatePrChange createUpdatePr(@NonNull PullRequest oldPullRequest, @NonNull PullRequest newPullRequest); 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<ReviewerChange> reviewerChanges);
/** /**
* Позволяет получить новые изменения. * Позволяет получить новые изменения.

View File

@ -2,7 +2,6 @@ package org.sadtech.bot.bitbucketbot.service.impl;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; 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.Change;
import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange; import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.NewPrChange;
import org.sadtech.bot.bitbucketbot.domain.change.pullrequest.ReviewersPrChange; 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.sadtech.bot.bitbucketbot.service.PersonService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -54,37 +52,14 @@ public class ChangeServiceImpl implements ChangeService {
} }
@Override @Override
public Change createReviewersPr(@NonNull PullRequest oldPullRequest, @NonNull PullRequest newPullRequest) { public Change createReviewersPr(String prName, String prUrl, String authorLogin, List<ReviewerChange> reviewerChanges) {
final Map<Long, Reviewer> oldReviewers = oldPullRequest.getReviewers().stream()
.collect(Collectors.toMap(Reviewer::getId, reviewer -> reviewer));
final Map<Long, Reviewer> newReviewers = newPullRequest.getReviewers().stream()
.collect(Collectors.toMap(Reviewer::getId, reviewer -> reviewer));
final List<ReviewerChange> 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<Long> 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())
);
return changeRepository.add( return changeRepository.add(
ReviewersPrChange.builder() ReviewersPrChange.builder()
.title(newPullRequest.getTitle()) .title(prName)
.url(newPullRequest.getUrl()) .url(prUrl)
.telegramId(personService.getTelegramIdByLogin(newPullRequest.getAuthorLogin()).orElse(null)) .telegramIds(
personService.getAllTelegramIdByLogin(Collections.singleton(authorLogin))
)
.reviewerChanges(reviewerChanges) .reviewerChanges(reviewerChanges)
.build() .build()
); );

View File

@ -13,7 +13,9 @@ import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus;
import org.sadtech.bot.bitbucketbot.domain.ReviewerStatus; 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.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.filter.PullRequestFilter;
import org.sadtech.bot.bitbucketbot.domain.util.ReviewerChange;
import org.sadtech.bot.bitbucketbot.exception.UpdateException; import org.sadtech.bot.bitbucketbot.exception.UpdateException;
import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository; import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository;
import org.sadtech.bot.bitbucketbot.service.ChangeService; 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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
@Service @Service
public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRequest, Long> implements PullRequestsService { public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRequest, Long> implements PullRequestsService {
@ -57,23 +62,55 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
@Override @Override
public PullRequest update(@NonNull PullRequest pullRequest) { public PullRequest update(@NonNull PullRequest pullRequest) {
final PullRequest oldPullRequest = findAndFillId(pullRequest); final PullRequest oldPullRequest = findAndFillId(pullRequest);
if (!oldPullRequest.getBitbucketVersion().equals(pullRequest.getBitbucketVersion())) {
oldPullRequest.setBitbucketVersion(pullRequest.getBitbucketVersion()); oldPullRequest.setBitbucketVersion(pullRequest.getBitbucketVersion());
oldPullRequest.setConflict(pullRequest.isConflict()); oldPullRequest.setConflict(pullRequest.isConflict());
oldPullRequest.setTitle(pullRequest.getTitle()); oldPullRequest.setTitle(pullRequest.getTitle());
oldPullRequest.setDescription(pullRequest.getDescription()); oldPullRequest.setDescription(pullRequest.getDescription());
oldPullRequest.setStatus(pullRequest.getStatus()); oldPullRequest.setStatus(pullRequest.getStatus());
oldPullRequest.setReviewers(pullRequest.getReviewers()); updateReviewers(oldPullRequest, pullRequest);
final PullRequest newPullRequest = pullRequestsRepository.save(oldPullRequest); final PullRequest newPullRequest = pullRequestsRepository.save(oldPullRequest);
if (!pullRequest.getBitbucketVersion().equals(newPullRequest.getBitbucketVersion())) {
changeService.createUpdatePr(pullRequest, newPullRequest); changeService.createUpdatePr(pullRequest, newPullRequest);
changeService.createReviewersPr(pullRequest, newPullRequest); }
return newPullRequest; return newPullRequest;
} }
return oldPullRequest;
private void updateReviewers(PullRequest oldPullRequest, PullRequest newPullRequest) {
final Map<String, Reviewer> oldReviewers = oldPullRequest.getReviewers().stream()
.collect(Collectors.toMap(Reviewer::getUserLogin, reviewer -> reviewer));
final Map<String, Reviewer> newReviewers = newPullRequest.getReviewers().stream()
.collect(Collectors.toMap(Reviewer::getUserLogin, reviewer -> reviewer));
final List<ReviewerChange> 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<String> 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 @NonNull

View File

@ -114,9 +114,8 @@ public class PullRequestBitbucketParser implements PullRequestParser {
private List<PullRequest> getExistsPr(@NonNull List<PullRequestJson> pullRequestJsons) { private List<PullRequest> getExistsPr(@NonNull List<PullRequestJson> pullRequestJsons) {
return pullRequestJsons.stream() return pullRequestJsons.stream()
.map(json -> pullRequestsService.getFirst(bitbucketIdAndPullRequestId(json))) .filter(json -> pullRequestsService.exists(bitbucketIdAndPullRequestId(json)))
.filter(Optional::isPresent) .map(json -> conversionService.convert(json, PullRequest.class))
.map(Optional::get)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }