name: mirror-selenium-releases

on:
  workflow_dispatch:
  workflow_call:

permissions:
  contents: read

jobs:
  build:
    if: github.event.repository.fork == false
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
      with:
        persist-credentials: false
    - name: Read api.github.com and filter response
      run: |
        set -euo pipefail
        cd common/mirror
        TOKEN="${{ secrets.GITHUB_TOKEN }}"
        JQ_FILTER='[.[] | {tag_name: .tag_name, assets: [.assets[] | {browser_download_url: .browser_download_url} ] } ]'
        page=1
        tmpfile="$(mktemp)"
        : > "$tmpfile"
        while :; do
          echo "Fetching SeleniumHQ/selenium releases page $page..."
          resp=$(curl -fsSL \
            -H "Authorization: token $TOKEN" \
            "https://api.github.com/repos/SeleniumHQ/selenium/releases?per_page=100&page=${page}")
          if [ "$(echo "$resp" | jq 'length')" -eq 0 ]; then
            break
          fi
          echo "$resp" | jq "$JQ_FILTER" >> "$tmpfile"
          page=$((page+1))
        done
        jq -s 'add' "$tmpfile" > selenium
        rm "$tmpfile"
    - name: Commit files
      id: git
      run: |
        CHANGES=$(git status -s)
        export CHANGES
        if [ -n "$CHANGES" ]; then
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
          git add common/mirror/selenium
          git commit -m "Update mirror info ($(date))" -a
          echo "commit=true" >> "$GITHUB_OUTPUT"
        fi
    - name: Push changes
      if: steps.git.outputs.commit == 'true'
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.SELENIUM_CI_TOKEN }}
        branch: ${{ github.ref }}
