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

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

View File

@ -13,14 +13,8 @@ public class AppConfig {
@Bean @Bean
public TaskScheduler taskScheduler() { public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(6); taskScheduler.setPoolSize(5);
return taskScheduler; 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 org.springframework.data.repository.query.Param;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -25,4 +26,10 @@ 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")
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; //package com.tsc.bitbucketbot.scheduler;
// //
//import com.tsc.bitbucketbot.domain.entity.PullRequest;
//import com.tsc.bitbucketbot.domain.entity.User; //import com.tsc.bitbucketbot.domain.entity.User;
//import com.tsc.bitbucketbot.service.PullRequestsService;
//import com.tsc.bitbucketbot.service.UserService; //import com.tsc.bitbucketbot.service.UserService;
//import lombok.RequiredArgsConstructor; //import lombok.RequiredArgsConstructor;
//import org.springframework.scheduling.annotation.Scheduled; //import org.springframework.scheduling.annotation.Scheduled;
@ -13,10 +15,14 @@
//public class SchedulerNotification { //public class SchedulerNotification {
// //
// private final UserService userService; // 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() { // public void goodMorning() {
// List<User> users = userService.getAllRegister(); // 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 org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -47,7 +48,17 @@ public class SchedulerPullRequest {
private final BitbucketConfig bitbucketConfig; private final BitbucketConfig bitbucketConfig;
@Scheduled(fixedRate = 30000) @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(); final List<User> users = userService.getAllRegister();
for (User user : users) { for (User user : users) {
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose(), user.getToken(), PullRequestSheetJson.class); Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestClose(), user.getToken(), PullRequestSheetJson.class);
@ -96,9 +107,9 @@ public class SchedulerPullRequest {
} }
} }
@Scheduled(fixedRate = 30000) private Set<Long> checkOpenPullRequest() {
public void checkOldPullRequest() {
final List<User> users = userService.getAllRegister(); final List<User> users = userService.getAllRegister();
final Set<Long> ids = new HashSet<>();
for (User user : users) { for (User user : users) {
Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen(), user.getToken(), PullRequestSheetJson.class); Optional<PullRequestSheetJson> sheetJson = Utils.urlToJson(bitbucketConfig.getUrlPullRequestOpen(), user.getToken(), PullRequestSheetJson.class);
while (sheetJson.isPresent() && sheetJson.get().getValues() != null && !sheetJson.get().getValues().isEmpty()) { 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)); .collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest));
final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsPullRequestBitbucket.keySet()); final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsPullRequestBitbucket.keySet());
if (!existsPullRequestBitbucket.isEmpty() && !pullRequests.isEmpty()) { 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) { if (pullRequestBitbucketSheet.getNextPageStart() != null) {
@ -126,23 +142,19 @@ public class SchedulerPullRequest {
} }
} }
} }
return ids;
} }
@NonNull @NonNull
private List<PullRequest> processingUpdate(Map<Long, PullRequest> newPullRequests, Set<PullRequest> pullRequests) { private void processingUpdate(Map<Long, PullRequest> newPullRequests, Set<PullRequest> pullRequests) {
List<PullRequest> updatePullRequest = new ArrayList<>();
for (PullRequest pullRequest : pullRequests) { for (PullRequest pullRequest : pullRequests) {
PullRequest newPullRequest = newPullRequests.get(pullRequest.getId()); PullRequest newPullRequest = newPullRequests.get(pullRequest.getId());
@NonNull boolean author = processingAuthor(pullRequest, newPullRequest); processingAuthor(pullRequest, newPullRequest);
@NonNull boolean reviewer = processingReviewer(pullRequest, newPullRequest); processingReviewer(pullRequest, newPullRequest);
if (author || reviewer) {
updatePullRequest.add(newPullRequest);
} }
} }
return updatePullRequest;
}
private boolean processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) { private void processingReviewer(PullRequest pullRequest, PullRequest newPullRequest) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
changeVersionPr(pullRequest, newPullRequest).ifPresent(stringBuilder::append); changeVersionPr(pullRequest, newPullRequest).ifPresent(stringBuilder::append);
String message = stringBuilder.toString(); String message = stringBuilder.toString();
@ -161,13 +173,11 @@ public class SchedulerPullRequest {
newPullRequest.getAuthor().getLogin())) newPullRequest.getAuthor().getLogin()))
.build()) .build())
); );
return true;
} }
return false;
} }
@NonNull @NonNull
private boolean processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) { private void processingAuthor(PullRequest pullRequest, PullRequest newPullRequest) {
final User author = pullRequest.getAuthor(); final User author = pullRequest.getAuthor();
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
if (author.getTelegramId() != null) { if (author.getTelegramId() != null) {
@ -176,10 +186,8 @@ public class SchedulerPullRequest {
final String message = stringBuilder.toString(); final String message = stringBuilder.toString();
if (!Message.EMPTY.equalsIgnoreCase(message)) { if (!Message.EMPTY.equalsIgnoreCase(message)) {
messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build()); messageSendService.add(MessageSend.builder().message(message).telegramId(author.getTelegramId()).build());
return true;
} }
} }
return false;
} }
@NonNull @NonNull

View File

@ -29,4 +29,8 @@ public interface PullRequestsService {
void deleteAll(@NonNull Set<Long> id); 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); pullRequestsRepository.deleteAllByIdIn(id);
} }
@Override
public List<PullRequest> getAllByReviewer(@NonNull String login) {
return pullRequestsRepository.findAllByReviewers(login);
}
@Override
public Set<Long> getAllId() {
return pullRequestsRepository.getAllIds();
}
} }