[과제]인텔리제이로 Live-study 만들기

정석용·2023년 4월 7일
0

assignment

목록 보기
2/2

과제

  • 과제내용
    - 깃헙 이슈 1번부터 18번까지 댓글을 순회하며 댓글을 남긴 사용자를 체크 할 것.
    참여율을 계산하세요.
    - 총 18회에 중에 몇 %를 참여했는지 소숫점 두자리가지 보여줄 것.
    - Github 자바 라이브러리를 사용하면 편리합니다.
    - 깃헙 API를 익명으로 호출하는데 제한이 있기 때문에 본인의 깃헙 프로젝트에 이슈를 만들고 테스트를 하시면 더 자주 테스트할 수 있습니다.

Git

Git 기반으로 하는 웹 기반의 버전 관리 플랫폼

API(Application Programming Interface)

응용 프로그램에서 사용할 수 있도록, 운영 체제나 프로그래밍 언어가 제공하는 기능을 제어할 수 있게 만든 인터페이스이다. 자바를 사용하기 쉽게 구현할 수 있도록 한 클래스 라이브러리의 집합이다.

인텔리제이

인텔리제이는 JetBrains사에서 개발한 자바 및 다른 JVM 기반 언어를 위한 통합 개발 환경(IDE) 중 하나

인텔리제이 환경설정
Git 과 연결을 위해 Github 자바 라이브러리에 접속하여 Github API For java 라이브러리를 복사 한다.
본 게시물에서는 1.313 버전을 사용한다.

maven

Maven은 자바 프로젝트의 빌드를 자동화 해주는 빌드 도구
라이브러리 관리, 의존성 관리, 빌드, 테스트, 배포 등 프로젝트 라이프 사이클의 모든 단계를 관리한다,

Maven은 XML 기반의 프로젝트 객체 모델(POM) 파일을 사용하여 프로젝트를 정의하며, 이를 기반으로 라이브러리 의존성 관리 및 빌드를 수행합니다. Maven은 중앙 저장소(Central Repository)에서 다양한 라이브러리를 제공하고, 이를 쉽게 가져와 사용할 수 있도록 합니다.

의존성 추가
GitHub API의 개체 지향 표현인 Java용 GitHub API 인 org.kohsuke를 사용하기 위해 인텔리제이 안에 복사한 의존성을 추가하여 선언해준다.

<!-- https://mvnrepository.com/artifact/org.kohsuke/github-api -->
<dependency>
    <groupId>org.kohsuke</groupId>
    <artifactId>github-api</artifactId>
    <version>1.313</version>
</dependency>

의존성 추가화면

의존성은 dependencies /dependencies 안에 선언되어 있어야한다.

import org.kohsuke.github.*;
import java.io.IOException;
import java.util.*;

public class study {

    private final String token = "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";
    private GitHub github;

    public static void main(String[] args){

        study app = new study();

        try {
            app.run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void run() throws IOException {

        // 연결
        connectGitApi();

        GHRepository ghRepository = github.getRepository("whiteship/live-study");
        // 참여자 이름, 참여 횟수
        Map<String, Integer> participant = new HashMap<>();
        // 모든 이슈를 가져온다.
        List<GHIssue> issues = ghRepository.getIssues(GHIssueState.ALL);

        //Find Issue
        for (GHIssue issue : issues) {
            // 각각의 Issue 에 존재하는 comment 들을 저장.
            List<GHIssueComment> commentList = issue.getComments();

            // 혹시 모를 유저 중복을 제거하기 위한 Set
            Set<String> nameList = new HashSet<>();

            addParticipantInSet(commentList, nameList);

            // 참여자 명단에서 비교한다.
            for (String s : nameList) {
                hasParticipantInSet(participant, s);
            }
        }
        printParticipantRate(participant);
    }

    private void hasParticipantInSet(Map<String, Integer> participant, String s) {
        if (!participant.containsKey(s)){
            participant.put(s, 1);
        } else {
            Integer integer = participant.get(s);
            participant.put(s, ++integer);
        }
    }

    private void addParticipantInSet(List<GHIssueComment> commentList, Set<String> name) throws IOException {
        for (GHIssueComment ghIssueComment : commentList) {
            name.add(ghIssueComment.getUser().getLogin());
        }
    }

    private void printParticipantRate(Map<String, Integer> participant) {
        participant.forEach((key, value)-> {
            double percent = (double) (value * 100) / 18;
            System.out.println(key+"  :  "+String.format("%.2f", percent)+"%");
        });
    }

    private void connectGitApi() throws IOException {
        github = new GitHubBuilder().withOAuthToken(token).build();
    }
}

Throws 함수
Throws 함수는 예외처리가 발생한 메소드를 호출한 메소드에게 예외를 처리한다. throws는 예외를 넘기고 try~catch는 예외를 처리한다는 것을 알수 있었다. 또 theows는 예외 발생 이후릐 코드는 실행하지 않고, try~catch는 예외 발생 이후의 코드를 실행한다는 점이 있다. 2가지 방법으로 예외처리를 하는 이유는 개발자가 원하는 예외처리를 만들 수 있게 하기 위함이다.

토큰 발급
GitApi 연결을 위해 토큰 발급이 필요하다. 발급 방법에는 깃허브 자신의 프로필에서 - setting - developer settings - personal access tokens 에 들어가 토큰을 발급 받으면 된다.

실행 결과

라이브 스터디에 댓글을 남긴 사용자 별 출석률이 표시 되었다.

래퍼런스

https://go-coding.tistory.com/10

profile
오늘도 성장중

0개의 댓글