Skip to content

Commit

Permalink
Merge pull request #18 from yasirroni/git_auto_updater
Browse files Browse the repository at this point in the history
Git auto updater
  • Loading branch information
yasirroni authored Jun 25, 2024
2 parents 346a3ae + aeb2729 commit bafae85
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 19 deletions.
83 changes: 83 additions & 0 deletions .github/scripts/update_requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/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 update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install -y python${version} python${version}-venv
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"

# Install pip using get-pip.py if it's not already installed
if ! command -v pip &> /dev/null; then
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
fi

# 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ jobs:
pip install -e ."[dev]"
pru
- 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 Down
61 changes: 61 additions & 0 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: update

on:
pull_request:

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- 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 }}
6 changes: 3 additions & 3 deletions pytests/requirements/3_10/requirements_mix_updated.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.32.2
numpy==1.26.4
requests==2.32.3
numpy==2.0.0
pandas==2.2.2
scipy==1.13.1
scipy==1.14.0
2 changes: 1 addition & 1 deletion pytests/requirements/3_10/requirements_single_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.32.2
requests==2.32.3
6 changes: 3 additions & 3 deletions pytests/requirements/3_11/requirements_mix_updated.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.32.2
numpy==1.26.4
requests==2.32.3
numpy==2.0.0
pandas==2.2.2
scipy==1.13.1
scipy==1.14.0
2 changes: 1 addition & 1 deletion pytests/requirements/3_11/requirements_single_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.32.2
requests==2.32.3
6 changes: 3 additions & 3 deletions pytests/requirements/3_12/requirements_mix_updated.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.32.2
numpy==1.26.4
requests==2.32.3
numpy==2.0.0
pandas==2.2.2
scipy==1.13.1
scipy==1.14.0
2 changes: 1 addition & 1 deletion pytests/requirements/3_12/requirements_single_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.32.2
requests==2.32.3
2 changes: 1 addition & 1 deletion pytests/requirements/3_8/requirements_mix_updated.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.32.2
requests==2.32.3
numpy==1.24.4
pandas==2.0.3
scipy==1.10.1
2 changes: 1 addition & 1 deletion pytests/requirements/3_8/requirements_single_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.32.2
requests==2.32.3
4 changes: 2 additions & 2 deletions pytests/requirements/3_9/requirements_mix_updated.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.32.2
numpy==1.26.4
requests==2.32.3
numpy==2.0.0
pandas==2.2.2
scipy==1.13.1
2 changes: 1 addition & 1 deletion pytests/requirements/3_9/requirements_single_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.32.2
requests==2.32.3

0 comments on commit bafae85

Please sign in to comment.