How to use GitHub V3 API to get commit count for a repo?

前端 未结 7 1953
小鲜肉
小鲜肉 2020-12-05 03:26

I am trying to count commits for many large github repos using the API, so I would like to avoid getting the entire list of commits (this way as an example:

7条回答
  •  无人及你
    2020-12-05 03:37

    I just made a little script to do this. It may not work with large repositories since it does not handle GitHub's rate limits. Also it requires the Python requests package.

    #!/bin/env python3.4
    import requests
    
    GITHUB_API_BRANCHES = 'https://%(token)s@api.github.com/repos/%(namespace)s/%(repository)s/branches'
    GUTHUB_API_COMMITS = 'https://%(token)s@api.github.com/repos/%(namespace)s/%(repository)s/commits?sha=%(sha)s&page=%(page)i'
    
    
    def github_commit_counter(namespace, repository, access_token=''):
        commit_store = list()
    
        branches = requests.get(GITHUB_API_BRANCHES % {
            'token': access_token,
            'namespace': namespace,
            'repository': repository,
        }).json()
    
        print('Branch'.ljust(47), 'Commits')
        print('-' * 55)
    
        for branch in branches:
            page = 1
            branch_commits = 0
    
            while True:
                commits = requests.get(GUTHUB_API_COMMITS % {
                    'token': access_token,
                    'namespace': namespace,
                    'repository': repository,
                    'sha': branch['name'],
                    'page': page
                }).json()
    
                page_commits = len(commits)
    
                for commit in commits:
                    commit_store.append(commit['sha'])
    
                branch_commits += page_commits
    
                if page_commits == 0:
                    break
    
                page += 1
    
            print(branch['name'].ljust(45), str(branch_commits).rjust(9))
    
        commit_store = set(commit_store)
        print('-' * 55)
        print('Total'.ljust(42), str(len(commit_store)).rjust(12))
    
    # for private repositories, get your own token from
    # https://github.com/settings/tokens
    # github_commit_counter('github', 'gitignore', access_token='fnkr:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
    github_commit_counter('github', 'gitignore')
    

提交回复
热议问题