Created
January 15, 2019 22:38
-
-
Save Fred-Barclay/336b0dd6cf78fbc85f35c33af7118272 to your computer and use it in GitHub Desktop.
Find the checksum of the most recent commit in a GitHub repository
This file contains hidden or 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
#!/usr/bin/env python3 | |
""" | |
Get the sha sum of the last commit on the master branch | |
of a github repository. | |
Suitable for using in shell scripts: i.e. | |
last_commit=`python3 ~/bin/get_last_commit.py $url` will | |
assign the sha sum of the last commit found at $url | |
to $last_commit. In this case (for bash at least) | |
print(shasum) will not actually print the sha sum to the | |
console. | |
""" | |
import json | |
import requests | |
import sys | |
main_url = sys.argv[1] | |
protocol, path = main_url.split('//') | |
components = path.split('/') | |
api_path = 'api.' + components[0] + '/repos/' + '/'.join(components[1:]) + '/commits/master' | |
api_url = protocol + '//' + api_path | |
commit = requests.get(api_url) | |
if commit.ok: | |
shasum = json.loads(commit.text)['sha'] | |
print(shasum) | |
sys.exit(0) | |
else: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment