Action to automatically make PRs when new updates occur #1
Workflow file for this run
This file contains 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: Update TZData | ||
on: | ||
pr: | ||
schedule: | ||
- cron: '0 9 * * *' # Runs daily at 9AM UTC | ||
workflow_dispatch: | ||
jobs: | ||
update-tzdata: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check if PR already exists | ||
id: check_pr_exists | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: | | ||
PR_EXISTS=$(gh pr list --search "Update tzdata to version" --json number --jq '.[] | .number') | ||
if [ -n "$PR_EXISTS" ]; then | ||
echo "A PR updating the tzdata version already exists, see http://github.com/python/tzdata/pulls/${PR_EXISTS}" | ||
exit 0 | ||
fi | ||
- name: Check out repository (shallow) | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 1 # Shallow clone to save time | ||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install tox | ||
sudo apt-get install gh | ||
- name: Run tox update | ||
run: tox -e update | ||
- name: Check for repository changes | ||
id: check_changes | ||
run: | | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "GitHub Action" | ||
# Check for changes | ||
if git diff --quiet; then | ||
echo "No changes detected." | ||
exit 0 | ||
fi | ||
# Check for changes in the news.d directory | ||
news_files=$(git diff --name-only --diff-filter=A | grep '^news.d/.*\.md' || true) | ||
if [ -z "$news_files" ]; then | ||
echo "No new file in news.d, failing the job." | ||
exit 1 | ||
fi | ||
if [ $(echo "$news_files" | wc -l) -ne 1 ]; then | ||
echo "More than one new file added in news.d, failing the job." | ||
exit 1 | ||
fi | ||
# Extract TZDATA_VERSION from filename | ||
TZDATA_VERSION=$(basename "$news_files" .md) | ||
# Extract TZDATA_NEWS from file content | ||
TZDATA_NEWS=$(cat "$news_files") | ||
echo "TZDATA_VERSION=$TZDATA_VERSION" >> $GITHUB_ENV | ||
echo "TZDATA_NEWS=$TZDATA_NEWS" >> $GITHUB_ENV | ||
- name: Commit and push changes | ||
if: steps.check_changes.outcome == 'success' | ||
run: | | ||
git add . | ||
git commit -m "Update tzdata to version $TZDATA_VERSION" | ||
git push origin HEAD | ||
- name: Create pull request | ||
if: steps.check_pr_exists.outcome == 'success' | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: | | ||
gh pr create --title "Update tzdata to version $TZDATA_VERSION" \ | ||
--body "$TZDATA_NEWS" \ | ||
--base main \ | ||
--head $(git rev-parse --abbrev-ref HEAD) \ | ||
--label "automatic-updates" |