name: Update Test262 tests

on:
  # Trigger at every Sunday UTC noon, or manually.
  schedule:
    - cron: '0 12 * * 0'
  workflow_dispatch:

jobs:
  update-test262:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout WPT repo
        uses: actions/checkout@v4
        with:
          path: wpt
      - name: Checkout Test262 repo
        uses: actions/checkout@v4
        with:
          repository: tc39/test262
          path: test262-spec
      - name: Copy Test262 tests to WPT
        run: wpt/tools/ci/update-test262.sh $(pwd)/test262-spec
      - name: Commit changes
        id: commit
        continue-on-error: true
        run: |
          cd wpt
          export BRANCH_NAME="$BRANCH_PREFIX-$(date +'%Y%m%d%H%M%S')"
          echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
          git config user.name "$GIT_AUTHOR_NAME"
          git config user.email "$GIT_AUTHOR_EMAIL"
          git checkout -B $BRANCH_NAME
          git add third_party/test262/
          git commit -m "$COMMIT_TITLE"
        env:
          GIT_AUTHOR_NAME: "wpt-pr-bot"
          GIT_AUTHOR_EMAIL: "wpt-pr-bot@users.noreply.github.com"
          BRANCH_PREFIX: "test262-update"
          COMMIT_TITLE: "Update Test262 tests"
      - name: Create PR
        # Check outcome for success as continue-on-error will mask failure.
        if: ${{ steps.commit.outcome == 'success' }}
        run: |
          cd wpt
          git push --set-upstream origin $BRANCH_NAME
          gh pr create --title "$COMMIT_TITLE" --body "$PR_BODY"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COMMIT_TITLE: "Update Test262 tests"
          PR_BODY: "Scheduled weekly update auto-generated by the '${{ github.workflow }}' workflow."
