Created
November 13, 2017 20:18
-
-
Save avullo/b8153522f015a8b908072833b95c3408 to your computer and use it in GitHub Desktop.
Push file update to GitHub repository using GitHub API in Python
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 | |
import base64 | |
import json | |
import datetime | |
def push_to_repo_branch(gitHubFileName, fileName, repo_slug, branch, user, token): | |
''' | |
Push file update to GitHub repo | |
:param gitHubFileName: the name of the file in the repo | |
:param fileName: the name of the file on the local branch | |
:param repo_slug: the github repo slug, i.e. username/repo | |
:param branch: the name of the branch to push the file to | |
:param user: github username | |
:param token: github user token | |
:return None | |
:raises Exception: if file with the specified name cannot be found in the repo | |
''' | |
message = "Automated update " + str(datetime.datetime.now()) | |
path = "https://api.github.com/repos/%s/branches/%s" % (repo_slug, branch) | |
r = requests.get(path, auth=(user,token)) | |
if not r.ok: | |
print("Error when retrieving branch info from %s" % path) | |
print("Reason: %s [%d]" % (r.text, r.status_code)) | |
raise | |
rjson = r.json() | |
treeurl = rjson['commit']['commit']['tree']['url'] | |
r2 = requests.get(treeurl, auth=(user,token)) | |
if not r2.ok: | |
print("Error when retrieving commit tree from %s" % treeurl) | |
print("Reason: %s [%d]" % (r2.text, r2.status_code)) | |
raise | |
r2json = r2.json() | |
sha = None | |
for file in r2json['tree']: | |
# Found file, get the sha code | |
if file['path'] == gitHubFileName: | |
sha = file['sha'] | |
# if sha is None after the for loop, we did not find the file name! | |
if sha is None: | |
print "Could not find " + gitHubFileName + " in repos 'tree' " | |
raise Exception | |
with open(fileName) as data: | |
content = base64.b64encode(data.read()) | |
# gathered all the data, now let's push | |
inputdata = {} | |
inputdata["path"] = gitHubFileName | |
inputdata["branch"] = branch | |
inputdata["message"] = message | |
inputdata["content"] = content | |
if sha: | |
inputdata["sha"] = str(sha) | |
updateURL = "https://api.github.com/repos/EBISPOT/RDF-platform/contents/" + gitHubFileName | |
try: | |
rPut = requests.put(updateURL, auth=(user,token), data = json.dumps(inputdata)) | |
if not rPut.ok: | |
print("Error when pushing to %s" % updateURL) | |
print("Reason: %s [%d]" % (rPut.text, rPut.status_code)) | |
raise Exception | |
except requests.exceptions.RequestException as e: | |
print 'Something went wrong! I will print all the information that is available so you can figure out what happend!' | |
print rPut | |
print rPut.headers | |
print rPut.text | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The url in line 61, It was should like this: f"https://api.github.com/repos/{repo_slug}/contents/" + gitHubFileName