PyGithub is a library for interacting with the GitHub API; it will not let you clone your repository or otherwise interact with a local git repository.
GitPython is a Python interface to git and can be used to clone a remote repository. In fact, there's an example of exactly that at the beginning of the tutorial to which you have linked.
That said, it's not clear from your question exactly what you hope to accomplish -- learning the git commandline interface is an important skill if you're going to be working with GitHub (or git in general). While you can probably perform many of the same tasks with GitPython, you are arguably going to be spending much of your time duplicating the existing functionality of the commandline tools.
Videos
» pip install PyGithub
I have created a small set of code for this. And it worked. Sharing it here, so others can get benefit from it.
Sample code:
This code will upload a file/replace the existing file.
Local file path: /tmp/file.txt
Github folder name: folder1/
from github import Github
g = Github("username", "password")
repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
with open('/tmp/file.txt', 'r') as file:
content = file.read()
# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
contents = repo.get_contents(git_file)
repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
print(git_file + ' UPDATED')
else:
repo.create_file(git_file, "committing files", content, branch="master")
print(git_file + ' CREATED')
This question is also related to this.
(note : not tested)
I think the code you linked does what you want.
This gist, written in ruby, shows a very similar scenario, but has the additional benefit of explicitly naming the API routes queried for each action (create_tree, create_commit, ...)
Check the docs for the PyGithub library : the methods are most probably wrappers around the same API calls.
I am using the python library pygithub to automatically create new branches and pull requests when certain files are updated. However, I am not sure how I would go about writing unit tests for this. I have a function that creates the branch and one that makes the pull request. I have tried looking at the pygithub tests for inspiration but I wasn't able to gain insight.
This would be very easy to test if I could simply allow pull requests and branches to be made, but I want to see if there is a way to do this without actually creating them. I looked into mocking objects, but I couldn't see how I that would actually be a real test since I am basically telling the objects what to do anyway.
How should I approach this problem? Is mocking even the right approach, or do I really need to create the branches and pull requests and delete them after?