name: Roll Browser into Playwright

on:
  repository_dispatch:
    types: [roll_into_pw]

jobs:
  roll:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: 16
    - run: npm i -g npm@8
    - run: npm ci
    - run: npm run build
    - name: Install dependencies
      run: npx playwright install-deps
    - name: Roll to new revision
      run: |
        ./utils/roll_browser.js ${{ github.event.client_payload.browser }} ${{ github.event.client_payload.revision }}
        npm run build
    - name: Prepare branch
      id: prepare-branch
      run: |
        BASE_POSITION="${{ steps.bump-chromium.outputs.BASE_POSITION }}"
        BRANCH_NAME="roll-into-pw-${{ github.event.client_payload.browser }}/${{ github.event.client_payload.revision }}"
        echo "::set-output name=BRANCH_NAME::$BRANCH_NAME"
        git config --global user.name github-actions
        git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
        git checkout -b "$BRANCH_NAME"
        git add .
        git commit -m "feat(${{ github.event.client_payload.browser }}): roll to r${{ github.event.client_payload.revision }}"
        git push origin $BRANCH_NAME
    - name: Create Pull Request
      uses: actions/github-script@v4
      with:
        script: |
          await github.pulls.create({
            owner: 'microsoft',
            repo: 'playwright',
            head: 'microsoft:${{ steps.prepare-branch.outputs.BRANCH_NAME }}',
            base: 'main',
            title: 'feat(${{ github.event.client_payload.browser }}): roll to r${{ github.event.client_payload.revision }}',
          });
