Java/STUDY HALLE

[Java] GitHub Library 사용법

무토(MUTO) 2020. 12. 6. 06:36

1. 자바에서 깃허브 의존성을 추가하는 방법

MVN Repository 에서 검색한 Github api 의존성
다음 의존성을 gradle에 추가한다.

2. API를 사용하는 방법

https://github-api.kohsuke.org

위의 사이트에 접속해서 레퍼런스 문서를 읽어보면서 어떤 기능이 있는지 살펴보아야 한다. 많은 사람들이 사용하는 api가 아니기 때문에 구글링을 해도 결과물이 없다.

3. 요구사항 실현하기

Main.java

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    static final int NUM_OF_ISSUES = 18;
    static final String oauthToken = "myToken";
    static final String repoURL = "whiteship/live-study";

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        Repository repository = new Repository(oauthToken, repoURL);

        List<String> allParticipants = getAllParticipants(repository);

        List<UserBoard> dashBoard = makeEmptyDashBoard(allParticipants);

        repository.checkUserParticipation(dashBoard);

        createBoard(bw, dashBoard);
        bw.flush();
        bw.close();
    }

    private static List<String> getAllParticipants(Repository repository) throws IOException {
        return repository.getAllParticipantsName()
            .stream()
            .sorted()
            .collect(Collectors.toList());
    }


    private static List<UserBoard> makeEmptyDashBoard(List<String> allParticipants) {
        return allParticipants.stream()
            .map(v -> new UserBoard(v, new boolean[NUM_OF_ISSUES]))
            .collect(Collectors.toList());
    }

    private static void createBoard(BufferedWriter bw, List<UserBoard> dashBoard)
        throws IOException {
        bw.write("| 참여자 |");
        for (int i = 1; i <= NUM_OF_ISSUES; i++) {
            bw.write(" " + i + "주차 |");
        }
        bw.write(" 참석율 |\n");

        bw.write("| --- |");
        for (int i = 1; i <= NUM_OF_ISSUES; i++) {
            bw.write(" --- |");
        }
        bw.write(" --- |\n");

        for (UserBoard userBoard : dashBoard) {
            bw.write("| " + userBoard.name + " |");
            boolean[] check = userBoard.participationCheck;
            for (boolean b : check) {
                if (b) {
                    bw.write(":white_check_mark:|");
                } else {
                    bw.write("|");
                }
            }
            bw.write(" " + userBoard.getRate() + " |\n");
        }
    }
}

UserBoard.java

public class UserBoard {

    String name;
    boolean[] participationCheck;

    public UserBoard(String name, boolean[] participationCheck) {
        this.name = name;
        this.participationCheck = participationCheck;
    }

    public String getRate() {
        return
            String.format("%.2f", (float) getTotalParticipation() / participationCheck.length * 100)
                + "%";
    }

    private int getTotalParticipation() {
        int count = 0;
        for (boolean b : participationCheck) {
            if (b) {
                count++;
            }
        }
        return count;
    }
}

Repository.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;

public class Repository {

    final int NUM_OF_ISSUES = 18;
    GitHub gitHub;
    GHRepository repository;

    public Repository(String token, String repoURL) throws IOException {
        gitHub = generateGitHubConnection(token);
        repository = gitHub.getRepository(repoURL);
    }

    private GitHub generateGitHubConnection(String oAuthAccessToken) throws IOException {
        return new GitHubBuilder()
            .withOAuthToken(oAuthAccessToken)
            .build();
    }

    public List<String> getAllParticipantsName() throws IOException {
        List<String> participants = new ArrayList<>();
        for (int i = 1; i <= NUM_OF_ISSUES; i++) {
            List<GHIssueComment> comments = repository.getIssue(i).getComments();
            for (GHIssueComment comment : comments) {
                String name = comment.getUser().getLogin();
                if (!participants.contains(name)) {
                    participants.add(name);
                }
            }
        }
        return participants;
    }

    public void checkUserParticipation(List<UserBoard> dashBoard) throws IOException {
        for (int i = 1; i <= NUM_OF_ISSUES; i++) {
            List<GHIssueComment> comments = repository.getIssue(i).getComments();
            if (comments.size() == 0) {
                return;
            }
            for (GHIssueComment comment : comments) {
                String name = comment.getUser().getLogin();
                for (UserBoard userBoard : dashBoard) {
                    if (userBoard.name.equals(name)) {
                        userBoard.participationCheck[i - 1] = true;
                    }
                }
            }
        }
    }
}

https://github.com/MoonHKLee/StudyHalle

해당 링크를 타고 들어가면 README.md 에 그려진 표를 볼 수 있다.
선장님이 숙제검사 표를 돌리지 않으셔도 따로 개인적으로 돌릴 수 있는 기반을 마련한 것이 마음에 든다.

'Java > STUDY HALLE' 카테고리의 다른 글

[Java] 클래스  (0) 2020.12.16
[Java] 선형 자료구조  (0) 2020.12.07
[Java] JUnit5  (0) 2020.12.05
[Java] 제어문  (0) 2020.12.05
[Java] 연산자  (1) 2020.11.27