name: Pin Browsers
on:
  schedule:
    - cron: 10 0 * * *
  workflow_dispatch:

permissions:
  contents: read

jobs:
  update:
    name: Update Pinned Browsers
    uses: ./.github/workflows/bazel.yml
    with:
      name: Pin Browsers
      run: bazel run //scripts:pinned_browsers
      artifact-name: pinned-browsers

  create-pr:
    name: Create Pull Request
    if: github.event.repository.fork == false
    runs-on: ubuntu-latest
    needs: update
    permissions:
      contents: write
      pull-requests: write
      actions: read
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          persist-credentials: false
      - name: Download patch
        id: download
        uses: actions/download-artifact@v8
        with:
          name: pinned-browsers
        continue-on-error: true
      - name: Apply Patch
        if: steps.download.outcome == 'success'
        run: |
          git apply --index changes.patch
          rm changes.patch
      - name: Create Pull Request
        if: steps.download.outcome == 'success'
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
          commit-message: "Update pinned browser versions"
          author: Selenium CI Bot <selenium-ci@users.noreply.github.com>
          base: trunk
          title: "[build] Automated Browser Version Update"
          body: |
            This is an automated pull request to update pinned browsers and drivers

            Merge after verifying the new browser versions are properly passing the tests
          branch: "pinned-browser-updates"
