ade's notes

PyGitHub Examples

This is a list of example code snippets for interacting with the Github API in Python via the PyGitHub package.

Use PyGitHub for interacting with GitHub repositories, pull requests, branches, and metadata through the GitHub API. See GitPython Examples for local Git operations.


Authenticate and Get Repository

Input: GitHub token and repository name
Output: Authenticates and loads the repo object

from github import Github
g = Github("your_github_token")
repo = g.get_repo("octocat/Hello-World")

Get Repository Description and License

Input: None
Output: Displays repo description and license name

print("Description:", repo.description)
print("License:", repo.get_license().license.name)

stdout

Description: My first repo on GitHub!
License: MIT License

List Files in Root Directory

Input: None
Output: Lists paths of files in the root of the repository

contents = repo.get_contents("")
for content in contents:
    print(content.path)

stdout

README.md
LICENSE
main.py

Get Tags

Input: None
Output: Lists Git tags in the repository

tags = repo.get_tags()
print([tag.name for tag in tags])

stdout

['v1.0.0', 'v1.1.0', 'v2.0.0']

Get Releases

Input: None
Output: Lists titles of published GitHub releases

releases = repo.get_releases()
print([release.title for release in releases])

stdout

['Initial Release', 'Bugfix Release', 'Major Update']

Create a Tag and Release

Input: Tag name, message, and commit SHA
Output: Creates both a Git tag and a GitHub release

repo.create_git_tag_and_release(
    tag="v1.2.0",
    tag_message="Release version 1.2.0",
    release_name="v1.2.0",
    release_message="Changelog:\n- New features\n- Fixes",
    object=repo.get_commits()[0].sha,
    type="commit"
)

List Branches

Input: None
Output: Lists all branches in the repository

branches = repo.get_branches()
for branch in branches:
    print(branch.name)

stdout

main
dev
feature/login

Get Latest Commit Message for a Branch

Input: Branch name
Output: Latest commit message on that branch

branch = repo.get_branch("main")
commit = repo.get_commit(branch.commit.sha)
print(commit.commit.message)

stdout

Refactor authentication middleware

Compare Two Branches

Input: Base and head branch names
Output: Shows commit distance between branches

comparison = repo.compare("main", "feature/login")
print("Ahead by:", comparison.ahead_by)
print("Behind by:", comparison.behind_by)

stdout

Ahead by: 3
Behind by: 1

Create a Pull Request

Input: PR title, description, head branch, base branch
Output: Creates a pull request on GitHub

repo.create_pull(
    title="Add login feature",
    body="Implements login using JWT",
    head="feature/login",
    base="main"
)

Merge a Pull Request (Squash)

Input: Pull request number
Output: Merges the PR using squash strategy

pr = repo.get_pull(42)
pr.merge(merge_method="squash")

List All Repos for a User

Input: GitHub username
Output: Lists all public repos owned by the user

user = g.get_user("octocat")
repos = user.get_repos()
for r in repos:
    print(r.full_name)

stdout

octocat/Hello-World
octocat/Spoon-Knife
octocat/octo-app

View .gitmodules File (if present)

Input: None
Output: Shows raw content of .gitmodules file

gitmodules = repo.get_contents(".gitmodules")
print(gitmodules.decoded_content.decode())

stdout

[submodule "libs/foo"]
    path = libs/foo
    url = https://github.com/example/foo.git

See Also