Skip to content

Commit c457b8f

Browse files
committed
ci: add FAQ last_modified workflow
1 parent b646893 commit c457b8f

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)