Skip to content

Commit

Permalink
ADD :: query git contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
gimhanul committed Dec 2, 2022
1 parent ec2bd51 commit 70e5ca4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation "com.graphql-java-kickstart:graphql-webclient-spring-boot-starter:1.0.0"
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.tomatoes.giti.domain.giti.presentation.dto.GitiResponse;
import com.tomatoes.giti.domain.giti.service.QueryGitiOrCreateService;
import com.tomatoes.giti.global.feign.github.dto.GithubInformationResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,7 +16,7 @@ public class GitiController {
private final QueryGitiOrCreateService queryGitiOrCreateService;

@GetMapping
public GithubInformationResponse getGiti(@RequestParam(name = "github") String githubId) {
public GitiResponse getGiti(@RequestParam(name = "github") String githubId) {
return queryGitiOrCreateService.execute(githubId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.tomatoes.giti.domain.giti.domain.repository.GitiRepository;
import com.tomatoes.giti.domain.giti.presentation.dto.GitiResponse;
import com.tomatoes.giti.global.feign.github.GithubClient;
import com.tomatoes.giti.global.feign.github.GithubGraphqlClient;
import com.tomatoes.giti.global.feign.github.dto.GithubInformationResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,16 +20,20 @@ public class QueryGitiOrCreateService {

private final GitiRepository gitiRepository;
private final GithubClient githubClient;
private final GithubGraphqlClient githubGraphqlClient;

@Transactional
public GithubInformationResponse execute(String githubId) {
public GitiResponse execute(String githubId) {
Optional<Giti> giti = gitiRepository.findByGithubId(githubId);

if (giti.isEmpty()) {
return githubClient.getGithubInformation(githubId);
int contributions = githubGraphqlClient.getContributions(githubId);
GithubInformationResponse githubInformationResponse = githubClient.getGithubInformation(githubId);
Giti savedGiti = gitiRepository.save(Giti.builder().build());
return GitiResponse.of(savedGiti);
}

return null;
return GitiResponse.of(giti.get());
}

private Giti createGiti() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface GithubClient {
@GetMapping("/users/{username}")
// GithubInformationResponse getGithubInformation(@RequestHeader(value = "Authorization") String token, @PathVariable String username);
GithubInformationResponse getGithubInformation(@PathVariable String username);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.tomatoes.giti.global.feign.github;

import com.tomatoes.giti.global.feign.github.dto.GithubContributionResponse;
import com.tomatoes.giti.global.properties.GithubProperties;
import graphql.kickstart.spring.webclient.boot.GraphQLRequest;
import graphql.kickstart.spring.webclient.boot.GraphQLResponse;
import graphql.kickstart.spring.webclient.boot.GraphQLWebClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class GithubGraphqlClient {

private final GraphQLWebClient graphQLWebClient;
private final GithubProperties githubProperties;

public int getContributions(String username) {
GraphQLRequest request = GraphQLRequest.builder()
.header("Authorization", "bearer " + githubProperties.getToken())
.query("query { \n" +
" user(login: \"" + username + "\") {\n" +
" contributionsCollection {\n" +
" contributionCalendar {\n" +
" totalContributions\n" +
" weeks {\n" +
" contributionDays {\n" +
" weekday\n" +
" date \n" +
" contributionCount \n" +
" color\n" +
" }\n" +
" }\n" +
" months {\n" +
" name\n" +
" year\n" +
" firstDay \n" +
" totalWeeks \n" +
" \n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" \n" +
"}")
.build();
GraphQLResponse response = graphQLWebClient.post(request).block();
return response.get("user", GithubContributionResponse.class)
.getContributionsCollection()
.getContributionCalendar()
.getTotalContributions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tomatoes.giti.global.feign.github.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class GithubContributionResponse {
private ContributionCollection contributionsCollection;

@Getter
@NoArgsConstructor
public static class ContributionCollection {
private ContributionCalendar contributionCalendar;
}

@Getter
@NoArgsConstructor
public static class ContributionCalendar {
private int totalContributions;
}
}
8 changes: 6 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ spring:
hibernate:
format_sql: true

github:
token: ${GITHUB_TOKEN}
github:
token: ${GITHUB_TOKEN}

graphql:
client:
url: https://api.github.com/graphql

debug: true

0 comments on commit 70e5ca4

Please sign in to comment.