.github/workflows/detect-new-repos.yml #1927
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Detect New Repositories | |
| on: | |
| schedule: | |
| - cron: "*/10 * * * *" # Runs every ten minutes | |
| workflow_dispatch: # Allows manual execution if needed | |
| jobs: | |
| check_new_repos: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Fetch repositories from GitHub API | |
| env: | |
| GH_PAT: ${{ secrets.GH_PAT }} | |
| run: | | |
| # Fetch latest repo | |
| RESPONSE=$(curl -s -H "Authorization: token $GH_PAT" \ | |
| "https://api.github.com/orgs/JechSpace/repos?sort=created&per_page=1") | |
| echo "GitHub API Response: $RESPONSE" | |
| # Validate response format | |
| NEW_REPO=$(echo "$RESPONSE" | jq -r 'if type=="array" and length>0 then .[0].full_name else "null" end') | |
| if [[ "$NEW_REPO" == "null" ]]; then | |
| echo "No new repositories found or invalid API response." | |
| exit 0 | |
| fi | |
| # Check if we already processed this repo | |
| echo "Last processed repo: $(cat last_repo.txt 2>/dev/null || echo 'none')" | |
| LAST_REPO=$(cat last_repo.txt 2>/dev/null || echo 'none') | |
| if [[ "$NEW_REPO" != "$LAST_REPO" ]]; then | |
| echo "New repository detected: $NEW_REPO" | |
| # Store latest repo name | |
| echo "$NEW_REPO" > last_repo.txt | |
| # Trigger the sync workflow | |
| curl -X POST -H "Authorization: token $GH_PAT" \ | |
| -H "Accept: application/vnd.github.everest-preview+json" \ | |
| "https://api.github.com/repos/JechSpace/.github/dispatches" \ | |
| -d "{\"event_type\": \"new_repo_created\", \"client_payload\": {\"repo_full_name\": \"$NEW_REPO\"}}" | |
| else | |
| echo "No new repositories found." | |
| fi |