name: Stale Issues
on:
  schedule:
    - cron: '0 3 * * 1'
  workflow_dispatch:

jobs:
  stale:
    runs-on: ubuntu-latest
    steps:
      - name: Check stale issues
        run: |
          GITEA="https://git.blackroad.io"
          TOKEN="${{ secrets.ADMIN_TOKEN }}"
          REPO="${{ github.repository }}"
          
          # Find issues older than 30 days with no activity
          CUTOFF=$(date -u -d '30 days ago' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-30d +%Y-%m-%dT%H:%M:%SZ)
          
          echo "Checking for stale issues (no activity since $CUTOFF)..."
          
          # List open issues
          ISSUES=$(curl -s "$GITEA/api/v1/repos/$REPO/issues?state=open&type=issues&sort=updated&limit=50" \
            -H "Authorization: token $TOKEN")
          
          echo "$ISSUES" | python3 -c "
          import sys, json
          from datetime import datetime, timedelta
          
          cutoff = datetime.utcnow() - timedelta(days=30)
          issues = json.load(sys.stdin)
          stale = [i for i in issues if datetime.fromisoformat(i['updated_at'].rstrip('Z')) < cutoff]
          
          print(f'Found {len(stale)} stale issues')
          for i in stale:
              print(f'  #{i[\"number\"]}: {i[\"title\"]} (updated {i[\"updated_at\"]})')
          " 2>/dev/null || echo "No stale issues found"
