Created
July 1, 2020 05:06
-
-
Save srkiNZ84/dfbe08cf2af1eee982a80e1e9619943b to your computer and use it in GitHub Desktop.
Python code to commit to GitHub
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
import base64 | |
import json | |
import time | |
import requests | |
import os | |
def push_to_github(filename, repo, branch, token): | |
url="https://api.github.com/repos/" + repo + "/contents/" + filename | |
fileContents = """Last timestamp: """ | |
fileContents = fileContents + str(time.time()) | |
base64content=base64.b64encode(fileContents.encode('utf-8')) | |
data = requests.get(url+'?ref='+branch, headers = {"Authorization": "token "+token}).json() | |
sha = data['sha'] | |
message = json.dumps({"message":"autoupdate", | |
"branch": branch, | |
"content": base64content.decode("utf-8") , | |
"sha": sha | |
}) | |
resp = requests.put(url, data = message, headers = {"Content-Type": "application/json", "Authorization": "token " + token}) | |
print(resp) | |
token = os.environ['GH_TOKEN'] | |
filename = os.environ['REPO_FILENAME'] | |
repo = os.environ['GH_REPOSITORY'] | |
branch = os.environ['GH_BRANCH'] | |
push_to_github(filename, repo, branch, token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment