name: "Auto-update process"
on:
  schedule:
    # “At 20:00 on Wednesday.”
    #
    # https://crontab.guru/#0_20_*_*_3
    - cron: '0 20 * * 3'

  # Allow manually triggering this request via the button at
  # https://github.com/WebAssembly/testsuite/actions/workflows/autoupdate.yml
  workflow_dispatch:

jobs:
  update:
    # Only run for non-`WebAssembly/testsuite` repositories if manually
    # scheduled. Otherwise don't run this for forks of the `testsuite` repo on a
    # cron job as that's not what most want anyway.
    if: "github.repository == 'WebAssembly/testsuite' || !github.event.schedule"
    name: Maybe create a PR with an update.
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          # Get the full git history as we might make a PR.
          fetch-depth: 0

      # Run the standard `./update-testsuite.sh` script after configuring git to
      # allow making commits.
      - run: |
          git config --global user.name 'WebAssembly/testsuite auto-update'
          git config --global user.email 'github-actions@users.noreply.github.com'
          ./update-testsuite.py

      # If the current HEAD is different then a commit was made, so make a PR.
      - run: |
          if [ "${{ github.sha }}" != $(git rev-parse HEAD) ]; then
            branch=update-$time
            git checkout -b $branch
            git push origin $branch:$branch
            gh pr create --fill
          fi
        env:
          GH_TOKEN: ${{ github.token }}
