release-1.3.0
This commit is contained in:
parent
3a09de1b04
commit
e02683277b
13
pom.xml
13
pom.xml
@ -10,9 +10,8 @@
|
||||
</parent>
|
||||
<groupId>com.tsc.bitbucketbot</groupId>
|
||||
<artifactId>bitbucketbot</artifactId>
|
||||
<version>1.3.0-SNAPSHOT</version>
|
||||
<name>bitbucketbot</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<version>1.3.0-RELEASE</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
@ -92,4 +91,12 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>uPagge</id>
|
||||
<name>Struchkov Mark</name>
|
||||
<email>upagge@mail.ru</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
</project>
|
||||
|
@ -6,6 +6,8 @@ import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
@ -24,4 +26,9 @@ public interface UserRepository extends JpaRepository<User, String> {
|
||||
@Query("SELECT u.telegramId FROM User u WHERE u.login=:login")
|
||||
Long findTelegramIdByLogin(String login);
|
||||
|
||||
@Query("SELECT u.telegramId FROM User u WHERE u.login IN :logins AND u.telegramId IS NOT NULL")
|
||||
List<Long> findAllTelegramIdByLogin(Set<String> logins);
|
||||
|
||||
Optional<User> findByLogin(String login);
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import com.tsc.bitbucketbot.service.Utils;
|
||||
import com.tsc.bitbucketbot.utils.Message;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -21,6 +22,7 @@ import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SchedulerComments {
|
||||
@ -38,6 +40,7 @@ public class SchedulerComments {
|
||||
|
||||
@Scheduled(cron = "0 */5 8-18 * * MON-FRI")
|
||||
public void test() {
|
||||
log.info("Начало сканирования комментариев");
|
||||
long newLastCommentId = commentService.getLastCommentId();
|
||||
long commentId = newLastCommentId + 1;
|
||||
long count = 0;
|
||||
@ -78,6 +81,7 @@ public class SchedulerComments {
|
||||
commentId += 1;
|
||||
} while (count < NO_COMMENT);
|
||||
commentService.saveLastCommentId(newLastCommentId);
|
||||
log.info("Конец сканирования комментариев");
|
||||
}
|
||||
|
||||
private String getPrUrl(long lastCommentId, PullRequest pullRequest) {
|
||||
|
@ -65,43 +65,42 @@ public class SchedulerPullRequest {
|
||||
for (User user : users) {
|
||||
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose(), user.getToken(), PullRequestSheetJson.class);
|
||||
while (sheetJson.isPresent() && sheetJson.get().getValues() != null && !sheetJson.get().getValues().isEmpty()) {
|
||||
final PullRequestSheetJson pullRequestBitbucketSheet = sheetJson.get();
|
||||
final List<PullRequestJson> bitbucketPullRequests = pullRequestBitbucketSheet.getValues().stream()
|
||||
final PullRequestSheetJson bitbucketSheet = sheetJson.get();
|
||||
final List<PullRequestJson> jsonsPr = bitbucketSheet.getValues().stream()
|
||||
.filter(
|
||||
pullRequestJson -> pullRequestsService.existsByBitbucketIdAndReposId(
|
||||
pullRequestJson.getId(),
|
||||
pullRequestJson.getFromRef().getRepository().getId()
|
||||
jsonPr -> pullRequestsService.existsByBitbucketIdAndReposId(
|
||||
jsonPr.getId(),
|
||||
jsonPr.getFromRef().getRepository().getId()
|
||||
)
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
final Set<Long> pullRequestId = bitbucketPullRequests.stream()
|
||||
final Set<Long> idPr = jsonsPr.stream()
|
||||
.map(
|
||||
pullRequestJson -> pullRequestsService.getIdByBitbucketIdAndReposId(
|
||||
pullRequestJson.getId(),
|
||||
pullRequestJson.getFromRef().getRepository().getId()
|
||||
jsonPr -> pullRequestsService.getIdByBitbucketIdAndReposId(
|
||||
jsonPr.getId(),
|
||||
jsonPr.getFromRef().getRepository().getId()
|
||||
)
|
||||
)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
for (PullRequestJson bitbucketPullRequest : bitbucketPullRequests) {
|
||||
final Optional<User> optUser = userService.getByLogin(bitbucketPullRequest.getAuthor().getUser().getName());
|
||||
for (PullRequestJson jsonPr : jsonsPr) {
|
||||
final Optional<User> optUser = userService.getByLogin(jsonPr.getAuthor().getUser().getName());
|
||||
if (optUser.isPresent()) {
|
||||
final User author = optUser.get();
|
||||
final Long telegramId = author.getTelegramId();
|
||||
if (telegramId != null) {
|
||||
final PullRequestStatus pullRequestStatus = PullRequestJsonConverter.convertPullRequestStatus(bitbucketPullRequest.getState());
|
||||
@NonNull final String message = Message.statusPullRequest(bitbucketPullRequest.getTitle(), bitbucketPullRequest.getLinks().getSelf().get(0).getHref(), PullRequestStatus.OPEN, pullRequestStatus);
|
||||
final PullRequestStatus statusPr = PullRequestJsonConverter.convertPullRequestStatus(jsonPr.getState());
|
||||
final String message = Message.statusPullRequest(jsonPr.getTitle(), jsonPr.getLinks().getSelf().get(0).getHref(), PullRequestStatus.OPEN, statusPr);
|
||||
messageSendService.add(MessageSend.builder().telegramId(telegramId).message(message).build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pullRequestsService.deleteAll(pullRequestId);
|
||||
pullRequestsService.deleteAll(idPr);
|
||||
|
||||
if (pullRequestBitbucketSheet.getNextPageStart() != null) {
|
||||
sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose() + pullRequestBitbucketSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class);
|
||||
if (bitbucketSheet.getNextPageStart() != null) {
|
||||
sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose() + bitbucketSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@ -115,30 +114,25 @@ public class SchedulerPullRequest {
|
||||
for (User user : users) {
|
||||
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen(), user.getToken(), PullRequestSheetJson.class);
|
||||
while (sheetJson.isPresent() && sheetJson.get().getValues() != null && !sheetJson.get().getValues().isEmpty()) {
|
||||
final PullRequestSheetJson pullRequestBitbucketSheet = sheetJson.get();
|
||||
final Map<Long, PullRequest> existsPullRequestBitbucket = pullRequestBitbucketSheet.getValues().stream()
|
||||
.filter(
|
||||
pullRequestJson -> pullRequestsService.existsByBitbucketIdAndReposId(
|
||||
pullRequestJson.getId(),
|
||||
pullRequestJson.getFromRef().getRepository().getId()
|
||||
)
|
||||
)
|
||||
.map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class))
|
||||
final PullRequestSheetJson jsonSheet = sheetJson.get();
|
||||
final Map<Long, PullRequest> existsJsonPr = jsonSheet.getValues().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class))
|
||||
.peek(pullRequest -> pullRequestsService.getIdByBitbucketIdAndReposId(pullRequest.getBitbucketId(), pullRequest.getRepositoryId()).ifPresent(pullRequest::setId))
|
||||
.filter(pullRequest -> pullRequest.getId() != null)
|
||||
.collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest));
|
||||
final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsPullRequestBitbucket.keySet());
|
||||
if (!existsPullRequestBitbucket.isEmpty() && !pullRequests.isEmpty()) {
|
||||
processingUpdate(existsPullRequestBitbucket, pullRequests);
|
||||
final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsJsonPr.keySet());
|
||||
if (!existsJsonPr.isEmpty() && !pullRequests.isEmpty()) {
|
||||
processingUpdate(existsJsonPr, pullRequests);
|
||||
ids.addAll(
|
||||
pullRequestsService.updateAll(existsPullRequestBitbucket.values()).stream()
|
||||
pullRequestsService.updateAll(existsJsonPr.values()).stream()
|
||||
.map(PullRequest::getId)
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
}
|
||||
|
||||
if (pullRequestBitbucketSheet.getNextPageStart() != null) {
|
||||
sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen() + pullRequestBitbucketSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class);
|
||||
if (jsonSheet.getNextPageStart() != null) {
|
||||
sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen() + jsonSheet.getNextPageStart(), bitbucketConfig.getToken(), PullRequestSheetJson.class);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@ -157,31 +151,40 @@ public class SchedulerPullRequest {
|
||||
}
|
||||
|
||||
private void processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) {
|
||||
changeVersionPr(pullRequest, newPullRequest).ifPresent(
|
||||
message -> newPullRequest.getReviewers().stream()
|
||||
.map(reviewer -> userService.getByLogin(reviewer.getUser()))
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.filter(user -> user.getTelegramId() != null)
|
||||
.forEach(user -> messageSendService.add(
|
||||
final Set<String> logins = newPullRequest.getReviewers().stream()
|
||||
.map(Reviewer::getUser)
|
||||
.collect(Collectors.toSet());
|
||||
if (!logins.isEmpty()) {
|
||||
Optional<String> optMessage = changeVersionPr(pullRequest, newPullRequest);
|
||||
if (optMessage.isPresent()) {
|
||||
final String message = optMessage.get();
|
||||
userService.getAllTelegramIdByLogin(logins).forEach(
|
||||
telegramId -> messageSendService.add(
|
||||
MessageSend.builder()
|
||||
.telegramId(user.getTelegramId())
|
||||
.telegramId(telegramId)
|
||||
.message(message)
|
||||
.build())
|
||||
.build()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private void processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) {
|
||||
final User author = pullRequest.getAuthor();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
StringBuilder builderMessage = new StringBuilder();
|
||||
if (author.getTelegramId() != null) {
|
||||
changeStatusPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append);
|
||||
changeReviewersPR(pullRequest, newPullRequest).ifPresent(stringBuilder::append);
|
||||
final String message = stringBuilder.toString();
|
||||
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());
|
||||
messageSendService.add(
|
||||
MessageSend.builder()
|
||||
.message(message)
|
||||
.telegramId(author.getTelegramId())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,6 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
*
|
||||
* @author upagge [31.01.2020]
|
||||
*/
|
||||
public interface PullRequestsService {
|
||||
|
||||
@NonNull
|
||||
|
@ -7,15 +7,8 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание интерфейса.
|
||||
*
|
||||
* @author upagge [30.01.2020]
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
List<User> getAll();
|
||||
|
||||
Optional<User> getByLogin(String login);
|
||||
|
||||
Set<String> existsByLogin(@NonNull Set<String> logins);
|
||||
@ -28,4 +21,6 @@ public interface UserService {
|
||||
|
||||
Optional<Long> getTelegramIdByLogin(@NonNull String login);
|
||||
|
||||
List<Long> getAllTelegramIdByLogin(Set<String> logins);
|
||||
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class Utils {
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
log.trace(e.getMessage());
|
||||
}
|
||||
if (sb != null) {
|
||||
try {
|
||||
|
@ -18,11 +18,6 @@ import org.springframework.stereotype.Component;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
*
|
||||
* @author upagge [01.02.2020]
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PullRequestJsonConverter implements Converter<PullRequestJson, PullRequest> {
|
||||
|
@ -5,11 +5,6 @@ import com.tsc.bitbucketbot.dto.bitbucket.UserJson;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
*
|
||||
* @author upagge [02.02.2020]
|
||||
*/
|
||||
@Service
|
||||
public class UserJsonConverter implements Converter<UserJson, User> {
|
||||
|
||||
|
@ -18,11 +18,6 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
*
|
||||
* @author upagge [31.01.2020]
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PullRequestsServiceImpl implements PullRequestsService {
|
||||
|
@ -16,11 +16,6 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
*
|
||||
* @author upagge [30.01.2020]
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
@ -28,11 +23,6 @@ public class UserServiceImpl implements UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final BitbucketConfig bitbucketConfig;
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> getByLogin(String login) {
|
||||
return userRepository.findById(login);
|
||||
@ -45,17 +35,21 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public Optional<User> reg(@NonNull User user) {
|
||||
if (userRepository.existsByLogin(user.getLogin()) && !userRepository.existsByTelegramId(user.getTelegramId())) {
|
||||
final Optional<User> optUser = userRepository.findByLogin(user.getLogin());
|
||||
if (optUser.isPresent()) {
|
||||
final User oldUser = optUser.get();
|
||||
if (oldUser.getTelegramId() == null) {
|
||||
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose(), user.getToken(), PullRequestSheetJson.class);
|
||||
if (sheetJson.isPresent()) {
|
||||
return Optional.of(userRepository.save(user));
|
||||
oldUser.setTelegramId(user.getTelegramId());
|
||||
return Optional.of(userRepository.save(oldUser));
|
||||
} else {
|
||||
throw new RegException("Ваш токен не валиден");
|
||||
}
|
||||
} else {
|
||||
throw new RegException("Пользователь с таким логином или телеграмом уже есть в системе");
|
||||
}
|
||||
}
|
||||
throw new RegException("Пользователь не найден, подождите обновление базы пользователей!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> addAll(Set<User> newUsers) {
|
||||
@ -72,4 +66,9 @@ public class UserServiceImpl implements UserService {
|
||||
return Optional.ofNullable(userRepository.findTelegramIdByLogin(login));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getAllTelegramIdByLogin(Set<String> logins) {
|
||||
return userRepository.findAllTelegramIdByLogin(logins);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user