我使用此代码从Guthub获取所有提交。我想仅从今天开始提交。
public void listCommits(String user_name, String password) throws IOException
{
GitHubClient client = new GitHubClient();
client.setCredentials(user_name, password);
RepositoryService service = new RepositoryService(client);
List<Repository> repositories = service.getRepositories();
for (int i = 0; i < repositories.size(); i++)
{
Repository get = repositories.get(i);
System.out.println("Repository Name: " + get.getName());
CommitService commitService = new CommitService(client);
for (RepositoryCommit commit : commitService.getCommits(get))
{
System.out.println("Repository commit: " + commit.getCommit().getMessage());
System.out.println("Repository commit date : " + commit.getCommit().getCommitter().getDate());
}
}
}
有没有办法从今天开始提交提交?
答案 0 :(得分:3)
总是很高兴知道您使用的是哪个库。 Github API已经&#34;自&#34; &#34;直到&#34;参数: https://developer.github.com/v3/repos/commits/
这些论点也可以在Kohsuke的库中找到: https://github.com/kohsuke/github-api/blob/master/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java
使用&#34;因为&#34; &#34;直到&#34;参数将使您免于请求不需要的数据并向服务器发出过多请求。
图书馆也可以在Maven中心使用:
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.77</version>
</dependency>
以下是适用于我的示例代码:
Properties props = new Properties();
props.setProperty("login", "somebody@somewhere.com");
props.setProperty("password", "YourGithubPassword");
GitHub gitHub = GitHubBuilder.fromProperties(props).build();
GHRepository repository = gitHub.getRepository("your/repo");
Calendar cal = Calendar.getInstance();
cal.set(2014, 0, 4);
Date since = cal.getTime();
cal.set(2014, 0, 14);
Date until = cal.getTime();
GHCommitQueryBuilder queryBuilder = repository.queryCommits().since(since).until(until);
PagedIterable<GHCommit> commits = queryBuilder.list();
Iterator<GHCommit> iterator = commits.iterator();
while (iterator.hasNext()) {
GHCommit commit = iterator.next();
System.out.println("Commit: " + commit.getSHA1() + ", info: " + commit.getCommitShortInfo().getMessage() + ", author: " + commit.getAuthor());
}