Created
December 17, 2017 13:19
-
-
Save yershalom/a7c08f9441d1aadb13777bce4c7cdc3b to your computer and use it in GitHub Desktop.
Easy way to calculate commits count from the github api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is one if you want total commits for a user the last 24h across all repos on GitHub.