Use the PyGithub library to interact with GitHub.

Use the PyGithub library.

Resources

In [1]:
from pathlib import Path
import itertools as it
from datetime import datetime, timedelta
In [2]:
from github import Github
from faker import Faker
In [3]:
fake = Faker()
In [4]:
GITHUB_USER = "dm-wyncode"
GITHUBPWD = Path(Path.home(), '.texpander/github_wc_pwd.txt').read_text().strip()
In [5]:
github = Github(GITHUB_USER, GITHUBPWD)
github_user = github.get_user()
for repo in it.islice(github_user.get_repos(), 0, 5):
    print(repo.name)
sharemyride
popgage
fll_hackathon
team-practice
ansible-create-users
In [6]:
[item for item in dir(github_user) if not item.startswith("_") and "repo" in item]
Out[6]:
['create_repo',
 'get_repo',
 'get_repos',
 'owned_private_repos',
 'public_repos',
 'repos_url',
 'total_private_repos']

Output of github_user.create_repo??

Signature:
github_user.create_repo(
    ['name', 'description=NotSet', 'homepage=NotSet', 'private=NotSet', 'has_issues=NotSet', 'has_wiki=NotSet', 'has_downloads=NotSet', 'has_projects=NotSet', 'auto_init=NotSet', 'license_template=NotSet', 'gitignore_template=NotSet', 'allow_squash_merge=NotSet', 'allow_merge_commit=NotSet', 'allow_rebase_merge=NotSet'],
)
In [7]:
FAKE_REPO_NAME = '-'.join(fake.catch_phrase().lower().split())
DESCRIPTION = fake.paragraph()
new_repo = github_user.create_repo(FAKE_REPO_NAME, description=DESCRIPTION)
In [8]:
print(new_repo.ssh_url)
git@github.com:dm-wyncode/open-source-coherent-flexibility.git
In [9]:
[item for item in dir(new_repo) if not item.startswith("_") and "delete" in item]
Out[9]:
['delete', 'delete_file']
In [10]:
TIMEDELTA = timedelta(hours=5)
In [11]:
for repo in (
    repo
    for repo in github_user.get_repos()
    if datetime.utcnow() - repo.updated_at <= TIMEDELTA
):
    repo.delete()
    print(f'"{repo.name}" has been deleted.')
"open-source-coherent-flexibility" has been deleted.
"re-engineered-bandwidth-monitored-product" has been deleted.
"versatile-zero-defect-focus-group" has been deleted.