From b8ea4c3f42ac7189ba98ce5a117348e511208aca Mon Sep 17 00:00:00 2001 From: kimday0326 Date: Thu, 6 Jun 2024 19:36:09 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20PR=20=EA=B8=B0=EB=8A=A5=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/file/application/FileService.java | 35 +++++++----- .../khu/gitbox/domain/file/entity/File.java | 2 +- .../application/PullRequestService.java | 56 +++++++------------ .../pullRequest/entity/PullRequest.java | 3 +- .../entity/PullRequestComment.java | 2 +- .../PullRequestCommentRepository.java | 3 +- .../presentation/PullRequestController.java | 30 +++++----- ...stDto.java => PullRequestGetResponse.java} | 4 +- src/main/resources/db/data.sql | 24 ++++---- 9 files changed, 74 insertions(+), 85 deletions(-) rename src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/{PullRequestDto.java => PullRequestGetResponse.java} (83%) diff --git a/src/main/java/com/khu/gitbox/domain/file/application/FileService.java b/src/main/java/com/khu/gitbox/domain/file/application/FileService.java index 9c56795..0f4d2e9 100644 --- a/src/main/java/com/khu/gitbox/domain/file/application/FileService.java +++ b/src/main/java/com/khu/gitbox/domain/file/application/FileService.java @@ -107,36 +107,41 @@ public FileGetResponse uploadNewVersionFile( .rootFileId(parentFile.getRootFileId()) .parentFileId(parentFile.getId()) .build(); - final File savedFile = fileRepository.save(newVersionFile); + fileRepository.save(newVersionFile); if (workspaceMembers.size() > 1) { // PR 생성 (PR 승인 시 부모 파일을 구버전으로) - createPullRequest(request, member, savedFile); + createPullRequest( + request.pullRequestTitle(), + request.pullRequestMessage(), + member.getId(), + newVersionFile, + parentFile); } else { // PR 승인 - savedFile.approve(parentFile); + newVersionFile.approve(parentFile); } // 워크스페이스 용량 업데이트 final Workspace workspace = workspaceService.findWorkspaceById(parentFile.getWorkspaceId()); - workspace.increaseUsedStorage(savedFile.getSize()); + workspace.increaseUsedStorage(newVersionFile.getSize()); - actionHistoryService.createActionHistory(workspace.getId(), member, savedFile, Action.PULL_REQUEST); - return FileGetResponse.of(savedFile); + actionHistoryService.createActionHistory(workspace.getId(), member, newVersionFile, Action.PULL_REQUEST); + return FileGetResponse.of(newVersionFile); } - private void createPullRequest(PullRequestCreateRequest request, Member member, File savedFile) { + private void createPullRequest(String title, String message, Long writerId, File newVersionFile, File parentFile) { // PR 생성 final PullRequest pullRequest = PullRequest.builder() - .title(request.pullRequestTitle()) - .message(request.pullRequestMessage()) - .writerId(member.getId()) - .fileId(savedFile.getId()) + .title(title) + .message(message) + .writerId(writerId) + .fileId(newVersionFile.getId()) + .parentFileId(parentFile.getId()) .build(); - pullRequestRepository.save(pullRequest); - - // 파일 PR ID 설정 - savedFile.updatePullRequestId(pullRequest.getId()); + Long pullRequestId = pullRequestRepository.save(pullRequest).getId(); + // 부모 파일 PR ID 설정 + parentFile.updatePullRequestId(pullRequestId); } // 파일 조회 diff --git a/src/main/java/com/khu/gitbox/domain/file/entity/File.java b/src/main/java/com/khu/gitbox/domain/file/entity/File.java index 12cdd70..c6ea0fa 100644 --- a/src/main/java/com/khu/gitbox/domain/file/entity/File.java +++ b/src/main/java/com/khu/gitbox/domain/file/entity/File.java @@ -121,7 +121,7 @@ public void reject(File parentFile) { this.status = FileStatus.REJECTED; } - public void initPullRequestId() { + private void initPullRequestId() { this.pullRequestId = null; } diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/application/PullRequestService.java b/src/main/java/com/khu/gitbox/domain/pullRequest/application/PullRequestService.java index ef01b5b..fed34d3 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/application/PullRequestService.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/application/PullRequestService.java @@ -10,7 +10,7 @@ import com.khu.gitbox.domain.pullRequest.infrastructure.PullRequestCommentRepository; import com.khu.gitbox.domain.pullRequest.infrastructure.PullRequestRepository; import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestCommentCreateRequest; -import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestDto; +import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestGetResponse; import com.khu.gitbox.domain.workspace.entity.WorkspaceMember; import com.khu.gitbox.domain.workspace.infrastructure.WorkspaceMemberRepository; import lombok.RequiredArgsConstructor; @@ -33,67 +33,51 @@ public class PullRequestService { private final WorkspaceMemberRepository workspaceMemberRepository; private final FileService fileService; - public PullRequestDto infoPullRequest(Long fileId) { - PullRequest pullRequest = pullRequestRepository.findByFileId(fileId).orElseThrow(() -> { - throw new CustomException(HttpStatus.NOT_FOUND, "pull-request가 존재하지 않습니다. 해당 파일을 다시 확인해주세요"); - }); + public PullRequestGetResponse getPullRequest(Long pullRequestId) { + PullRequest pullRequest = pullRequestRepository.findById(pullRequestId).orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "해당 pull-request를 찾을 수 없습니다.")); + Member writer = memberRepository.findById(pullRequest.getWriterId()).orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "작성자가 존재하지 않습니다.")); + File file = fileService.findFileById(pullRequest.getFileId()); - Member writer = memberRepository.findById(pullRequest.getWriterId()).orElseThrow(() -> { - throw new CustomException(HttpStatus.NOT_FOUND, "작성자가 존재하지 않습니다."); - }); - File file = fileService.findFileById(fileId); + List comments = pullRequestCommentRepository.findAllByPullRequestId(pullRequest.getId()); - List commentList = pullRequestCommentRepository.findAllByPullRequestId(pullRequest.getId()) - .orElseThrow(() -> { - throw new CustomException(HttpStatus.NOT_FOUND, "코멘트를 찾을 수 없습니다."); - }); - - PullRequestDto pullRequestDto = PullRequestDto.builder() + return PullRequestGetResponse.builder() .title(pullRequest.getTitle()) .message(pullRequest.getMessage()) .writer(writer.getEmail()) + .parentFileId(file.getParentFileId()) + .fileId(file.getId()) .fileUrl(file.getUrl()) + .comments(comments) .build(); - - if (!commentList.isEmpty()) { - pullRequestDto.setComments(commentList); - } - return pullRequestDto; } - public void isApprovedPullRequest(PullRequestCommentCreateRequest pullRequestCommentCreateRequest, Long reviewerId, - Long fileId) { - File file = fileService.findFileById(fileId); - + public void createComment(PullRequestCommentCreateRequest request, Long reviewerId, Long pullRequestId) { + PullRequest pullRequest = pullRequestRepository.findById(pullRequestId) + .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "해당 pull-request를 찾을 수 없습니다.")); + File file = fileService.findFileById(pullRequest.getFileId()); if (file.getWriterId().equals(reviewerId)) { throw new CustomException(HttpStatus.BAD_REQUEST, "본인이 작성한 파일에 대한 pull-request는 승인할 수 없습니다."); } - PullRequest pullRequest = pullRequestRepository.findByFileId(fileId) - .orElseThrow(() -> new CustomException(HttpStatus.BAD_REQUEST, "현재 보낸 파일(fileId)을 찾을 수 없습니다.")); - - // comment 내용 저장 PullRequestComment pullRequestComment = new PullRequestComment( - pullRequestCommentCreateRequest.getComment(), - pullRequestCommentCreateRequest.getIsApproved(), + request.getComment(), + request.getIsApproved(), reviewerId, pullRequest.getId() ); pullRequestCommentRepository.save(pullRequestComment); - List responsers = pullRequestCommentRepository.findAllByPullRequestId( - pullRequest.getId()).get(); - + List comments = pullRequestCommentRepository.findAllByPullRequestId(pullRequest.getId()); List members = workspaceMemberRepository.findByWorkspaceId(file.getWorkspaceId()); - if (members.size() - 1 == responsers.size()) { + if (members.size() - 1 == comments.size()) { log.info("모든 멤버가 리뷰를 완료했습니다."); int trueCount = 0; int falseCount = 0; - for (PullRequestComment responser : responsers) { - if (responser.getIsApproved()) + for (PullRequestComment comment : comments) { + if (comment.isApproved()) trueCount++; else falseCount++; diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequest.java b/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequest.java index ce86068..0161466 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequest.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequest.java @@ -32,10 +32,11 @@ public class PullRequest extends BaseEntity { private Long fileId; @Builder - public PullRequest(String title, String message, Long writerId, Long fileId) { + public PullRequest(String title, String message, Long writerId, Long fileId, Long parentFileId) { this.title = title; this.message = message; this.writerId = writerId; this.fileId = fileId; + this.parentFileId = parentFileId; } } diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequestComment.java b/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequestComment.java index ac9a8bb..bea7890 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequestComment.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/entity/PullRequestComment.java @@ -20,7 +20,7 @@ public class PullRequestComment extends BaseEntity { private String comment; @Column(name = "is_approved", nullable = false) - private Boolean isApproved; + private boolean isApproved; @Column(name = "reviewer_id", nullable = false) private Long reviewerId; diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/infrastructure/PullRequestCommentRepository.java b/src/main/java/com/khu/gitbox/domain/pullRequest/infrastructure/PullRequestCommentRepository.java index 3ba9a85..069729b 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/infrastructure/PullRequestCommentRepository.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/infrastructure/PullRequestCommentRepository.java @@ -5,10 +5,9 @@ import org.springframework.stereotype.Repository; import java.util.List; -import java.util.Optional; @Repository public interface PullRequestCommentRepository extends JpaRepository { - Optional> findAllByPullRequestId(Long pullRequestId); + List findAllByPullRequestId(Long pullRequestId); } diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/PullRequestController.java b/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/PullRequestController.java index 3964a8e..b077035 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/PullRequestController.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/PullRequestController.java @@ -1,38 +1,36 @@ package com.khu.gitbox.domain.pullRequest.presentation; -import com.khu.gitbox.auth.provider.JwtTokenProvider; import com.khu.gitbox.common.response.ApiResponse; import com.khu.gitbox.domain.pullRequest.application.PullRequestService; import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestCommentCreateRequest; -import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestDto; +import com.khu.gitbox.domain.pullRequest.presentation.dto.PullRequestGetResponse; import com.khu.gitbox.util.SecurityContextUtil; +import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor -@RequestMapping("/api/files/{fileId}") +@RequestMapping("/api/pull-request") public class PullRequestController { - private final PullRequestService pullRequestService; - private final JwtTokenProvider jwtTokenProvider; - - @GetMapping("/pr") - public ApiResponse infoPullRequest(@PathVariable Long fileId) { - PullRequestDto pullRequestDto = pullRequestService.infoPullRequest(fileId); - return ApiResponse.ok(pullRequestDto); + @Operation(summary = "PR 상세 정보 조회") + @GetMapping("/{pullRequestId}") + public ApiResponse getPullRequest(@PathVariable Long pullRequestId) { + PullRequestGetResponse pullRequestGetResponse = pullRequestService.getPullRequest(pullRequestId); + return ApiResponse.ok(pullRequestGetResponse); } - @PostMapping("/pr") + @Operation(summary = "PR 코멘트 생성 (PR 승인/거절)") + @PostMapping("/{pullRequestId}/comments") public ApiResponse createPullRequestComment( - @RequestBody PullRequestCommentCreateRequest pullRequestCommentCreateRequest, - @PathVariable Long fileId) { - + @RequestBody PullRequestCommentCreateRequest commentCreateRequest, + @PathVariable Long pullRequestId) { Long reviewerId = SecurityContextUtil.getCurrentMemberId(); - pullRequestService.isApprovedPullRequest(pullRequestCommentCreateRequest, reviewerId, fileId); + pullRequestService.createComment(commentCreateRequest, reviewerId, pullRequestId); - return ApiResponse.created(pullRequestCommentCreateRequest.getIsApproved()); + return ApiResponse.created(commentCreateRequest.getIsApproved()); } } diff --git a/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestDto.java b/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestGetResponse.java similarity index 83% rename from src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestDto.java rename to src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestGetResponse.java index 2f0286c..234286b 100644 --- a/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestDto.java +++ b/src/main/java/com/khu/gitbox/domain/pullRequest/presentation/dto/PullRequestGetResponse.java @@ -12,10 +12,12 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class PullRequestDto { +public class PullRequestGetResponse { private String title; private String message; private String writer; + private Long parentFileId; + private Long fileId; private String fileUrl; private List comments; diff --git a/src/main/resources/db/data.sql b/src/main/resources/db/data.sql index 3cd16df..05c9e2a 100644 --- a/src/main/resources/db/data.sql +++ b/src/main/resources/db/data.sql @@ -31,19 +31,19 @@ INSERT INTO gitbox.file (is_deleted, is_latest, created_at, folder_id, parent_fi VALUES (false, true, '2024-05-31 14:58:06.197759', 1, null, null, 1, 2955371, '2024-05-31 14:58:06.221400', 1, 1, 1, 'temp-image.jpeg', 'https://gitbox-file-bucket.s3.ap-northeast-2.amazonaws.com/temp-image.jpeg_30bc7', 'APPROVED', null, 'JPEG'); -INSERT INTO gitbox.file (is_deleted, is_latest, created_at, folder_id, parent_file_id, pull_request_id, root_file_id, - size, updated_at, version, workspace_id, writer_id, name, url, status, tag, type) -VALUES (false, false, '2024-05-31 14:59:49.801787', 1, 1, 1, 1, 2955371, '2024-05-31 15:01:12.329042', 2, 1, 1, - '새로운 버전 파일', 'https://gitbox-file-bucket.s3.ap-northeast-2.amazonaws.com/temp-image-2.jpeg_c1cbc', 'PENDING', - 'RED', 'JPEG'); - -INSERT INTO gitbox.pull_request (created_at, file_id, parent_file_id, updated_at, writer_id, message, title) -VALUES ('2024-05-31 14:59:49.821650', 2, null, '2024-05-31 14:59:49.821650', 1, '확인부탁드려요.', '새로운 파일 버전입니다.'); +-- INSERT INTO gitbox.file (is_deleted, is_latest, created_at, folder_id, parent_file_id, pull_request_id, root_file_id, +-- size, updated_at, version, workspace_id, writer_id, name, url, status, tag, type) +-- VALUES (false, false, '2024-05-31 14:59:49.801787', 1, 1, 1, 1, 2955371, '2024-05-31 15:01:12.329042', 2, 1, 1, +-- '새로운 버전 파일', 'https://gitbox-file-bucket.s3.ap-northeast-2.amazonaws.com/temp-image-2.jpeg_c1cbc', 'PENDING', +-- 'RED', 'JPEG'); +-- +-- INSERT INTO gitbox.pull_request (created_at, file_id, parent_file_id, updated_at, writer_id, message, title) +-- VALUES ('2024-05-31 14:59:49.821650', 2, null, '2024-05-31 14:59:49.821650', 1, '확인부탁드려요.', '새로운 파일 버전입니다.'); INSERT INTO gitbox.action_history (created_at, file_id, member_id, updated_at, workspace_id, file_name, member_name, action) VALUES ('2024-05-31 14:58:06.210129', 1, 1, '2024-05-31 14:58:06.210129', 1, 'temp-image.jpeg', 'string', 'UPLOAD'); -INSERT INTO gitbox.action_history (created_at, file_id, member_id, updated_at, workspace_id, file_name, member_name, - action) -VALUES ('2024-05-31 14:59:49.856268', 2, 1, '2024-05-31 14:59:49.856268', 1, 'temp-image-2.jpeg', 'string', - 'PULL_REQUEST'); +-- INSERT INTO gitbox.action_history (created_at, file_id, member_id, updated_at, workspace_id, file_name, member_name, +-- action) +-- VALUES ('2024-05-31 14:59:49.856268', 2, 1, '2024-05-31 14:59:49.856268', 1, 'temp-image-2.jpeg', 'string', +-- 'PULL_REQUEST');