|
| 1 | +name: Update last_modified |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + types: |
| 8 | + - opened |
| 9 | + - synchronize |
| 10 | + - reopened |
| 11 | + paths: |
| 12 | + - "_articles/faq/*.md" |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + update-on-master-pr: |
| 19 | + if: github.actor != 'github-actions[bot]' && github.event.pull_request.head.repo.full_name == github.repository |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Checkout PR head branch |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + ref: ${{ github.event.pull_request.head.ref }} |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Collect target files |
| 29 | + id: targets |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + set -euo pipefail |
| 33 | +
|
| 34 | + git fetch origin "${{ github.event.pull_request.base.ref }}" |
| 35 | +
|
| 36 | + git diff --name-only "origin/${{ github.event.pull_request.base.ref }}"...HEAD \ |
| 37 | + | grep -E '^_articles/faq/[^/]+\.md$' \ |
| 38 | + | sort -u > changed_files.txt || true |
| 39 | +
|
| 40 | + count="$(wc -l < changed_files.txt | tr -d ' ')" |
| 41 | + echo "count=$count" >> "$GITHUB_OUTPUT" |
| 42 | +
|
| 43 | + - name: Update front matter |
| 44 | + if: steps.targets.outputs.count != '0' |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | + timestamp="$(date '+%Y-%m-%d %H:%M:%S %z')" |
| 49 | +
|
| 50 | + while IFS= read -r file; do |
| 51 | + [[ -f "$file" ]] || continue |
| 52 | +
|
| 53 | + TS="$timestamp" perl -0777 -i -pe ' |
| 54 | + BEGIN { $ts = $ENV{"TS"}; } |
| 55 | +
|
| 56 | + s{\A---\R(.*?)\R---(?:\R|$)}{ |
| 57 | + my $fm = $1; |
| 58 | +
|
| 59 | + if ($fm =~ /^last_modified:.*$/m) { |
| 60 | + $fm =~ s/^last_modified:.*$/last_modified: $ts/m; |
| 61 | + } elsif ($fm =~ /^date:.*$/m) { |
| 62 | + $fm =~ s/^(date:.*)$/$1\nlast_modified: $ts/m; |
| 63 | + } else { |
| 64 | + $fm .= "\nlast_modified: $ts"; |
| 65 | + } |
| 66 | +
|
| 67 | + "---\n$fm\n---\n"; |
| 68 | + }es; |
| 69 | + ' "$file" |
| 70 | + done < changed_files.txt |
| 71 | +
|
| 72 | + - name: Commit and push to PR branch |
| 73 | + if: steps.targets.outputs.count != '0' |
| 74 | + shell: bash |
| 75 | + run: | |
| 76 | + set -euo pipefail |
| 77 | +
|
| 78 | + if git diff --quiet; then |
| 79 | + echo "No timestamp updates needed." |
| 80 | + exit 0 |
| 81 | + fi |
| 82 | +
|
| 83 | + git config user.name "github-actions[bot]" |
| 84 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 85 | + while IFS= read -r file; do |
| 86 | + [[ -f "$file" ]] || continue |
| 87 | + git add "$file" |
| 88 | + done < changed_files.txt |
| 89 | + git commit -m "chore: update last_modified timestamps" |
| 90 | + git push origin HEAD:${{ github.event.pull_request.head.ref }} |
0 commit comments