|
| 1 | +name: Update JSON Date |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'json/**.json' |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +jobs: |
| 12 | + update-app-files: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + permissions: |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Generate a token |
| 21 | + id: generate-token |
| 22 | + uses: actions/create-github-app-token@v1 |
| 23 | + with: |
| 24 | + app-id: ${{ vars.APP_ID }} |
| 25 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 26 | + |
| 27 | + - name: Generate dynamic branch name |
| 28 | + id: timestamp |
| 29 | + run: echo "BRANCH_NAME=pr-update-json-$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV |
| 30 | + |
| 31 | + - name: Set up GH_TOKEN |
| 32 | + env: |
| 33 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + run: | |
| 35 | + echo "GH_TOKEN=${GH_TOKEN}" >> $GITHUB_ENV |
| 36 | +
|
| 37 | + - name: Checkout Repository |
| 38 | + uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + fetch-depth: 2 # Ensure we have the last two commits |
| 41 | + |
| 42 | + - name: Get Previous Commit |
| 43 | + id: prev_commit |
| 44 | + run: | |
| 45 | + PREV_COMMIT=$(git rev-parse HEAD^) |
| 46 | + echo "Previous commit: $PREV_COMMIT" |
| 47 | + echo "prev_commit=$PREV_COMMIT" >> $GITHUB_ENV |
| 48 | +
|
| 49 | + - name: Get Newly Added JSON Files |
| 50 | + id: new_json_files |
| 51 | + run: | |
| 52 | + git diff --name-only --diff-filter=A ${{ env.prev_commit }} HEAD | grep '^json/.*\.json$' > new_files.txt || true |
| 53 | + echo "New files detected:" |
| 54 | + cat new_files.txt || echo "No new files." |
| 55 | +
|
| 56 | + - name: Disable file mode changes |
| 57 | + run: git config core.fileMode false |
| 58 | + |
| 59 | + - name: Set up Git |
| 60 | + run: | |
| 61 | + git config --global user.name "GitHub Actions" |
| 62 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 63 | +
|
| 64 | + - name: Change JSON Date |
| 65 | + id: change-json-date |
| 66 | + run: | |
| 67 | + current_date=$(date +"%Y-%m-%d") |
| 68 | + while IFS= read -r file; do |
| 69 | + # Skip empty lines |
| 70 | + [[ -z "$file" ]] && continue |
| 71 | +
|
| 72 | + if [[ -f "$file" ]]; then |
| 73 | + echo "Processing $file..." |
| 74 | + current_json_date=$(jq -r '.date_created // empty' "$file") |
| 75 | + if [[ -z "$current_json_date" || "$current_json_date" != "$current_date" ]]; then |
| 76 | + echo "Updating $file with date $current_date" |
| 77 | + jq --arg date "$current_date" '.date_created = $date' "$file" > temp.json && mv temp.json "$file" |
| 78 | + else |
| 79 | + echo "Date in $file is already up to date." |
| 80 | + fi |
| 81 | + else |
| 82 | + echo "Warning: File $file not found!" |
| 83 | + fi |
| 84 | + done < new_files.txt |
| 85 | + rm new_files.txt |
| 86 | +
|
| 87 | + - name: Check if there are any changes |
| 88 | + run: | |
| 89 | + echo "Checking for changes..." |
| 90 | + git add -A # Untracked Dateien aufnehmen |
| 91 | + git status |
| 92 | + if git diff --cached --quiet; then |
| 93 | + echo "No changes detected." |
| 94 | + echo "changed=false" >> "$GITHUB_ENV" |
| 95 | + else |
| 96 | + echo "Changes detected:" |
| 97 | + git diff --stat --cached |
| 98 | + echo "changed=true" >> "$GITHUB_ENV" |
| 99 | + fi |
| 100 | +
|
| 101 | + # Step 7: Commit and create PR if changes exist |
| 102 | + - name: Commit and create PR if changes exist |
| 103 | + if: env.changed == 'true' |
| 104 | + run: | |
| 105 | + |
| 106 | + |
| 107 | + git commit -m "Update date in json" |
| 108 | + git checkout -b ${{ env.BRANCH_NAME }} |
| 109 | + git push origin ${{ env.BRANCH_NAME }} |
| 110 | +
|
| 111 | + gh pr create --title "[core] update date in json" \ |
| 112 | + --body "This PR is auto-generated by a GitHub Action to update the date in json." \ |
| 113 | + --head ${{ env.BRANCH_NAME }} \ |
| 114 | + --base main \ |
| 115 | + --label "automated pr" |
| 116 | + env: |
| 117 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 118 | + |
| 119 | + - name: Approve pull request |
| 120 | + if: env.changed == 'true' |
| 121 | + env: |
| 122 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 123 | + run: | |
| 124 | + PR_NUMBER=$(gh pr list --head "${{ env.BRANCH_NAME }}" --json number --jq '.[].number') |
| 125 | + if [ -n "$PR_NUMBER" ]; then |
| 126 | + gh pr review $PR_NUMBER --approve |
| 127 | + fi |
| 128 | +
|
| 129 | + - name: No changes detected |
| 130 | + if: env.changed == 'false' |
| 131 | + run: echo "No changes to commit. Workflow completed successfully." |
0 commit comments