Skip to content

Commit

Permalink
feat: 가장 최신 깃허브 과제 제출이력 조회 기능 구현 (#626)
Browse files Browse the repository at this point in the history
* docs: 투두 추가

* chore: 로컬 환경에서 깃허브 라이브러리 로깅 설정

* refactor: 깃허브 클라이언트 패키지 변경

* feat: 깃허브 과제 경로 상수 추가

* feat: 깃허브 과제 없는 경우 예외 에러코드 추가

* feat: 가장 최신 제출이력 조회 로직 구현

* docs: 주석 추가

* feat: 응답에 커밋 해시 추가
  • Loading branch information
uwoobeat authored Aug 15, 2024
1 parent 8ce2d09 commit da08b2b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.gdschongik.gdsc.domain.study.dto.response.AssignmentHistoryResponse;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.util.MemberUtil;
import com.gdschongik.gdsc.infra.client.github.GithubClient;
import com.gdschongik.gdsc.infra.github.client.GithubClient;
import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum AssignmentSubmissionStatus {
PENDING("제출 전"),
FAILURE("제출 실패"),
SUCCESS("제출 성공"),
CANCELLED("과제 휴강");
CANCELLED("과제 휴강"); // TODO: 제거 및 DB에서 삭제

private final String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class GithubConstant {

public static final String GITHUB_DOMAIN = "github.com/";
public static final String GITHUB_ASSIGNMENT_PATH = "week%d/WIL.md";

private GithubConstant() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public enum ErrorCode {
ASSIGNMENT_DEADLINE_INVALID(HttpStatus.CONFLICT, "과제 마감 기한이 현재보다 빠릅니다."),

// Github
GITHUB_REPOSITORY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 레포지토리입니다.");
GITHUB_REPOSITORY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 레포지토리입니다."),
GITHUB_ASSIGNMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 과제 파일입니다."),
;

private final HttpStatus status;
private final String message;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.gdschongik.gdsc.infra.github.client;

import static com.gdschongik.gdsc.global.common.constant.GithubConstant.*;

import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import com.gdschongik.gdsc.infra.github.dto.response.GithubAssignmentSubmissionResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import lombok.RequiredArgsConstructor;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class GithubClient {

private final GitHub github;

public GHRepository getRepository(String ownerRepo) {
try {
return github.getRepository(ownerRepo);
} catch (IOException e) {
throw new CustomException(ErrorCode.GITHUB_REPOSITORY_NOT_FOUND);
}
}

public GithubAssignmentSubmissionResponse getLatestAssignmentSubmission(String repo, int week) {
try {
GHRepository ghRepository = getRepository(repo);
String assignmentPath = GITHUB_ASSIGNMENT_PATH.formatted(week);

// GHContent#getSize() 의 경우 한글 문자열을 byte 단위로 계산하기 때문에, 직접 content를 읽어서 길이를 계산
GHContent ghContent = ghRepository.getFileContent(assignmentPath);
String content = new String(ghContent.read().readAllBytes());

GHCommit ghLatestCommit = ghRepository
.queryCommits()
.path(assignmentPath)
.list()
.toList()
.get(0);

LocalDateTime committedAt = ghLatestCommit
.getCommitDate()
.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();

return new GithubAssignmentSubmissionResponse(ghLatestCommit.getSHA1(), content.length(), committedAt);
} catch (IOException e) {
throw new CustomException(ErrorCode.GITHUB_ASSIGNMENT_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gdschongik.gdsc.infra.github.dto.response;

import java.time.LocalDateTime;

public record GithubAssignmentSubmissionResponse(String commitHash, Integer size, LocalDateTime committedAt) {}
1 change: 1 addition & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ logging:
level:
org.springframework.orm.jpa: DEBUG
org.springframework.transaction: DEBUG
org.kohsuke.github: debug

0 comments on commit da08b2b

Please sign in to comment.