ProxmoxVE/.github/workflows/update_json_date.yml
2025-02-11 10:33:05 +01:00

103 lines
3.2 KiB
YAML

name: Auto Update JSON-Dateien
on:
push:
branches:
- main
paths:
- 'json/**.json'
workflow_dispatch:
jobs:
update-json-dates:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- 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 }}
# Repository auschecken
- name: Checkout repository
uses: actions/checkout@v4
# Git-Setup
- name: Set up Git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# JSON-Dateien aktualisieren
- name: Update JSON date_created
run: |
TODAY=$(date -u +"%Y-%m-%d")
CHANGED=false
for FILE in json/*.json; do
if [[ -f "$FILE" ]]; then
DATE_IN_JSON=$(jq -r '.date_created' "$FILE" 2>/dev/null || echo "")
if [[ "$DATE_IN_JSON" != "$TODAY" ]]; then
echo "Updating date_created in $FILE: $DATE_IN_JSON -> $TODAY"
jq --arg date "$TODAY" '.date_created = $date' "$FILE" > tmp.json && mv tmp.json "$FILE"
CHANGED=true
fi
fi
done
if [[ "$CHANGED" == "true" ]]; then
echo "CHANGED=true" >> $GITHUB_ENV
else
echo "CHANGED=false" >> $GITHUB_ENV
fi
# Falls Änderungen existieren: Commit und PR erstellen
- name: Commit and create PR if changes exist
if: env.CHANGED == 'true'
run: |
git add json/*.json
git commit -m "Auto-update JSON date_created fields"
git checkout -b pr-update-json-dates
git push origin pr-update-json-dates --force
gh pr create --title "[core] Auto-update JSON files" \
--body "This PR is auto-generated by a GitHub Action to update the `date_created` field in JSON files." \
--head pr-update-json-dates \
--base main \
--label "automated pr"
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
# PR automatisch approven
- name: Approve pull request
if: env.CHANGED == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
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
fi
# PR erneut approven, falls erforderlich
- name: Re-approve pull request after update
if: env.CHANGED == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
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
fi
# Falls keine Änderungen erkannt wurden
- name: No changes detected
if: env.CHANGED == 'false'
run: echo "No changes to commit. Workflow completed successfully."