-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub_workflows_autoset-markdownpage-date.yml
More file actions
117 lines (102 loc) · 3.6 KB
/
Copy pathgithub_workflows_autoset-markdownpage-date.yml
File metadata and controls
117 lines (102 loc) · 3.6 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
name: Auto-set markdownpages frontmatter date
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'markdownpages/**'
permissions:
contents: write
pull-requests: write
jobs:
set-date:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
- name: Update frontmatter date to today
shell: bash
run: |
set -euo pipefail
BASE="${{ github.event.pull_request.base.sha }}"
HEAD="${{ github.event.pull_request.head.sha }}"
TODAY="$(date -u +%Y-%m-%d)" # Change to local TZ if needed
echo "Using date: $TODAY"
if ! CHANGED_FILES=$(git diff --name-only "$BASE" "$HEAD" -- 'markdownpages/**/*.md' 'markdownpages/*.md'); then
echo "::error::Failed to determine changed markdownpages files between $BASE and $HEAD"
exit 1
fi
if [[ -z "$CHANGED_FILES" ]]; then
echo "No changed markdownpages files."
exit 0
fi
changed_any=0
while IFS= read -r file; do
[[ -f "$file" ]] || continue
# Ensure file starts with frontmatter
if ! head -n1 "$file" | grep -qx '---'; then
echo "Skipping $file (no frontmatter at top)"
continue
fi
# If date exists in frontmatter, replace first occurrence.
# Otherwise insert date line right before closing frontmatter.
if awk '
NR==1 && $0=="---" {infm=1; next}
infm && $0=="---" {exit}
infm && $0 ~ /^date:[[:space:]]*/ {found=1}
END {exit(found?0:1)}
' "$file"; then
awk -v today="$TODAY" '
BEGIN {infm=0; done=0}
NR==1 && $0=="---" {infm=1; print; next}
infm && $0=="---" {
infm=0
print
next
}
infm && !done && $0 ~ /^date:[[:space:]]*/ {
print "date: " today
done=1
next
}
{print}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
echo "Updated date in $file"
changed_any=1
else
awk -v today="$TODAY" '
BEGIN {infm=0; inserted=0}
NR==1 && $0=="---" {infm=1; print; next}
infm && $0=="---" && !inserted {
print "date: " today
inserted=1
print
infm=0
next
}
{print}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
echo "Inserted date in $file"
changed_any=1
fi
done <<< "$CHANGED_FILES"
if [[ "$changed_any" -eq 0 ]]; then
echo "No files changed by updater."
exit 0
fi
- name: Commit and push if changed
shell: bash
run: |
set -euo pipefail
if git diff --quiet; then
echo "No git changes to commit."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add markdownpages
git commit -m "chore: auto-update markdownpages frontmatter date to today"
git push