Изменение в утреннем сообщении

This commit is contained in:
upagge 2020-03-12 11:36:15 +03:00
parent c3b20d026e
commit e9d43f8e66
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
5 changed files with 40 additions and 29 deletions

View File

@ -30,6 +30,9 @@ public interface PullRequestsRepository extends JpaRepository<PullRequest, Long>
@Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer AND r.status =:status") @Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer AND r.status =:status")
List<PullRequest> findAllByReviewerAndStatuses(@Param("reviewer") String reviewer, @Param("status") ReviewerStatus status); List<PullRequest> findAllByReviewerAndStatuses(@Param("reviewer") String reviewer, @Param("status") ReviewerStatus status);
@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);
@Query("SELECT p.id from PullRequest p") @Query("SELECT p.id from PullRequest p")
Set<Long> getAllIds(); Set<Long> getAllIds();

View File

@ -22,18 +22,20 @@ public class SchedulerNotification {
private final PullRequestsService pullRequestsService; private final PullRequestsService pullRequestsService;
private final MessageSendService messageSendService; private final MessageSendService messageSendService;
// Утреннее сообщение
@Scheduled(cron = "0 15 8 * * MON-FRI") @Scheduled(cron = "0 15 8 * * MON-FRI")
public void goodMorning() { public void goodMorning() {
List<User> allRegister = userService.getAllRegister(); List<User> allRegister = userService.getAllRegister();
for (User user : allRegister) { for (User user : allRegister) {
List<PullRequest> pullRequests = pullRequestsService.getAllByReviewerAndStatuses( List<PullRequest> pullRequestsReviews = pullRequestsService.getAllByReviewerAndStatuses(
user.getLogin(), user.getLogin(),
ReviewerStatus.NEEDS_WORK ReviewerStatus.NEEDS_WORK
); );
List<PullRequest> pullRequestsNeedWork = pullRequestsService.getAllByAuthorAndReviewerStatus(user.getLogin(), ReviewerStatus.UNAPPROVED);
messageSendService.add( messageSendService.add(
MessageSend.builder() MessageSend.builder()
.telegramId(user.getTelegramId()) .telegramId(user.getTelegramId())
.message(Message.goodMorningStatistic(pullRequests)) .message(Message.goodMorningStatistic(pullRequestsReviews, pullRequestsNeedWork))
.build() .build()
); );
} }

View File

@ -33,6 +33,8 @@ public interface PullRequestsService {
@NonNull @NonNull
List<PullRequest> getAllByReviewerAndStatuses(String login, ReviewerStatus statuses); List<PullRequest> getAllByReviewerAndStatuses(String login, ReviewerStatus statuses);
List<PullRequest> getAllByAuthorAndReviewerStatus(@NonNull String login, @NonNull ReviewerStatus status);
Set<Long> getAllId(); Set<Long> getAllId();
} }

View File

@ -67,6 +67,11 @@ public class PullRequestsServiceImpl implements PullRequestsService {
return pullRequestsRepository.findAllByReviewerAndStatuses(login, reviewerStatus); return pullRequestsRepository.findAllByReviewerAndStatuses(login, reviewerStatus);
} }
@Override
public List<PullRequest> getAllByAuthorAndReviewerStatus(@NonNull String login, @NonNull ReviewerStatus status) {
return pullRequestsRepository.findAllByAuthorAndReviewerStatus(login, status);
}
@Override @Override
public Set<Long> getAllId() { public Set<Long> getAllId() {
return pullRequestsRepository.getAllIds(); return pullRequestsRepository.getAllIds();

View File

@ -9,7 +9,6 @@ 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.Optional;
import java.util.Set;
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.*;
@ -32,7 +31,7 @@ public class Message {
@NonNull @NonNull
public static String newPullRequest(PullRequest pullRequest) { public static String newPullRequest(PullRequest pullRequest) {
return Smile.FUN + " *Новый Pull Request*" + Smile.BREAK + return Smile.FUN + " *Новый Pull Request*" + Smile.BREAK +
linkPr(pullRequest.getName(), pullRequest.getUrl()) + link(pullRequest.getName(), pullRequest.getUrl()) +
Smile.HR + Smile.HR +
Smile.AUTHOR + ": " + pullRequest.getAuthor().getLogin() + Smile.AUTHOR + ": " + pullRequest.getAuthor().getLogin() +
Smile.TWO_BREAK; Smile.TWO_BREAK;
@ -41,7 +40,7 @@ public class Message {
@NonNull @NonNull
public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) { public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) {
return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR + return Smile.PEN + " *Изменился статус вашего ПР*" + Smile.HR +
linkPr(name, url) + Smile.BREAK + link(name, url) + Smile.BREAK +
oldStatus.name() + " -> " + newStatus.name() + oldStatus.name() + " -> " + newStatus.name() +
Smile.TWO_BREAK; Smile.TWO_BREAK;
} }
@ -82,7 +81,7 @@ public class Message {
return Optional.of( return Optional.of(
Smile.PEN + " *Изменения ревьюверов вашего ПР*" + Smile.PEN + " *Изменения ревьюверов вашего ПР*" +
Smile.HR + Smile.HR +
linkPr(pullRequest.getName(), pullRequest.getUrl()) + Smile.BREAK + link(pullRequest.getName(), pullRequest.getUrl()) + Smile.BREAK +
createMessage createMessage
); );
} }
@ -92,56 +91,56 @@ public class Message {
@NonNull @NonNull
public static String updatePullRequest(String pullRequestName, String prUrl, String author) { public static String updatePullRequest(String pullRequestName, String prUrl, String author) {
return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BREAK + return Smile.UPDATE + " *Обновление Pull Request*" + Smile.BREAK +
linkPr(pullRequestName, prUrl) + link(pullRequestName, prUrl) +
Smile.HR + Smile.HR +
Smile.AUTHOR + ": " + author + Smile.AUTHOR + ": " + author +
Smile.TWO_BREAK; Smile.TWO_BREAK;
} }
@NonNull @NonNull
public static String goodMorningStatistic(List<PullRequest> pullRequests) { public static String goodMorningStatistic(List<PullRequest> pullRequestsReviews, List<PullRequest> pullRequestsNeedWork) {
StringBuilder message = new StringBuilder().append(Smile.SUN).append(" Доброе утро ").append(Smile.SUN).append(Smile.HR); StringBuilder message = new StringBuilder().append(Smile.SUN).append(" Доброе утро ").append(Smile.SUN).append(Smile.HR);
if (!pullRequests.isEmpty()) { if (!pullRequestsReviews.isEmpty()) {
message.append("Сегодня тебя ждет проверка целых ").append(pullRequests.size()).append(" ПР!").append(Smile.TWO_BREAK) message.append("Сегодня тебя ждет проверка ").append(pullRequestsReviews.size()).append(" ПР!").append(Smile.TWO_BREAK)
.append("Топ старых ПР:").append(Smile.BREAK); .append("Топ старых ПР:").append(Smile.BREAK);
List<PullRequest> oldPr = pullRequests.stream() List<PullRequest> oldPr = pullRequestsReviews.stream()
.sorted(COMPARATOR) .sorted(COMPARATOR)
.limit(PR_COUNT) .limit(PR_COUNT)
.collect(Collectors.toList()); .collect(Collectors.toList());
oldPr.forEach( oldPr.forEach(pullRequest -> message.append(topPr(pullRequest)));
pullRequest -> message.append(topPr(pullRequest)) message.append(Smile.BREAK);
);
Set<Long> oldPrIds = oldPr.stream().map(PullRequest::getId).collect(Collectors.toSet());
if (pullRequests.size() > PR_COUNT) {
message.append(Smile.BREAK).append("Свежие ПР:").append(Smile.BREAK);
pullRequests
.stream()
.filter(pullRequest -> !oldPrIds.contains(pullRequest.getId()))
.sorted(COMPARATOR.reversed())
.limit(PR_COUNT)
.forEach(
pullRequest -> message.append(topPr(pullRequest))
);
}
} else { } else {
message.append("Ты либо самый лучший работник, либо тебе не доверяют проверку ПР ").append(Smile.MEGA_FUN).append(Smile.TWO_BREAK) message.append("Ты либо самый лучший работник, либо тебе не доверяют проверку ПР ").append(Smile.MEGA_FUN).append(Smile.TWO_BREAK)
.append("Поздравляю, у тебя ни одного ПР на проверку!").append(Smile.BREAK); .append("Поздравляю, у тебя ни одного ПР на проверку!").append(Smile.BREAK);
} }
if (!pullRequestsNeedWork.isEmpty()) {
message.append(Smile.BREAK).append(Smile.DANGEROUS).append(" Так же у тебя на доработке находится ").append(pullRequestsNeedWork.size()).append(" ПР").append(Smile.BREAK);
message.append(needWorkPr(pullRequestsNeedWork)).append(Smile.BREAK);
}
if (dayX()) { if (dayX()) {
message.append(Smile.BREAK).append(Smile.FUN).append(" Кстати, поздравляю, сегодня день З").append(Smile.BREAK) message.append(Smile.BREAK).append(Smile.FUN).append(" Кстати, поздравляю, сегодня день З").append(Smile.BREAK)
.append(Smile.DANGEROUS).append("И раз такое дело, то напоминаю, что в виду независящих от разработчика условий, бот работает на платном VDS. Поэтому всячески приветствуются ") .append(Smile.DANGEROUS).append("И раз такое дело, то напоминаю, что в виду независящих от разработчика условий, бот работает на платном VDS. Поэтому всячески приветствуются ")
.append(linkPr("донаты на оплату сервера", DONATION_LINK)).append(Smile.BREAK); .append(link("донаты на оплату сервера", DONATION_LINK)).append(Smile.BREAK);
} }
message message
.append(Smile.BREAK) .append(Smile.BREAK)
.append("Удачного дня ").append(Smile.FLOWER).append(Smile.TWO_BREAK); .append("Удачного дня ").append(Smile.FLOWER).append(Smile.TWO_BREAK);
return message.toString();
}
private static String needWorkPr(@NonNull List<PullRequest> pullRequestsNeedWork) {
final StringBuilder message = new StringBuilder();
pullRequestsNeedWork.stream()
.limit(3)
.forEach(
pullRequest -> message.append("-- ").append(link(pullRequest.getName(), pullRequest.getUrl())).append(Smile.BREAK)
);
return message.toString(); return message.toString();
} }
private static String topPr(PullRequest pullRequest) { private static String topPr(PullRequest pullRequest) {
return Smile.statusPr(pullRequest.getUpdateDate()) + " " + return Smile.statusPr(pullRequest.getUpdateDate()) + " " +
linkPr(pullRequest.getName(), pullRequest.getUrl()) + link(pullRequest.getName(), pullRequest.getUrl()) +
Smile.BREAK; Smile.BREAK;
} }
@ -151,7 +150,7 @@ public class Message {
} }
@NonNull @NonNull
private static String linkPr(String name, String url) { private static String link(String name, String url) {
return "[" + name + "](" + url + ")"; return "[" + name + "](" + url + ")";
} }