Skip to content

Commit 68b6cd4

Browse files
committed
feat: 시간 단위로 커밋 업데이트 체크하도록 추가
1 parent e857621 commit 68b6cd4

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

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

+44
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,48 @@ private StreakResult calculateStreaks(List<LocalDate> commitDates) {
213213

214214
return new StreakResult(currentStreak, maxStreak);
215215
}
216+
217+
// 시간별 커밋 분석
218+
public TotalCommitResponseDto getUpdateCommits(String username, LocalDateTime since, LocalDateTime until) {
219+
String query = String.format("""
220+
query {
221+
user(login: "%s") {
222+
contributionsCollection(from: "%s", to: "%s") {
223+
commitContributionsByRepository {
224+
contributions(first: 100) {
225+
nodes {
226+
occurredAt # ✅ 시간 정보 포함
227+
}
228+
}
229+
}
230+
}
231+
}
232+
}""", username, since.format(DateTimeFormatter.ISO_DATE_TIME), until.format(DateTimeFormatter.ISO_DATE_TIME));
233+
234+
Map<String, String> requestBody = Map.of("query", query);
235+
236+
TotalCommitGraphQLResponse response = webClient.post()
237+
.header("Authorization", "bearer " + PAT)
238+
.bodyValue(requestBody)
239+
.retrieve()
240+
.bodyToMono(TotalCommitGraphQLResponse.class)
241+
.block();
242+
243+
if (response == null || response.getData() == null || response.getData().getUser() == null) {
244+
throw new RuntimeException("Failed to fetch GitHub data");
245+
}
246+
247+
TotalCommitGraphQLResponse.ContributionsCollection contributions =
248+
response.getData().getUser().getContributionsCollection();
249+
250+
List<LocalDate> commitDates = extractCommitDates(contributions.getContributionCalendar());
251+
StreakResult streaks = calculateStreaks(commitDates);
252+
253+
return new TotalCommitResponseDto(
254+
contributions.getTotalCommitContributions(),
255+
contributions.getRestrictedContributionsCount(),
256+
streaks.currentStreak,
257+
streaks.maxStreak
258+
);
259+
}
216260
}

src/main/java/cmf/commitField/domain/user/entity/User.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class User extends BaseEntity {
3030
private String avatarUrl; //아바타
3131
private Boolean status; //로그인 true, 로그아웃 false
3232
private LocalDateTime lastCommitted; // 마지막 커밋 시간
33+
private long commitCount;
3334

3435
@Enumerated(EnumType.STRING) // DB에 저장될 때 String 형태로 저장됨
3536
private Role role;
@@ -64,6 +65,7 @@ public User(String username, String email, String nickname, String avatarUrl, Bo
6465
this.userChatRooms = ucr;
6566
this.chatMsgs = cmsg;
6667
this.lastCommitted = LocalDateTime.now();
68+
this.commitCount = 0;
6769
}
6870

6971
public void addPets(Pet pet){

src/main/java/cmf/commitField/domain/user/service/CustomOAuth2UserService.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmf.commitField.domain.user.service;
22

33
import cmf.commitField.domain.commit.sinceCommit.service.CommitCacheService;
4+
import cmf.commitField.domain.commit.totalCommit.service.TotalCommitService;
45
import cmf.commitField.domain.pet.entity.Pet;
56
import cmf.commitField.domain.pet.repository.PetRepository;
67
import cmf.commitField.domain.user.entity.CustomOAuth2User;
@@ -25,6 +26,7 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService {
2526
private final PetRepository petRepository;
2627
private final HttpServletRequest request; // HttpServletRequest를 주입 받음.
2728
private final CommitCacheService commitCacheService;
29+
private final TotalCommitService totalCommitService;
2830

2931
@Override
3032
public OAuth2User loadUser(OAuth2UserRequest userRequest) {
@@ -62,6 +64,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {
6264
petRepository.save(pet);
6365

6466
user.addPets(pet);
67+
user.setCommitCount(totalCommitService.getTotalCommitCount(user.getUsername()).getTotalCommitContributions());
6568

6669
// 회원가입한 유저는 커밋 기록에 상관없이 Redis에 입력해둔다.
6770
commitCacheService.updateCachedCommitCount(user.getUsername(),0);

src/main/resources/application-dev.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spring:
77
jpa:
88
open-in-view: false
99
hibernate:
10-
ddl-auto: update
10+
ddl-auto: create
1111
autoconfigure: # 로컬에서 실행할 때는 Redis와 Session 설정을 제외
1212
exclude:
1313
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

src/main/resources/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ spring:
1818
jpa:
1919
open-in-view: false
2020
hibernate:
21-
ddl-auto: update
21+
ddl-auto: create
2222
properties:
2323
hibernate:
2424
default_batch_fetch_size: 100

0 commit comments

Comments
 (0)