From 8a0867866dc7a73c24c1e29bec20c084aaec291d Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Wed, 26 Jun 2024 05:28:54 +0700 Subject: [PATCH] add auto updater --- .github/scripts/update_requirements.sh | 73 ++++++++++++++++++++++++ .github/workflows/update.yml | 78 ++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 .github/scripts/update_requirements.sh create mode 100644 .github/workflows/update.yml diff --git a/.github/scripts/update_requirements.sh b/.github/scripts/update_requirements.sh new file mode 100644 index 0000000..4bbc593 --- /dev/null +++ b/.github/scripts/update_requirements.sh @@ -0,0 +1,73 @@ +#!/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') + +# Function to install Python if not already installed +install_python() { + local version="$1" + + if ! command -v python${version} &> /dev/null; then + echo "Python ${version} not found. Installing..." + sudo apt-get update + sudo apt-get install -y python${version} + else + echo "Python ${version} is already installed." + fi +} + +for version in "${PYTHON_VERSIONS[@]}"; do + echo "Processing Python $version" + + # Install Python if not already installed + install_python "$version" + + # Create virtual environment specific to the current Python version + python${version} -m venv "env_${version}" + source "env_${version}/bin/activate" + + # Ensure pip is up-to-date within the virtual environment + pip install --upgrade pip + + # Install pru into the virtual environment + pip install pru + + # Calculate checksums before running pru + minor_version=$(python${version} -c "import sys; print(f'{sys.version_info.minor}')") + checksum_before_single=$(md5sum "pytests/requirements/3_${minor_version}/requirements_single_updated.txt" | cut -d ' ' -f 1) + checksum_before_mix=$(md5sum "pytests/requirements/3_${minor_version}/requirements_mix_updated.txt" | cut -d ' ' -f 1) + + # Run pru to update requirements within the virtual environment + pru -r "pytests/requirements/3_${minor_version}/requirements_single_updated.txt" + pru -r "pytests/requirements/3_${minor_version}/requirements_mix_updated.txt" + + # Calculate checksums after running pru + checksum_after_single=$(md5sum "pytests/requirements/3_${minor_version}/requirements_single_updated.txt" | cut -d ' ' -f 1) + checksum_after_mix=$(md5sum "pytests/requirements/3_${minor_version}/requirements_mix_updated.txt" | cut -d ' ' -f 1) + + # Check if any requirements file was updated + if [ "$checksum_before_single" != "$checksum_after_single" ]; then + UPDATED_FILES+=("pytests/requirements/3_${minor_version}/requirements_single_updated.txt") + fi + if [ "$checksum_before_mix" != "$checksum_after_mix" ]; then + UPDATED_FILES+=("pytests/requirements/3_${minor_version}/requirements_mix_updated.txt") + fi + + # Deactivate the virtual environment + deactivate + # Remove the virtual environment directory + rm -rf "env_${version}" +done + +if [ ${#UPDATED_FILES[@]} -ne 0 ]; then + echo "Requirements updated. Creating pull request." + echo "::set-output name=updated::true" + echo "::set-output name=updated_files::${UPDATED_FILES[*]}" + echo "::set-output name=update_date::$DATE" +else + echo "No requirements updated." + echo "::set-output name=updated::false" +fi diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml new file mode 100644 index 0000000..c60bdd3 --- /dev/null +++ b/.github/workflows/update.yml @@ -0,0 +1,78 @@ +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.7 + + - name: Check out repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pru + + - name: Determine current branch + id: current_branch + run: echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV + + - name: Run requirements updater script + id: run_pru + run: | + bash .github/scripts/update_requirements.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Configure git + if: steps.run_pru.outputs.updated == 'true' + run: | + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git config --global user.name "github-actions[bot]" + + - name: Create new branch + if: steps.run_pru.outputs.updated == 'true' + run: | + BRANCH_NAME="update-requirements-${GITHUB_RUN_ID}" + git checkout -b $BRANCH_NAME + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + echo "DATE=${{ steps.run_pru.outputs.update_date }}" >> $GITHUB_ENV + + - name: Commit changes + if: steps.run_pru.outputs.updated == 'true' + run: | + git add . + git commit -m "Update requirements based on failed tests at $DATE" + env: + DATE: ${{ steps.run_pru.outputs.update_date }} + + - name: Push changes + if: steps.run_pru.outputs.updated == 'true' + run: | + git push origin $BRANCH_NAME + env: + BRANCH_NAME: ${{ env.BRANCH_NAME }} + + - name: Create Pull Request + 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-${{ github.run_id }} + title: 'Update requirements 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