Уведомление по утрам

This commit is contained in:
upagge 2020-03-03 02:36:46 +03:00
parent 4b7b14f6df
commit c09d6b2bab
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
9 changed files with 107 additions and 41 deletions

View File

@ -39,4 +39,7 @@ public class User {
@Column(name = "telegram_id") @Column(name = "telegram_id")
private Long telegramId; private Long telegramId;
@Column(name = "full_name")
private String fullName;
} }

View File

@ -1,5 +1,6 @@
package com.tsc.bitbucketbot.repository.jpa; package com.tsc.bitbucketbot.repository.jpa;
import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
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;
@ -26,8 +27,8 @@ public interface PullRequestsRepository extends JpaRepository<PullRequest, Long>
void deleteAllByIdIn(Collection<Long> id); void deleteAllByIdIn(Collection<Long> id);
@Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer") @Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer AND r.status =:status")
List<PullRequest> findAllByReviewers(@Param("reviewer") String reviewer); List<PullRequest> findAllByReviewerAndStatuses(@Param("reviewer") String reviewer, @Param("status") ReviewerStatus status);
@Query("SELECT p.id from PullRequest p") @Query("SELECT p.id from PullRequest p")
Set<Long> getAllIds(); Set<Long> getAllIds();

View File

@ -1,28 +1,40 @@
//package com.tsc.bitbucketbot.scheduler; package com.tsc.bitbucketbot.scheduler;
//
//import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.MessageSend;
//import com.tsc.bitbucketbot.domain.entity.User; import com.tsc.bitbucketbot.domain.ReviewerStatus;
//import com.tsc.bitbucketbot.service.PullRequestsService; import com.tsc.bitbucketbot.domain.entity.PullRequest;
//import com.tsc.bitbucketbot.service.UserService; import com.tsc.bitbucketbot.domain.entity.User;
//import lombok.RequiredArgsConstructor; import com.tsc.bitbucketbot.service.MessageSendService;
//import org.springframework.scheduling.annotation.Scheduled; import com.tsc.bitbucketbot.service.PullRequestsService;
//import org.springframework.stereotype.Service; import com.tsc.bitbucketbot.service.UserService;
// import com.tsc.bitbucketbot.utils.Message;
//import java.util.List; import lombok.RequiredArgsConstructor;
// import org.springframework.scheduling.annotation.Scheduled;
//@Service import org.springframework.stereotype.Service;
//@RequiredArgsConstructor
//public class SchedulerNotification { import java.util.List;
//
// private final UserService userService; @Service
// private final PullRequestsService pullRequestsService; @RequiredArgsConstructor
// public class SchedulerNotification {
//// @Scheduled(cron = "0 9 * * MON-FRI")
// @Scheduled(fixedRate = 50000) private final UserService userService;
// public void goodMorning() { private final PullRequestsService pullRequestsService;
// List<User> users = userService.getAllRegister(); private final MessageSendService messageSendService;
// List<PullRequest> mstruchkov = pullRequestsService.getAllByReviewer("mstruchkov");
// System.out.println(); @Scheduled(cron = "0 9 * * MON-FRI")
// } public void goodMorning() {
// User user = userService.getByLogin("mstruchkov").get();
//} List<PullRequest> pullRequests = pullRequestsService.getAllByReviewerAndStatuses(
user.getLogin(),
ReviewerStatus.NEEDS_WORK
);
messageSendService.add(
MessageSend.builder()
.telegramId(3000811L)
.message(Message.goodMorningStatistic(user.getFullName(), pullRequests))
.build()
);
}
}

View File

@ -1,5 +1,6 @@
package com.tsc.bitbucketbot.service; package com.tsc.bitbucketbot.service;
import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import lombok.NonNull; import lombok.NonNull;
@ -29,7 +30,8 @@ public interface PullRequestsService {
void deleteAll(@NonNull Set<Long> id); void deleteAll(@NonNull Set<Long> id);
List<PullRequest> getAllByReviewer(@NonNull String login); @NonNull
List<PullRequest> getAllByReviewerAndStatuses(String login, ReviewerStatus statuses);
Set<Long> getAllId(); Set<Long> getAllId();

View File

@ -16,6 +16,7 @@ public class UserJsonConverter implements Converter<UserJson, User> {
@Override @Override
public User convert(UserJson source) { public User convert(UserJson source) {
return User.builder() return User.builder()
.fullName(source.getDisplayName())
.login(source.getName()) .login(source.getName())
.build(); .build();
} }

View File

@ -1,5 +1,6 @@
package com.tsc.bitbucketbot.service.impl; package com.tsc.bitbucketbot.service.impl;
import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
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;
@ -60,9 +61,10 @@ public class PullRequestsServiceImpl implements PullRequestsService {
pullRequestsRepository.deleteAllByIdIn(id); pullRequestsRepository.deleteAllByIdIn(id);
} }
@NonNull
@Override @Override
public List<PullRequest> getAllByReviewer(@NonNull String login) { public List<PullRequest> getAllByReviewerAndStatuses(String login, ReviewerStatus reviewerStatus) {
return pullRequestsRepository.findAllByReviewers(login); return pullRequestsRepository.findAllByReviewerAndStatuses(login, reviewerStatus);
} }
@Override @Override

View File

@ -20,14 +20,15 @@ import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*;
public class Message { public class Message {
public static final String EMPTY = ""; public static final String EMPTY = "";
private static final String BREAK = "\n"; public static final String BREAK = "\n";
private static final String TWO_BREAK = "\n\n"; public static final String TWO_BREAK = "\n\n";
private static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB"; public static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB";
private static final String SMILE_PEN = "✏️"; public static final String SMILE_PEN = "✏️";
private static final String SMILE_NEW_PR = "\uD83C\uDF89"; public static final String SMILE_NEW_PR = "\uD83C\uDF89";
private static final String SMILE_UPDATE = "\uD83D\uDD04"; public static final String SMILE_UPDATE = "\uD83D\uDD04";
private static final String HR = "\n -- -- -- -- --\n"; 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() { private Message() {
throw new IllegalStateException("Утилитарный класс"); throw new IllegalStateException("Утилитарный класс");
@ -101,4 +102,28 @@ public class Message {
SMILE_AUTHOR + ": " + author + SMILE_AUTHOR + ": " + author +
TWO_BREAK; TWO_BREAK;
} }
@NonNull
public static String goodMorningStatistic(String userName, List<PullRequest> 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();
}
} }

View File

@ -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<PullRequest> {
@Override
public int compare(PullRequest pullRequest, PullRequest t1) {
return pullRequest.getUpdateDate().compareTo(t1.getUpdateDate());
}
}

View File

@ -25,4 +25,10 @@
</addColumn> </addColumn>
</changeSet> </changeSet>
<changeSet id="user-fullname" author="upagge">
<addColumn tableName="user" schemaName="public" catalogName="pg_catalog">
<column name="full_name" type="varchar(50)"/>
</addColumn>
</changeSet>
</databaseChangeLog> </databaseChangeLog>