Skip to content

Instantly share code, notes, and snippets.

@gowoonsori
Created January 26, 2021 07:10
Show Gist options
  • Save gowoonsori/da030f5cf396048e96344b5b4a3ec4b1 to your computer and use it in GitHub Desktop.
Save gowoonsori/da030f5cf396048e96344b5b4a3ec4b1 to your computer and use it in GitHub Desktop.
Java 와 Github Api 이용하여 repo Isuue 댓글 대시보드창 만들기
package GithubIsuueDashBoad;
import org.kohsuke.github.*;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
public class DashBoard {
private GitHub github;
private Logger logger = Logger.getGlobal();
private int totlaIssues;
public DashBoard(String email, String passwd) {
try {
GitHubBuilder builder = new GitHubBuilder();
this.github = builder.withPassword(email, passwd).build();
logger.info("계정 연결 성공");
} catch (IOException e) {
e.printStackTrace();
logger.info("계정 연결 실패");
}
}
public DashBoard(String token) {
try {
GitHubBuilder builder = new GitHubBuilder();
this.github = builder.withOAuthToken(token).build();
logger.info("계정 연결 성공");
} catch (IOException e) {
e.printStackTrace();
logger.info("계정 연결 실패");
}
}
public List<GHIssue> getIssues(String repoName) {
try {
GHRepository repo = this.github.getRepository(repoName);
logger.info("계정 repo 생성 성공");
return repo.getIssues(GHIssueState.ALL);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public Map<String, List<GHIssue>> getComments(List<GHIssue> issues) {
Map<String, List<GHIssue>> userList = new HashMap<>();
Set<String> nameList = new HashSet<>();
this.totlaIssues = issues.size();
for (GHIssue issue : issues) {
try {
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
String name = comment.getUser().getLogin();
nameList.add(name);
}
for (String name : nameList) {
if (userList.containsKey(name)) userList.get(name).add(issue);
else {
List<GHIssue> newIssusList = new ArrayList<>();
newIssusList.add(issue);
userList.put(name, newIssusList);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return userList;
}
public void showStatistics(Map<String, List<GHIssue>> userList) {
System.out.println("-- 참여 현황 --");
for (Map.Entry<String, List<GHIssue>> nameIssues : userList.entrySet()) {
System.out.println(
"User (" + nameIssues.getKey() + ") : " + (nameIssues.getValue().size()*100.0) / this.totlaIssues + "%");
}
}
}
package javaStudy;
import javaStudy.GithubIsuueDashBoad.DashBoard;
public class Github {
public static void main(String[] args) {
DashBoard dashBoard = new DashBoard("my-token");
dashBoard.showStatistics(dashBoard.getComments(dashBoard.getIssues("witheship/live-study")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment