@@ -32,28 +32,87 @@ public class TotalCommitService {
32
32
.defaultHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
33
33
.build ();
34
34
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
+ // 연속 커밋 수 정보도 같이 반환
36
71
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
42
79
);
43
80
81
+ Map <String , String > requestBody = Map .of ("query" , query );
82
+
44
83
TotalCommitGraphQLResponse response = webClient .post ()
45
84
.header ("Authorization" , "bearer " + PAT )
46
85
.bodyValue (requestBody )
47
86
.retrieve ()
48
87
.bodyToMono (TotalCommitGraphQLResponse .class )
49
88
.block ();
50
89
90
+ if (response == null || response .getData () == null || response .getData ().getUser () == null ) {
91
+ throw new RuntimeException ("Failed to fetch GitHub data" );
92
+ }
93
+
51
94
TotalCommitGraphQLResponse .ContributionsCollection contributions =
52
95
response .getData ().getUser ().getContributionsCollection ();
53
96
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
+
54
111
return new TotalCommitResponseDto (
55
112
contributions .getTotalCommitContributions (),
56
- contributions .getRestrictedContributionsCount ()
113
+ contributions .getRestrictedContributionsCount (),
114
+ currentStreak ,
115
+ maxStreak
57
116
);
58
117
}
59
118
0 commit comments