Skip to content

Commit fea1947

Browse files
committed
Feat: 특정 기간 레포지토리 별 커밋 수 불러오기
1 parent 646a1d7 commit fea1947

File tree

8 files changed

+141
-9
lines changed

8 files changed

+141
-9
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmf.commitField.domain.commit.sinceCommit.controller;
2+
3+
import cmf.commitField.domain.commit.sinceCommit.dto.SinceCommitResponseDto;
4+
import cmf.commitField.domain.commit.sinceCommit.service.SinceCommitService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.format.annotation.DateTimeFormat;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.time.LocalDateTime;
13+
import java.util.List;
14+
15+
@RestController
16+
@RequiredArgsConstructor
17+
public class SinceCommitController {
18+
private final SinceCommitService sinceCommitService;
19+
20+
@GetMapping("/api/github/commits-since")
21+
public ResponseEntity<List<SinceCommitResponseDto>> getCommits(
22+
@RequestParam String owner,
23+
@RequestParam String repo,
24+
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime since,
25+
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime until
26+
) {
27+
List<SinceCommitResponseDto> sinceCommits = sinceCommitService.getSinceCommits(owner, repo, since, until);
28+
29+
return ResponseEntity.ok(sinceCommits);
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmf.commitField.domain.commit.sinceCommit.dto;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
6+
@Getter
7+
@NoArgsConstructor
8+
public class SinceCommitResponseDto {
9+
private String sha;
10+
private Commit commit;
11+
private Author author;
12+
private String html_url;
13+
14+
15+
@Getter
16+
@NoArgsConstructor
17+
public static class Commit {
18+
private CommitAuthor author; // 원래 코드 작성자
19+
private String message;
20+
21+
@Getter
22+
@NoArgsConstructor
23+
public static class CommitAuthor {
24+
private String name; // 커밋한 사람의 깃허브 이름
25+
private String email;
26+
private String date;
27+
}
28+
}
29+
30+
@Getter
31+
@NoArgsConstructor
32+
public static class Author {
33+
private String login; // 커밋한 사람의 깃허브 id
34+
private String avatar_url;
35+
private String html_url; // 사용자의 깃허브 주소
36+
}
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmf.commitField.domain.commit.sinceCommit.service;
2+
3+
import cmf.commitField.domain.commit.sinceCommit.dto.SinceCommitResponseDto;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.core.ParameterizedTypeReference;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.web.reactive.function.client.WebClient;
11+
12+
import java.time.LocalDateTime;
13+
import java.time.format.DateTimeFormatter;
14+
import java.util.List;
15+
16+
@Slf4j
17+
@Service
18+
public class SinceCommitService {
19+
private static final String BASE_URL = "https://api.github.com";
20+
// ?since=2024-01-01T00:00:00Z&until=2025-02-1T23:59:59Z
21+
22+
@Value("${github.token}")
23+
private String PAT;
24+
25+
private final WebClient webClient = WebClient.builder()
26+
.baseUrl(BASE_URL)
27+
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
28+
.build();
29+
30+
public List<SinceCommitResponseDto> getSinceCommits(String owner, String repo, LocalDateTime since, LocalDateTime until) {
31+
try {
32+
return webClient.get()
33+
.uri(uriBuilder -> uriBuilder
34+
.path("/repos/{owner}/{repo}/commits")
35+
.queryParam("since", since.format(DateTimeFormatter.ISO_DATE_TIME))
36+
.queryParam("until", until.format(DateTimeFormatter.ISO_DATE_TIME))
37+
.build(owner, repo))
38+
.header("Authorization", "bearer " + PAT)
39+
.retrieve()
40+
.bodyToMono(new ParameterizedTypeReference<List<SinceCommitResponseDto>>() {})
41+
.block();
42+
} catch (Exception e) {
43+
log.error("GitHub API 호출 중 오류 발생: {}", e.getMessage());
44+
throw new RuntimeException("GitHub API 호출 중 오류가 발생했습니다.", e);
45+
}
46+
}
47+
}
48+
/*
49+
ParameterizedTypeReference를 사용하는 주요 이유
50+
1. 제네릭 타입 정보 보존
51+
2. 복잡한 타입 구조 처리 가능
52+
3. 타입 안전성 보장
53+
4. 런타임에 올바른 타입으로 역직렬화 가능
54+
55+
ParameterizedTypeReference를 사용하지 않으면
56+
// 잘못된 예시
57+
List<GitHubCommitResponse> commits = webClient.get()
58+
.retrieve()
59+
.bodyToMono(List.class)
60+
.block();
61+
62+
// 런타임 에러 발생 가능
63+
GitHubCommitResponse commit = commits.get(0); // ClassCastException 발생 위험
64+
*/

src/main/java/cmf/commitField/domain/totalCommit/config/WebConfig.java renamed to src/main/java/cmf/commitField/domain/commit/totalCommit/config/WebConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmf.commitField.domain.totalCommit.config;
1+
package cmf.commitField.domain.commit.totalCommit.config;
22

33
import org.springframework.web.servlet.config.annotation.CorsRegistry;
44
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

src/main/java/cmf/commitField/domain/totalCommit/controller/TotalCommitController.java renamed to src/main/java/cmf/commitField/domain/commit/totalCommit/controller/TotalCommitController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package cmf.commitField.domain.totalCommit.controller;
1+
package cmf.commitField.domain.commit.totalCommit.controller;
22

3-
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
4-
import cmf.commitField.domain.totalCommit.service.TotalCommitService;
3+
import cmf.commitField.domain.commit.totalCommit.dto.TotalCommitResponseDto;
4+
import cmf.commitField.domain.commit.totalCommit.service.TotalCommitService;
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.web.bind.annotation.GetMapping;
77
import org.springframework.web.bind.annotation.PathVariable;

src/main/java/cmf/commitField/domain/totalCommit/dto/TotalCommitGraphQLResponse.java renamed to src/main/java/cmf/commitField/domain/commit/totalCommit/dto/TotalCommitGraphQLResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmf.commitField.domain.totalCommit.dto;
1+
package cmf.commitField.domain.commit.totalCommit.dto;
22

33
import lombok.Getter;
44
import lombok.NoArgsConstructor;

src/main/java/cmf/commitField/domain/totalCommit/dto/TotalCommitResponseDto.java renamed to src/main/java/cmf/commitField/domain/commit/totalCommit/dto/TotalCommitResponseDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmf.commitField.domain.totalCommit.dto;
1+
package cmf.commitField.domain.commit.totalCommit.dto;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;

src/main/java/cmf/commitField/domain/totalCommit/service/TotalCommitService.java renamed to src/main/java/cmf/commitField/domain/commit/totalCommit/service/TotalCommitService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package cmf.commitField.domain.totalCommit.service;
1+
package cmf.commitField.domain.commit.totalCommit.service;
22

3-
import cmf.commitField.domain.totalCommit.dto.TotalCommitGraphQLResponse;
4-
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
3+
import cmf.commitField.domain.commit.totalCommit.dto.TotalCommitGraphQLResponse;
4+
import cmf.commitField.domain.commit.totalCommit.dto.TotalCommitResponseDto;
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.http.HttpHeaders;

0 commit comments

Comments
 (0)