From 3d10c3dd0614df94c0dfa423eddca6d380fae157 Mon Sep 17 00:00:00 2001 From: Remco de Boer Date: Wed, 2 Jun 2021 13:11:08 +0200 Subject: [PATCH] feat: enforce auto-close milestone workflow (#22) * chore: move expected workflow files to subdir --- setup.cfg | 5 ++- .../check_dev_files/__init__.py | 2 + .../check_dev_files/auto_close_milestone.py | 38 +++++++++++++++++++ .../pin_requirements_scripts.py | 2 +- src/repoma/workflows/milestone.yml | 14 +++++++ .../{ => workflows}/requirements-cron.yml | 0 .../{ => workflows}/requirements-pr.yml | 0 7 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/repoma/pre_commit_hooks/check_dev_files/auto_close_milestone.py create mode 100644 src/repoma/workflows/milestone.yml rename src/repoma/{ => workflows}/requirements-cron.yml (100%) rename src/repoma/{ => workflows}/requirements-pr.yml (100%) diff --git a/setup.cfg b/setup.cfg index 2cd20027..fae41b6e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -82,5 +82,6 @@ repoma = .github/ISSUE_TEMPLATE/* .gitpod.yml py.typed - requirements-cron.yml - requirements-pr.yml + workflows/milestone.yml + workflows/requirements-cron.yml + workflows/requirements-pr.yml diff --git a/src/repoma/pre_commit_hooks/check_dev_files/__init__.py b/src/repoma/pre_commit_hooks/check_dev_files/__init__.py index 8e7e2c23..02981d68 100644 --- a/src/repoma/pre_commit_hooks/check_dev_files/__init__.py +++ b/src/repoma/pre_commit_hooks/check_dev_files/__init__.py @@ -6,6 +6,7 @@ from repoma.pre_commit_hooks.errors import PrecommitError +from . import auto_close_milestone from .check_labels import check_has_labels from .cspell_config import check_cspell_config from .editor_config_hook import check_editor_config_hook @@ -44,6 +45,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: args = parser.parse_args(argv) fix = not args.no_fix try: + auto_close_milestone.check_workflow_file() check_editor_config_hook() if not args.allow_labels: check_has_labels(fix) diff --git a/src/repoma/pre_commit_hooks/check_dev_files/auto_close_milestone.py b/src/repoma/pre_commit_hooks/check_dev_files/auto_close_milestone.py new file mode 100644 index 00000000..7747c56d --- /dev/null +++ b/src/repoma/pre_commit_hooks/check_dev_files/auto_close_milestone.py @@ -0,0 +1,38 @@ +# cspell:ignore mhutchie +"""Add a GitHub Action that auto-closes milestones on a new release. + +See `github.com/mhutchie/update-milestone-on-release +`_. +""" + + +import os + +from repoma.pre_commit_hooks.errors import PrecommitError + +__THIS_MODULE_DIR = os.path.abspath(os.path.dirname(__file__)) + + +def check_workflow_file() -> None: + github_workflow_dir = ".github/workflows" + workflow_file = "milestone.yml" + expected_workflow_path = os.path.abspath( + f"{__THIS_MODULE_DIR}/../../workflows/{workflow_file}" + ) + with open(expected_workflow_path) as stream: + expected_content = stream.read() + workflow_path = f"{github_workflow_dir}/{workflow_file}" + if not os.path.exists(workflow_path): + write_script(expected_content, path=workflow_path) + raise PrecommitError(f'Created "{workflow_path}" workflow') + + with open(workflow_path) as stream: + existing_content = stream.read() + if existing_content != expected_content: + write_script(expected_content, path=workflow_path) + raise PrecommitError(f'Updated "{workflow_path}" workflow') + + +def write_script(content: str, path: str) -> None: + with open(path, "w") as stream: + stream.write(content) diff --git a/src/repoma/pre_commit_hooks/check_dev_files/pin_requirements_scripts.py b/src/repoma/pre_commit_hooks/check_dev_files/pin_requirements_scripts.py index 1281b34d..23dccbba 100644 --- a/src/repoma/pre_commit_hooks/check_dev_files/pin_requirements_scripts.py +++ b/src/repoma/pre_commit_hooks/check_dev_files/pin_requirements_scripts.py @@ -52,7 +52,7 @@ def update_github_workflows() -> None: def upgrade_workflow(workflow_file: str) -> None: expected_workflow_path = os.path.abspath( - f"{__THIS_MODULE_DIR}/../../{workflow_file}" + f"{__THIS_MODULE_DIR}/../../workflows/{workflow_file}" ) with open(expected_workflow_path) as stream: expected_content = stream.read() diff --git a/src/repoma/workflows/milestone.yml b/src/repoma/workflows/milestone.yml new file mode 100644 index 00000000..c83f13dc --- /dev/null +++ b/src/repoma/workflows/milestone.yml @@ -0,0 +1,14 @@ +name: Close Milestone on Release + +on: + release: + types: [published] + +jobs: + update-milestone: + runs-on: ubuntu-latest + steps: + # cspell:ignore mhutchie + - uses: mhutchie/update-milestone-on-release@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/repoma/requirements-cron.yml b/src/repoma/workflows/requirements-cron.yml similarity index 100% rename from src/repoma/requirements-cron.yml rename to src/repoma/workflows/requirements-cron.yml diff --git a/src/repoma/requirements-pr.yml b/src/repoma/workflows/requirements-pr.yml similarity index 100% rename from src/repoma/requirements-pr.yml rename to src/repoma/workflows/requirements-pr.yml