-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_posts.py
39 lines (31 loc) · 1.3 KB
/
check_posts.py
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
import datetime
import re
import subprocess
import os
# Get added files in current branch
added_files = subprocess.run(
["git", "diff", "--name-only", "--diff-filter=A", "origin/main", "HEAD"],
encoding="utf-8",
stdout=subprocess.PIPE,
check=True,
).stdout
for filename in added_files.split("\n"):
if filename.startswith("content/posts/"):
with open(filename) as fh:
# Ugly, but easy. Rather than creating some clever regex that will
# only match the metdata within "---" and "---", just read the
# first 10 lines and be happy with it.
metadata_block = "".join([next(fh) for _ in range(10)])
metadata = re.findall("(\w+):\s*(.*)", metadata_block, flags=re.M)
for line in metadata:
key, value = line
# checks
if key == "draft" and value.lower() == "true":
raise RuntimeError(
"Draft is enabled. It should be disabled before merge!"
)
if key == "date":
# Try to load it as a datetime:
date = datetime.datetime.fromisoformat(value)
if datetime.date.today() != date.date():
raise RuntimeError(f"Date is too old: {date.date()}. (today {datetime.date.today()})")