diff --git a/.github/workflows/update-currencies.yml b/.github/workflows/update-currencies.yml index 1b08a4bc..2a17e300 100644 --- a/.github/workflows/update-currencies.yml +++ b/.github/workflows/update-currencies.yml @@ -4,97 +4,71 @@ on: push: branches: - main + workflow_dispatch: jobs: update-currencies: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v2 + # 1) Pull down code + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 - - name: Set up Python - uses: actions/setup-python@v2 + # 2) Install Python + cache dependencies + - name: Set up Python 3.8 + uses: actions/setup-python@v4 with: python-version: '3.8' + cache: 'pip' - name: Install dependencies run: | python -m pip install --upgrade pip 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 - - name: Check for changes - id: check_changes + # 4) Detect both “any changes” and “is it an empty array?” + - name: Check for meaningful updates + id: detect run: | - if [[ $(git status --porcelain) ]]; then - echo "Changes detected." - echo "changes=true" >> $GITHUB_ENV + # any git-tracked changes? + if [ -n "$(git status --porcelain)" ]; then + echo "changes=true" >> $GITHUB_OUTPUT else - echo "No changes detected." - echo "changes=false" >> $GITHUB_ENV + echo "changes=false" >> $GITHUB_OUTPUT fi - - name: Delete existing update-currencies branch - run: | - if git show-ref --verify --quiet refs/heads/update-currencies; then - git branch -D update-currencies - echo "Deleted existing update-currencies branch." - else - echo "No existing update-currencies branch to delete." - fi + # are we looking at an empty JSON array? + empty=$(python - << 'PYCODE' +import json +data = json.load(open('backend/internal/core/currencies/currencies.json')) +print("true" if not data else "false") +PYCODE +) + echo "empty=$empty" >> $GITHUB_OUTPUT - - name: Create new update-currencies branch - if: env.changes == 'true' - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" + # 5) Only if we have changes AND the file isn’t just `[]`, open a PR + - name: Create or update Pull Request + if: steps.detect.outputs.changes == 'true' && steps.detect.outputs.empty == 'false' + uses: peter-evans/create-pull-request@v5 + 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 - # Create a new branch - git checkout -b update-currencies - git add backend/internal/core/currencies/currencies.json - git commit -m "Update currencies.json" - - # 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." + # 6) Otherwise, just log and exit + - name: No update required + if: steps.detect.outputs.changes == 'false' || steps.detect.outputs.empty == 'true' + run: echo "No meaningful currency updates found (either no changes or got an empty array). Skipping PR."