ghrepo

raw JSON →
0.7.1 verified Sat May 09 auth: no python

A Python library for parsing and constructing GitHub repository URLs and specifiers (e.g., 'owner/repo'). Current version 0.7.1, requires Python >=3.8. Released on PyPI with moderate cadence.

pip install ghrepo
error ValueError: Could not parse 'owner/repo/' as a GitHub repo
cause Input string has trailing slash, which is invalid for this library.
fix
Strip trailing slash: GHRepo('owner/repo'.rstrip('/'))
error AttributeError: 'str' object has no attribute 'owner'
cause User called GHRepo as a function returning string? Actually GHRepo is a class, but if someone writes repo = GHRepo('url') and then repo = 'something', they get this.
fix
Ensure proper usage: repo = GHRepo('url') then use repo.owner, not reassign repo to string.
deprecated The 'from_url' class method is deprecated in favor of GHRepo() constructor directly.
fix Use GHRepo(url) instead of GHRepo.from_url(url).
gotcha Strings with trailing slash cause ValueError. URLs must not end with a slash.
fix Strip trailing slashes before passing to GHRepo or parse().
gotcha GitHub Enterprise URLs are not supported. Only github.com (or github.com subdomains?) tested.
fix Use a dedicated library like github3.py for enterprise support, or manually handle.

Basic usage: create GHRepo from URL or specifier, get components and URLs.

from ghrepo import GHRepo, parse

# Parse a URL
repo = GHRepo('https://github.com/owner/repo')
print(repo.owner, repo.name)  # owner repo

# Construct from specifier
repo2 = GHRepo('owner/repo')
print(repo2.url)  # https://github.com/owner/repo

# Parse using function
repo3 = parse('owner/repo')
print(repo3.full_name)  # owner/repo

# Auth (example with token)
import os
token = os.environ.get('GITHUB_TOKEN', '')
if token:
    repo4 = GHRepo('owner/repo', token=token)
    print(repo4.clone_url)  # https://x-access-token:...@github.com/owner/repo.git