2017-03-09 101 views
0

可以通过提供相关的提交散列来接收来自调用github Search APIgithub Search API中给定提交的详细信息,现在我需要通过使用github java API (org.eclipse.egit.github.*)来获得相同的响应,这可以在here中找到。根据他们在here中发现的版本2.1.5的文档,CommitService class中没有方法通过仅提供提交哈希来获取提交信息。有没有解决方法来达到他们?在此先感谢如何使用github java API(org.eclipse.egit.github。*)来搜索给定的提交散列

回答

1

您可以使用CommitService.getCommit(IRepositoryIdProvider, String)方法,只提供一个参数,即提交将被搜索的存储库。例如,

GitHubClient client = new GitHubClient(server).setCredentials(login, token); 
RepositoryService repoService = new RepositoryService(client); 

// If you know which repository to search (you know the owner and repo name) 
Repository repository = repoService.getRepository(owner, repoName); 

CommitService commitService = new CommitService(client) 
Commit commit1 = commitService.getCommit(repository, sha).getCommit(); 
System.out.println("Author: " + commit1.getAuthor().getName()); 
System.out.println("Message: " + commit1.getMessage()); 
System.out.println("URL: " + commit1.getUrl()); 

或者,你可能只是通过从RepositoryService.getRepositories()方法返回,如果你不知道每个库循环要搜索的存储库。例如,

List<Repository> repositories = repoService.getRepositories(); 
Commit commit2 = null; 
for (Repository repo : repositories) { 
    try { 
     commit2 = commitService.getCommit(repo, sha).getCommit(); 
     System.out.println("Repo: " + repo.getName()); 
     System.out.println("Author: " + commit2.getAuthor().getName()); 
     System.out.println("Message: " + commit2.getMessage()); 
     System.out.println("URL: " + commit2.getUrl()); 
     break; 
    } catch (RequestException re) { 
     if (re.getMessage().endsWith("Not Found (404)")) { 
      continue; 
     } else { 
      throw re; 
     } 
    } 
}