Удаление потерянных (удаленных) ПР

This commit is contained in:
upagge 2020-03-03 01:26:11 +03:00
parent 5b96965518
commit 4b7b14f6df
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
7 changed files with 56 additions and 33 deletions

View File

@ -30,12 +30,6 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.google.code.gson</groupId>-->
<!-- <artifactId>gson</artifactId>-->
<!-- <version>2.8.5</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
@ -46,7 +40,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>

View File

@ -13,14 +13,8 @@ public class AppConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(6);
taskScheduler.setPoolSize(5);
return taskScheduler;
}
// @Bean
// public ObjectMapper objectMapper(ObjectMapper objectMapper) {
// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// return objectMapper;
// }
}

View File

@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@ -25,4 +26,10 @@ public interface PullRequestsRepository extends JpaRepository<PullRequest, Long>
void deleteAllByIdIn(Collection<Long> id);
@Query("SELECT p FROM PullRequest p LEFT JOIN p.reviewers r WHERE r.user=:reviewer")
List<PullRequest> findAllByReviewers(@Param("reviewer") String reviewer);
@Query("SELECT p.id from PullRequest p")
Set<Long> getAllIds();
}

View File

@ -1,6 +1,8 @@
//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;
@ -13,10 +15,14 @@
//public class SchedulerNotification {
//
// private final UserService userService;
// private final PullRequestsService pullRequestsService;
//
// @Scheduled(cron = "0 9 * * MON-FRI")
//// @Scheduled(cron = "0 9 * * MON-FRI")
// @Scheduled(fixedRate = 50000)
// public void goodMorning() {
// List<User> users = userService.getAllRegister();
// List<PullRequest> mstruchkov = pullRequestsService.getAllByReviewer("mstruchkov");
// System.out.println();
// }
//
//}

View File

@ -24,6 +24,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -47,7 +48,17 @@ public class SchedulerPullRequest {
private final BitbucketConfig bitbucketConfig;
@Scheduled(fixedRate = 30000)
public void checkClosePullRequest() {
public void checkOldPullRequest() {
Set<Long> existsId = pullRequestsService.getAllId();
Set<Long> openId = checkOpenPullRequest();
checkClosePullRequest();
existsId.removeAll(openId);
if (!existsId.isEmpty()) {
pullRequestsService.deleteAll(existsId);
}
}
private void checkClosePullRequest() {
final List<User> users = userService.getAllRegister();
for (User user : users) {
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose(), user.getToken(), PullRequestSheetJson.class);
@ -96,9 +107,9 @@ public class SchedulerPullRequest {
}
}
@Scheduled(fixedRate = 30000)
public void checkOldPullRequest() {
private Set<Long> checkOpenPullRequest() {
final List<User> users = userService.getAllRegister();
final Set<Long> ids = new HashSet<>();
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()) {
@ -116,7 +127,12 @@ public class SchedulerPullRequest {
.collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest));
final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsPullRequestBitbucket.keySet());
if (!existsPullRequestBitbucket.isEmpty() && !pullRequests.isEmpty()) {
pullRequestsService.updateAll(processingUpdate(existsPullRequestBitbucket, pullRequests));
processingUpdate(existsPullRequestBitbucket, pullRequests);
ids.addAll(
pullRequestsService.updateAll(existsPullRequestBitbucket.values()).stream()
.map(PullRequest::getId)
.collect(Collectors.toSet())
);
}
if (pullRequestBitbucketSheet.getNextPageStart() != null) {
@ -126,23 +142,19 @@ public class SchedulerPullRequest {
}
}
}
return ids;
}
@NonNull
private List<PullRequest> processingUpdate(Map<Long, PullRequest> newPullRequests, Set<PullRequest> pullRequests) {
List<PullRequest> updatePullRequest = new ArrayList<>();
private void processingUpdate(Map<Long, PullRequest> newPullRequests, Set<PullRequest> pullRequests) {
for (PullRequest pullRequest : pullRequests) {
PullRequest newPullRequest = newPullRequests.get(pullRequest.getId());
@NonNull boolean author = processingAuthor(pullRequest, newPullRequest);
@NonNull boolean reviewer = processingReviewer(pullRequest, newPullRequest);
if (author || reviewer) {
updatePullRequest.add(newPullRequest);
}
processingAuthor(pullRequest, newPullRequest);
processingReviewer(pullRequest, newPullRequest);
}
return updatePullRequest;
}
private boolean processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) {
private void processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) {
StringBuilder stringBuilder = new StringBuilder();
changeVersionPr(pullRequest, newPullRequest).ifPresent(stringBuilder::append);
String message = stringBuilder.toString();
@ -161,13 +173,11 @@ public class SchedulerPullRequest {
newPullRequest.getAuthor().getLogin()))
.build())
);
return true;
}
return false;
}
@NonNull
private boolean processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) {
private void processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) {
final User author = pullRequest.getAuthor();
StringBuilder stringBuilder = new StringBuilder();
if (author.getTelegramId() != null) {
@ -176,10 +186,8 @@ public class SchedulerPullRequest {
final String message = stringBuilder.toString();
if (!Message.EMPTY.equalsIgnoreCase(message)) {
messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build());
return true;
}
}
return false;
}
@NonNull

View File

@ -29,4 +29,8 @@ public interface PullRequestsService {
void deleteAll(@NonNull Set<Long> id);
List<PullRequest> getAllByReviewer(@NonNull String login);
Set<Long> getAllId();
}

View File

@ -60,4 +60,14 @@ public class PullRequestsServiceImpl implements PullRequestsService {
pullRequestsRepository.deleteAllByIdIn(id);
}
@Override
public List<PullRequest> getAllByReviewer(@NonNull String login) {
return pullRequestsRepository.findAllByReviewers(login);
}
@Override
public Set<Long> getAllId() {
return pullRequestsRepository.getAllIds();
}
}