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

Creating Repository

import requests
import os
import json
token = os.environ.get("GITHUB_TOKEN")
export GITHUB_TOKEN="<enter your token here>
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)
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)
python3 github_repo.py
Please enter the repo name you want to create : abc123
<Response [201]>

Deleting Repository

username = input("Please enter your GitHub username: ")
r = requests.
delete("https://api.github.com/repos/{}/{}".format(username,deleterepo), headers=headers)
print(r)
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)
python3 github_repo.py
Please enter the repo name you want to delete : abc123
Please enter your GitHub username: 100daysofdevops
<Response [204]>

Listing Repositories

data = {"type": "all", "sort":"full_name", "direction": "asc"}
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'])
python3 github_repo.pyPlease enter your GitHub username: 100daysofdevops
100daysofdevops
21_days_of_aws_using_terraform
21_Days_of_Docker

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.

pip3 install PyGithub
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)
# repo = g.get_repo("<username>/<reponame>")
repo = g.get_repo(“100daysofdevops/100daysofdevops”)
print(list(repo.get_branches()))
repo = g.get_repo(“100daysofdevops/100daysofdevops”)print(repo.stargazers_count)
open_issues = repo.get_issues(state=’open’)for issue in open_issues:print(issue)
contents = repo.get_contents(“”)for content_file in contents:print(content_file)
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)
repo = g.get_repo(“100daysofdevops/100daysofdevops”)repo.create_issue(title=”This is a new test issue”)
repo = g.get_repo(“100daysofdevops/100daysofdevops”)pulls = repo.get_pulls(state=’open’, sort=’created’, base=’master’)for pr in pulls:print(pr.number)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

4.91K Followers

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