如何使用github java API(org.eclipse.egit.github。*)来搜索给定的提交哈希

时间:2017-03-09 06:00:26

标签: java api github github-api egit

通过提供相关提交哈希,可以通过调用here中的github Search API来接收有关给定提交的详细信息,现在我需要使用github java API (org.eclipse.egit.github.*)获得相同的响应。可以在here中找到。根据{{​​3}}中找到的版本2.1.5的文档,CommitService class中没有方法通过仅提供提交哈希来获取提交信息。是否有解决方法可以达到目标?提前致谢

1 个答案:

答案 0 :(得分:2)

您可以使用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;
        }
    }
}   
相关问题