使用 Python 将 add/commit 数据库文件推送到 Github 的函数 3?

Function that would push, add/commit a database file to Github using Python 3?

有人知道更好的方法来创建 add/commit 数据库文件并将其推送到 Github 的函数吗? 我使用的代码一直给我错误。

TypeError: a bytes-like object is required, not 'str'

代码:

import requests
import base64
import json
import datetime

def push_to_repo_branch(gitHubFileName, fileName, repo_slug, branch, user, token):
    
    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))
        
    rjson = r.json()
    
    treeurl = rjson['commit']['commit']['tree']['url']
    # print(treeurl)
    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))

    
    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' ")

    with open(fileName) as data:
        byte_content = data.read()
        content = base64.b64encode(byte_content)

    # 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))

    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)

经过一些修改,我能够让它工作!!

GitHub 文档 https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents%22 很有帮助。

我会把我的代码和其他文件一起放在GitHubhttps://github.com/Kamuzinzi/Auto_push上,供任何想使用它的人and/or贡献。

但是如果你赶时间....检查这个代码:


import base64
import requests
import base64
import json
import datetime
from credentials import GITHUB_TOKEN


def push_file(fileName, repo_slug, branch, user, token):
    '''
    Push file update to GitHub 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 = f"Automated backup created for the file {fileName} as of {str(datetime.date.today())}"
    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))
        
    rjson = r.json()
    
    treeurl = rjson['commit']['commit']['tree']['url']
    # print(treeurl)
    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))

    
    r2json = r2.json()
    sha = None

    for file in r2json['tree']:
        # Found file, get the sha code
        if file['path'] == fileName:
            sha = file['sha']

    # if sha is None after the for loop, we did not find the file name!
    if sha is None:
        print ("\nThe file " + fileName + " is not in repos 'tree'. \nLet's create a new one .. \n", end=",\n 1 \n 2 \n 3 \n")

    with open(fileName, 'rb') as data:
        byte_content = data.read()
        content = base64.b64encode(byte_content).decode("ascii")

    # gathered all the data, now let's push
    inputdata = {}
    inputdata["branch"] = branch
    inputdata["message"] = message
    inputdata["content"] = content

    if sha:
        inputdata["sha"] = str(sha)

    updateURL = f"https://api.github.com/repos/{repo_slug}/contents/{fileName}"  
    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))
        print("Done!!\n")

    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)


fileName = "updatedFile.txt"
repositoryName = "username/repository"
branch = "branchName"
username = "Git_username"
token = GITHUB_TOKEN #please check from credentials.py and remember it has to be confidential 


push_file(fileName,repositoryName,branch,user=username,token=token)

记得创建一个文件“credentials.py”并将您的令牌“GITHUB_TOKEN”保存在其中。