我想用Github更新一个文件,并将它提交到一个分支中。我很难做出承诺。SHA与预期的不匹配。
{
'documentation_url': 'https://developer.github.com/enterprise/2.7/v3/repos/contents/',
'message': 'pom.xml does not match de42fdd980f9b8067a2af982de46b8d5547e4597'
}
我所做的工作如下:
import hashlib
myfile = "new content of my README"
resulting_file = base64.b64encode(bytes(myfile, "utf-8"))
file_as_str = str(resulting_file.decode('utf-8'))
sha = hashlib.sha1(file_as_str.encode('utf-8')).hexdigest()
url = 'https://someurl.com/someproject/contents/README.md?access_token=' + access_token
data = '{"message": "bla bla", "content": "'+file_as_str+'", "sha": "'+sha+'", "branch": "'+branch+'"}'
response = requests.put(url, data=data)
我不想使用lib来更好地理解正在发生的事情。可能SHA没有正确生成,但我无法确定原因。有人能帮忙吗?
发布于 2016-10-05 13:10:35
您不需要计算新文件的SHA。相反,您必须提供要替换的文件的SHA。您可以通过使用获取内容对文件执行requests.get()
来获得此结果。
url = 'https://api.github.com/repos/someowner/someproject/contents/pom.xml'
r = requests.get(url)
sha = r.json()['sha']
然后使用sha
请求中的PUT
值更新文件:
with open('myfile', 'rb') as f:
content = str(base64.b64encode(f.read()), encoding='utf8')
data = {'message': 'bla bla', 'content': content, 'sha': sha, 'branch': branch}
r = requests.put(url, json=data)
发布于 2016-10-05 12:40:05
GitHub按以下方式计算散列:
sha1("blob " + filesize + "\0" + data)
因此,请使用以下代码:
with open(filepath, 'rb') as file_for_hash:
data = file_for_hash.read()
filesize = len(data)
sha = hashlib.sha1("blob " + filesize + "\0" + data).hexdigest()
https://stackoverflow.com/questions/39873507
复制相似问题