Skip to content

Workflow file for this run

name: PR Validation and Auto-formatting
on:
pull_request:
jobs:
auto-format:
name: Auto-format and Push Changes
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout Repository
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
# Step 3: Install tools (black and isort)
- name: Install Tools
run: |
echo "Installing tools..."
python -m pip install --upgrade pip
pip install black isort
# Step 4: Run black and isort to format the code
- name: Run Black and isort
run: |
echo "Running black..."
black --line-length=119 .
echo "Running isort..."
isort .
# Step 5: Commit and push changes if any files were modified
- name: Commit and Push Changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Configuring git user..."
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
echo "Checking for changes..."
git status
if [ -n "$(git status --porcelain)" ]; then
echo "Changes detected. Staging files..."
git add .
echo "Committing changes..."
git commit -m "Auto-format code with Black and isort"
echo "Pushing changes to the branch..."
git push origin ${{ github.head_ref }}
echo "Push completed successfully."
else
echo "No changes detected. Nothing to commit or push."
fi
validate-version:
needs: auto-format
if: github.event.pull_request.base.ref == 'main' && github.event.pull_request.head.ref == 'patch'
name: Validate and Update Version
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Fetch main branch
run: git fetch origin main:main
- name: Fetch main branch into a temporary branch
run: |
git fetch origin main
git checkout -b temp-main origin/main
- name: Get VERSION from patch branch
id: get_patch_version
run: |
VERSION=$(grep -m 1 -oP '(?<=^VERSION = ")[^"]+' apps/pv_opt/pv_opt.py)
if [ -z "$VERSION" ]; then
echo "Error: VERSION not found in apps/pv_opt/pv_opt.py on patch branch." >&2
exit 1
fi
echo "patch_version=$VERSION" >> $GITHUB_ENV
- name: Get VERSION from main branch
id: get_main_version
run: |
git checkout temp-main
VERSION=$(grep -m 1 -oP '(?<=^VERSION = ")[^"]+' apps/pv_opt/pv_opt.py)
if [ -z "$VERSION" ]; then
echo "Error: VERSION not found in apps/pv_opt/pv_opt.py on main branch." >&2
exit 1
fi
echo "main_version=$VERSION" >> $GITHUB_ENV
- name: Validate or Fix Version Increment
id: validate_or_fix_version
run: |
patch_version=$patch_version
main_version=$main_version
# Extract PATCH numbers
main_patch=$(echo "$main_version" | awk -F '.' '{print $3}')
patch_patch=$(echo "$patch_version" | awk -F '.' '{print $3}')
# Check if the patch version is incremented correctly
if [ "$patch_patch" -ne $((main_patch + 1)) ]; then
echo "Warning: PATCH version is not incremented correctly. Fixing..."
new_patch_version=$(echo "$main_version" | awk -F '.' '{print $1"."$2"."($3+1)}')
sed -i "s/^VERSION = \".*\"/VERSION = \"$new_patch_version\"/" apps/pv_opt/pv_opt.py
echo "Corrected version to $new_patch_version."
echo "patch_version=$new_patch_version" >> $GITHUB_ENV
fi
- name: Update README.md version
run: |
sed -i "1s/v[0-9]*\.[0-9]*\.[0-9]*/v$patch_version/" README.md
commit-changes:
needs:
- auto-format
- validate-version
name: Commit and Push All Changes
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Configure Git User
run: |
git config user.name "github-actions"
git config user.email "[email protected]"
- name: Pull Latest Changes
run: |
git checkout -b patch || git checkout patch
git pull origin patch --rebase
- name: Commit All Changes
run: |
git add README.md apps/pv_opt/pv_opt.py
git commit --amend --no-edit || echo "No changes to commit"
- name: Push Changes
run: |
git push --force-with-lease --set-upstream origin patch
# - name: Commit changes
# if: success()
# run: |
# echo "Configuring git user..."
# git config user.name "github-actions"
# git config user.email "[email protected]"
# echo "Checking out the branch..."
# git checkout -b patch || git checkout patch
# echo "Staging all changes..."
# git add README.md apps/pv_opt/pv_opt.py
# echo "Committing temporary changes..."
# git commit -m "Temporary commit for pull rebase" || echo "No changes to commit"
# echo "Pulling latest changes to avoid conflicts..."
# git pull origin patch --rebase
# echo "Amending commit with updated changes..."
# git add README.md apps/pv_opt/pv_opt.py
# git commit --amend --no-edit
# echo "Pushing changes to the remote branch..."
# git push --force-with-lease --set-upstream origin patch