Новая прослойка Change

This commit is contained in:
upagge 2020-04-07 16:51:25 +03:00
parent 7fe3388207
commit 0f2179832d
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
21 changed files with 447 additions and 117 deletions

View File

@ -1,6 +1,5 @@
package com.tsc.bitbucketbot.dto; package com.tsc.bitbucketbot.domain;
import com.tsc.bitbucketbot.domain.PullRequestStatus;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

View File

@ -1,8 +1,6 @@
package com.tsc.bitbucketbot.domain; package com.tsc.bitbucketbot.domain;
/** /**
* TODO: Добавить комментарий енума.
*
* @author upagge [31.01.2020] * @author upagge [31.01.2020]
*/ */
public enum PullRequestStatus { public enum PullRequestStatus {

View File

@ -0,0 +1,25 @@
package com.tsc.bitbucketbot.domain.change;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.Set;
@Getter
@EqualsAndHashCode(of = "id")
public abstract class Change {
protected final ChangeType type;
protected final LocalDateTime localDateTime = LocalDateTime.now();
protected final Set<Long> telegramId;
@Setter
protected Long id;
protected Change(ChangeType type, Set<Long> telegramId) {
this.type = type;
this.telegramId = telegramId;
}
}

View File

@ -0,0 +1,7 @@
package com.tsc.bitbucketbot.domain.change;
public enum ChangeType {
STATUS_PR, UPDATE_PR, REVIEWERS, NEW_PR
}

View File

@ -0,0 +1,29 @@
package com.tsc.bitbucketbot.domain.change;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import java.util.Set;
@Getter
@EqualsAndHashCode(callSuper = true)
public class NewPrChange extends PrChange {
private final String description;
private final String author;
@Builder
private NewPrChange(
@Singular("telegramId") Set<Long> telegramId,
String name,
String url,
String description,
String author) {
super(ChangeType.NEW_PR, telegramId, name, url);
this.description = description;
this.author = author;
}
}

View File

@ -0,0 +1,21 @@
package com.tsc.bitbucketbot.domain.change;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.Set;
@Getter
@EqualsAndHashCode(callSuper = true)
public abstract class PrChange extends Change {
private final String name;
private final String url;
protected PrChange(ChangeType type, Set<Long> telegramId, String name, String url) {
super(type, telegramId);
this.name = name;
this.url = url;
}
}

View File

@ -0,0 +1,28 @@
package com.tsc.bitbucketbot.domain.change;
import com.tsc.bitbucketbot.domain.util.ReviewerChange;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import java.util.List;
import java.util.Set;
@Getter
@EqualsAndHashCode(callSuper = true)
public class ReviewersPrChange extends PrChange {
private final List<ReviewerChange> reviewerChanges;
@Builder
private ReviewersPrChange(
@Singular("telegramId") Set<Long> telegramId,
String name,
String url,
List<ReviewerChange> reviewerChanges) {
super(ChangeType.REVIEWERS, telegramId, name, url);
this.reviewerChanges = reviewerChanges;
}
}

View File

@ -0,0 +1,30 @@
package com.tsc.bitbucketbot.domain.change;
import com.tsc.bitbucketbot.domain.PullRequestStatus;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import java.util.Set;
@Getter
@EqualsAndHashCode(callSuper = true)
public class StatusPrChange extends PrChange {
private final PullRequestStatus oldStatus;
private final PullRequestStatus newStatus;
@Builder
private StatusPrChange(
@Singular("telegramId") Set<Long> telegramId,
String name,
String url,
PullRequestStatus oldStatus,
PullRequestStatus newStatus) {
super(ChangeType.STATUS_PR, telegramId, name, url);
this.oldStatus = oldStatus;
this.newStatus = newStatus;
}
}

View File

@ -0,0 +1,25 @@
package com.tsc.bitbucketbot.domain.change;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import java.util.Set;
@Getter
@EqualsAndHashCode(callSuper = true)
public class UpdatePrChange extends PrChange {
private final String author;
@Builder
private UpdatePrChange(
@Singular("telegramId") Set<Long> telegramId,
String name,
String url, String author) {
super(ChangeType.UPDATE_PR, telegramId, name, url);
this.author = author;
}
}

View File

@ -2,7 +2,7 @@ package com.tsc.bitbucketbot.exception;
class BitbucketBotException extends RuntimeException { class BitbucketBotException extends RuntimeException {
BitbucketBotException(String message) { public BitbucketBotException(String message) {
super(message); super(message);
} }

View File

@ -0,0 +1,9 @@
package com.tsc.bitbucketbot.exception;
public class NotFoundException extends BitbucketBotException {
public NotFoundException(String message) {
super(message);
}
}

View File

@ -0,0 +1,16 @@
package com.tsc.bitbucketbot.repository;
import com.tsc.bitbucketbot.domain.change.Change;
import lombok.NonNull;
import java.util.List;
public interface ChangeRepository {
void add(@NonNull Change change);
List<Change> getAll();
void deleteAll(@NonNull List<Change> changes);
}

View File

@ -0,0 +1,35 @@
package com.tsc.bitbucketbot.repository.impl;
import com.tsc.bitbucketbot.domain.change.Change;
import com.tsc.bitbucketbot.repository.ChangeRepository;
import lombok.NonNull;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public class ChangeRepositoryImpl implements ChangeRepository {
private List<Change> list = new ArrayList<>();
private long count = 0;
@Override
public void add(@NonNull Change change) {
change.setId(count++);
list.add(change);
}
@Override
public List<Change> getAll() {
final ArrayList<Change> changes = new ArrayList<>(list);
return changes;
}
@Override
public void deleteAll(@NonNull List<Change> changes) {
list.removeAll(changes);
}
}

View File

@ -1,9 +1,9 @@
package com.tsc.bitbucketbot.repository.jpa; package com.tsc.bitbucketbot.repository.jpa;
import com.tsc.bitbucketbot.domain.IdAndStatusPr;
import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
@ -34,8 +34,8 @@ public interface PullRequestsRepository extends JpaRepository<PullRequest, Long>
@Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE p.author.login=:author AND r.status=:reviewerStatus") @Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE p.author.login=:author AND r.status=:reviewerStatus")
List<PullRequest> findAllByAuthorAndReviewerStatus(@Param("author") String author, @Param("reviewerStatus") ReviewerStatus reviewerStatus); List<PullRequest> findAllByAuthorAndReviewerStatus(@Param("author") String author, @Param("reviewerStatus") ReviewerStatus reviewerStatus);
@Query("SELECT new com.tsc.bitbucketbot.dto.IdAndStatusPr(p.id, p.status) FROM PullRequest p WHERE p.status IN :statuses") @Query("SELECT new com.tsc.bitbucketbot.domain.IdAndStatusPr(p.id, p.status) FROM PullRequest p WHERE p.status IN :statuses")
Set<IdAndStatusPr> findAllIdByStatusIn(@Param("statuses") Collection<PullRequestStatus> statuses); Set<IdAndStatusPr> findAllIdByStatusIn(@Param("statuses") Set<PullRequestStatus> statuses);
@Query("SELECT p.id from PullRequest p") @Query("SELECT p.id from PullRequest p")
Set<Long> findAllIds(); Set<Long> findAllIds();

View File

@ -0,0 +1,68 @@
package com.tsc.bitbucketbot.scheduler;
import com.tsc.bitbucketbot.domain.change.Change;
import com.tsc.bitbucketbot.domain.change.NewPrChange;
import com.tsc.bitbucketbot.domain.change.ReviewersPrChange;
import com.tsc.bitbucketbot.domain.change.StatusPrChange;
import com.tsc.bitbucketbot.domain.change.UpdatePrChange;
import com.tsc.bitbucketbot.exception.NotFoundException;
import com.tsc.bitbucketbot.service.ChangeService;
import com.tsc.bitbucketbot.service.MessageSendService;
import com.tsc.bitbucketbot.utils.Message;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class SchedulerChangeParsing {
private final MessageSendService messageSendService;
private final ChangeService changeService;
// @Scheduled(cron = "0 * * * * *")
@Scheduled(fixedRate = 5000)
public void parsing() {
final List<Change> newChange = changeService.getNew().stream()
.filter(change -> change.getTelegramId() != null && !change.getTelegramId().isEmpty())
.collect(Collectors.toList());
for (Change change : newChange) {
final String message = generateMessage(change);
System.out.println(message);
// change.getTelegramId().forEach(
// telegramId -> messageSendService.add(
// MessageSend.builder()
// .telegramId(telegramId)
// .message(message)
// .build()
// )
// );
}
}
private String generateMessage(@NonNull Change change) {
String message;
switch (change.getType()) {
case NEW_PR:
message = Message.generate(((NewPrChange) change));
break;
case REVIEWERS:
message = Message.generate(((ReviewersPrChange) change));
break;
case STATUS_PR:
message = Message.generate(((StatusPrChange) change));
break;
case UPDATE_PR:
message = Message.generate(((UpdatePrChange) change));
break;
default:
throw new NotFoundException("Нет обработчика для типа " + change.getType().name());
}
return message;
}
}

View File

@ -1,22 +1,23 @@
package com.tsc.bitbucketbot.scheduler; package com.tsc.bitbucketbot.scheduler;
import com.tsc.bitbucketbot.config.BitbucketConfig; import com.tsc.bitbucketbot.config.BitbucketConfig;
import com.tsc.bitbucketbot.domain.MessageSend; import com.tsc.bitbucketbot.domain.IdAndStatusPr;
import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.change.NewPrChange;
import com.tsc.bitbucketbot.domain.change.ReviewersPrChange;
import com.tsc.bitbucketbot.domain.change.StatusPrChange;
import com.tsc.bitbucketbot.domain.change.UpdatePrChange;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.domain.entity.Reviewer; import com.tsc.bitbucketbot.domain.entity.Reviewer;
import com.tsc.bitbucketbot.domain.entity.User; import com.tsc.bitbucketbot.domain.entity.User;
import com.tsc.bitbucketbot.domain.util.ReviewerChange; import com.tsc.bitbucketbot.domain.util.ReviewerChange;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import com.tsc.bitbucketbot.dto.bitbucket.sheet.PullRequestSheetJson; import com.tsc.bitbucketbot.dto.bitbucket.sheet.PullRequestSheetJson;
import com.tsc.bitbucketbot.service.MessageSendService; import com.tsc.bitbucketbot.service.ChangeService;
import com.tsc.bitbucketbot.service.PullRequestsService; import com.tsc.bitbucketbot.service.PullRequestsService;
import com.tsc.bitbucketbot.service.UserService; import com.tsc.bitbucketbot.service.UserService;
import com.tsc.bitbucketbot.service.Utils; import com.tsc.bitbucketbot.service.Utils;
import com.tsc.bitbucketbot.utils.Message;
import com.tsc.bitbucketbot.utils.Pair; import com.tsc.bitbucketbot.utils.Pair;
import com.tsc.bitbucketbot.utils.Smile;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
@ -35,6 +36,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.DECLINED; import static com.tsc.bitbucketbot.domain.PullRequestStatus.DECLINED;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.DELETE;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.MERGED; import static com.tsc.bitbucketbot.domain.PullRequestStatus.MERGED;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.OPEN; import static com.tsc.bitbucketbot.domain.PullRequestStatus.OPEN;
@ -49,7 +51,7 @@ public class SchedulerPullRequest {
private final PullRequestsService pullRequestsService; private final PullRequestsService pullRequestsService;
private final UserService userService; private final UserService userService;
private final MessageSendService messageSendService; private final ChangeService changeService;
private final ConversionService conversionService; private final ConversionService conversionService;
private final BitbucketConfig bitbucketConfig; private final BitbucketConfig bitbucketConfig;
@ -72,10 +74,13 @@ public class SchedulerPullRequest {
final Set<PullRequest> deletePr = pullRequestsService.getAllById(ids); final Set<PullRequest> deletePr = pullRequestsService.getAllById(ids);
deletePr.stream() deletePr.stream()
.filter(pullRequest -> pullRequest.getAuthor().getTelegramId() != null) .filter(pullRequest -> pullRequest.getAuthor().getTelegramId() != null)
.forEach(pullRequest -> messageSendService.add( .forEach(pullRequest -> changeService.add(
MessageSend.builder() StatusPrChange.builder()
.name(pullRequest.getName())
.url(pullRequest.getUrl())
.oldStatus(pullRequest.getStatus())
.newStatus(DELETE)
.telegramId(pullRequest.getAuthor().getTelegramId()) .telegramId(pullRequest.getAuthor().getTelegramId())
.message(Message.statusPullRequest(pullRequest.getName(), pullRequest.getUrl(), pullRequest.getStatus(), PullRequestStatus.DELETE))
.build() .build()
)); ));
pullRequestsService.updateAll( pullRequestsService.updateAll(
@ -98,12 +103,15 @@ public class SchedulerPullRequest {
.filter(pullRequest -> pullRequest.getId() != null) .filter(pullRequest -> pullRequest.getId() != null)
.collect(Collectors.toList()); .collect(Collectors.toList());
for (PullRequest pullRequest : newPrs) { for (PullRequest pullRequest : newPrs) {
final User author = pullRequest.getAuthor(); changeService.add(
final Long telegramId = author.getTelegramId(); StatusPrChange.builder()
if (telegramId != null) { .name(pullRequest.getName())
final String message = Message.statusPullRequest(pullRequest.getName(), pullRequest.getUrl(), PullRequestStatus.OPEN, pullRequest.getStatus()); .url(pullRequest.getUrl())
messageSendService.add(MessageSend.builder().telegramId(telegramId).message(message).build()); .oldStatus(pullRequest.getStatus())
} .newStatus(OPEN)
.telegramId(pullRequest.getAuthor().getTelegramId())
.build()
);
} }
ids.addAll( ids.addAll(
@ -165,62 +173,37 @@ public class SchedulerPullRequest {
} }
private void processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) { private void processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) {
final Set<String> logins = newPullRequest.getReviewers().stream() if (isUpdatePr(pullRequest, newPullRequest)) {
.map(Reviewer::getUser) final Set<String> logins = newPullRequest.getReviewers().stream()
.collect(Collectors.toSet()); .map(Reviewer::getUser)
if (!logins.isEmpty()) { .collect(Collectors.toSet());
Optional<String> optMessage = changeVersionPr(pullRequest, newPullRequest); final List<Long> telegramIds = userService.getAllTelegramIdByLogin(logins);
if (optMessage.isPresent()) { changeService.add(
final String message = optMessage.get(); UpdatePrChange.builder()
userService.getAllTelegramIdByLogin(logins).forEach( .name(newPullRequest.getName())
telegramId -> messageSendService.add( .url(newPullRequest.getUrl())
MessageSend.builder() .author(newPullRequest.getAuthor().getLogin())
.telegramId(telegramId) .telegramId(telegramIds)
.message(message) .build()
.build() );
)
);
}
} }
} }
@NonNull @NonNull
private void processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) { private void processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) {
final User author = pullRequest.getAuthor(); changeStatusPR(pullRequest, newPullRequest);
StringBuilder builderMessage = new StringBuilder(); changeReviewersPR(pullRequest, newPullRequest);
if (author.getTelegramId() != null) {
changeStatusPR(pullRequest, newPullRequest).ifPresent(builderMessage::append);
changeReviewersPR(pullRequest, newPullRequest).ifPresent(builderMessage::append);
final String message = builderMessage.toString();
if (!Smile.Constants.EMPTY.equalsIgnoreCase(message)) {
messageSendService.add(
MessageSend.builder()
.message(message)
.telegramId(author.getTelegramId())
.build()
);
}
}
} }
@NonNull @NonNull
private Optional<String> changeVersionPr(PullRequest pullRequest, PullRequest newPullRequest) { private boolean isUpdatePr(PullRequest pullRequest, PullRequest newPullRequest) {
LocalDateTime oldDate = pullRequest.getUpdateDate(); LocalDateTime oldDate = pullRequest.getUpdateDate();
LocalDateTime newDate = newPullRequest.getUpdateDate(); LocalDateTime newDate = newPullRequest.getUpdateDate();
if (!oldDate.isEqual(newDate)) { return !oldDate.isEqual(newDate);
return Optional.of(
Message.updatePullRequest(
newPullRequest.getName(),
newPullRequest.getUrl(),
newPullRequest.getAuthor().getLogin()
)
);
}
return Optional.empty();
} }
@NonNull @NonNull
private Optional<String> changeReviewersPR(PullRequest pullRequest, PullRequest newPullRequest) { private void changeReviewersPR(PullRequest pullRequest, PullRequest newPullRequest) {
final Map<String, Reviewer> oldReviewers = pullRequest.getReviewers().stream() final Map<String, Reviewer> oldReviewers = pullRequest.getReviewers().stream()
.collect(Collectors.toMap(Reviewer::getUser, reviewer -> reviewer)); .collect(Collectors.toMap(Reviewer::getUser, reviewer -> reviewer));
final Map<String, Reviewer> newReviewers = newPullRequest.getReviewers().stream() final Map<String, Reviewer> newReviewers = newPullRequest.getReviewers().stream()
@ -241,21 +224,36 @@ public class SchedulerPullRequest {
final Set<String> oldLogins = oldReviewers.keySet(); final Set<String> oldLogins = oldReviewers.keySet();
oldLogins.removeAll(newReviewers.keySet()); oldLogins.removeAll(newReviewers.keySet());
oldLogins.forEach(login -> reviewerChanges.add(ReviewerChange.ofDeleted(login))); oldLogins.forEach(login -> reviewerChanges.add(ReviewerChange.ofDeleted(login)));
return Message.statusReviewers(pullRequest, reviewerChanges); if (!reviewerChanges.isEmpty()) {
changeService.add(
ReviewersPrChange.builder()
.name(pullRequest.getName())
.url(pullRequest.getUrl())
.reviewerChanges(reviewerChanges)
.telegramId(newPullRequest.getAuthor().getTelegramId())
.build()
);
}
} }
@NonNull @NonNull
private Optional<String> changeStatusPR(PullRequest pullRequest, PullRequest newPullRequest) { private void changeStatusPR(PullRequest pullRequest, PullRequest newPullRequest) {
final PullRequestStatus oldStatus = pullRequest.getStatus(); final PullRequestStatus oldStatus = pullRequest.getStatus();
final PullRequestStatus newStatus = newPullRequest.getStatus(); final PullRequestStatus newStatus = newPullRequest.getStatus();
if (!oldStatus.equals(newStatus)) { if (!oldStatus.equals(newStatus)) {
return Optional.of(Message.statusPullRequest(pullRequest.getName(), pullRequest.getUrl(), oldStatus, newStatus)); changeService.add(
StatusPrChange.builder()
.name(newPullRequest.getName())
.url(newPullRequest.getUrl())
.oldStatus(oldStatus)
.newStatus(newStatus)
.telegramId(newPullRequest.getAuthor().getTelegramId())
.build()
);
} }
return Optional.empty();
} }
@Scheduled(fixedRate = 30000) @Scheduled(fixedRate = 30000)
public void checkNewPullRequest() { public void checkNewPullRequest() {
final List<User> users = userService.getAllRegister(); final List<User> users = userService.getAllRegister();
@ -283,19 +281,20 @@ public class SchedulerPullRequest {
private void sendNotificationNewPullRequest(@NonNull List<PullRequest> newPullRequests) { private void sendNotificationNewPullRequest(@NonNull List<PullRequest> newPullRequests) {
if (!newPullRequests.isEmpty()) { if (!newPullRequests.isEmpty()) {
newPullRequests.forEach( for (PullRequest newPullRequest : newPullRequests) {
pullRequest -> pullRequest.getReviewers().stream() final List<Long> reviewerTelegramIds = userService.getAllTelegramIdByLogin(newPullRequest.getReviewers().stream()
.map(reviewer -> userService.getByLogin(reviewer.getUser())) .map(Reviewer::getUser)
.filter(Optional::isPresent) .collect(Collectors.toSet()));
.map(Optional::get) changeService.add(
.filter(user -> user.getTelegramId() != null) NewPrChange.builder()
.forEach(user -> messageSendService.add( .name(newPullRequest.getName())
MessageSend.builder() .url(newPullRequest.getUrl())
.telegramId(user.getTelegramId()) .description(newPullRequest.getDescription())
.message(Message.newPullRequest(pullRequest)) .author(newPullRequest.getAuthor().getLogin())
.build() .telegramId(reviewerTelegramIds)
)) .build()
); );
}
} }
} }

View File

@ -0,0 +1,14 @@
package com.tsc.bitbucketbot.service;
import com.tsc.bitbucketbot.domain.change.Change;
import lombok.NonNull;
import java.util.List;
public interface ChangeService {
void add(@NonNull Change change);
List<Change> getNew();
}

View File

@ -1,10 +1,10 @@
package com.tsc.bitbucketbot.service; package com.tsc.bitbucketbot.service;
import com.tsc.bitbucketbot.domain.IdAndStatusPr;
import com.tsc.bitbucketbot.domain.Pagination; import com.tsc.bitbucketbot.domain.Pagination;
import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -37,7 +37,7 @@ public interface PullRequestsService {
Set<Long> getAllId(); Set<Long> getAllId();
Set<IdAndStatusPr> getAllId(Collection<PullRequestStatus> statuses); Set<IdAndStatusPr> getAllId(Set<PullRequestStatus> statuses);
Page<PullRequest> getAll(@NonNull Pagination pagination); Page<PullRequest> getAll(@NonNull Pagination pagination);

View File

@ -0,0 +1,31 @@
package com.tsc.bitbucketbot.service.impl;
import com.tsc.bitbucketbot.domain.change.Change;
import com.tsc.bitbucketbot.repository.ChangeRepository;
import com.tsc.bitbucketbot.service.ChangeService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class ChangeServiceImpl implements ChangeService {
private final ChangeRepository changeRepository;
@Override
public void add(@NonNull Change change) {
changeRepository.add(change);
}
@Override
public List<Change> getNew() {
final List<Change> changes = changeRepository.getAll();
changeRepository.deleteAll(changes);
return changes;
}
}

View File

@ -1,10 +1,10 @@
package com.tsc.bitbucketbot.service.impl; package com.tsc.bitbucketbot.service.impl;
import com.tsc.bitbucketbot.domain.IdAndStatusPr;
import com.tsc.bitbucketbot.domain.Pagination; import com.tsc.bitbucketbot.domain.Pagination;
import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import com.tsc.bitbucketbot.repository.jpa.PullRequestsRepository; import com.tsc.bitbucketbot.repository.jpa.PullRequestsRepository;
import com.tsc.bitbucketbot.service.PullRequestsService; import com.tsc.bitbucketbot.service.PullRequestsService;
import lombok.NonNull; import lombok.NonNull;
@ -79,7 +79,7 @@ public class PullRequestsServiceImpl implements PullRequestsService {
} }
@Override @Override
public Set<IdAndStatusPr> getAllId(Collection<PullRequestStatus> statuses) { public Set<IdAndStatusPr> getAllId(Set<PullRequestStatus> statuses) {
return pullRequestsRepository.findAllIdByStatusIn(statuses); return pullRequestsRepository.findAllIdByStatusIn(statuses);
} }

View File

@ -1,6 +1,9 @@
package com.tsc.bitbucketbot.utils; package com.tsc.bitbucketbot.utils;
import com.tsc.bitbucketbot.domain.PullRequestStatus; import com.tsc.bitbucketbot.domain.change.NewPrChange;
import com.tsc.bitbucketbot.domain.change.ReviewersPrChange;
import com.tsc.bitbucketbot.domain.change.StatusPrChange;
import com.tsc.bitbucketbot.domain.change.UpdatePrChange;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.domain.util.ReviewerChange; import com.tsc.bitbucketbot.domain.util.ReviewerChange;
import com.tsc.bitbucketbot.dto.bitbucket.CommentJson; import com.tsc.bitbucketbot.dto.bitbucket.CommentJson;
@ -9,10 +12,11 @@ import lombok.NonNull;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*; import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.DELETED;
import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.NEW;
import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.OLD;
/** /**
* Генерирует сообщения для отправки. * Генерирует сообщения для отправки.
@ -24,36 +28,35 @@ public class Message {
private static final UpdateDataComparator COMPARATOR = new UpdateDataComparator(); private static final UpdateDataComparator COMPARATOR = new UpdateDataComparator();
private static final Integer PR_COUNT = 4; private static final Integer PR_COUNT = 4;
private static final String DONATION_LINK = "https://www.tinkoff.ru/sl/1T9s4esiMf"; private static final String DONATION_LINK = "https://www.tinkoff.ru/sl/1T9s4esiMf";
public static final String HELP_LINK = "https://nuzhnapomosh.ru/about/"; private static final String HELP_LINK = "https://nuzhnapomosh.ru/about/";
private Message() { private Message() {
throw new IllegalStateException("Утилитарный класс"); throw new IllegalStateException("Утилитарный класс");
} }
@NonNull @NonNull
public static String newPullRequest(PullRequest pullRequest) { public static String generate(NewPrChange newPrChange) {
String message = Smile.FUN + " *Новый Pull Request*" + Smile.BR + String message = Smile.FUN + " *Новый Pull Request*" + Smile.BR +
link(pullRequest.getName(), pullRequest.getUrl()) + link(newPrChange.getName(), newPrChange.getUrl()) +
Smile.HR; Smile.HR;
if (pullRequest.getDescription() != null && !"".equals(pullRequest.getDescription())) { if (newPrChange.getDescription() != null && !"".equals(newPrChange.getDescription())) {
message += pullRequest.getDescription() + Smile.HR; message += newPrChange.getDescription() + Smile.HR;
} }
message += Smile.AUTHOR + ": " + pullRequest.getAuthor().getLogin() + Smile.TWO_BR; message += Smile.AUTHOR + ": " + newPrChange.getAuthor() + Smile.TWO_BR;
return message; return message;
} }
@NonNull public static String generate(@NonNull StatusPrChange change) {
public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) {
return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR + return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR +
link(name, url) + Smile.BR + link(change.getName(), change.getUrl()) + Smile.BR +
oldStatus.name() + " -> " + newStatus.name() + change.getOldStatus().name() + " -> " + change.getNewStatus().name() +
Smile.TWO_BR; Smile.TWO_BR;
} }
@NonNull @NonNull
public static Optional<String> statusReviewers(PullRequest pullRequest, List<ReviewerChange> reviewerChanges) { public static String generate(@NonNull ReviewersPrChange reviewersChange) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
final Map<ReviewerChange.Type, List<ReviewerChange>> changes = reviewerChanges.stream() final Map<ReviewerChange.Type, List<ReviewerChange>> changes = reviewersChange.getReviewerChanges().stream()
.collect(Collectors.groupingBy(ReviewerChange::getType)); .collect(Collectors.groupingBy(ReviewerChange::getType));
if (changes.containsKey(OLD)) { if (changes.containsKey(OLD)) {
stringBuilder.append(Smile.BR).append("Изменили свое решение:").append(Smile.BR); stringBuilder.append(Smile.BR).append("Изменили свое решение:").append(Smile.BR);
@ -80,25 +83,18 @@ public class Message {
.map(ReviewerChange::getName).collect(Collectors.joining(",")) .map(ReviewerChange::getName).collect(Collectors.joining(","))
); );
} }
final String createMessage = stringBuilder.toString(); final String createMessage = stringBuilder.toString();
if (!Smile.Constants.EMPTY.equalsIgnoreCase(createMessage)) { return Smile.PEN + " *Изменения ревьюверов вашего ПР*" +
return Optional.of( Smile.HR +
Smile.PEN + " *Изменения ревьюверов вашего ПР*" + link(reviewersChange.getName(), reviewersChange.getUrl()) + Smile.BR +
Smile.HR + createMessage;
link(pullRequest.getName(), pullRequest.getUrl()) + Smile.BR +
createMessage
);
}
return Optional.empty();
} }
@NonNull public static String generate(@NonNull UpdatePrChange change) {
public static String updatePullRequest(String pullRequestName, String prUrl, String author) {
return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BR + return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BR +
link(pullRequestName, prUrl) + link(change.getName(), change.getUrl()) +
Smile.HR + Smile.HR +
Smile.AUTHOR + ": " + author + Smile.AUTHOR + ": " + change.getAuthor() +
Smile.TWO_BR; Smile.TWO_BR;
} }