Skip to content

Commit

Permalink
fix auto updater
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirroni committed Jun 25, 2024
1 parent a6c4028 commit a558f99
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 43 deletions.
58 changes: 58 additions & 0 deletions .github/scripts/update_requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -e

PYTHON_VERSIONS=("3.7" "3.8" "3.9" "3.10" "3.11" "3.12")
UPDATED_FILES=()
DATE=$(date -u +'%Y-%m-%d')

for version in "${PYTHON_VERSIONS[@]}"; do
echo "Processing Python $version"

pyenv install -s $version
pyenv virtualenv -f $version venv_$version
pyenv activate venv_$version

pip install pru

checksum_before_single=$(md5sum pytests/requirements/${version}/requirements_single.txt | cut -d ' ' -f 1)
checksum_before_mix=$(md5sum pytests/requirements/${version}/requirements_mix.txt | cut -d ' ' -f 1)

pru -r pytests/requirements/${version}/requirements_single.txt
pru -r pytests/requirements/${version}/requirements_mix.txt

checksum_after_single=$(md5sum pytests/requirements/${version}/requirements_single.txt | cut -d ' ' -f 1)
checksum_after_mix=$(md5sum pytests/requirements/${version}/requirements_mix.txt | cut -d ' ' -f 1)

if [ "$checksum_before_single" != "$checksum_after_single" ]; then
UPDATED_FILES+=("pytests/requirements/${version}/requirements_single.txt")
fi
if [ "$checksum_before_mix" != "$checksum_after_mix" ]; then
UPDATED_FILES+=("pytests/requirements/${version}/requirements_mix.txt")
fi

pyenv deactivate
pyenv uninstall -f venv_$version
done

if [ ${#UPDATED_FILES[@]} -ne 0 ]; then
echo "Requirements updated. Creating pull request."
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"

BRANCH_NAME="update-requirements-${GITHUB_RUN_ID}"
git checkout -b $BRANCH_NAME

git add ${UPDATED_FILES[@]}
git commit -m "Update requirements based on failed tests
- Updated files: ${UPDATED_FILES[@]}
- Date: $DATE"

git push origin $BRANCH_NAME

gh pr create --title "Update requirements for Python versions on $DATE" \
--body "This PR updates the following requirements files based on the output of failed tests:\n- ${UPDATED_FILES[@]}\nDate of update: $DATE" \
--label "update, automated-pr"
else
echo "No requirements updated."
fi
51 changes: 8 additions & 43 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: CI

on:
pull_request:
Expand Down Expand Up @@ -45,19 +45,16 @@ jobs:
- name: Upgrade pip and install dependencies
run: |
python -m pip install --upgrade pip
pip install pru
pip install pru -e ."[dev]"
pru
- name: Install package
run: pip install -e ."[dev]"

- name: Test package
- name: Run tests
id: pytest
run: |
python3 -m pytest . -c pyproject.toml --cov-report term-missing --cov=src/pru
continue-on-error: true

- name: Test package 2
- name: Run tests with last failed
id: pytest2
if: steps.pytest.outcome != 'success'
run: |
Expand All @@ -69,44 +66,12 @@ jobs:
if: steps.pytest.outcome != 'success' && steps.pytest2.outcome != 'success'
run: |
python_version_minor=$(python -c "import sys; print(f'{sys.version_info.minor}')")
checksum_before_single=$(md5sum pytests/requirements/3_${python_version_minor}/requirements_single.txt | cut -d ' ' -f 1)
checksum_before_mix=$(md5sum pytests/requirements/3_${python_version_minor}/requirements_mix.txt | cut -d ' ' -f 1)
pru -r pytests/requirements/3_${python_version_minor}/requirements_single.txt
pru -r pytests/requirements/3_${python_version_minor}/requirements_mix.txt
checksum_after_single=$(md5sum pytests/requirements/3_${python_version_minor}/requirements_single.txt | cut -d ' ' -f 1)
checksum_after_mix=$(md5sum pytests/requirements/3_${python_version_minor}/requirements_mix.txt | cut -d ' ' -f 1)
updated_files=""
if [ "$checksum_before_single" != "$checksum_after_single" ]; then
updated_files="pytests/requirements/3_${python_version_minor}/requirements_single.txt"
fi
if [ "$checksum_before_mix" != "$checksum_after_mix" ]; then
if [ -n "$updated_files" ]; then
updated_files="$updated_files, "
fi
updated_files="${updated_files}pytests/requirements/3_${python_version_minor}/requirements_mix.txt"
fi
echo "::set-output name=updated::$(if [ "$updated_files" ]; then echo true; else echo false; fi)"
echo "::set-output name=updated_files::$updated_files"
echo "::set-output name=update_date::$(date -u +'%Y-%m-%d')"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Pull Request if requirements updated
if: steps.run_pru.outputs.updated == 'true'
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: |
Update requirements based on failed tests
- Updated files: ${{ steps.run_pru.outputs.updated_files }}
- Date: ${{ steps.run_pru.outputs.update_date }}
branch: update-requirements-${{ matrix.python-version }}-${{ github.run_id }}
title: 'Update requirements for Python ${{ matrix.python-version }} on ${{ steps.run_pru.outputs.update_date }}'
body: |
This PR updates the following requirements files based on the output of failed tests:
- ${{ steps.run_pru.outputs.updated_files }}
Date of update: ${{ steps.run_pru.outputs.update_date }}
labels: update, automated-pr
echo "Contents of pytests/requirements/3_${python_version_minor}/requirements_single.txt:"
cat pytests/requirements/3_${python_version_minor}/requirements_single.txt
echo "Contents of pytests/requirements/3_${python_version_minor}/requirements_mix.txt:"
cat pytests/requirements/3_${python_version_minor}/requirements_mix.txt
- name: Fail the job if tests fail
if: steps.pytest.outcome != 'success' && steps.pytest2.outcome != 'success'
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Update Requirements

on:
pull_request:

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8 # Any default version for running the script

- name: Check out repository
uses: actions/checkout@v4

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pru
- name: Run requirements updater script
run: |
bash .github/scripts/update_requirements.sh
env:
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}

0 comments on commit a558f99

Please sign in to comment.