Day 13–101 Days of DevOps — GitHub API using Python and PyGithub Module

Prashant Lakhera
7 min readJul 13, 2021

--

Welcome to Day 13 of 101 Days of DevOps. The topic for today is GitHub API using Python and using PyGithub Module.

To view the complete course, please check the below url.

For more info, register via the below link

YouTube Channel link

GitHub is an integral part of the DevOps cycle. As DevOps engineers, we use to interact with the GitHub website every day. But your task is to automate as much as possible. Isn’t it’s great that common GitHub tasks like creating or deleting repo, listing repo can be automated? This is where you are going to use GitHub API with Python to automate these tasks.

Pre-requisites

Before we start using GitHub API we first need to generate a personal access token that allows us to authenticate against the API. Go to https://github.com/settings/tokens and click on Generate new token.

In the next screen, select the scope of the token. Which scope you choose depends upon which action you want to perform. Please check the GitHub documentation before selecting the scope https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps

Now you have a personal access token, you are all set to make a request to the GitHub API endpoint which is https://api.github.com.

Creating Repository

In order to create a repository go to GitHub documentation https://docs.github.com/en/rest/reference/repos#create-a-repository-for-the-authenticated-user

In the GitHub doc it’s mentioned that in order to create a repo, you need to make a POST request(POST request is made when you want to some changes). Also,/user/repos need to be added to the base URL which is https://api.github.com.

If you scroll down on the page, you will see they even give you some sample and response codes.

Let’s start working on code using the above shell example. The first step is to import the modules:

  • requests: is used in order to make POST requests to the GITHUB API endpoint.
  • os: (optional) in order to authenticate to the API endpoint we need to use the personal access token we have created. This we can hardcode into our code or follow security best practices to export it via an environment variable.
  • json: This is how we are going to provide data to the API in the form of JSON string.
import requests
import os
import json
  • With all the modules imported the next step is to get the token from the environment variable.
token = os.environ.get("GITHUB_TOKEN")

NOTE: In order to set the environment variable using the export command

export GITHUB_TOKEN="<enter your token here>
  • In the next part of the code, we are going to ask the user to enter the repository name he wants to create. The next few parameters are setting the GitHub API endpoint using the GITHUB_API_URL variable. Headers include the token and then repository name which is sent in json format.
reponame = input("Please enter the repo name you want to create : ")
GITHUB_API_URL = "https://api.github.com/"
headers = {"Authorization": "token {}".format(token)}
data = {"name": "{}".format(reponame)}
  • Once you have everything in place the next step is to make a POST request. Here we have introduced json.dumps() function whose work is to convert json file into a json string.
r = requests.post(GITHUB_API_URL +"user/repos" + "", data=json.dumps(data), headers=headers)
  • The final code will look like this
import requests
import os
import json

token = os.environ.get("GITHUB_TOKEN")
reponame = input("Please enter the repo name you want to create : ")

GITHUB_API_URL = "https://api.github.com/"
headers = {"Authorization": "token {}".format(token)}
data = {"name": "{}".format(reponame)}

r = requests.post(GITHUB_API_URL +"user/repos" + "", data=json.dumps(data), headers=headers)
print(r)
  • If you save the above code in the github_repo.py file and execute it. It will prompt you for the repo name and then you will get a response as 201 which means repo created successfully. Go to your GitHub account and verify that.
python3 github_repo.py
Please enter the repo name you want to create : abc123
<Response [201]>

Deleting Repository

  • Check the sample and response code
  • The entire code remains with some minor modifications, as this time you need a username, the repository you want to delete, and as usual, you are making a delete request this time.
username = input("Please enter your GitHub username: ")
r = requests.
delete("https://api.github.com/repos/{}/{}".format(username,deleterepo), headers=headers)
print(r)
  • The complete code will look like this
import requests
import os
import json

token = os.environ.get("GITHUB_TOKEN")
reponame = input("Please enter the repo name you want to delete : ")

GITHUB_API_URL = "https://api.github.com/"
headers = {"Authorization": "token {}".format(token)}
data = {"name": "{}".format(reponame)}

username = input("Please enter your GitHub username: ")
r = requests.
delete("https://api.github.com/repos/{}/{}".format(username, reponame), headers=headers)
print(r)
  • If you execute the above code, it will prompt for reponame and username and then you will get a response as 204 which means repo created successfully. Go to your GitHub account and verify that.
python3 github_repo.py
Please enter the repo name you want to delete : abc123
Please enter your GitHub username: 100daysofdevops
<Response [204]>

Listing Repositories

One more time let’s go to the GitHub documentation https://docs.github.com/en/rest/reference/repos#list-repositories-for-a-user

  • Listing repos is similar to what we have done above for creating or delete a repo but the only tricky part is to figure out the data part.
data = {"type": "all", "sort":"full_name", "direction": "asc"}
  • The final code will look like this. The output we receive in the form of a dictionary so we just iterate over it.
import requests
import os
import json


data = {"type": "all", "sort":"full_name", "direction": "asc"}

username = input("Please enter your GitHub username: ")
output = requests.get("https://api.github.com/users/{}/repos".format(username), data=json.dumps(data))
output = json.loads(output.text)

for reponame in output:
print(reponame['name'])
  • If you save the above code in the file and execute it
python3 github_repo.pyPlease enter your GitHub username: 100daysofdevops
100daysofdevops
21_days_of_aws_using_terraform
21_Days_of_Docker

NOTE: Listing user doesn’t require any authentication i.e no token needs to be passed.

I have barely scratched the surface, please check the GitHub documentation to learn about the resource available in the GitHub REST API.

Now if you don’t want to figure out these APIs by yourself and looking for some Python library then you can use PyGithub.

As per their official documentation

PyGithub is a Python library to use the Github API v3. With it, you can manage your Github resources (repositories, user profiles, organizations, etc.) from Python scripts.

PyGithub is a third party library, so you first need to install it

pip3 install PyGithub

Some common operations

  • List all the repos
from github import Githubimport osaccess_token=os.environ.get(“GITHUB_TOKEN”)# using an access tokeng = Github(access_token)for repo in g.get_user().get_repos():print(repo.name)
  • To get the list of branches
# repo = g.get_repo("<username>/<reponame>")
repo = g.get_repo(“100daysofdevops/100daysofdevops”)
print(list(repo.get_branches()))
  • To get the list of all the stars in your repo
repo = g.get_repo(“100daysofdevops/100daysofdevops”)print(repo.stargazers_count)
  • Get list of open issues
open_issues = repo.get_issues(state=’open’)for issue in open_issues:print(issue)
  • Get all of the contents of the root directory of the repository
contents = repo.get_contents(“”)for content_file in contents:print(content_file)
  • Get all of the contents of the repository recursively
repo = g.get_repo(“100daysofdevops/100daysofdevops”)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:print(file_content)
  • Create issue
repo = g.get_repo(“100daysofdevops/100daysofdevops”)repo.create_issue(title=”This is a new test issue”)
  • Get Pull Requests by Query
repo = g.get_repo(“100daysofdevops/100daysofdevops”)pulls = repo.get_pulls(state=’open’, sort=’created’, base=’master’)for pr in pulls:print(pr.number)

For more examples please check PyGithub official documentation

I am looking forward to you guys joining the amazing journey.

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet