Skip to content

Commit

Permalink
所属ヘッダの指定ミスを検出するCIタスクを追加 #1203
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Oct 19, 2023
1 parent 8916ccb commit d73d6be
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/meta_header_check.yml
@@ -0,0 +1,19 @@
name: meta header check

on: [push, pull_request, workflow_dispatch]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- uses: actions/checkout@v2
- name: check
run: python3 .github/workflows/script/meta_header_check.py
54 changes: 54 additions & 0 deletions .github/workflows/script/meta_header_check.py
@@ -0,0 +1,54 @@
import glob
import os
import sys
import re

def find_header_or_module(line: str) -> (re.Match, str):
m = re.fullmatch(r'[\*-] (.*?)\[meta header\]', line, re.MULTILINE)
if m:
return m, "header"

m = re.fullmatch(r'[\*-] (.*?)\[meta module\]', line, re.MULTILINE)
if m:
return m, "module"
return None, ""

def check_header(text: str, filename: str) -> bool:
found_error: bool = False

for line in text.split("\n"):
m, header = find_header_or_module(line)
if m:
dirs = filename.split("/")
meta_header = m.group(1)
if len(dirs) < 2:
found_error = True
print("[{}] invalid directory: {}".format(filename, meta_header))
elif len(dirs) == 2 and dirs[0] in ("reference", "module"):
own_filename = os.path.splitext(os.path.basename(filename))[0]
if meta_header != own_filename:
found_error = True
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format(
filename, header, meta_header, own_filename))
elif len(dirs) > 2 and dirs[0] in ("reference", "module"):
own_header = dirs[1]
if meta_header != own_header:
found_error = True
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format(
filename, header, meta_header, own_header))

break

return not found_error

if __name__ == '__main__':
found_error = False
for p in sorted(list(glob.glob("**/*.md", recursive=True))):
with open(p) as f:
text = f.read()

if not check_header(text, p):
found_error = True

if found_error:
sys.exit(1)

0 comments on commit d73d6be

Please sign in to comment.