Get all commits from today

送分小仙女□ 提交于 2020-01-17 05:03:26

问题


I use this code to get all commits from Guthub. I would like to get the commits only from today.

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());
            }
        }
    }

Is there any way to get the commits only from today?


回答1:


Always good to know which library are you using. Github API has "since" and "until" parameters: https://developer.github.com/v3/repos/commits/

Also those arguments are available in the Kohsuke's library: https://github.com/kohsuke/github-api/blob/master/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java

Using "since" and "until" parameters will save you from requesting unneeded data and making too many requests to the server.

The library is also available in Maven central:

    <dependency>
        <groupId>org.kohsuke</groupId>
        <artifactId>github-api</artifactId>
        <version>1.77</version>
    </dependency>

Here's the sample code that worked for me:

    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());
    }


来源:https://stackoverflow.com/questions/39017648/get-all-commits-from-today

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!