From fe2dd13c93e2c7784abc99bc1b42775cacbd7021 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Wed, 23 Nov 2022 22:01:07 +0100 Subject: [PATCH 1/5] Add yunohost-bot forks cleanup script --- bot-repo-cleanup/cleanup.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 bot-repo-cleanup/cleanup.py diff --git a/bot-repo-cleanup/cleanup.py b/bot-repo-cleanup/cleanup.py new file mode 100644 index 0000000..336ef9c --- /dev/null +++ b/bot-repo-cleanup/cleanup.py @@ -0,0 +1,28 @@ +#!venv/bin/python3 + +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") + +print("| Repository ".ljust(22) + " | Decision |") +print("| ".ljust(22, '-') + " | -------- |") + +for repo in u.get_repos(): + delete = False + if repo.parent.full_name.split('/')[0] == "YunoHost-Apps": + prs = [] + for pr in repo.parent.get_pulls(state='open', sort='created'): + prs.append(pr) + 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 |") From c2de636ba44b12aed3fabb974928c3dc0568c2e2 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Sat, 26 Nov 2022 20:45:17 +0100 Subject: [PATCH 2/5] Comment the bot-repo-cleanup script --- bot-repo-cleanup/cleanup.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bot-repo-cleanup/cleanup.py b/bot-repo-cleanup/cleanup.py index 336ef9c..51f15a1 100644 --- a/bot-repo-cleanup/cleanup.py +++ b/bot-repo-cleanup/cleanup.py @@ -1,5 +1,6 @@ #!venv/bin/python3 +# Obtained with `pip install PyGithub`, better within a venv from github import Github from github.Workflow import Workflow @@ -7,15 +8,21 @@ from github.Workflow import Workflow g = Github("TOKEN_REPLACE_ME") u = g.get_user("yunohost-bot") +# Let's build a minimalistic summary table print("| Repository ".ljust(22) + " | Decision |") print("| ".ljust(22, '-') + " | -------- |") +# For each repositories belonging to the bot (user `u`), assume we will not delete it for repo in u.get_repos(): delete = False + # Proceed iff the repository is a fork (`parent` key is set) of a repository in our apps organization if repo.parent.full_name.split('/')[0] == "YunoHost-Apps": prs = [] + # Build the list of PRs currently opened in the repository + # (the get_pulls method returns an iterable, not the full list) for pr in repo.parent.get_pulls(state='open', sort='created'): prs.append(pr) + # If none of the PRs are opened by the bot, delete the repository if not any([ (pr.user == u) for pr in prs ]): delete = True else: From 2dca5670def90f680a07069529ba768d47fa3835 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Sat, 26 Nov 2022 20:53:50 +0100 Subject: [PATCH 3/5] Optimize the bot-repo-cleanup script --- bot-repo-cleanup/cleanup.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/bot-repo-cleanup/cleanup.py b/bot-repo-cleanup/cleanup.py index 51f15a1..ac2944a 100644 --- a/bot-repo-cleanup/cleanup.py +++ b/bot-repo-cleanup/cleanup.py @@ -12,24 +12,16 @@ u = g.get_user("yunohost-bot") print("| Repository ".ljust(22) + " | Decision |") print("| ".ljust(22, '-') + " | -------- |") -# For each repositories belonging to the bot (user `u`), assume we will not delete it +# For each repositories belonging to the bot (user `u`) for repo in u.get_repos(): - delete = False # Proceed iff the repository is a fork (`parent` key is set) of a repository in our apps organization - if repo.parent.full_name.split('/')[0] == "YunoHost-Apps": - prs = [] - # Build the list of PRs currently opened in the repository - # (the get_pulls method returns an iterable, not the full list) - for pr in repo.parent.get_pulls(state='open', sort='created'): - prs.append(pr) - # If none of the PRs are opened by the bot, delete the repository - if not any([ (pr.user == u) for pr in prs ]): - delete = True - else: + if repo.parent.full_name.split('/')[0] != "YunoHost-Apps": print("| "+repo.name.ljust(20) + " | Skipping |") continue - if delete: - print("| "+repo.name.ljust(20) + " | Deleting |") - repo.delete() else: - print("| "+repo.name.ljust(20) + " | Keeping |") + # If none of the PRs are opened by the bot, delete the repository + if not any([ (pr.user == u) for pr in list(repo.parent.get_pulls(state='open', sort='created')) ]): + print("| "+repo.name.ljust(20) + " | Deleting |") + repo.delete() + else: + print("| "+repo.name.ljust(20) + " | Keeping |") From 096752fa286e01a65f329b79f7f521cc98bb4b9e Mon Sep 17 00:00:00 2001 From: tituspijean Date: Sat, 26 Nov 2022 21:03:19 +0100 Subject: [PATCH 4/5] Remove superfluous continue clause in bot-repo-cleanup --- bot-repo-cleanup/cleanup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot-repo-cleanup/cleanup.py b/bot-repo-cleanup/cleanup.py index ac2944a..727ff90 100644 --- a/bot-repo-cleanup/cleanup.py +++ b/bot-repo-cleanup/cleanup.py @@ -17,7 +17,6 @@ for repo in u.get_repos(): # Proceed iff the repository is a fork (`parent` key is set) of a repository in our apps organization if repo.parent.full_name.split('/')[0] != "YunoHost-Apps": print("| "+repo.name.ljust(20) + " | Skipping |") - continue else: # If none of the PRs are opened by the bot, delete the repository if not any([ (pr.user == u) for pr in list(repo.parent.get_pulls(state='open', sort='created')) ]): From 8960287b22ae1af98dea8ad2c795971dd703abb6 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Thu, 29 Dec 2022 14:50:32 +0100 Subject: [PATCH 5/5] Add a file for storing the token --- .gitignore | 3 ++- bot-repo-cleanup/cleanup.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 97f8654..ce6c63a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .apps_cache builds tools/README-generator/venv/ +tools/bot-repo-cleanup/.github_token tools/autopatches/login -tools/autopatches/token \ No newline at end of file +tools/autopatches/token diff --git a/bot-repo-cleanup/cleanup.py b/bot-repo-cleanup/cleanup.py index 727ff90..f2275c6 100644 --- a/bot-repo-cleanup/cleanup.py +++ b/bot-repo-cleanup/cleanup.py @@ -5,7 +5,7 @@ from github import Github from github.Workflow import Workflow # API token for yunohost-bot, with "delete_repo" right -g = Github("TOKEN_REPLACE_ME") +g = Github(open(".github_token").read().strip()) u = g.get_user("yunohost-bot") # Let's build a minimalistic summary table