Skip to content

Commit 77c7c53

Browse files
authored
Merge pull request #42 from CommitField/feat/#41
Feat: 특정 사용자의 전체 커밋 수 불러오기
2 parents 35ab422 + 646a1d7 commit 77c7c53

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ dependencies {
6262
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6")
6363
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6")
6464

65+
// WebClient
66+
implementation ("org.springframework.boot:spring-boot-starter-webflux")
6567
}
6668

6769
tasks.withType<Test> {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmf.commitField.domain.totalCommit.config;
2+
3+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
4+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
5+
6+
// CORS 오류 설정 용
7+
public class WebConfig implements WebMvcConfigurer {
8+
@Override
9+
public void addCorsMappings(CorsRegistry registry) {
10+
registry.addMapping("/**")
11+
.allowedOrigins("http://localhost:8090") // 프론트엔드 도메인
12+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
13+
.allowedHeaders("*")
14+
.allowCredentials(true);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmf.commitField.domain.totalCommit.controller;
2+
3+
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
4+
import cmf.commitField.domain.totalCommit.service.TotalCommitService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequiredArgsConstructor
12+
public class TotalCommitController {
13+
private final TotalCommitService totalCommitService;
14+
15+
@GetMapping("/api/commits/{username}")
16+
public TotalCommitResponseDto getTotalCommits(@PathVariable String username) {
17+
return totalCommitService.getTotalCommitCount(username);
18+
}
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmf.commitField.domain.totalCommit.dto;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
6+
/*
7+
응답구조 토대로 구현
8+
{
9+
"data": {
10+
"user": {
11+
"contributionsCollection": {
12+
"totalCommitContributions": 116,
13+
"restrictedContributionsCount": 0
14+
}
15+
}
16+
}
17+
}
18+
*/
19+
@Getter
20+
@NoArgsConstructor
21+
public class TotalCommitGraphQLResponse {
22+
private Data data;
23+
24+
@Getter
25+
@NoArgsConstructor
26+
public static class Data {
27+
CommitUser user;
28+
}
29+
30+
31+
@Getter
32+
@NoArgsConstructor
33+
public static class CommitUser {
34+
private ContributionsCollection contributionsCollection;
35+
}
36+
37+
@Getter
38+
@NoArgsConstructor
39+
public static class ContributionsCollection {
40+
private long totalCommitContributions;
41+
private long restrictedContributionsCount;
42+
}
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmf.commitField.domain.totalCommit.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
@Getter
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class TotalCommitResponseDto {
11+
private long totalCommitContributions;
12+
private long restrictedContributionsCount;
13+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmf.commitField.domain.totalCommit.service;
2+
3+
import cmf.commitField.domain.totalCommit.dto.TotalCommitGraphQLResponse;
4+
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.beans.factory.annotation.Value;
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.util.Map;
13+
14+
15+
@Service
16+
@RequiredArgsConstructor
17+
public class TotalCommitService {
18+
private static final String BASE_URL = "https://api.github.com/graphql";
19+
20+
@Value("${github.token}")
21+
private String PAT;
22+
23+
24+
private final WebClient webClient = WebClient.builder()
25+
.baseUrl(BASE_URL)
26+
.defaultHeader(HttpHeaders.CONTENT_TYPE , MediaType.APPLICATION_JSON_VALUE)
27+
.build();
28+
29+
public TotalCommitResponseDto getTotalCommitCount(String username) {
30+
// String query = String.format("""
31+
// {"query": "query { user(login: \\\"%s\\\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }"}
32+
// """, username);
33+
// GraphQL 쿼리를 Map으로 구성
34+
Map<String, String> requestBody = Map.of(
35+
"query", String.format(
36+
"query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
37+
username
38+
)
39+
);
40+
41+
TotalCommitGraphQLResponse response = webClient.post()
42+
.header("Authorization", "bearer " + PAT)
43+
.bodyValue(requestBody)
44+
.retrieve()
45+
.bodyToMono(TotalCommitGraphQLResponse.class)
46+
.block();
47+
48+
TotalCommitGraphQLResponse.ContributionsCollection contributions = response.getData().getUser().getContributionsCollection();
49+
50+
return new TotalCommitResponseDto(
51+
contributions.getTotalCommitContributions(),
52+
contributions.getRestrictedContributionsCount()
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)