on: 
  workflow_dispatch:
    inputs:
      ref:
        description: Reference on ScratchAddons/ScratchAddons
        required: false
        default: master

name: Addons Data Update

jobs:
  update:
    name: Update
    runs-on: ubuntu-latest
    steps:
      - name: Checkout ScratchAddons/website-v2
        uses: actions/checkout@v4
        with:
          path: repo
      - name: Checkout ScratchAddons/website-v2-script
        uses: actions/checkout@v4
        with:
          repository: ScratchAddons/website-v2-script
          path: script
      - name: Checkout ScratchAddons/ScratchAddons
        uses: actions/checkout@v4
        with:
          repository: ScratchAddons/ScratchAddons
          path: sa
          ref: ${{ github.event.inputs.ref }}

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.os }}-node-

      - name: Execute script
        run: | 
          echo Running...
          cd repo
          bash ../script/sh/hugo-update-addons-data.sh

      - name: Generate token 
        uses: tibdex/github-app-token@v2
        id: generate-token
        with:
          app_id: ${{ secrets.BOT_APP_ID }}
          private_key: ${{ secrets.BOT_PRIVATE_KEY }}
      - name: Create pull request
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{ steps.generate-token.outputs.token }}
          commit-message: Update addons data (${{ github.event.inputs.ref }})
          branch: create-pull-request/addon-data
          committer: scratchaddons-bot[bot] <73682299+scratchaddons-bot[bot]@users.noreply.github.com>
          delete-branch: true
          path: ./repo
          title: Update addons data
          body: |
            This updates the addons data based on the main repo. (ref: ```${{ github.event.inputs.ref }}```)

              - It is recommended to do a rebase merge than other methods.
              - Make sure to not merge something that isn't released yet! Check it first!
              - If this gets merged, **please trigger [the same update workflow on the website-i18n repo](https://github.com/ScratchAddons/website-i18n/actions/workflows/update-addons-data.yml).** (use ref. ```${{ github.event.inputs.ref }}```)
