Skip to content

Commit

Permalink
ci: Created a workflow to update python linting dependencies and pre-…
Browse files Browse the repository at this point in the history
…commit hook versions inside of a PR. (#146)
  • Loading branch information
nfelt14 authored Feb 15, 2024
1 parent 5369d67 commit 7db20ba
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/update-python-and-pre-commit-dependencies.yml
Original file line number Diff line number Diff line change
@@ -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' && contains(github.head_ref, '/pip/') }}
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 }}>
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 29 additions & 2 deletions scripts/update_development_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -44,17 +62,26 @@ 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")
latest_dependency_versions.append(dependency + f"=={latest_dep_version}")

# 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"')
Expand Down

0 comments on commit 7db20ba

Please sign in to comment.