From c66afdf420f5c67824c83da14754b0e39e24d536 Mon Sep 17 00:00:00 2001 From: alvrogd Date: Wed, 4 Dec 2024 17:37:51 +0100 Subject: [PATCH] Actions: Add action to ensure the table is updated This new action will ensure that we don't forget to update the `README.md` whenever a new check is added to the catalog. --- .github/utils/ValidateReadmeTable.py | 52 +++++++++++++++++++++++++++ .github/workflows/validate-readme.yml | 23 ++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 .github/utils/ValidateReadmeTable.py create mode 100644 .github/workflows/validate-readme.yml diff --git a/.github/utils/ValidateReadmeTable.py b/.github/utils/ValidateReadmeTable.py new file mode 100644 index 0000000..63579cd --- /dev/null +++ b/.github/utils/ValidateReadmeTable.py @@ -0,0 +1,52 @@ +import os +import re +import sys +from typing import Final + +CHECK_ID_PATTERN: Final[str] = r"(PWR\d+|PWD\d+|RMK\d+)" +CHECKS_DIR: Final[str] = "Checks/" +README_FILE: Final[str] = "README.md" +TABLE_MARKER: Final[str] = "|" + + +def extractIdsFromTable(readmeFile: str) -> set[str]: + with open(readmeFile, "r") as file: + return { + match.group(1) + for line in file.readlines() + if line.startswith(TABLE_MARKER) + and (match := re.search(CHECK_ID_PATTERN, line)) + } + + +def extractIdsFromChecksDirectory(cheksDirectory: str) -> set[str]: + return {entry.name for entry in os.scandir(cheksDirectory) if entry.is_dir()} + + +def main(): + tableIds: Final[set[str]] = extractIdsFromTable(README_FILE) + checksDirIds: Final[set[str]] = extractIdsFromChecksDirectory(CHECKS_DIR) + + missingFromTable: Final[set[str]] = checksDirIds - tableIds + extraInTable: Final[set[str]] = tableIds - checksDirIds + + if missingFromTable: + print( + f"[Error] The following IDs are missing from the README table: " + f"{', '.join(missingFromTable)}" + ) + if extraInTable: + print( + f"[Error] The following IDs are listed in the README table but do " + f"not exist in the checks directory: {', '.join(extraInTable)}" + ) + + if missingFromTable or extraInTable: + sys.exit(1) + else: + print("README table is up-to-date!") + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/.github/workflows/validate-readme.yml b/.github/workflows/validate-readme.yml new file mode 100644 index 0000000..f929daf --- /dev/null +++ b/.github/workflows/validate-readme.yml @@ -0,0 +1,23 @@ +name: Validate README Table + +on: + push: + paths: + - '.github/workflows/**' + - 'Checks/**' + - 'README.md' + +jobs: + validate-readme: + runs-on: ubuntu-24.04 + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Validate README Table + run: python .github/utils/ValidateReadmeTable.py