From 266d3c461ab9d9e6d9deb8f3fa5ab2e54b408071 Mon Sep 17 00:00:00 2001 From: Nicholas Felt Date: Wed, 14 Feb 2024 15:08:24 -0800 Subject: [PATCH] ci: Created a workflow to update python linting dependencies and pre-commit hook versions inside of a PR. --- ...ate-python-and-pre-commit-dependencies.yml | 39 +++++++++++++++++++ .pre-commit-config.yaml | 2 +- scripts/update_development_dependencies.py | 31 ++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/update-python-and-pre-commit-dependencies.yml diff --git a/.github/workflows/update-python-and-pre-commit-dependencies.yml b/.github/workflows/update-python-and-pre-commit-dependencies.yml new file mode 100644 index 00000000..8e08ce9c --- /dev/null +++ b/.github/workflows/update-python-and-pre-commit-dependencies.yml @@ -0,0 +1,39 @@ +--- +name: Update python linting dependencies in-sync with pre-commit +on: + pull_request: + branches: [main] +jobs: + update-python-and-pre-commit-deps: + name: Update python linters and pre-commit dependencies + runs-on: ubuntu-latest + if: github.event.pull_request.user.login == 'dependabot' + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.head_ref }} + token: ${{ secrets.TEK_OPENSOURCE_TOKEN }} + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: x # any version + check-latest: true + - name: Install workflow dependencies + run: pip install poetry yamlfix toml-sort requests pre-commit-update + - uses: crazy-max/ghaction-import-gpg@v6 + with: + gpg_private_key: ${{ secrets.TEK_OPENSOURCE_GPG_SIGNING_KEY_PRIVATE }} + passphrase: ${{ secrets.TEK_OPENSOURCE_GPG_SIGNING_KEY_PASSPHRASE }} + git_user_signingkey: true + git_commit_gpgsign: true + - name: Run updater script + run: python scripts/update_development_dependencies.py --no-install + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: 'ci: Update python linters and pre-commit dependencies.' + commit_user_name: ${{ vars.TEK_OPENSOURCE_NAME }} + commit_user_email: ${{ vars.TEK_OPENSOURCE_EMAIL }} + commit_author: ${{ vars.TEK_OPENSOURCE_NAME }} <${{ vars.TEK_OPENSOURCE_EMAIL }}> diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7a5e7571..52903232 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,7 +29,7 @@ repos: - id: pretty-format-json args: [--autofix, --indent=4] - repo: https://github.com/Lucas-C/pre-commit-hooks - rev: v1.5.4 + rev: v1.5.5 hooks: - id: remove-tabs - id: forbid-tabs diff --git a/scripts/update_development_dependencies.py b/scripts/update_development_dependencies.py index 009916bf..8c92a763 100644 --- a/scripts/update_development_dependencies.py +++ b/scripts/update_development_dependencies.py @@ -3,6 +3,7 @@ This script will update the development dependencies that are pinned in the pyproject.toml and .pre- commit-config.yaml files. """ +import argparse import shlex import subprocess import sys @@ -25,6 +26,23 @@ ) +def parse_arguments() -> argparse.Namespace: + """Parse the command line arguments. + + Returns: + The parsed Namespace. + """ + parser = argparse.ArgumentParser() + parser.add_argument( + "--no-install", + action="store_true", + dest="no_install", + help="Indicate if packages should not be installed via poetry (Primarily used in CI).", + ) + + return parser.parse_args() + + def _run_cmd_in_subprocess(command: str) -> None: """Run the given command in a subprocess. @@ -44,6 +62,9 @@ def main() -> None: repository_root_directory = script_location.parent.parent latest_dependency_versions: List[str] = [] + args = parse_arguments() + lock_only = args.no_install + # Get the latest versions for each of the dependencies to update for dependency in DEPENDENCIES_TO_UPDATE: latest_dep_version = get_latest_version(dependency.split("[", maxsplit=1)[0], "pypi") @@ -51,10 +72,16 @@ def main() -> None: # Update dependencies in pyproject.toml using poetry dependencies = " ".join(f'"{x}"' for x in latest_dependency_versions) - _run_cmd_in_subprocess(f'"{python_executable}" -m poetry add --group=dev {dependencies}') + poetry_add_cmd = f'"{python_executable}" -m poetry add --group=dev {dependencies}' + if lock_only: + poetry_add_cmd += " --lock" + _run_cmd_in_subprocess(poetry_add_cmd) # Run poetry update - _run_cmd_in_subprocess(f'"{python_executable}" -m poetry update') + poetry_update_cmd = f'"{python_executable}" -m poetry update' + if lock_only: + poetry_update_cmd += " --lock" + _run_cmd_in_subprocess(poetry_update_cmd) # Update pre-commit config file _run_cmd_in_subprocess(f'"{python_script_location}/pre-commit-update"')