Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…orea into feat/#703
  • Loading branch information
chlwlstlf committed Nov 4, 2024
2 parents 481ab65 + a5e2ba9 commit 88ffda1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions backend/src/main/java/corea/exception/ExceptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "깃허브 인증 서버가 원활하게 작동하지 않습니다."),
;

Expand Down
11 changes: 11 additions & 0 deletions backend/src/main/java/corea/global/util/FutureUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package corea.global.util;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class FutureUtil {

public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) {
return CompletableFuture.supplyAsync(supplier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,27 +38,26 @@ public GithubPullRequestReviewInfo provideReviewInfo(String prLink) {
}

private GithubPullRequestReviewInfo getGithubPullRequestReviewInfo(String prLink) {
List<GithubPullRequestReview> reviews = getAllPullRequestReviews(prLink);
Map<String, GithubPullRequestReview> result = collectByGithubUserId(reviews);

return new GithubPullRequestReviewInfo(result);
CompletableFuture<List<GithubPullRequestReview>> reviewFuture = supplyAsync(() -> reviewClient.getPullRequestReviews(prLink));
CompletableFuture<List<GithubPullRequestReview>> 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<GithubPullRequestReview> getAllPullRequestReviews(String prLink) {
List<GithubPullRequestReview> reviews = reviewClient.getPullRequestReviews(prLink);
List<GithubPullRequestReview> comments = commentClient.getPullRequestReviews(prLink);

return Stream.concat(reviews.stream(), comments.stream())
.toList();
private Map<String, GithubPullRequestReview> collectPullRequestReviews(List<GithubPullRequestReview> reviews, List<GithubPullRequestReview> comments) {
return collectByGithubUserId(Stream.concat(reviews.stream(), comments.stream()));
}

private Map<String, GithubPullRequestReview> collectByGithubUserId(List<GithubPullRequestReview> reviews) {
return reviews.stream()
.collect(Collectors.toMap(
GithubPullRequestReview::getGithubUserId,
Function.identity(),
(x, y) -> x
));
private Map<String, GithubPullRequestReview> collectByGithubUserId(Stream<GithubPullRequestReview> reviews) {
return reviews.collect(Collectors.toMap(
GithubPullRequestReview::getGithubUserId,
Function.identity(),
(x, y) -> x
));
}

private void validatePrLink(String prUrl) {
Expand All @@ -75,7 +77,9 @@ private List<String> extractPrLinkParts(String prUrl) {

private boolean isInvalidGithubPrUrl(List<String> 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);
}
}

0 comments on commit 88ffda1

Please sign in to comment.