简体   繁体   中英

Get all commits from today

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM