Search code examples
pythongithubpygithub

query github commits from a certain date using pygithub


have the following code:

from github import Github
...
g = github(token)
query = f'org:{org_name} author:{username} since=2021-10-19T00:00:00Z'
commits = g.search_commits(query, sort='author-date', order='desc')

If I have the "since" parameter there - I get 0 results. If I take it out, I get hundreds - and that's the problem. I'm doing some processing which can work this way so want to be incremental - ie cache the results and when I call it a few days later - only find the new commits since the last time I read.

From the documentation, this should work - but ... it doesn't - what am I doing wrong.


Solution

  • Short answer: Try to use: committer-date:<2023-01-01T01:00:00Z with >, >=, <, <=

    Long answer: Basically, pygithub is forwarding the request to REST as mentioned in this link https://pygithub.readthedocs.io/en/stable/github.html?highlight=search_commits#github.MainClass.Github.search_commits

    search_commits(query: str, sort: Opt[str] = NotSet, order: Opt[str] = NotSet, **qualifiers) → PaginatedList[Commit]
    
    Calls:  
    GET /search/commits
    
    Parameters: 
    query – string
    sort – string (‘author-date’, ‘committer-date’)
    order – string (‘asc’, ‘desc’)
    qualifiers – keyword dict query qualifiers
    Return type:    
    PaginatedList of github.Commit.Commit
    

    There is a committer-date you can use, such like committer-date:<2023-01-01T01:00:00Z. For the correct syntax, you can refer to https://docs.github.com/en/search-github/searching-on-github/searching-commits#search-by-authored-or-committed-date

    Qualifier   Example
    author-date:YYYY-MM-DD  author-date:<2016-01-01 matches commits authored before 2016-01-01.
    committer-date:YYYY-MM-DD   committer-date:>2016-01-01 matches commits committed after 2016-01-01.
    

    and more generic syntax here: https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates

    You can also test your search syntax here

    https://github.com/search?q=repo:neozhu/CleanArchitectureWithBlazorServer committer-date:<2023-01-01T01:00:00Z&type=commits