Skip to content

Commit

Permalink
fix: PR 기능 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimday0326 committed Jun 6, 2024
1 parent 07a4831 commit b8ea4c3
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// 파일 조회
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/khu/gitbox/domain/file/entity/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void reject(File parentFile) {
this.status = FileStatus.REJECTED;
}

public void initPullRequestId() {
private void initPullRequestId() {
this.pullRequestId = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PullRequestComment> comments = pullRequestCommentRepository.findAllByPullRequestId(pullRequest.getId());

List<PullRequestComment> 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<PullRequestComment> responsers = pullRequestCommentRepository.findAllByPullRequestId(
pullRequest.getId()).get();

List<PullRequestComment> comments = pullRequestCommentRepository.findAllByPullRequestId(pullRequest.getId());
List<WorkspaceMember> 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++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PullRequestCommentRepository extends JpaRepository<PullRequestComment, Long> {

Optional<List<PullRequestComment>> findAllByPullRequestId(Long pullRequestId);
List<PullRequestComment> findAllByPullRequestId(Long pullRequestId);
}
Original file line number Diff line number Diff line change
@@ -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<PullRequestDto> infoPullRequest(@PathVariable Long fileId) {
PullRequestDto pullRequestDto = pullRequestService.infoPullRequest(fileId);

return ApiResponse.ok(pullRequestDto);
@Operation(summary = "PR 상세 정보 조회")
@GetMapping("/{pullRequestId}")
public ApiResponse<PullRequestGetResponse> getPullRequest(@PathVariable Long pullRequestId) {
PullRequestGetResponse pullRequestGetResponse = pullRequestService.getPullRequest(pullRequestId);
return ApiResponse.ok(pullRequestGetResponse);
}

@PostMapping("/pr")
@Operation(summary = "PR 코멘트 생성 (PR 승인/거절)")
@PostMapping("/{pullRequestId}/comments")
public ApiResponse<Boolean> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PullRequestComment> comments;

Expand Down
24 changes: 12 additions & 12 deletions src/main/resources/db/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit b8ea4c3

Please sign in to comment.