aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/update-stops-data.yml
blob: 439247b58e4c46e85e19f99d7c92c0a52007ede0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 src/stop_downloader/vigo/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