name: Check ort stable release

on:
  schedule:
    # Every Monday at 09:00 UTC
    - cron: '0 9 * * 1'
  workflow_dispatch:

permissions:
  issues: write

jobs:
  check-ort:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Check crates.io for ort stable
        id: check
        run: |
          LATEST=$(curl -s https://crates.io/api/v1/crates/ort | jq -r '.crate.max_version')
          echo "latest=$LATEST" >> "$GITHUB_OUTPUT"

          CURRENT=$(grep -E '^ort\s*=' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          echo "current=$CURRENT" >> "$GITHUB_OUTPUT"

          # Detect if latest is a stable 2.0.0+ release (no rc/beta/alpha/pre)
          if echo "$LATEST" | grep -qiE '(rc|beta|alpha|pre|dev)'; then
            echo "stable=false" >> "$GITHUB_OUTPUT"
            echo "ort $LATEST is still pre-release (current: $CURRENT)"
          else
            echo "stable=true" >> "$GITHUB_OUTPUT"
            echo "ort $LATEST looks stable (current: $CURRENT)"
          fi

      - name: Open issue if stable ort is available
        if: steps.check.outputs.stable == 'true' && steps.check.outputs.latest != steps.check.outputs.current
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          LATEST="${{ steps.check.outputs.latest }}"
          CURRENT="${{ steps.check.outputs.current }}"

          # Check if an open issue already exists
          EXISTING=$(gh issue list --repo "${{ github.repository }}" \
            --search "ort stable $LATEST" --state open --json number | jq length)

          if [ "$EXISTING" -gt 0 ]; then
            echo "Issue already exists for ort $LATEST"
            exit 0
          fi

          gh issue create \
            --repo "${{ github.repository }}" \
            --title "deps: ort $LATEST stable is available (current: $CURRENT)" \
            --body "crates.io now has \`ort $LATEST\` which appears to be a stable (non-pre-release) version.

Current pin: \`$CURRENT\`

**Migration notes:**
- Check [pykeio/ort CHANGELOG](https://github.com/pykeio/ort/releases/tag/v$LATEST) for breaking changes.
- Update \`Cargo.toml\` and run full test suite (unit + E2E + benchmark).
- Verify CoreML and CUDA feature builds still pass.

This issue was auto-generated by the [check-ort-stable workflow](.github/workflows/check-ort-stable.yml)."
