github有一系列api作为查询的接口,同时还有用各种语言封装的版本。详情<a href="https://developer.github.com/v3/repos/branches/">点击这里</a>
当我今天打算调用getStats()方法时遇到了很坑的问题:返回值总是Null
代码如下:
GitHubClient client = new GitHubClient(); client.setCredentials("username", "password"); CommitService coms = new CommitService(client); RepositoryService service = new RepositoryService(client); Repository repo = service.getRepository("BokunoMasayume","bootstrap-note"); for(RepositoryCommit rc : coms.getCommits(repo)){ System.out.println("total: "+rc.getStats()); }
我去查了github的原始api,发现当查询一个仓库的所有commits时,返回的commit列表中的每个commit信息都有所省略,而stats这个属性正好被省略了,
而在通过commit-sha和仓库查询的单个commit信息中有stats属性
commit集:https://api.github.com/repos/BokunoMasayume/bootstrap-note/commits
特定commit:https://api.github.com/repos/BokunoMasayume/bootstrap-note/commits/3084c0a49fbca0c496abac80191f04b929141d95
于是我在代码中加了一行<code>rc = coms.getCommit(repo, rc.getSha());</code>,将列表中的RepositoryCommit对象变成了单独(理解意思就好)的RepositoryCommit对象,果然,再次运行getStats正常了。
1 GitHubClient client = new GitHubClient(); 2 client.setCredentials("username", "password"); 3 CommitService coms = new CommitService(client); 4 RepositoryService service = new RepositoryService(client); 5 Repository repo = service.getRepository("BokunoMasayume","bootstrap-note"); 6 for(RepositoryCommit rc : coms.getCommits(repo)){ 7 rc = coms.getCommit(repo, rc.getSha()); 8 System.out.println("total: "+rc.getStats().getTotal); 9 }