api를 처음 사용하다 보니 사전시직을 갖추는데 너무 오랜 시간이 들었다. 처음 api를 봤을때는 모든 기능을 알아야 사용할수 있을 것 같아서 많은 블로그와 후기들을 찾아보는데 많은 시간을 썼다. 그렇지만 답을 찾지는 못했는데. api라 해도 사람마다 코드를 짜는 방식이 다르듯 방대한 api를 사람마다 다르게 사용한다는 것을 깨달을수 있었다
다시 처음으로 돌아가 다 지우고 내가 만들고자 하는 바를 하나씩 하다보니 감이 잡히고 틀이 잡히는? 느낌이 들었다. 모든 것을 알고 시작하려 하기 보다는 만들면서 이해하는게 한결 쉽게 조작할 수 있었다
GitHub API for Java –
What is this? This library defines an object oriented representation of the GitHub API. By "object oriented" we mean there are classes that correspond to the domain model of GitHub (such as GHUser and GHRepository), operations that act on them as defined a
github-api.kohsuke.org
순서
깃헙 api를 가보면 공식문서를 볼수가 있다. 처음 api를 사용하게 된다면 약간 어지러문데 예시를 따라 천천히 구현하면 되었다
github api를 사용하기 위해서 내 github과 연결을 시켜야하는데 연결방법이 여러개가 있다
1. 사용자 이름 비밀번호
2. 개인 액세스 토큰
3. jwt 토큰
4. 환경 변수를 통한 연결
나는 개인액세스 토큰을 통해서 연결을 했다
그 후 Maven에 의존성을 추가함으로서 github 라이브러리를 사용할 수 있도록 만든다
Maven은 자바 프로젝트 관리 도구중 하나로 Maven과 Tomcat이 있다
프로젝트의 빌드, 테스트, 배포를 관리하기 위한 도구입니다. 프로젝트의 의존성 관리와 라이브러리 다운로드 등을 편리하게 처리할 수 있게 해준다.
더 자세히 말하자면 개발자들이 공통된 구조와 규칙에 따라 작업할 수 있도록 해주며, 의존성관리를 통일되게 관리한다
pom.xml 파일을 통해 설정을 관리한다
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>TIL</artifactId>
<version>1.0-SNAPSHOT</version>
------------------------------------------------------------------------------------
<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.315</version>
</dependency>
</dependencies>
------------------------------------------------------------------------------------
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
사용할 버전을 찾아서 의존성부여하는 코드를 pom.xml에 넣게 되면 github api를 사용할 수가 있다!
그후 github api를 사용해 내 깃헙 가져오고 폴더 가져오며 입맛대로 만들면된다
public class live_study_Dashboard {
private static final String token = "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";
static int total_issue = 18;
public static void main(String[] args) throws IOException {
GitHub github = new GitHubBuilder().withJwtToken(token).build();
GHRepository repository = github.getRepository("whiteship/live-study");
// github (이름/프로젝트명)
List<IssueWithCreationDate> closeIssues = new ArrayList<>();
// 프로젝트에서 있어지는 이슈들
for (GHIssue issue : repository.getIssues(GHIssueState.CLOSED)) {
String createdAt = issue.getCreatedAt().toString();
closeIssues.add(new IssueWithCreationDate(issue, createdAt));
}
closeIssues.sort(Comparator.comparing(IssueWithCreationDate::getCreatedAt));
for (IssueWithCreationDate issues : closeIssues) {
GHIssue issue = issues.getIssue();
System.out.println(issue.getTitle());
System.out.println("-------------------------------------------------------------------------------------");
List<GHIssueComment> comments = issue.getComments();
int cnt = 0;
for (GHIssueComment comment : comments) {
String username = comment.getUser().getName();
if (username == null)
continue;
System.out.println("숙제 한 사람 : " + username);
cnt++;
if (cnt == 10)
break;
}
System.out.println();
}
}
// 이슈 날짜순 저장 위한 데이터 형식
static class IssueWithCreationDate {
private GHIssue issue;
private String createdAt;
public IssueWithCreationDate(GHIssue issue, String createdAt) {
this.issue = issue;
this.createdAt = createdAt;
}
public GHIssue getIssue() {
return issue;
}
public String getCreatedAt() {
return createdAt;
}
}
}
결과
'TIL > 백기선 온라인 자바 스터디' 카테고리의 다른 글
6주차. 상속 (1) | 2023.10.22 |
---|---|
5주차. 클래스 (1) | 2023.10.15 |
4주차 -1. Junit5, 제어문 학습, Linkedlist, Stack, Queue 구현 (0) | 2023.08.26 |
3주차. 자바가 제공하는 다양한 연산자 학습하기 (1) | 2023.06.02 |
2주차. 자바 데이터 타입, 변수, 배열 이해하기 (1) | 2023.05.16 |