1
0
Fork 0
ynh-apps_tools/bot-repo-cleanup/cleanup.py

36 lines
1.3 KiB
Python
Raw Normal View History

2022-11-23 22:01:07 +01:00
#!venv/bin/python3
2022-11-26 20:45:17 +01:00
# Obtained with `pip install PyGithub`, better within a venv
2022-11-23 22:01:07 +01:00
from github import Github
from github.Workflow import Workflow
# API token for yunohost-bot, with "delete_repo" right
g = Github("TOKEN_REPLACE_ME")
u = g.get_user("yunohost-bot")
2022-11-26 20:45:17 +01:00
# Let's build a minimalistic summary table
2022-11-23 22:01:07 +01:00
print("| Repository ".ljust(22) + " | Decision |")
print("| ".ljust(22, '-') + " | -------- |")
2022-11-26 20:45:17 +01:00
# For each repositories belonging to the bot (user `u`), assume we will not delete it
2022-11-23 22:01:07 +01:00
for repo in u.get_repos():
delete = False
2022-11-26 20:45:17 +01:00
# Proceed iff the repository is a fork (`parent` key is set) of a repository in our apps organization
2022-11-23 22:01:07 +01:00
if repo.parent.full_name.split('/')[0] == "YunoHost-Apps":
prs = []
2022-11-26 20:45:17 +01:00
# Build the list of PRs currently opened in the repository
# (the get_pulls method returns an iterable, not the full list)
2022-11-23 22:01:07 +01:00
for pr in repo.parent.get_pulls(state='open', sort='created'):
prs.append(pr)
2022-11-26 20:45:17 +01:00
# If none of the PRs are opened by the bot, delete the repository
2022-11-23 22:01:07 +01:00
if not any([ (pr.user == u) for pr in prs ]):
delete = True
else:
print("| "+repo.name.ljust(20) + " | Skipping |")
continue
if delete:
print("| "+repo.name.ljust(20) + " | Deleting |")
repo.delete()
else:
print("| "+repo.name.ljust(20) + " | Keeping |")