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