Рабочая версия

This commit is contained in:
upagge 2020-04-07 11:25:08 +03:00
parent 8bc397dc8c
commit 7fe3388207
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
3 changed files with 27 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.domain.entity.Reviewer; import com.tsc.bitbucketbot.domain.entity.Reviewer;
import com.tsc.bitbucketbot.domain.entity.User; import com.tsc.bitbucketbot.domain.entity.User;
import com.tsc.bitbucketbot.domain.util.ReviewerChange; import com.tsc.bitbucketbot.domain.util.ReviewerChange;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import com.tsc.bitbucketbot.dto.bitbucket.sheet.PullRequestSheetJson; import com.tsc.bitbucketbot.dto.bitbucket.sheet.PullRequestSheetJson;
import com.tsc.bitbucketbot.service.MessageSendService; import com.tsc.bitbucketbot.service.MessageSendService;
import com.tsc.bitbucketbot.service.PullRequestsService; import com.tsc.bitbucketbot.service.PullRequestsService;
@ -31,6 +32,11 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.DECLINED;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.MERGED;
import static com.tsc.bitbucketbot.domain.PullRequestStatus.OPEN;
/** /**
* @author upagge [30.01.2020] * @author upagge [30.01.2020]
@ -39,6 +45,8 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor @RequiredArgsConstructor
public class SchedulerPullRequest { public class SchedulerPullRequest {
private static final Set<PullRequestStatus> STATUSES = Stream.of(MERGED, OPEN, DECLINED).collect(Collectors.toSet());
private final PullRequestsService pullRequestsService; private final PullRequestsService pullRequestsService;
private final UserService userService; private final UserService userService;
private final MessageSendService messageSendService; private final MessageSendService messageSendService;
@ -47,7 +55,9 @@ public class SchedulerPullRequest {
@Scheduled(fixedRate = 30000) @Scheduled(fixedRate = 30000)
public void checkOldPullRequest() { public void checkOldPullRequest() {
final Set<Long> existsId = pullRequestsService.getAllId(); final Set<Long> existsId = pullRequestsService.getAllId(STATUSES).stream()
.map(IdAndStatusPr::getId)
.collect(Collectors.toSet());
final Set<Long> openId = checkOpenPullRequest(); final Set<Long> openId = checkOpenPullRequest();
final Set<Long> closeId = checkClosePullRequest(); final Set<Long> closeId = checkClosePullRequest();
final Set<Long> newNotExistsId = existsId.stream() final Set<Long> newNotExistsId = existsId.stream()
@ -119,17 +129,17 @@ public class SchedulerPullRequest {
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()) {
final PullRequestSheetJson jsonSheet = sheetJson.get(); final PullRequestSheetJson jsonSheet = sheetJson.get();
final Map<Long, PullRequest> existsJsonPr = jsonSheet.getValues().stream() final Map<Long, PullRequest> existsPr = jsonSheet.getValues().stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class)) .map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class))
.peek(pullRequest -> pullRequestsService.getIdByBitbucketIdAndReposId(pullRequest.getBitbucketId(), pullRequest.getRepositoryId()).ifPresent(pullRequest::setId)) .peek(pullRequest -> pullRequestsService.getIdByBitbucketIdAndReposId(pullRequest.getBitbucketId(), pullRequest.getRepositoryId()).ifPresent(pullRequest::setId))
.filter(pullRequest -> pullRequest.getId() != null) .filter(pullRequest -> pullRequest.getId() != null)
.collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest)); .collect(Collectors.toMap(PullRequest::getId, pullRequest -> pullRequest));
final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsJsonPr.keySet()); final Set<PullRequest> pullRequests = pullRequestsService.getAllById(existsPr.keySet());
if (!existsJsonPr.isEmpty() && !pullRequests.isEmpty()) { if (!existsPr.isEmpty() && !pullRequests.isEmpty()) {
processingUpdate(existsJsonPr, pullRequests); processingUpdate(existsPr, pullRequests);
ids.addAll( ids.addAll(
pullRequestsService.updateAll(existsJsonPr.values()).stream() pullRequestsService.updateAll(existsPr.values()).stream()
.map(PullRequest::getId) .map(PullRequest::getId)
.collect(Collectors.toSet()) .collect(Collectors.toSet())
); );

View File

@ -1,8 +1,10 @@
package com.tsc.bitbucketbot.service; package com.tsc.bitbucketbot.service;
import com.tsc.bitbucketbot.domain.Pagination; import com.tsc.bitbucketbot.domain.Pagination;
import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -35,6 +37,8 @@ public interface PullRequestsService {
Set<Long> getAllId(); Set<Long> getAllId();
Set<IdAndStatusPr> getAllId(Collection<PullRequestStatus> statuses);
Page<PullRequest> getAll(@NonNull Pagination pagination); Page<PullRequest> getAll(@NonNull Pagination pagination);
List<PullRequest> getAllByAuthor(@NonNull String login, @NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); List<PullRequest> getAllByAuthor(@NonNull String login, @NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo);

View File

@ -1,8 +1,10 @@
package com.tsc.bitbucketbot.service.impl; package com.tsc.bitbucketbot.service.impl;
import com.tsc.bitbucketbot.domain.Pagination; import com.tsc.bitbucketbot.domain.Pagination;
import com.tsc.bitbucketbot.domain.PullRequestStatus;
import com.tsc.bitbucketbot.domain.ReviewerStatus; import com.tsc.bitbucketbot.domain.ReviewerStatus;
import com.tsc.bitbucketbot.domain.entity.PullRequest; import com.tsc.bitbucketbot.domain.entity.PullRequest;
import com.tsc.bitbucketbot.dto.IdAndStatusPr;
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;
import lombok.NonNull; import lombok.NonNull;
@ -76,6 +78,11 @@ public class PullRequestsServiceImpl implements PullRequestsService {
return pullRequestsRepository.findAllIds(); return pullRequestsRepository.findAllIds();
} }
@Override
public Set<IdAndStatusPr> getAllId(Collection<PullRequestStatus> statuses) {
return pullRequestsRepository.findAllIdByStatusIn(statuses);
}
@Override @Override
public Page<PullRequest> getAll(@NonNull Pagination pagination) { public Page<PullRequest> getAll(@NonNull Pagination pagination) {
return pullRequestsRepository.findAll(PageRequest.of(pagination.getPage(), pagination.getSize())); return pullRequestsRepository.findAll(PageRequest.of(pagination.getPage(), pagination.getSize()));