2025-02-11 09:35:53 +00:00
|
|
|
name: Auto Update JSON-Dateien (new files only)
|
2025-02-11 08:43:21 +00:00
|
|
|
|
2025-02-11 08:41:38 +00:00
|
|
|
on:
|
2025-02-11 09:33:05 +00:00
|
|
|
push:
|
|
|
|
branches:
|
|
|
|
- main
|
2025-02-11 08:41:38 +00:00
|
|
|
workflow_dispatch:
|
|
|
|
|
|
|
|
jobs:
|
2025-02-11 09:33:05 +00:00
|
|
|
update-json-dates:
|
2025-02-11 08:41:38 +00:00
|
|
|
runs-on: ubuntu-latest
|
2025-02-11 09:33:05 +00:00
|
|
|
|
|
|
|
permissions:
|
|
|
|
contents: write
|
|
|
|
pull-requests: write
|
|
|
|
|
2025-02-11 08:41:38 +00:00
|
|
|
steps:
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: Generate a token
|
|
|
|
id: generate-token
|
|
|
|
uses: actions/create-github-app-token@v1
|
|
|
|
with:
|
|
|
|
app-id: ${{ vars.APP_ID }}
|
|
|
|
private-key: ${{ secrets.APP_PRIVATE_KEY }}
|
|
|
|
|
|
|
|
- name: Checkout repository
|
2025-02-11 08:41:38 +00:00
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: Set up Git
|
2025-02-11 08:59:09 +00:00
|
|
|
run: |
|
2025-02-11 09:33:05 +00:00
|
|
|
git config --global user.name "GitHub Actions"
|
|
|
|
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
2025-02-11 08:59:09 +00:00
|
|
|
|
2025-02-11 09:34:00 +00:00
|
|
|
- name: Find newly added JSON files
|
|
|
|
id: find_new_json
|
|
|
|
run: |
|
|
|
|
NEW_JSON_FILES=$(git log --diff-filter=A --name-only --pretty=format: -- json/*.json || true)
|
|
|
|
|
|
|
|
if [[ -z "$NEW_JSON_FILES" ]]; then
|
|
|
|
echo "CHANGED=false" >> $GITHUB_ENV
|
|
|
|
else
|
|
|
|
echo "$NEW_JSON_FILES" > new_json_files.txt
|
|
|
|
echo "CHANGED=true" >> $GITHUB_ENV
|
|
|
|
fi
|
|
|
|
|
|
|
|
- name: Update date_created in new JSON files
|
|
|
|
if: env.CHANGED == 'true'
|
2025-02-11 09:33:05 +00:00
|
|
|
run: |
|
|
|
|
TODAY=$(date -u +"%Y-%m-%d")
|
2025-02-11 09:34:00 +00:00
|
|
|
UPDATED=false
|
2025-02-11 08:59:09 +00:00
|
|
|
|
2025-02-11 09:34:00 +00:00
|
|
|
while read -r FILE; do
|
2025-02-11 09:33:05 +00:00
|
|
|
if [[ -f "$FILE" ]]; then
|
|
|
|
DATE_IN_JSON=$(jq -r '.date_created' "$FILE" 2>/dev/null || echo "")
|
2025-02-11 08:59:09 +00:00
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
if [[ "$DATE_IN_JSON" != "$TODAY" ]]; then
|
|
|
|
jq --arg date "$TODAY" '.date_created = $date' "$FILE" > tmp.json && mv tmp.json "$FILE"
|
2025-02-11 09:34:00 +00:00
|
|
|
UPDATED=true
|
2025-02-11 09:33:05 +00:00
|
|
|
fi
|
|
|
|
fi
|
2025-02-11 09:34:00 +00:00
|
|
|
done < new_json_files.txt
|
2025-02-11 08:59:09 +00:00
|
|
|
|
2025-02-11 09:34:00 +00:00
|
|
|
if [[ "$UPDATED" == "true" ]]; then
|
|
|
|
echo "UPDATED=true" >> $GITHUB_ENV
|
2025-02-11 09:33:05 +00:00
|
|
|
else
|
2025-02-11 09:34:00 +00:00
|
|
|
echo "UPDATED=false" >> $GITHUB_ENV
|
2025-02-11 09:33:05 +00:00
|
|
|
fi
|
2025-02-11 08:59:09 +00:00
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: Commit and create PR if changes exist
|
2025-02-11 09:34:00 +00:00
|
|
|
if: env.UPDATED == 'true'
|
2025-02-11 09:30:20 +00:00
|
|
|
run: |
|
2025-02-11 09:33:05 +00:00
|
|
|
git add json/*.json
|
2025-02-11 09:34:00 +00:00
|
|
|
git commit -m "Auto-update JSON date_created fields (new files only)"
|
2025-02-11 09:33:05 +00:00
|
|
|
git checkout -b pr-update-json-dates
|
|
|
|
git push origin pr-update-json-dates --force
|
2025-02-11 09:34:00 +00:00
|
|
|
gh pr create --title "[core] Auto-update new JSON files" \
|
2025-02-11 09:35:53 +00:00
|
|
|
--body "This PR is auto-generated to update the `date_created` field in newly created JSON files." \
|
2025-02-11 09:33:05 +00:00
|
|
|
--head pr-update-json-dates \
|
|
|
|
--base main \
|
|
|
|
--label "automated pr"
|
|
|
|
env:
|
|
|
|
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
|
2025-02-11 09:30:20 +00:00
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: Approve pull request
|
2025-02-11 09:34:00 +00:00
|
|
|
if: env.UPDATED == 'true'
|
2025-02-11 09:33:05 +00:00
|
|
|
env:
|
|
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
2025-02-11 08:41:38 +00:00
|
|
|
run: |
|
2025-02-11 09:33:05 +00:00
|
|
|
PR_NUMBER=$(gh pr list --head "pr-update-json-dates" --json number --jq '.[].number')
|
|
|
|
if [ -n "$PR_NUMBER" ]; then
|
|
|
|
gh pr review $PR_NUMBER --approve
|
2025-02-11 08:44:17 +00:00
|
|
|
fi
|
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: Re-approve pull request after update
|
2025-02-11 09:34:00 +00:00
|
|
|
if: env.UPDATED == 'true'
|
2025-02-11 08:41:38 +00:00
|
|
|
env:
|
2025-02-11 09:33:05 +00:00
|
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
2025-02-11 08:41:38 +00:00
|
|
|
run: |
|
2025-02-11 09:33:05 +00:00
|
|
|
PR_NUMBER=$(gh pr list --head "pr-update-json-dates" --json number --jq '.[].number')
|
|
|
|
if [ -n "$PR_NUMBER" ]; then
|
|
|
|
gh pr review $PR_NUMBER --approve
|
2025-02-11 09:30:20 +00:00
|
|
|
fi
|
|
|
|
|
2025-02-11 09:33:05 +00:00
|
|
|
- name: No changes detected
|
2025-02-11 09:34:00 +00:00
|
|
|
if: env.UPDATED == 'false'
|
|
|
|
run: echo "No new JSON files needed an update. Workflow completed successfully."
|