From a5e2ba9423f34311dad36f8a8dd7b6b2e68399c5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:25:30 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20=EA=B9=83=ED=97=88=EB=B8=8C=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0,=EC=BD=94=EB=A9=98=ED=8A=B8=20API=20=EB=B9=84?= =?UTF-8?q?=EB=8F=99=EA=B8=B0=EB=A1=9C=20=EB=8F=99=EC=8B=9C=EC=97=90=20?= =?UTF-8?q?=EB=B3=B4=EB=82=B4=EA=B2=8C=20=EB=B3=80=EA=B2=BD(#701)=20(#702)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: prLink 검증 로직 위치 변경 * refactor: 깃허브 pr 링크를 api url로 변환하는 기능 수정 * test: 테스트 메서드 위치 변경 * feat: PAT 발급받는 기능 구현 * refactor: 중복 코드 추상 클래스로 제거 * refactor: 클래스명 변경 GithubPullRequestClient라는 클래스명을 이미 사용중이라 변경 * refactor: pr 리뷰 정보를 가져오는 기능 메서드 분리 * refactor: 변수명 변경 * refactor: pr link 검증 로직 메서드 분리 * feat: CompletableFuture 반환하는 유틸 함수 구현 * feat: 비동기를 통해 동시 요청 보내는 기능 구현 --------- Co-authored-by: gyungchan Jo Co-authored-by: 희선이 <98307410+youngsu5582@users.noreply.github.com> --- .../java/corea/exception/ExceptionType.java | 1 + .../java/corea/global/util/FutureUtil.java | 11 +++++ .../infrastructure/GithubReviewProvider.java | 42 ++++++++++--------- 3 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 backend/src/main/java/corea/global/util/FutureUtil.java diff --git a/backend/src/main/java/corea/exception/ExceptionType.java b/backend/src/main/java/corea/exception/ExceptionType.java index d61877058..172d5aeb0 100644 --- a/backend/src/main/java/corea/exception/ExceptionType.java +++ b/backend/src/main/java/corea/exception/ExceptionType.java @@ -40,6 +40,7 @@ public enum ExceptionType { AUTOMATIC_UPDATE_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 방에 예약된 자동 업데이트 정보를 찾을 수 없습니다."), SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버에 문제가 발생했습니다."), + GITHUB_SERVER_ERROR(HttpStatus.SERVICE_UNAVAILABLE, "깃허브 서버가 원활하게 작동하지 않습니다."), GITHUB_AUTHORIZATION_ERROR(HttpStatus.SERVICE_UNAVAILABLE, "깃허브 인증 서버가 원활하게 작동하지 않습니다."), ; diff --git a/backend/src/main/java/corea/global/util/FutureUtil.java b/backend/src/main/java/corea/global/util/FutureUtil.java new file mode 100644 index 000000000..4b638c20e --- /dev/null +++ b/backend/src/main/java/corea/global/util/FutureUtil.java @@ -0,0 +1,11 @@ +package corea.global.util; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; + +public class FutureUtil { + + public static CompletableFuture supplyAsync(Supplier supplier) { + return CompletableFuture.supplyAsync(supplier); + } +} diff --git a/backend/src/main/java/corea/review/infrastructure/GithubReviewProvider.java b/backend/src/main/java/corea/review/infrastructure/GithubReviewProvider.java index 623b97522..d61922349 100644 --- a/backend/src/main/java/corea/review/infrastructure/GithubReviewProvider.java +++ b/backend/src/main/java/corea/review/infrastructure/GithubReviewProvider.java @@ -9,10 +9,13 @@ import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; +import static corea.global.util.FutureUtil.supplyAsync; + @Component @RequiredArgsConstructor public class GithubReviewProvider { @@ -35,27 +38,26 @@ public GithubPullRequestReviewInfo provideReviewInfo(String prLink) { } private GithubPullRequestReviewInfo getGithubPullRequestReviewInfo(String prLink) { - List reviews = getAllPullRequestReviews(prLink); - Map result = collectByGithubUserId(reviews); - - return new GithubPullRequestReviewInfo(result); + CompletableFuture> reviewFuture = supplyAsync(() -> reviewClient.getPullRequestReviews(prLink)); + CompletableFuture> commentFuture = supplyAsync(() -> commentClient.getPullRequestReviews(prLink)); + + return reviewFuture + .thenCombine(commentFuture, this::collectPullRequestReviews) + .exceptionally(e -> {throw new CoreaException(ExceptionType.GITHUB_SERVER_ERROR);}) + .thenApply(GithubPullRequestReviewInfo::new) + .join(); } - private List getAllPullRequestReviews(String prLink) { - List reviews = reviewClient.getPullRequestReviews(prLink); - List comments = commentClient.getPullRequestReviews(prLink); - - return Stream.concat(reviews.stream(), comments.stream()) - .toList(); + private Map collectPullRequestReviews(List reviews, List comments) { + return collectByGithubUserId(Stream.concat(reviews.stream(), comments.stream())); } - private Map collectByGithubUserId(List reviews) { - return reviews.stream() - .collect(Collectors.toMap( - GithubPullRequestReview::getGithubUserId, - Function.identity(), - (x, y) -> x - )); + private Map collectByGithubUserId(Stream reviews) { + return reviews.collect(Collectors.toMap( + GithubPullRequestReview::getGithubUserId, + Function.identity(), + (x, y) -> x + )); } private void validatePrLink(String prUrl) { @@ -75,7 +77,9 @@ private List extractPrLinkParts(String prUrl) { private boolean isInvalidGithubPrUrl(List prLinkParts) { return prLinkParts.size() != VALID_URL_SPLIT_COUNT || - !prLinkParts.get(DOMAIN_PREFIX_INDEX).contains(GITHUB_PREFIX) || - !prLinkParts.get(GITHUB_PULL_REQUEST_URL_INDEX).equals(GITHUB_PULL_REQUEST_DOMAIN); + !prLinkParts.get(DOMAIN_PREFIX_INDEX) + .contains(GITHUB_PREFIX) || + !prLinkParts.get(GITHUB_PULL_REQUEST_URL_INDEX) + .equals(GITHUB_PULL_REQUEST_DOMAIN); } }