name: Auto Update JSON-Dateien (new files only) on: push: branches: - main 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 }} - name: Checkout repository uses: actions/checkout@v4 - name: Set up Git run: | git config --global user.name "GitHub Actions" git config --global user.email "github-actions[bot]@users.noreply.github.com" - 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' run: | TODAY=$(date -u +"%Y-%m-%d") UPDATED=false while read -r FILE; do if [[ -f "$FILE" ]]; then DATE_IN_JSON=$(jq -r '.date_created' "$FILE" 2>/dev/null || echo "") if [[ "$DATE_IN_JSON" != "$TODAY" ]]; then jq --arg date "$TODAY" '.date_created = $date' "$FILE" > tmp.json && mv tmp.json "$FILE" UPDATED=true fi fi done < new_json_files.txt if [[ "$UPDATED" == "true" ]]; then echo "UPDATED=true" >> $GITHUB_ENV else echo "UPDATED=false" >> $GITHUB_ENV fi - name: Commit and create PR if changes exist if: env.UPDATED == 'true' run: | git add json/*.json git commit -m "Auto-update JSON date_created fields (new files only)" git checkout -b pr-update-json-dates git push origin pr-update-json-dates --force gh pr create --title "[core] Auto-update new JSON files" \ --body "This PR is auto-generated to update the `date_created` field in newly created JSON files." \ --head pr-update-json-dates \ --base main \ --label "automated pr" env: GH_TOKEN: ${{ steps.generate-token.outputs.token }} - name: Approve pull request if: env.UPDATED == '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 - name: Re-approve pull request after update if: env.UPDATED == '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 - name: No changes detected if: env.UPDATED == 'false' run: echo "No new JSON files needed an update. Workflow completed successfully."