Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pp449 committed Nov 4, 2024
2 parents ca9b3c5 + a5e2ba9 commit 19ab332
Show file tree
Hide file tree
Showing 49 changed files with 498 additions and 407 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package corea.auth.infrastructure;

import corea.global.config.Constants;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

@EnableConfigurationProperties(GithubProperties.class)
@Component
public class GithubPersonalAccessTokenProvider {

private static final Random RANDOM = new Random();

private final List<String> personalAccessTokens;

public GithubPersonalAccessTokenProvider(GithubProperties githubProperties) {
this.personalAccessTokens = githubProperties.pullRequest()
.tokens();
}

public String getRandomPersonalAccessToken() {
if (personalAccessTokens.isEmpty()) {
return "";
}
return Constants.TOKEN_TYPE + personalAccessTokens.get(getRandomIndex());
}

private int getRandomIndex() {
return RANDOM.nextInt(personalAccessTokens.size());
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public DevelopFeedback findDevelopFeedback(long roomId, long deliverId, String u
return developFeedbackRepository.findByRoomIdAndDeliverIdAndReceiverUsername(roomId, deliverId, username)
.orElseThrow(() -> new CoreaException(ExceptionType.FEEDBACK_NOT_FOUND));
}

public boolean existsByRoomIdAndDeliverAndReceiver(long roomId, long deliverId, long receiverId) {
return developFeedbackRepository.existsByRoomIdAndDeliverIdAndReceiverId(roomId, deliverId, receiverId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public SocialFeedback findSocialFeedback(long roomId, long deliverId, String use
return socialFeedbackRepository.findByRoomIdAndDeliverIdAndReceiverUsername(roomId, deliverId, username)
.orElseThrow(() -> new CoreaException(ExceptionType.FEEDBACK_NOT_FOUND));
}

public boolean existsByRoomIdAndDeliverAndReceiver(long rooomId, long deliverId, long receiverId) {
return socialFeedbackRepository.existsByRoomIdAndDeliverIdAndReceiverId(rooomId, deliverId, receiverId);
}
}
25 changes: 25 additions & 0 deletions backend/src/main/java/corea/feedback/dto/FeedbackOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import corea.feedback.util.FeedbackKeywordConverter;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Collections;
import java.util.List;

@Schema(description = "개발 피드백 + 커뮤니케이션 피드백 조회 응답")
Expand All @@ -23,6 +24,9 @@ public record FeedbackOutput(@Schema(description = "피드백 아이디", exampl
@Schema(description = "유저 이름", example = "jcoding-play")
String username,

@Schema(description = "내가 상대방의 피드백 작성을 완료하였는지 여부", example = "false")
boolean isWrited,

@Schema(description = "선택한 피드백 키워드", example = "[\"코드를 이해하기 쉬웠어요\", \"컨벤션이 잘 지켜졌어요\"]")
List<String> feedbackKeywords,

Expand All @@ -32,13 +36,17 @@ public record FeedbackOutput(@Schema(description = "피드백 아이디", exampl
@Schema(description = "부가 작성 가능한 피드백 텍스트", example = "처음 자바를 접해봤다고 했는데 생각보다 매우 잘 구성되어 있는 코드였습니다. ...")
String feedbackText) {

private static final boolean FEEDBACK_COMPLETE = true;
private static final boolean FEEDBACK_INCOMPLETE = false;

public static FeedbackOutput fromReceiver(DevelopFeedback developFeedback) {
return new FeedbackOutput(
developFeedback.getId(),
developFeedback.getRoomId(),
developFeedback.getDeliver().getId(),
developFeedback.getDeliver().getThumbnailUrl(),
developFeedback.getDeliver().getUsername(),
FEEDBACK_COMPLETE,
FeedbackKeywordConverter.convertToMessages(developFeedback.getKeywords()),
developFeedback.getEvaluatePoint(),
developFeedback.getFeedBackText()
Expand All @@ -52,6 +60,7 @@ public static FeedbackOutput fromDeliver(DevelopFeedback developFeedback) {
developFeedback.getReceiver().getId(),
developFeedback.getReceiver().getThumbnailUrl(),
developFeedback.getReceiver().getUsername(),
FEEDBACK_COMPLETE,
FeedbackKeywordConverter.convertToMessages(developFeedback.getKeywords()),
developFeedback.getEvaluatePoint(),
developFeedback.getFeedBackText()
Expand All @@ -65,6 +74,7 @@ public static FeedbackOutput fromReceiver(SocialFeedback socialFeedback) {
socialFeedback.getDeliver().getId(),
socialFeedback.getDeliver().getThumbnailUrl(),
socialFeedback.getDeliver().getUsername(),
FEEDBACK_COMPLETE,
FeedbackKeywordConverter.convertToMessages(socialFeedback.getKeywords()),
socialFeedback.getEvaluatePoint(),
socialFeedback.getFeedBackText()
Expand All @@ -78,9 +88,24 @@ public static FeedbackOutput fromDeliver(SocialFeedback socialFeedback) {
socialFeedback.getReceiver().getId(),
socialFeedback.getReceiver().getThumbnailUrl(),
socialFeedback.getReceiver().getUsername(),
FEEDBACK_COMPLETE,
FeedbackKeywordConverter.convertToMessages(socialFeedback.getKeywords()),
socialFeedback.getEvaluatePoint(),
socialFeedback.getFeedBackText()
);
}

public static FeedbackOutput masking(FeedbackOutput feedbackOutput) {
return new FeedbackOutput(
feedbackOutput.feedbackId(),
feedbackOutput.roomId,
feedbackOutput.receiverId,
feedbackOutput.profile,
feedbackOutput.username,
FEEDBACK_INCOMPLETE,
Collections.emptyList(),
0,
""
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package corea.feedback.dto;

import corea.room.domain.Room;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Collections;
import java.util.List;

@Schema(description = "개발 피드백 + 커뮤니케이션 피드백 작성 응답")
Expand All @@ -20,6 +22,9 @@ public record FeedbackResponse(@Schema(description = "피드백 아이디", exam
@Schema(description = "유저 이름", example = "jcoding-play")
String username,

@Schema(description = "내가 상대방의 피드백 작성을 완료하였는지 여부", example = "false")
boolean isWrited,

@Schema(description = "선택한 피드백 키워드", example = "[\"코드를 이해하기 쉬웠어요\", \"컨벤션이 잘 지켜졌어요\"]")
List<String> feedbackKeywords,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private FeedbackResponse toFeedbackResponse(FeedbackOutput output) {
output.receiverId(),
output.profile(),
output.username(),
output.isWrited(),
output.feedbackKeywords(),
output.evaluationPoint(),
output.feedbackText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import corea.room.domain.Room;
import corea.room.repository.RoomRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.function.TriFunction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -41,23 +42,49 @@ public UserFeedbackResponse getReceivedFeedback(long feedbackReceiverId) {
Map<Long, List<FeedbackOutput>> developFeedbackOutput = developFeedbackReader.collectReceivedDevelopFeedback(feedbackReceiverId);
Map<Long, List<FeedbackOutput>> socialFeedbackOutput = socialFeedbackReader.collectReceivedSocialFeedback(feedbackReceiverId);

maskingIfNoFeedbackDeliver(feedbackReceiverId, developFeedbackOutput, socialFeedbackOutput);

return getUserFeedbackResponse(developFeedbackOutput, socialFeedbackOutput, Room::isClosed);
}

private UserFeedbackResponse getUserFeedbackResponse(Map<Long, List<FeedbackOutput>> developFeedbackOutput, Map<Long, List<FeedbackOutput>> socialFeedbackOutput, Predicate<Room> predicate) {
private UserFeedbackResponse getUserFeedbackResponse(Map<Long, List<FeedbackOutput>> developFeedbackOutput, Map<Long, List<FeedbackOutput>> socialFeedbackOutput, Predicate<Room> roomStatusPredicate) {
Map<Long, List<FeedbackResponse>> developFeedbacks = feedbackMapper.toFeedbackResponseMap(developFeedbackOutput);
Map<Long, List<FeedbackResponse>> socialFeedbacks = feedbackMapper.toFeedbackResponseMap(socialFeedbackOutput);

List<FeedbacksResponse> feedbacksResponses = getFeedbacksResponses(developFeedbacks, socialFeedbacks, predicate);
List<FeedbacksResponse> feedbacksResponses = getFeedbacksResponses(developFeedbacks, socialFeedbacks, roomStatusPredicate);
return new UserFeedbackResponse(feedbacksResponses);
}

private List<FeedbacksResponse> getFeedbacksResponses(Map<Long, List<FeedbackResponse>> developFeedbacks, Map<Long, List<FeedbackResponse>> socialFeedbacks, Predicate<Room> predicate) {
private void maskingIfNoFeedbackDeliver(long receiverId, Map<Long, List<FeedbackOutput>> developFeedbackOutput, Map<Long, List<FeedbackOutput>> socialFeedbackOutput) {
developFeedbackOutput.forEach((key, value) -> developFeedbackOutput.put(key, maskingFeedback(receiverId, value, true)));
socialFeedbackOutput.forEach((key, value) -> socialFeedbackOutput.put(key, maskingFeedback(receiverId, value, false)));
}

private List<FeedbackOutput> maskingFeedback(long receiverId, List<FeedbackOutput> feedbackOutputs, boolean needToCheckSocialFeedback) {

TriFunction<Long, Long, Long, Boolean> feedbackExistencePredicate = needToCheckSocialFeedback ? socialFeedbackReader::existsByRoomIdAndDeliverAndReceiver : developFeedbackReader::existsByRoomIdAndDeliverAndReceiver;
return feedbackOutputs.stream()
.map(feedbackOutput -> {
if (needToMasking(receiverId, feedbackOutput, feedbackExistencePredicate)) {
return FeedbackOutput.masking(feedbackOutput);
}
return feedbackOutput;
})
.toList();
}

private boolean needToMasking(long receiverId, FeedbackOutput feedbackResponse, TriFunction<Long, Long, Long, Boolean> feedbackExistencePredicate) {
long roomId = feedbackResponse.roomId();
long deliverId = feedbackResponse.receiverId();
return !feedbackExistencePredicate.apply(roomId, receiverId, deliverId);
}

private List<FeedbacksResponse> getFeedbacksResponses(Map<Long, List<FeedbackResponse>> developFeedbacks, Map<Long, List<FeedbackResponse>> socialFeedbacks, Predicate<Room> roomStatusPredicate) {
List<Long> roomIds = extractDistinctKeyStreams(developFeedbacks, socialFeedbacks).toList();
List<Room> rooms = roomRepository.findAllById(roomIds);

return rooms.stream()
.filter(predicate)
.filter(roomStatusPredicate)
.map(room -> FeedbacksResponse.of(room, emptyListIfNull(developFeedbacks.get(room.getId())), emptyListIfNull(socialFeedbacks.get(room.getId()))))
.toList();
}
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
@@ -1,22 +1,19 @@
package corea.matching.infrastructure;

import corea.auth.infrastructure.GithubProperties;
import corea.auth.infrastructure.GithubPersonalAccessTokenProvider;
import corea.matching.infrastructure.dto.PullRequestData;
import corea.matching.infrastructure.dto.PullRequestResponse;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

import java.util.List;
import java.util.Random;

import static org.springframework.http.MediaType.APPLICATION_JSON;

@EnableConfigurationProperties(GithubProperties.class)
@Component
@RequiredArgsConstructor
public class GithubPullRequestClient {

private static final String GITHUB_URL = "https://github.com/";
Expand All @@ -26,25 +23,18 @@ public class GithubPullRequestClient {
private static final String PAGE = "&page=";
private static final String STATE_OPEN = "?state=open";
private static final String DIRECTION = "&direction=asc";
private static final Random RANDOM = new Random();

private static final Logger log = LogManager.getLogger(GithubPullRequestClient.class);

private final RestClient restClient;
private final List<String> personalAccessTokens;

public GithubPullRequestClient(final RestClient restClient, final GithubProperties githubProperties) {
this.restClient = restClient;
this.personalAccessTokens = githubProperties.pullRequest()
.tokens();
}
private final GithubPersonalAccessTokenProvider githubPersonalAccessTokenProvider;

public PullRequestData getPullRequestListWithPageNumber(String repositoryLink, int perPageSize, int pageNumber) {
String requestLink = constructApiLink(repositoryLink, perPageSize, pageNumber);
log.debug("요청 링크:{}", requestLink);
PullRequestResponse[] response = restClient.get()
.uri(requestLink)
.header(HttpHeaders.AUTHORIZATION, getRandomPersonalAccessToken())
.header(HttpHeaders.AUTHORIZATION, githubPersonalAccessTokenProvider.getRandomPersonalAccessToken())
.accept(APPLICATION_JSON)
.retrieve()
.body(PullRequestResponse[].class);
Expand All @@ -67,11 +57,4 @@ private String constructApiLink(String repositoryLink, int pageSize, int pageNum
private String convertApiLink(String repositoryLink) {
return repositoryLink.replace(GITHUB_URL, GITHUB_REPO_API_URL);
}

private String getRandomPersonalAccessToken() {
if (personalAccessTokens.isEmpty()) {
return "";
}
return "Bearer " + personalAccessTokens.get(RANDOM.nextInt(personalAccessTokens.size()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package corea.review.infrastructure;

import corea.auth.infrastructure.GithubPersonalAccessTokenProvider;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

@Component
public class GithubPullRequestCommentClient extends GithubReviewClient {

public GithubPullRequestCommentClient(RestClient restClient, GithubPullRequestUrlExchanger githubPullRequestUrlExchanger, GithubPersonalAccessTokenProvider githubPersonalAccessTokenProvider) {
super(restClient, githubPullRequestUrlExchanger, githubPersonalAccessTokenProvider);
}

@Override
protected String prLinkToGithubApiUrl(String prLink) {
GithubPullRequestUrlExchanger githubPullRequestUrlExchanger = getGithubPullRequestUrlExchanger();
return githubPullRequestUrlExchanger.prLinkToCommentApiUrl(prLink);
}
}
Loading

0 comments on commit 19ab332

Please sign in to comment.