Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a file using PyGithub?

I want to know which method should I call (and on which object) and how to call that method (required parameters and their meanings).

like image 716
Abhishek Kumar Avatar asked Nov 16 '16 11:11

Abhishek Kumar


2 Answers

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

See: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub

like image 145
Sergey Luchko Avatar answered Nov 13 '22 21:11

Sergey Luchko


As of 2021, the PyGithub API has changed and there's an example of how to do this: 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")}

For .update_file, the first string is the message, the second string is the new contents of the file. Here's the API description:

update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)
like image 25
Mixchange Avatar answered Nov 13 '22 20:11

Mixchange