Interacting with GitHub Using Python

By | March 19, 2017

In this short article I will show two different ways to interact with GitHub using Python. First I will use the GitHub API directly, sending and receiving HTTP request and even parsing the JSON responses. Next I will rely on the git command that I have installed on my computer and execute it from Python to clone a repository, make modifications to it, commit the modifications and finally push the modifications to the remote repository. This latter approach can be used with any Git repository and is not limited to GitHub repositories.

Note that I am just dabbling in Python so I will be very grateful if you comment on anything that can be improved!

Using the GitHub API

GitHub has an API that, among other things, allow you to interact with repositories on GitHub.

GitHub Developer Documentation

The GitHub developer’s entry web page can be found here. From there you can find the documentation of their API, which currently is version 3. Their API contains much more than the little part I use here that allows me to retrieve information about the commits in a repository. I found the documentation well written and easy to understand.

Invoking the GitHub API from Python

Before I knew about any of the GitHub API client libraries for Python I tried to interact directly with the GitHub API. After having had a look at a few of those libraries, I decided to keep this code as I felt that it was reasonably easy to understand and that it does what it is supposed to do. The following function retrieves a string containing the date of the last commit for a user’s GitHub repository:

Note that when using the above code, you need to define a global variable named github_user which is to contain your own GitHub user name. This name will be enclosed in the User-Agent HTTP header with each request to GitHub, which uses this to keep track of who makes requests.
Since the above code only reads public data from a repository, no authentication is required. For requests to the GitHub API that are not authenticated, there is a rate limit of 60 requests per second as of writing this article.

With the above function, I can now find out when the last commit was made to a GitHub repository, for instance my own Message-Cowboy repository:

Cloning, Committing and Pushing

Spurred by the success of the above code snippet, I went on to the next task: Clone a repository, modify a file, commit and push the modification to GitHub.
First I tried the same approach that I used above but gave up after realizing it was not a trivial task. Next I tried a few GitHub client libraries for Python but none of them seemed very straightforward given the task at hand.

Executing Git Commands from Python

After having searched for a while, I found what I think is a very elegant solution: Use the local git shell command from Python. I polished the code a little and this is the result:

The above code assumes that you have the git command installed and configured on your computer. I have only tested it on *nix operating systems and in addition, I only implemented the Git commands needed. Implementing additional commands should be trivial. Note that this code can be used with any Git repository, not only GitHub.

Programmatically Update a File in a Git Repository

With the above code in place, it became simple to write code to clone a remote repository, add/modify a file in it, commit the changes and push them back to the remote repository. In this example I add/update a file named .date in a remote repository:

Note that I use the execute_shell_command function from earlier to run the date command and pipe its output into the .date file to get the current date and time.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *