Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Created July 1, 2020 05:06
Show Gist options
  • Save srkiNZ84/dfbe08cf2af1eee982a80e1e9619943b to your computer and use it in GitHub Desktop.
Save srkiNZ84/dfbe08cf2af1eee982a80e1e9619943b to your computer and use it in GitHub Desktop.
Python code to commit to GitHub
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