@@ -32,28 +32,87 @@ public class TotalCommitService {
3232 .defaultHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
3333 .build ();
3434
35- // 기존 메서드
35+ // // 기존 메서드
36+ // public TotalCommitResponseDto getTotalCommitCount(String username) {
37+ // Map<String, String> requestBody = Map.of(
38+ // "query", String.format(
39+ // "query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
40+ // username
41+ // )
42+ // );
43+ //
44+ // TotalCommitGraphQLResponse response = webClient.post()
45+ // .header("Authorization", "bearer " + PAT)
46+ // .bodyValue(requestBody)
47+ // .retrieve()
48+ // .bodyToMono(TotalCommitGraphQLResponse.class)
49+ // .block();
50+ //
51+ // TotalCommitGraphQLResponse.ContributionsCollection contributions =
52+ // response.getData().getUser().getContributionsCollection();
53+ //
54+ // return new TotalCommitResponseDto(
55+ // contributions.getTotalCommitContributions(),
56+ // contributions.getRestrictedContributionsCount()
57+ // );
58+ // // streak 계산 부분 추가
59+ // List<LocalDate> commitDates = extractCommitDates(contributions.getContributionCalendar());
60+ // StreakResult streaks = calculateStreaks(commitDates);
61+ //
62+ // return new TotalCommitResponseDto(
63+ // contributions.getTotalCommitContributions(),
64+ // contributions.getRestrictedContributionsCount(),
65+ // streaks.currentStreak,
66+ // streaks.maxStreak
67+ // );
68+ // }
69+
70+ // 연속 커밋 수 정보도 같이 반환
3671 public TotalCommitResponseDto getTotalCommitCount (String username ) {
37- Map <String , String > requestBody = Map .of (
38- "query" , String .format (
39- "query { user(login: \" %s\" ) { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }" ,
40- username
41- )
72+ // GraphQL 쿼리를 수정하여 contributionCalendar도 함께 요청
73+ String query = String .format (
74+ "query { user(login: \" %s\" ) { contributionsCollection { " +
75+ "totalCommitContributions restrictedContributionsCount " +
76+ "contributionCalendar { totalContributions weeks { contributionDays { contributionCount date } } } " +
77+ "} } }" ,
78+ username
4279 );
4380
81+ Map <String , String > requestBody = Map .of ("query" , query );
82+
4483 TotalCommitGraphQLResponse response = webClient .post ()
4584 .header ("Authorization" , "bearer " + PAT )
4685 .bodyValue (requestBody )
4786 .retrieve ()
4887 .bodyToMono (TotalCommitGraphQLResponse .class )
4988 .block ();
5089
90+ if (response == null || response .getData () == null || response .getData ().getUser () == null ) {
91+ throw new RuntimeException ("Failed to fetch GitHub data" );
92+ }
93+
5194 TotalCommitGraphQLResponse .ContributionsCollection contributions =
5295 response .getData ().getUser ().getContributionsCollection ();
5396
97+ // streak 기본값 설정
98+ int currentStreak = 0 ;
99+ int maxStreak = 0 ;
100+
101+ // contributionCalendar가 존재하는 경우에만 streak 계산
102+ if (contributions .getContributionCalendar () != null ) {
103+ List <LocalDate > commitDates = extractCommitDates (contributions .getContributionCalendar ());
104+ if (!commitDates .isEmpty ()) {
105+ StreakResult streaks = calculateStreaks (commitDates );
106+ currentStreak = streaks .currentStreak ;
107+ maxStreak = streaks .maxStreak ;
108+ }
109+ }
110+
54111 return new TotalCommitResponseDto (
55112 contributions .getTotalCommitContributions (),
56- contributions .getRestrictedContributionsCount ()
113+ contributions .getRestrictedContributionsCount (),
114+ currentStreak ,
115+ maxStreak
57116 );
58117 }
59118
0 commit comments