Skip to content

所属ヘッダの指定ミスを検出するCIタスクを追加 #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/meta_header_check.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)