如何使用 PyGithub 更新文件?
How to update a file using PyGithub?
我想知道我应该调用哪个方法(以及在哪个对象上)以及如何调用该方法(必需参数及其含义)。
import github
g = github.Github(token)
# or g = github.Github(login, password)
repo = g.get_user().get_repo("repo_name")
file = repo.get_file_contents("/your_file.txt")
# update
repo.update_file("/your_file.txt", "your_commit_message", "your_new_file_content", file.sha)
If you are using token then you should have at least repo scope
of your token to do it.
https://developer.github.com/v3/oauth/#scopes
参见: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub
截至 2021 年,PyGithub API 已更改,并且有一个如何执行此操作的示例:https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository
repo = g.get_repo("PyGithub/PyGithub")
contents = repo.get_contents("test.txt", ref="test")
repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
# {'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}
对于.update_file
,第一个字符串是消息,第二个字符串是文件的新内容。这是 API description:
update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)
我想知道我应该调用哪个方法(以及在哪个对象上)以及如何调用该方法(必需参数及其含义)。
import github
g = github.Github(token)
# or g = github.Github(login, password)
repo = g.get_user().get_repo("repo_name")
file = repo.get_file_contents("/your_file.txt")
# update
repo.update_file("/your_file.txt", "your_commit_message", "your_new_file_content", file.sha)
If you are using token then you should have at least repo scope of your token to do it. https://developer.github.com/v3/oauth/#scopes
参见: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub
截至 2021 年,PyGithub API 已更改,并且有一个如何执行此操作的示例:https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository
repo = g.get_repo("PyGithub/PyGithub")
contents = repo.get_contents("test.txt", ref="test")
repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
# {'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}
对于.update_file
,第一个字符串是消息,第二个字符串是文件的新内容。这是 API description:
update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)