name: Update Stops Data on: schedule: - cron: '0 0 * * *' workflow_dispatch: jobs: update-stops-data: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v5 with: fetch-depth: 0 # Fetch all branches - name: Check for existing branch and PR id: check_existing env: GH_TOKEN: ${{ github.token }} run: | # Look for existing open PR with our title existing_pr=$(gh pr list --json number,headRefName --search "Update stops data in:title author:app/github-actions is:open" --limit 1) if [[ $(echo "$existing_pr" | jq length) -gt 0 ]]; then branch_name=$(echo "$existing_pr" | jq -r '.[0].headRefName') pr_number=$(echo "$existing_pr" | jq -r '.[0].number') echo "branch_name=$branch_name" >> $GITHUB_OUTPUT echo "pr_number=$pr_number" >> $GITHUB_OUTPUT echo "has_existing=true" >> $GITHUB_OUTPUT echo "Found existing PR #$pr_number with branch $branch_name" else branch_name="update-stops-data" echo "branch_name=$branch_name" >> $GITHUB_OUTPUT echo "has_existing=false" >> $GITHUB_OUTPUT echo "No existing PR found, will use branch $branch_name" fi - name: Setup branch env: BRANCH_NAME: ${{ steps.check_existing.outputs.branch_name }} HAS_EXISTING: ${{ steps.check_existing.outputs.has_existing }} run: | git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" if [[ "$HAS_EXISTING" == "true" ]]; then # Checkout existing branch git fetch origin $BRANCH_NAME git checkout $BRANCH_NAME echo "Checked out existing branch $BRANCH_NAME" else # Create new branch git checkout -b $BRANCH_NAME echo "Created new branch $BRANCH_NAME" fi - name: Install uv uses: astral-sh/setup-uv@v7 - name: Run download script run: uv run data/download-stops.py - name: Commit and push changes if any id: commit env: BRANCH_NAME: ${{ steps.check_existing.outputs.branch_name }} HAS_EXISTING: ${{ steps.check_existing.outputs.has_existing }} run: | git add src/frontend/public/stops.json if git diff --staged --exit-code; then echo "No changes to commit" echo "changes_made=false" >> $GITHUB_OUTPUT else git commit -m "Update stops data" git push origin $BRANCH_NAME --force-with-lease echo "changes_made=true" >> $GITHUB_OUTPUT echo "Committed and pushed changes to $BRANCH_NAME" fi - name: Create or update Pull Request if: steps.commit.outputs.changes_made == 'true' env: GH_TOKEN: ${{ github.token }} BRANCH_NAME: ${{ steps.check_existing.outputs.branch_name }} HAS_EXISTING: ${{ steps.check_existing.outputs.has_existing }} PR_NUMBER: ${{ steps.check_existing.outputs.pr_number }} run: | if [[ "$HAS_EXISTING" == "true" ]]; then # Update existing PR with a comment gh pr comment $PR_NUMBER --body "🔄 Stops data updated on $(date -u +"%Y-%m-%d %H:%M:%S UTC")" echo "Updated existing PR #$PR_NUMBER" else # Create new PR gh pr create \ --title "Update stops data" \ --body "Automatically generated PR with updated stops data from scheduled fetch." \ --base main \ --head "$BRANCH_NAME" echo "Created new PR" fi