Полностью работает механизм с ПР
This commit is contained in:
parent
2f1e929422
commit
7839fceaa4
@ -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<Long> telegramIds,
|
||||
String title,
|
||||
String url,
|
||||
List<ReviewerChange> reviewerChanges) {
|
||||
super(ChangeType.REVIEWERS, Collections.singleton(telegramId), title, url);
|
||||
super(ChangeType.REVIEWERS, telegramIds, title, url);
|
||||
this.reviewerChanges = reviewerChanges;
|
||||
}
|
||||
|
||||
|
@ -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<Reviewer> reviewers;
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "pullRequest")
|
||||
private List<Reviewer> reviewers = new ArrayList<>();
|
||||
|
||||
public void setReviewers(List<Reviewer> reviewers) {
|
||||
reviewers.forEach(reviewer -> reviewer.setPullRequest(this));
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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<ReviewerChange> reviewerChanges);
|
||||
|
||||
/**
|
||||
* Позволяет получить новые изменения.
|
||||
|
@ -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<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())
|
||||
);
|
||||
public Change createReviewersPr(String prName, String prUrl, String authorLogin, List<ReviewerChange> 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()
|
||||
);
|
||||
|
@ -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<PullRequest, Long> implements PullRequestsService {
|
||||
@ -57,23 +62,55 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
|
||||
@Override
|
||||
public PullRequest update(@NonNull PullRequest pullRequest) {
|
||||
final PullRequest oldPullRequest = findAndFillId(pullRequest);
|
||||
oldPullRequest.setBitbucketVersion(pullRequest.getBitbucketVersion());
|
||||
oldPullRequest.setConflict(pullRequest.isConflict());
|
||||
oldPullRequest.setTitle(pullRequest.getTitle());
|
||||
oldPullRequest.setDescription(pullRequest.getDescription());
|
||||
oldPullRequest.setStatus(pullRequest.getStatus());
|
||||
updateReviewers(oldPullRequest, pullRequest);
|
||||
|
||||
if (!oldPullRequest.getBitbucketVersion().equals(pullRequest.getBitbucketVersion())) {
|
||||
oldPullRequest.setBitbucketVersion(pullRequest.getBitbucketVersion());
|
||||
oldPullRequest.setConflict(pullRequest.isConflict());
|
||||
oldPullRequest.setTitle(pullRequest.getTitle());
|
||||
oldPullRequest.setDescription(pullRequest.getDescription());
|
||||
oldPullRequest.setStatus(pullRequest.getStatus());
|
||||
oldPullRequest.setReviewers(pullRequest.getReviewers());
|
||||
|
||||
final PullRequest newPullRequest = pullRequestsRepository.save(oldPullRequest);
|
||||
|
||||
final PullRequest newPullRequest = pullRequestsRepository.save(oldPullRequest);
|
||||
if (!pullRequest.getBitbucketVersion().equals(newPullRequest.getBitbucketVersion())) {
|
||||
changeService.createUpdatePr(pullRequest, newPullRequest);
|
||||
changeService.createReviewersPr(pullRequest, newPullRequest);
|
||||
|
||||
return newPullRequest;
|
||||
}
|
||||
return oldPullRequest;
|
||||
|
||||
return newPullRequest;
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -114,9 +114,8 @@ public class PullRequestBitbucketParser implements PullRequestParser {
|
||||
|
||||
private List<PullRequest> getExistsPr(@NonNull List<PullRequestJson> 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());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user