Skip to content

Commit

Permalink
Actions: Add action to ensure the table is updated
Browse files Browse the repository at this point in the history
This new action will ensure that we don't forget to update the
`README.md` whenever a new check is added to the catalog.
  • Loading branch information
alvrogd committed Dec 5, 2024
1 parent 83a239c commit c66afdf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/utils/ValidateReadmeTable.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions .github/workflows/validate-readme.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c66afdf

Please sign in to comment.