name: "Update known versions"
on:
  workflow_dispatch:
  schedule:
    - cron: "0 4 * * *" # Run every day at 4am UTC
  repository_dispatch:
    types: [ pypi_release ]

permissions: {}

jobs:
  build:
    runs-on: ubuntu-24.04-arm
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version: "20"
      - name: Update known versions
        id: update-known-versions
        run:
          node dist/update-known-versions/index.js
          src/download/checksum/known-checksums.ts
          version-manifest.json
          ${{ secrets.GITHUB_TOKEN }}
      - name: Check for changes
        id: changes_exist
        run: |
          git status --porcelain
          if [ -n "$(git status --porcelain)" ]; then
              echo "changes_exist=true" >> "$GITHUB_OUTPUT"
          else
              echo "changes_exist=false" >> "$GITHUB_OUTPUT"
          fi
      - name: Compile changes
        if: ${{ steps.changes_exist.outputs.changes_exist == 'true' }}
        run: npm ci && npm run all
      - name: Commit and push changes
        if: ${{ steps.changes_exist.outputs.changes_exist == 'true' }}
        run: |
          git config user.name "$GITHUB_ACTOR"
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add .
          git commit -m "chore: update known versions for $LATEST_VERSION"
          git push origin HEAD:refs/heads/main
        env:
          LATEST_VERSION: ${{ steps.update-known-versions.outputs.latest-version }}
