From 94cdb2ca9e15f62e65d20cdf278d71341a696379 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 --- tools/bot-repo-cleanup/cleanup.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tools/bot-repo-cleanup/cleanup.py diff --git a/tools/bot-repo-cleanup/cleanup.py b/tools/bot-repo-cleanup/cleanup.py new file mode 100644 index 00000000..336ef9cc --- /dev/null +++ b/tools/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 e0200c729e56d63cc55267d5e3a9d3144067634d 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 --- tools/bot-repo-cleanup/cleanup.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/bot-repo-cleanup/cleanup.py b/tools/bot-repo-cleanup/cleanup.py index 336ef9cc..51f15a1e 100644 --- a/tools/bot-repo-cleanup/cleanup.py +++ b/tools/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 89f1da0ca3077d3f0719c760c03a51435244b85c 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 --- tools/bot-repo-cleanup/cleanup.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/tools/bot-repo-cleanup/cleanup.py b/tools/bot-repo-cleanup/cleanup.py index 51f15a1e..ac2944a4 100644 --- a/tools/bot-repo-cleanup/cleanup.py +++ b/tools/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 c87472b0e24aaaeb5122c6c89ec4d755e480ca12 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 --- tools/bot-repo-cleanup/cleanup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/bot-repo-cleanup/cleanup.py b/tools/bot-repo-cleanup/cleanup.py index ac2944a4..727ff90a 100644 --- a/tools/bot-repo-cleanup/cleanup.py +++ b/tools/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 1f1d5986da5bd229414f9f08a1087db78c4106aa 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 ++- tools/bot-repo-cleanup/cleanup.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 97f86544..ce6c63a1 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/tools/bot-repo-cleanup/cleanup.py b/tools/bot-repo-cleanup/cleanup.py index 727ff90a..f2275c61 100644 --- a/tools/bot-repo-cleanup/cleanup.py +++ b/tools/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