diff --git a/src/main/java/com/tsc/bitbucketbot/domain/entity/User.java b/src/main/java/com/tsc/bitbucketbot/domain/entity/User.java index 18dcf33..72e10e7 100644 --- a/src/main/java/com/tsc/bitbucketbot/domain/entity/User.java +++ b/src/main/java/com/tsc/bitbucketbot/domain/entity/User.java @@ -39,4 +39,7 @@ public class User { @Column(name = "telegram_id") private Long telegramId; + @Column(name = "full_name") + private String fullName; + } diff --git a/src/main/java/com/tsc/bitbucketbot/repository/jpa/PullRequestsRepository.java b/src/main/java/com/tsc/bitbucketbot/repository/jpa/PullRequestsRepository.java index 4b6f6be..b14b6e5 100644 --- a/src/main/java/com/tsc/bitbucketbot/repository/jpa/PullRequestsRepository.java +++ b/src/main/java/com/tsc/bitbucketbot/repository/jpa/PullRequestsRepository.java @@ -1,5 +1,6 @@ package com.tsc.bitbucketbot.repository.jpa; +import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.entity.PullRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -26,8 +27,8 @@ public interface PullRequestsRepository extends JpaRepository void deleteAllByIdIn(Collection id); - @Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer") - List findAllByReviewers(@Param("reviewer") String reviewer); + @Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer AND r.status =:status") + List findAllByReviewerAndStatuses(@Param("reviewer") String reviewer, @Param("status") ReviewerStatus status); @Query("SELECT p.id from PullRequest p") Set getAllIds(); diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerNotification.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerNotification.java index 9e265fe..68b9d19 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerNotification.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerNotification.java @@ -1,28 +1,40 @@ -//package com.tsc.bitbucketbot.scheduler; -// -//import com.tsc.bitbucketbot.domain.entity.PullRequest; -//import com.tsc.bitbucketbot.domain.entity.User; -//import com.tsc.bitbucketbot.service.PullRequestsService; -//import com.tsc.bitbucketbot.service.UserService; -//import lombok.RequiredArgsConstructor; -//import org.springframework.scheduling.annotation.Scheduled; -//import org.springframework.stereotype.Service; -// -//import java.util.List; -// -//@Service -//@RequiredArgsConstructor -//public class SchedulerNotification { -// -// private final UserService userService; -// private final PullRequestsService pullRequestsService; -// -//// @Scheduled(cron = "0 9 * * MON-FRI") -// @Scheduled(fixedRate = 50000) -// public void goodMorning() { -// List users = userService.getAllRegister(); -// List mstruchkov = pullRequestsService.getAllByReviewer("mstruchkov"); -// System.out.println(); -// } -// -//} +package com.tsc.bitbucketbot.scheduler; + +import com.tsc.bitbucketbot.domain.MessageSend; +import com.tsc.bitbucketbot.domain.ReviewerStatus; +import com.tsc.bitbucketbot.domain.entity.PullRequest; +import com.tsc.bitbucketbot.domain.entity.User; +import com.tsc.bitbucketbot.service.MessageSendService; +import com.tsc.bitbucketbot.service.PullRequestsService; +import com.tsc.bitbucketbot.service.UserService; +import com.tsc.bitbucketbot.utils.Message; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class SchedulerNotification { + + private final UserService userService; + private final PullRequestsService pullRequestsService; + private final MessageSendService messageSendService; + + @Scheduled(cron = "0 9 * * MON-FRI") + public void goodMorning() { + User user = userService.getByLogin("mstruchkov").get(); + List pullRequests = pullRequestsService.getAllByReviewerAndStatuses( + user.getLogin(), + ReviewerStatus.NEEDS_WORK + ); + messageSendService.add( + MessageSend.builder() + .telegramId(3000811L) + .message(Message.goodMorningStatistic(user.getFullName(), pullRequests)) + .build() + ); + } + +} diff --git a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java index 571e9f0..a0b0fa6 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java +++ b/src/main/java/com/tsc/bitbucketbot/service/PullRequestsService.java @@ -1,5 +1,6 @@ package com.tsc.bitbucketbot.service; +import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.entity.PullRequest; import lombok.NonNull; @@ -29,7 +30,8 @@ public interface PullRequestsService { void deleteAll(@NonNull Set id); - List getAllByReviewer(@NonNull String login); + @NonNull + List getAllByReviewerAndStatuses(String login, ReviewerStatus statuses); Set getAllId(); diff --git a/src/main/java/com/tsc/bitbucketbot/service/converter/UserJsonConverter.java b/src/main/java/com/tsc/bitbucketbot/service/converter/UserJsonConverter.java index 48e11a2..b7006fb 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/converter/UserJsonConverter.java +++ b/src/main/java/com/tsc/bitbucketbot/service/converter/UserJsonConverter.java @@ -16,6 +16,7 @@ public class UserJsonConverter implements Converter { @Override public User convert(UserJson source) { return User.builder() + .fullName(source.getDisplayName()) .login(source.getName()) .build(); } diff --git a/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java b/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java index f79f092..150db25 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java +++ b/src/main/java/com/tsc/bitbucketbot/service/impl/PullRequestsServiceImpl.java @@ -1,5 +1,6 @@ package com.tsc.bitbucketbot.service.impl; +import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.repository.jpa.PullRequestsRepository; import com.tsc.bitbucketbot.service.PullRequestsService; @@ -60,9 +61,10 @@ public class PullRequestsServiceImpl implements PullRequestsService { pullRequestsRepository.deleteAllByIdIn(id); } + @NonNull @Override - public List getAllByReviewer(@NonNull String login) { - return pullRequestsRepository.findAllByReviewers(login); + public List getAllByReviewerAndStatuses(String login, ReviewerStatus reviewerStatus) { + return pullRequestsRepository.findAllByReviewerAndStatuses(login, reviewerStatus); } @Override diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Message.java b/src/main/java/com/tsc/bitbucketbot/utils/Message.java index 09b3c1b..d6ccff1 100644 --- a/src/main/java/com/tsc/bitbucketbot/utils/Message.java +++ b/src/main/java/com/tsc/bitbucketbot/utils/Message.java @@ -20,14 +20,15 @@ import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*; public class Message { public static final String EMPTY = ""; - private static final String BREAK = "\n"; - private static final String TWO_BREAK = "\n\n"; - private static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB️"; - private static final String SMILE_PEN = "✏️"; - private static final String SMILE_NEW_PR = "\uD83C\uDF89"; - private static final String SMILE_UPDATE = "\uD83D\uDD04"; - private static final String HR = "\n -- -- -- -- --\n"; - + public static final String BREAK = "\n"; + public static final String TWO_BREAK = "\n\n"; + public static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB️"; + public static final String SMILE_PEN = "✏️"; + public static final String SMILE_NEW_PR = "\uD83C\uDF89"; + public static final String SMILE_UPDATE = "\uD83D\uDD04"; + public static final String SMILE_SUN = "\uD83D\uDD06"; + public static final String SMILE_PIN = "\uD83D\uDCCD"; + public static final String HR = "\n -- -- -- -- --\n"; private Message() { throw new IllegalStateException("Утилитарный класс"); @@ -101,4 +102,28 @@ public class Message { SMILE_AUTHOR + ": " + author + TWO_BREAK; } + + @NonNull + public static String goodMorningStatistic(String userName, List pullRequests) { + StringBuilder message = new StringBuilder(SMILE_SUN).append(" Доброе утро, ").append(userName).append("!") + .append(HR); + if (!pullRequests.isEmpty()) { + message.append("Сегодня тебя ждет процерка целых ").append(pullRequests.size()).append(" ПР!").append(TWO_BREAK) + .append("Позволь представить, горячая десятка:").append(BREAK); + pullRequests.stream() + .sorted(new UpdateDataComparator()) + .limit(10) + .forEach(pullRequest -> message.append(SMILE_PIN) + .append("[").append(pullRequest.getName()).append("](").append(pullRequest.getUrl()).append(")").append(BREAK)); + } else { + message.append("Ты либо самый лучший рабоник, либо тебе не доверяют проверку ПР :D").append(BREAK) + .append("Поздравляю, у тебя ни одного ПР на проверку!").append(BREAK); + } + return message + .append(BREAK) + .append("Удачной работы сегодня!") + .toString(); + } + + } diff --git a/src/main/java/com/tsc/bitbucketbot/utils/UpdateDataComparator.java b/src/main/java/com/tsc/bitbucketbot/utils/UpdateDataComparator.java new file mode 100644 index 0000000..1610f1b --- /dev/null +++ b/src/main/java/com/tsc/bitbucketbot/utils/UpdateDataComparator.java @@ -0,0 +1,14 @@ +package com.tsc.bitbucketbot.utils; + +import com.tsc.bitbucketbot.domain.entity.PullRequest; + +import java.util.Comparator; + +public class UpdateDataComparator implements Comparator { + + @Override + public int compare(PullRequest pullRequest, PullRequest t1) { + return pullRequest.getUpdateDate().compareTo(t1.getUpdateDate()); + } + +} diff --git a/src/main/resources/liquibase/change-set/v1.2.0.xml b/src/main/resources/liquibase/change-set/v1.2.0.xml index dc8c7ec..ce2c5f2 100644 --- a/src/main/resources/liquibase/change-set/v1.2.0.xml +++ b/src/main/resources/liquibase/change-set/v1.2.0.xml @@ -25,4 +25,10 @@ + + + + + + \ No newline at end of file