-
-
Save puredanger/f6b09089f50321fca30a7928045bc845 to your computer and use it in GitHub Desktop.
Java stream github api example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String author(RepositoryCommit commit) { | |
User author = commit.getAuthor(); | |
return (author == null) ? commit.getCommit().getAuthor().getName() : author.getLogin(); | |
} | |
private static String mostCommits(CommitService commitSvc, Repository repo) { | |
System.out.println(repo.getName()); | |
try { | |
List<RepositoryCommit> commits = commitSvc.getCommits(repo); | |
Map<String, List<RepositoryCommit>> commitsByAuthor = commits.stream().collect(Collectors.groupingBy(Main::author)); | |
Comparator<Map.Entry<String,List<RepositoryCommit>>> comparator = new Comparator<Map.Entry<String,List<RepositoryCommit>>>() { | |
@Override | |
public int compare(Map.Entry<String,List<RepositoryCommit>> e1, Map.Entry<String,List<RepositoryCommit>> e2) { | |
return e2.getValue().size() - e1.getValue().size(); | |
} | |
}; | |
Optional<Map.Entry<String,List<RepositoryCommit>>> max = commitsByAuthor.entrySet().stream().sorted(comparator).findFirst(); | |
if(max.isPresent()) { | |
return max.get().getKey(); | |
} else { | |
return null; | |
} | |
} catch(Exception e) { | |
System.out.println("\tcontinuing but saw... " + e.getMessage()); | |
return null; | |
} | |
} | |
public static Map<String, String> mostCommitsPerRepo2(GitHubClient client, String org) throws Exception { | |
RepositoryService repoSvc = new RepositoryService(client); | |
CommitService commitSvc = new CommitService(client); | |
List<Repository> repos = repoSvc.getRepositories(org); | |
List<String> repoNames = repos.stream().map(Repository::getName).collect(Collectors.toList()); | |
List<String> users = repos.stream().map((repo) -> mostCommits(commitSvc, repo)).collect(Collectors.toList()); | |
;; need zip! | |
Iterator<String> userIter = users.iterator(); | |
Map<String, String> commitsPerRepo = new HashMap<>(); | |
for(String repoName : repoNames) { | |
commitsPerRepo.put(repoName, userIter.next()); | |
} | |
return commitsPerRepo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment