Skip to content

Instantly share code, notes, and snippets.

@fronbasal
Created February 13, 2024 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fronbasal/bd0dd5ddc0e081a854f273c590a4765a to your computer and use it in GitHub Desktop.
Save fronbasal/bd0dd5ddc0e081a854f273c590a4765a to your computer and use it in GitHub Desktop.
Clean up github repository (set public repositories private, delete forks)
import github3
import click
@click.command()
@click.argument('access_token', required=True, type=str)
def main(access_token):
gh = github3.login(token=access_token)
username = gh.me().login
for repo in gh.repositories_by(username, type='owner', sort='created', direction='asc'):
# private tree
if repo.private:
continue
# fork tree
if repo.fork:
try:
if click.confirm(f'{repo.html_url} is a fork. Want to delete it?'):
if repo.delete():
print(f'{repo.html_url} is now deleted.')
else:
print(f'Failed to delete {repo.html_url}.')
except Exception as e:
print(f'Failed to delete {repo.html_url}: {e}')
continue
if click.confirm(f'{repo.html_url} is public. Do you want to make it private?'):
try:
if repo.edit(name=repo.name, private=True):
print(f'{repo.html_url} is now private.')
else:
if click.confirm(f'Failed to make {repo.html_url} private. Delete it?'):
if repo.delete():
print(f'{repo.html_url} is now deleted.')
else:
print(f'Failed to delete {repo.html_url}.')
except Exception as e:
print(f'Failed to modify {repo.html_url}: {e}')
continue
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment