diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-10-09 10:29:11 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-09 10:29:11 +0200 |
| commit | 679729b24d35fdfe7fcc70881599c9b01043b1cd (patch) | |
| tree | 900c1f6d182f3d62c594042d485ce1835645f301 | |
| parent | cc8a8e1cb3e79a4ed6300e97c068e90bf5de39a0 (diff) | |
Reuse existing branch and PR for stops data updates
- Check for existing open PR before creating new branch
- Checkout and update existing branch instead of creating timestamped ones
- Use force-with-lease to safely update branch with new data
- Add timestamped comment to existing PR instead of closing/recreating
- Fetch all branches to ensure existing branch is available
| -rw-r--r-- | .github/workflows/update-stops-data.yml | 100 |
1 files changed, 69 insertions, 31 deletions
diff --git a/.github/workflows/update-stops-data.yml b/.github/workflows/update-stops-data.yml index 619f7dd..6268b5f 100644 --- a/.github/workflows/update-stops-data.yml +++ b/.github/workflows/update-stops-data.yml @@ -10,55 +10,93 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + 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@v5 + uses: astral-sh/setup-uv@v7 - name: Run download script run: uv run data/download-stops.py - - name: Commit changes if any + - 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 config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" 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 - # Create a new branch with timestamp - BRANCH_NAME="update-stops-data-$(date +%Y-%m-%d-%H%M%S)" - git checkout -b $BRANCH_NAME git commit -m "Update stops data" - git push origin $BRANCH_NAME - echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT + 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 Pull Request + + - name: Create or update Pull Request if: steps.commit.outputs.changes_made == 'true' env: GH_TOKEN: ${{ github.token }} - BRANCH_NAME: ${{ steps.commit.outputs.branch_name }} + 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: | - # Close any existing PRs created by this action - existing_prs=$(gh pr list --json number,title,headRefName --search "Update stops data in:title head:update-stops-data- author:app/github-actions is:open") - - if [[ $(echo "$existing_prs" | jq length) -gt 0 ]]; then - echo "Found existing PRs to close" - echo "$existing_prs" | jq -c '.[]' | while read pr; do - pr_number=$(echo "$pr" | jq -r '.number') - echo "Closing PR #$pr_number" - gh pr close $pr_number --comment "Superseded by a new PR with updated data" - git push origin --delete $(echo "$pr" | jq -r '.headRefName') - done + 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 - - # 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" |
