From 396329f4b6bd4393ae846736e0694b984c4083ba Mon Sep 17 00:00:00 2001 From: mQzLjP <91550006+mQzLjP@users.noreply.github.com> Date: Sat, 17 May 2025 08:04:44 +0800 Subject: [PATCH] feat: archive repos by regex (#54) --- README.md | 4 ++-- joint_teapot/app.py | 8 +++++--- joint_teapot/workers/gitea.py | 16 +++++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9bc4b35..9b0f9da 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,9 @@ pytest -svv ## Commands & Features -### `archive-all-repos` +### `archive-repos` -archive all repos in gitea organization +archive repos in gitea organization according to regex (dry-run enabled by default) ### `unwatch-all-repos` diff --git a/joint_teapot/app.py b/joint_teapot/app.py index c5a975b..ab70c74 100644 --- a/joint_teapot/app.py +++ b/joint_teapot/app.py @@ -141,9 +141,11 @@ def close_all_issues() -> None: tea.pot.gitea.close_all_issues() -@app.command("archive-all-repos", help="archive all repos in gitea organization") -def archive_all_repos() -> None: - tea.pot.gitea.archive_all_repos() +@app.command( + "archive-repos", help="archive repos in gitea organization according to regex" +) +def archive_repos(regex: str = Argument(".+"), dry_run: bool = Option(True)) -> None: + tea.pot.gitea.archive_repos(regex, dry_run) @app.command("unwatch-all-repos", help="unwatch all repos in gitea organization") diff --git a/joint_teapot/workers/gitea.py b/joint_teapot/workers/gitea.py index 36ff7ed..0667ca8 100644 --- a/joint_teapot/workers/gitea.py +++ b/joint_teapot/workers/gitea.py @@ -413,11 +413,17 @@ class Gitea: self.org_name, repo_name, issue.number, body={"state": "closed"} ) - def archive_all_repos(self) -> None: - for repo in list_all(self.organization_api.org_list_repos, self.org_name): - self.repository_api.repo_edit( - self.org_name, repo.name, body={"archived": True} - ) + def archive_repos(self, regex: str = ".+", dry_run: bool = True) -> None: + if dry_run: + logger.info("Dry run enabled. No changes will be made to the repositories.") + logger.info(f"Archiving repos with name matching {regex}") + for repo_name in self.get_all_repo_names(): + if re.match(regex, repo_name): + logger.info(f"Archived {repo_name}") + if not dry_run: + self.repository_api.repo_edit( + self.org_name, repo_name, body={"archived": True} + ) def unwatch_all_repos(self) -> None: for repo in list_all(self.organization_api.org_list_repos, self.org_name):