Skip to content

Commit 3418df2

Browse files
authored
ci: ensure that databases table in docs/schema.md is valid (#395)
This should catch mistakes like #393 --------- Signed-off-by: Gareth Jones <[email protected]>
1 parent 6989ead commit 3418df2

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

.github/workflows/checks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ jobs:
7979
| xargs -I '{}' bash -c \
8080
'echo "::error file={}::This needs to be regenerated by running \`python3 ./scripts/update-ecosystems-lists.py\`" && false'
8181
82+
validate_schema_databases_table:
83+
permissions:
84+
contents: read # to fetch code (actions/checkout)
85+
name: Ensure schema databases table is valid
86+
runs-on: ubuntu-latest
87+
steps:
88+
- name: Check out code
89+
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
90+
with:
91+
persist-credentials: false
92+
- uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0
93+
with:
94+
python-version: 3.13
95+
- run: python3 ./scripts/validate-schema-table.py
96+
8297
validate-osv-schema:
8398
name: Validate OSV Schema
8499
permissions:

scripts/validate-schema-table.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from html.parser import HTMLParser
5+
6+
7+
class UnexpectedTagError(Exception):
8+
pass
9+
10+
11+
class HTMLValidator(HTMLParser):
12+
def __init__(self) -> None:
13+
super().__init__()
14+
15+
self.__tags: list[str] = []
16+
17+
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
18+
self.__tags.append(tag)
19+
20+
def handle_endtag(self, tag: str) -> None:
21+
last_tag = self.__tags.pop()
22+
if last_tag != tag:
23+
raise UnexpectedTagError(last_tag)
24+
25+
26+
def extract_schema_table() -> tuple[str, int]:
27+
raw_table = ''
28+
index = -1
29+
30+
with open('docs/schema.md') as f:
31+
for i, line in enumerate(f.readlines()):
32+
if line == '<table>\n':
33+
index = i + 1
34+
raw_table += line
35+
if raw_table != '':
36+
raw_table += line
37+
if line == '</table>\n':
38+
break
39+
return raw_table, index
40+
41+
42+
table, starting_line = extract_schema_table()
43+
44+
validator = HTMLValidator()
45+
46+
try:
47+
validator.feed(table)
48+
except UnexpectedTagError as e:
49+
if 'CI' in os.environ:
50+
print(
51+
f'::error file=docs/schema.md,line={starting_line}::unexpected {e} tag in table - ensure that the tags are properly paired'
52+
)
53+
print(
54+
f'unexpected {e} tag in docs/schema.md databases table - ensure that the tags are properly paired'
55+
)
56+
exit(1)

0 commit comments

Comments
 (0)