-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub_workflows_validate-markdownpage-date.yml
More file actions
90 lines (73 loc) · 2.58 KB
/
Copy pathgithub_workflows_validate-markdownpage-date.yml
File metadata and controls
90 lines (73 loc) · 2.58 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
name: Validate markdownpages frontmatter date
on:
pull_request:
paths:
- 'markdownpages/**'
push:
branches: [ main ]
paths:
- 'markdownpages/**'
permissions:
contents: read
jobs:
validate-date:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate frontmatter date is today
shell: bash
run: |
set -euo pipefail
# Determine base/head for changed files
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BASE="${{ github.event.pull_request.base.sha }}"
HEAD="${{ github.event.pull_request.head.sha }}"
else
BASE="${{ github.event.before }}"
HEAD="${{ github.sha }}"
fi
TODAY="$(TZ=UTC0 git show -s --date=format:%Y-%m-%d --format=%cd "$HEAD")"
echo "Expected date (UTC): $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 files in markdownpages."
exit 0
fi
FAIL=0
while IFS= read -r file; do
[[ -f "$file" ]] || continue
# Extract frontmatter (between first two --- lines) and read date
FRONTMATTER=$(awk '
NR==1 && $0=="---" {infm=1; next}
infm && $0=="---" {exit}
infm {print}
' "$file")
if [[ -z "$FRONTMATTER" ]]; then
echo "::error file=$file::No frontmatter found."
FAIL=1
continue
fi
FILE_DATE=$(printf "%s\n" "$FRONTMATTER" | sed -nE 's/^date:[[:space:]]*"?([0-9]{4}-[0-9]{2}-[0-9]{2})"?$/\1/p' | head -n1)
if [[ -z "$FILE_DATE" ]]; then
echo "::error file=$file::No valid date: YYYY-MM-DD in frontmatter."
FAIL=1
continue
fi
if [[ "$FILE_DATE" != "$TODAY" ]]; then
echo "::error file=$file::Frontmatter date '$FILE_DATE' must be '$TODAY'."
FAIL=1
else
echo "OK: $file date is $FILE_DATE"
fi
done <<< "$CHANGED_FILES"
if [[ "$FAIL" -ne 0 ]]; then
echo "Validation failed."
exit 1
fi
echo "All changed markdownpages files have today's date."