mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 13:23:14 +01:00
Update update-currencies.yml
Upgrades to the latest Action versions (checkout@v3, setup-python@v4). Caches Python dependencies. Detects both “any file changes” and “empty array” in one step (so won’t ever commit an emty array). Replaces manual branch/CURL logic with peter-evans/create-pull-request, which handles branch creation, rebasing and PR creation in one go.
This commit is contained in:
116
.github/workflows/update-currencies.yml
vendored
116
.github/workflows/update-currencies.yml
vendored
@@ -4,97 +4,71 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
update-currencies:
|
update-currencies:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
# 1) Pull down code
|
||||||
uses: actions/checkout@v2
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Set up Python
|
# 2) Install Python + cache dependencies
|
||||||
uses: actions/setup-python@v2
|
- name: Set up Python 3.8
|
||||||
|
uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: '3.8'
|
python-version: '3.8'
|
||||||
|
cache: 'pip'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install requests
|
pip install requests
|
||||||
|
|
||||||
- name: Run currency fetch script
|
# 3) Run your update script
|
||||||
|
- name: Fetch & write currencies.json
|
||||||
run: python .github/scripts/update_currencies.py
|
run: python .github/scripts/update_currencies.py
|
||||||
|
|
||||||
- name: Check for changes
|
# 4) Detect both “any changes” and “is it an empty array?”
|
||||||
id: check_changes
|
- name: Check for meaningful updates
|
||||||
|
id: detect
|
||||||
run: |
|
run: |
|
||||||
if [[ $(git status --porcelain) ]]; then
|
# any git-tracked changes?
|
||||||
echo "Changes detected."
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
echo "changes=true" >> $GITHUB_ENV
|
echo "changes=true" >> $GITHUB_OUTPUT
|
||||||
else
|
else
|
||||||
echo "No changes detected."
|
echo "changes=false" >> $GITHUB_OUTPUT
|
||||||
echo "changes=false" >> $GITHUB_ENV
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Delete existing update-currencies branch
|
# are we looking at an empty JSON array?
|
||||||
run: |
|
empty=$(python - << 'PYCODE'
|
||||||
if git show-ref --verify --quiet refs/heads/update-currencies; then
|
import json
|
||||||
git branch -D update-currencies
|
data = json.load(open('backend/internal/core/currencies/currencies.json'))
|
||||||
echo "Deleted existing update-currencies branch."
|
print("true" if not data else "false")
|
||||||
else
|
PYCODE
|
||||||
echo "No existing update-currencies branch to delete."
|
)
|
||||||
fi
|
echo "empty=$empty" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Create new update-currencies branch
|
# 5) Only if we have changes AND the file isn’t just `[]`, open a PR
|
||||||
if: env.changes == 'true'
|
- name: Create or update Pull Request
|
||||||
run: |
|
if: steps.detect.outputs.changes == 'true' && steps.detect.outputs.empty == 'false'
|
||||||
git config --global user.name "github-actions[bot]"
|
uses: peter-evans/create-pull-request@v5
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
branch: update-currencies
|
||||||
|
base: main
|
||||||
|
title: "Update currencies.json"
|
||||||
|
body: |
|
||||||
|
This PR was opened automatically by our GitHub Action.
|
||||||
|
commit-message: "chore: update currencies.json"
|
||||||
|
path: backend/internal/core/currencies/currencies.json
|
||||||
|
committer: GitHub Actions <actions@github.com>
|
||||||
|
|
||||||
# Create a new branch
|
# 6) Otherwise, just log and exit
|
||||||
git checkout -b update-currencies
|
- name: No update required
|
||||||
git add backend/internal/core/currencies/currencies.json
|
if: steps.detect.outputs.changes == 'false' || steps.detect.outputs.empty == 'true'
|
||||||
git commit -m "Update currencies.json"
|
run: echo "No meaningful currency updates found (either no changes or got an empty array). Skipping PR."
|
||||||
|
|
||||||
# Fetch the latest changes from the remote
|
|
||||||
git fetch origin
|
|
||||||
|
|
||||||
# Attempt to rebase with the latest changes
|
|
||||||
if git show-ref --verify --quiet refs/remotes/origin/update-currencies; then
|
|
||||||
if ! git rebase origin/update-currencies; then
|
|
||||||
echo "Rebase conflicts occurred. Please resolve them manually."
|
|
||||||
echo "To resolve conflicts, check out the 'update-currencies' branch locally."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "No existing remote branch 'update-currencies'. Skipping rebase."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Push the new branch to the remote
|
|
||||||
if ! git push --set-upstream origin update-currencies; then
|
|
||||||
echo "Push failed, trying to fetch and rebase again."
|
|
||||||
git fetch origin
|
|
||||||
if git show-ref --verify --quiet refs/remotes/origin/update-currencies; then
|
|
||||||
if ! git rebase origin/update-currencies; then
|
|
||||||
echo "Second rebase failed. Please resolve manually."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "No existing remote branch 'update-currencies'. Skipping rebase."
|
|
||||||
fi
|
|
||||||
if ! git push --set-upstream origin update-currencies; then
|
|
||||||
echo "Second push failed. Please resolve manually."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create a pull request
|
|
||||||
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
|
||||||
-X POST \
|
|
||||||
-d '{"title": "Update currencies", "head": "update-currencies", "base": "main"}' \
|
|
||||||
https://api.github.com/repos/${{ github.repository }}/pulls
|
|
||||||
|
|
||||||
- name: Notify no changes
|
|
||||||
if: env.changes == 'false'
|
|
||||||
run: echo "Currencies up-to-date with API, skipping commit."
|
|
||||||
|
|||||||
Reference in New Issue
Block a user