name: "Update challenges on owasp.org/www-project-juice-shop (legacy)"

on:
  push:
    branches: [ master, develop ]
    paths:
      - 'data/static/challenges.yml'
      - 'data/static/codefixes/*.info.yml'
  workflow_dispatch:

jobs:
  UpdateChallengesOnWebsite:
    if: github.repository == 'juice-shop/juice-shop'
    runs-on: ubuntu-latest
    steps:
    - name: Check out Git repository
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4.2.2
      with:
        token: ${{ secrets.BOT_TOKEN }}
        repository: OWASP/www-project-juice-shop
        ref: master
    - name: Check out juice-shop source
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4.2.2
      with:
        path: juice-shop
    - name: Update challenges.yml
      run: |
        if [ "${{ github.ref_name }}" = "master" ]; then
          DEST_FILE="_data/challenges.yml"
        else
          DEST_FILE="_data/challenges_dev.yml"
        fi
        rm -f $DEST_FILE
        cp juice-shop/data/static/challenges.yml $DEST_FILE
    - name: Update snippets
      run: |
        if [ "${{ github.ref_name }}" = "master" ]; then
          DEST_FILE="_data/snippets.json"
        else
          DEST_FILE="_data/snippets_dev.json"
        fi
        rm -f $DEST_FILE
        CHALLENGES=$(ls juice-shop/data/static/codefixes/*.info.yml | xargs -n 1 basename | sed 's/\.info\.yml//' | jq -R . | jq -s -c '{"challenges": .}')
        echo $CHALLENGES > $DEST_FILE
    - uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 #v5.0.1
      with:
        file_pattern: '_data/challenges.yml _data/snippets.json _data/challenges_dev.yml _data/snippets_dev.json'
        commit_message: "🤖 Auto-update challenges and snippets for ${{ github.ref_name }} from ${{ github.sha }}"
        branch: master
        commit_options: '--signoff'

        # Optional commit user and author settings
        commit_user_name: JuiceShopBot
        commit_user_email: 61591748+JuiceShopBot@users.noreply.github.com
        commit_author: JuiceShopBot <61591748+JuiceShopBot@users.noreply.github.com>
