Skip to content

Instantly share code, notes, and snippets.

@nazt
Forked from yershalom/get_commit_count.py
Created November 6, 2020 03:50
Show Gist options
  • Save nazt/d36b925ee7df3a8e45536049971c05ed to your computer and use it in GitHub Desktop.
Save nazt/d36b925ee7df3a8e45536049971c05ed to your computer and use it in GitHub Desktop.

Revisions

  1. @yershalom yershalom created this gist Dec 17, 2017.
    40 changes: 40 additions & 0 deletions get_commit_count.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    import requests

    base_url = 'https://api.github.com'


    def get_all_commits_count(owner, repo, sha):
    first_commit = get_first_commit(owner, repo)
    compare_url = '{}/repos/{}/{}/compare/{}...{}'.format(base_url, owner, repo, first_commit, sha)

    commit_req = requests.get(compare_url)
    commit_count = commit_req.json()['total_commits'] + 1
    print(commit_count)
    return commit_count


    def get_first_commit(owner, repo):
    url = '{}/repos/{}/{}/commits'.format(base_url, owner, repo)
    req = requests.get(url)
    json_data = req.json()

    if req.headers.get('Link'):
    page_url = req.headers.get('Link').split(',')[1].split(';')[0].split('<')[1].split('>')[0]
    req_last_commit = requests.get(page_url)
    first_commit = req_last_commit.json()
    first_commit_hash = first_commit[-1]['sha']
    else:
    first_commit_hash = json_data[-1]['sha']
    return first_commit_hash


    def main():
    owner = 'getredash'
    repo = 'redash'
    # Took the last commit, Can do it automatically also but keeping it open
    sha = '5ba15ef35074a88daa5032ec4bec34d3a22a607e'
    get_all_commits_count(owner, repo, sha)


    if __name__ == '__main__':
    main()