This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# 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. Inspecting changes..." | |
git diff | |
echo "Staging files..." | |
git add . | |
echo "Committing changes..." | |
git commit -m "Auto-format code with Black and isort" | |
echo "Pushing changes to branch..." | |
git push | |
echo "Push completed successfully." | |
else | |
echo "No changes detected. Nothing to commit or push." | |
validate-version: | |
if: github.event.pull_request.base.ref == 'main' && github.event.pull_request.head.ref == 'patch' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Extract version from patch branch | |
id: get_patch_version | |
run: | | |
grep -Eo 'VERSION=[0-9]+\.[0-9]+\.[0-9]+' <your-version-file> | cut -d'=' -f2 > patch_version.txt | |
echo "patch_version=$(cat patch_version.txt)" >> $GITHUB_ENV | |
- name: Extract version from main branch | |
id: get_main_version | |
run: | | |
git fetch origin main:main | |
git checkout main | |
grep -Eo 'VERSION=[0-9]+\.[0-9]+\.[0-9]+' <your-version-file> | cut -d'=' -f2 > main_version.txt | |
echo "main_version=$(cat main_version.txt)" >> $GITHUB_ENV | |
- name: Compare versions | |
id: compare_versions | |
run: | | |
IFS='.' read -r main_major main_minor main_patch <<< "$main_version" | |
IFS='.' read -r patch_major patch_minor patch_patch <<< "$patch_version" | |
if [[ "$main_major" -ne "$patch_major" || "$main_minor" -ne "$patch_minor" ]]; then | |
echo "::error::Major and minor versions must match between main and patch." | |
exit 1 | |
fi | |
if [[ $((main_patch + 1)) -ne "$patch_patch" ]]; then | |
echo "::warning::Patch version is not incremented by 1. Updating to match." | |
patch_patch=$((main_patch + 1)) | |
patch_version="$main_major.$main_minor.$patch_patch" | |
echo "patch_version=$patch_version" >> $GITHUB_ENV | |
echo "VERSION=$patch_version" > <your-version-file> | |
fi | |
echo "Version check passed with version $patch_version." | |
- name: Update version in README.md | |
id: update_readme | |
run: | | |
sed -i "1s/v$main_version/v$patch_version/" README.md | |
- name: Commit changes | |
if: success() | |
run: | | |
git config user.name "github-actions" | |
git config user.email "[email protected]" | |
git add README.md | |
git commit -m "Update version in README.md to $patch_version" | |
git push origin patch | |