Общий рефакторинг

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

View File

@ -24,7 +24,6 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -201,7 +200,7 @@ public class SchedulerPullRequest {
.map(test -> conversionService.convert(test.getValue(), PullRequest.class))
.collect(Collectors.toList());
final List<PullRequest> newPullRequests = pullRequestsService.addAll(newPullRequest);
sendNotification(newPullRequests);
sendNotificationNewPullRequest(newPullRequests);
if (pullRequestBitbucketSheet.getNextPageStart() != null) {
sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen() + pullRequestBitbucketSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class);
} else {
@ -211,35 +210,22 @@ public class SchedulerPullRequest {
}
}
private void sendNotification(@NonNull List<PullRequest> newPullRequests) {
private void sendNotificationNewPullRequest(@NonNull List<PullRequest> newPullRequests) {
if (!newPullRequests.isEmpty()) {
Map<Long, StringBuilder> map = new HashMap<>();
newPullRequests.forEach(
pullRequest -> pullRequest.getReviewers().forEach(
reviewer -> test(pullRequest, reviewer, map)
)
pullRequest -> pullRequest.getReviewers().stream()
.map(reviewer -> userService.getByLogin(reviewer.getUser()))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(user -> user.getTelegramId() != null)
.forEach(user -> messageSendService.add(
MessageSend.builder()
.telegramId(user.getTelegramId())
.message(Message.newPullRequest(pullRequest))
.build()
))
);
map.forEach((key, value) -> messageSendService.add(MessageSend.builder().telegramId(key).message(value.toString()).build()));
}
}
private void test(PullRequest pullRequest, Reviewer reviewer, Map<Long, StringBuilder> map) {
userService.getByLogin(reviewer.getUser()).ifPresent(
user -> {
final Long telegramId = user.getTelegramId();
if (telegramId != null) {
if (!map.containsKey(telegramId)) {
map.put(telegramId, new StringBuilder());
}
map.get(telegramId).append("\uD83C\uDF89 *Новый Pull Request*\n")
.append("[").append(pullRequest.getName()).append("](").append(pullRequest.getUrl()).append(")\n")
.append("\uD83D\uDC68\u200D\uD83D\uDCBB: ").append(pullRequest.getAuthor().getLogin())
.append("\n-- -- -- -- --\n")
.append("\uD83D\uDCCC: ").append("#").append(pullRequest.getAuthor().getLogin()).append(" #pullRequest")
.append("\n\n");
}
}
);
}
}

View File

@ -29,6 +29,4 @@ public interface PullRequestsService {
void deleteAll(@NonNull Set<Long> id);
Optional<PullRequest> update(PullRequest pullRequest);
}

View File

@ -17,6 +17,9 @@ public class MessageSendServiceImpl implements MessageSendService {
@Override
public void add(@NonNull MessageSend messageSend) {
messageSend.setMessage(
messageSend.getMessage().replace("localhost", "192.168.236.164:7990")
);
messageSendRepository.add(messageSend);
}

View File

@ -49,14 +49,6 @@ public class PullRequestsServiceImpl implements PullRequestsService {
return pullRequestsRepository.saveAll(updatePullRequests);
}
@Override
public Optional<PullRequest> update(PullRequest pullRequest) {
if (pullRequestsRepository.existsById(pullRequest.getId())) {
pullRequestsRepository.save(pullRequest);
}
return Optional.empty();
}
@Override
public Optional<Long> getIdByBitbucketIdAndReposId(@NonNull Long bitbucketId, @NonNull Long repositoryId) {
return pullRequestsRepository.findIdByBitbucketIdAndRepositoryId(bitbucketId, repositoryId);

View File

@ -13,7 +13,7 @@ import java.util.stream.Collectors;
import static com.tsc.bitbucketbot.domain.util.ReviewerChange.Type.*;
/**
* TODO: Добавить описание класса.
* Генерирует сообщения для отправки.
*
* @author upagge [07.02.2020]
*/
@ -29,10 +29,20 @@ public class Message {
throw new IllegalStateException("Утилитарный класс");
}
@NonNull
public static String newPullRequest(PullRequest pullRequest) {
return "\uD83C\uDF89 *Новый Pull Request*\n" +
"[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")\n" +
"\uD83D\uDC68\u200D\uD83D\uDCBB: " + pullRequest.getAuthor().getLogin() +
"\n-- -- -- -- --\n" +
"\uD83D\uDCCC: " + "#" + pullRequest.getAuthor().getLogin() + " #pullRequest" +
"\n\n";
}
@NonNull
public static String statusPullRequest(String name, String url, PullRequestStatus oldStatus, PullRequestStatus newStatus) {
return "✏️ *Изменился статус вашего ПР*" + BREAK +
"[" + name + "](" + url.replace("localhost", "192.168.236.164") + ")" + BREAK +
"[" + name + "](" + url + ")" + BREAK +
oldStatus.name() + " -> " + newStatus.name() +
BREAK + "-- -- -- --" + BREAK +
"\uD83D\uDCCC: #pullRequest #change" +
@ -74,7 +84,7 @@ public class Message {
if (!EMPTY.equalsIgnoreCase(createMessage)) {
return Optional.of(
SMILE_PEN + " *Изменения ревьюверов вашего ПР*" + BREAK +
"[" + pullRequest.getName() + "](" + pullRequest.getUrl().replace("localhost", "192.168.236.164") + ")" + BREAK +
"[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")" + BREAK +
createMessage
+ "\n-- -- -- -- --"
);