|
| 1 | +name: Check for tzdata updates |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 9 * * *' # Runs daily at 9AM UTC |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + check-pr-exists: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + outputs: |
| 12 | + pr_exists: ${{ steps.check_pr_exists.outputs.pr_exists }} |
| 13 | + steps: |
| 14 | + - name: Check if PR already exists |
| 15 | + id: check_pr_exists |
| 16 | + env: |
| 17 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 18 | + run: | |
| 19 | + PR_EXISTS=$(gh pr --repo $GITHUB_REPOSITORY \ |
| 20 | + list --search "Update tzdata to version" \ |
| 21 | + --json number --jq '.[] | .number') |
| 22 | + if [ -n "$PR_EXISTS" ]; then |
| 23 | + echo "A PR updating the tzdata version already exists: https://github.com/python/tzdata/pulls/${PR_EXISTS}" |
| 24 | + echo "pr_exists=true" >> $GITHUB_OUTPUT |
| 25 | + exit 0 |
| 26 | + else |
| 27 | + echo "pr_exists=false" >> $GITHUB_OUTPUT |
| 28 | + fi |
| 29 | +
|
| 30 | + check-for-updates: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + needs: check-pr-exists |
| 33 | + permissions: |
| 34 | + pull-requests: write |
| 35 | + contents: write |
| 36 | + if: needs.check-pr-exists.outputs.pr_exists == 'false' # Run only if no PR exists |
| 37 | + steps: |
| 38 | + - name: Check out repository (shallow) |
| 39 | + uses: actions/checkout@v3 |
| 40 | + with: |
| 41 | + fetch-depth: 1 # Shallow clone to save time |
| 42 | + |
| 43 | + - name: Set up Python 3.12 |
| 44 | + uses: actions/setup-python@v4 |
| 45 | + with: |
| 46 | + python-version: '3.12' |
| 47 | + |
| 48 | + - name: Install dependencies |
| 49 | + run: | |
| 50 | + python -m pip install tox |
| 51 | + sudo apt-get install gh |
| 52 | +
|
| 53 | + - name: Run tox update |
| 54 | + run: tox -e update |
| 55 | + |
| 56 | + - name: Check for repository changes and commit |
| 57 | + id: check_changes |
| 58 | + run: | |
| 59 | + git config --global user.email "[email protected]" |
| 60 | + git config --global user.name "GitHub Action" |
| 61 | +
|
| 62 | + # Check for changes |
| 63 | + if git diff --quiet; then |
| 64 | + echo "No changes detected." |
| 65 | + echo "CHANGES_DETECTED=false" >> $GITHUB_ENV |
| 66 | + exit 0 |
| 67 | + fi |
| 68 | +
|
| 69 | + # Check for changes in the news.d directory |
| 70 | + git add -A |
| 71 | + news_files=$(git diff --cached --name-only --diff-filter=A | grep '^news.d/.*\.md' || true) |
| 72 | +
|
| 73 | + if [ -z "$news_files" ]; then |
| 74 | + echo "No new file in news.d, failing the job." |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | +
|
| 78 | + if [ $(echo "$news_files" | wc -l) -ne 1 ]; then |
| 79 | + echo "More than one new file added in news.d, failing the job." |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + echo "CHANGES_DETECTED=true" >> $GITHUB_ENV |
| 83 | +
|
| 84 | + # Extract TZDATA_VERSION from filename |
| 85 | + TZDATA_VERSION=$(basename "$news_files" .md) |
| 86 | +
|
| 87 | + # Extract TZDATA_NEWS from file content |
| 88 | + TZDATA_NEWS=$(cat "$news_files") |
| 89 | +
|
| 90 | + echo "TZDATA_VERSION=$TZDATA_VERSION" >> $GITHUB_ENV |
| 91 | + echo "TZDATA_NEWS=$TZDATA_NEWS" >> $GITHUB_ENV |
| 92 | +
|
| 93 | + - name: Commit changes |
| 94 | + id: commit_changes |
| 95 | + if: env.CHANGES_DETECTED == 'true' |
| 96 | + run: | |
| 97 | + git checkout -b "updates/update_${TZDATA_VERSION}" |
| 98 | + git commit -m "Update tzdata to version $TZDATA_VERSION" \ |
| 99 | + -m "$TZDATA_NEWS" |
| 100 | + git push --force origin "updates/update_${TZDATA_VERSION}" |
| 101 | +
|
| 102 | + - name: Create pull request |
| 103 | + env: |
| 104 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 105 | + if: env.CHANGES_DETECTED == 'true' |
| 106 | + run: | |
| 107 | + gh pr create --title "Update tzdata to version $TZDATA_VERSION" \ |
| 108 | + --body "$TZDATA_NEWS" \ |
| 109 | + --base master \ |
| 110 | + --head $(git rev-parse --abbrev-ref HEAD) \ |
| 111 | + --label "automatic-updates" |
0 commit comments