如何使用PyGithub更新文件?

9

我想知道应该调用哪个方法(以及在哪个对象上)以及如何调用该方法(必需的参数及其含义)。


两个开始的地方:https://developer.github.com/v3/repos/contents/ 和 https://github.com/PyGithub/PyGithub/blob/master/github/InputFileContent.py - Emil Vikström
2个回答

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

如果您使用token,则需要至少具有repo范围的token才能执行此操作。https://developer.github.com/v3/oauth/#scopes 请参见:https://developer.github.com/v3/repos/contents/https://github.com/PyGithub/PyGithub

6
截至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描述
update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)

2
谢谢您。请注意,唯一的实际区别似乎是 get_file_contentsget_contents - Luke Sawczak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接