Как обновить файл с помощью PyGithub?

Я хочу знать, какой метод я должен вызывать (и какой объект) и как вызвать этот метод (необходимые параметры и их значения).

Заранее спасибо!

2 ответа

Решение
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)

Если вы используете токен, у вас должен быть хотя бы объем репо, чтобы сделать это. https://developer.github.com/v3/oauth/

См. https://developer.github.com/v3/repos/contents/ и https://github.com/PyGithub/PyGithub

По состоянию на 2021 год API PyGithub изменился, и есть пример того, как это сделать: 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)
Другие вопросы по тегам