From c5bfcb7d2c018c3518010508403778ca2a881347 Mon Sep 17 00:00:00 2001 From: Marian Steinbach Date: Mon, 16 Dec 2024 11:36:40 +0100 Subject: [PATCH] Check content dir for for file and folder name validity (#2427) --- .github/workflows/validate.yaml | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index 8c3e8bd1d4..5de5e58a48 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -77,6 +77,46 @@ jobs: exit 1 fi + - name: Check file and folder names in content + shell: python {0} + run: | + # Walk content folder, check file names to use only allowed characters + from pathlib import Path + import re + import os + import sys + + allowed_chars = re.compile(r'^[a-z0-9-\.+]+$') + allowed_chars_static = re.compile(r'^[a-z0-9-\._]+$') # images etc. may use the underscore + + content_dir = 'src/content' + errors = [] + for root, dirs, files in os.walk(content_dir): + for file in files: + full_path = Path(root, file) + if full_path.name == '_index.md': + continue + if full_path.name == '_template.md.tpl': + continue + + if full_path.suffix.lower() == '.md': + if not allowed_chars.match(full_path.stem): + errors.append(f"- FILE {full_path}") + else: + # static files + if not allowed_chars_static.match(full_path.name): + errors.append(f"- FILE {full_path}") + + for dir in dirs: + if not allowed_chars.match(dir): + errors.append(f"- DIR {Path(root, dir)}") + + if len(errors) > 0: + sys.stderr.write("The following file/folder names use invalid characters. Only lowercase letters, digits, hyphens and period are allowed.\n") + for error in errors: + sys.stderr.write(error + "\n") + sys.exit(1) + - name: Check for moved or deleted files run: | git --no-pager diff --name-status --diff-filter=RD "refs/heads/${GITHUB_BASE_REF}" -- . | tee files.txt