Skip to content

Commit 9c27cc0

Browse files
committed
Add a script to check for duplicate query IDs
1 parent 5bb1319 commit 9c27cc0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

misc/scripts/check-query-ids.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pathlib import Path
2+
import re
3+
import sys
4+
from typing import Dict, List, Optional
5+
6+
ID = re.compile(r" +\* +@id\s*(.*)")
7+
8+
def get_query_id(query_path: Path) -> Optional[str]:
9+
with open(query_path) as f:
10+
for line in f:
11+
m = ID.match(line)
12+
if m:
13+
return m.group(1)
14+
return None
15+
16+
def main():
17+
# Map query IDs to paths of queries with those IDs. We want to check that this is a 1:1 map.
18+
query_ids: Dict[str, List[str]] = {}
19+
20+
# Just check src folders for now to avoid churn
21+
for query_path in Path().glob("**/src/**/*.ql"):
22+
# Skip compiled query packs
23+
if any(p == ".codeql" for p in query_path.parts):
24+
continue
25+
query_id = get_query_id(query_path)
26+
if query_id is not None:
27+
query_ids.setdefault(query_id, []).append(str(query_path))
28+
29+
fail = False
30+
for query_id, query_paths in query_ids.items():
31+
if len(query_paths) > 1:
32+
fail = True
33+
print(f"Query ID {query_id} is used in multiple queries:")
34+
for query_path in query_paths:
35+
print(f" - {query_path}")
36+
37+
if fail:
38+
print("FAIL: duplicate query IDs found in src folders. Please assign these queries unique IDs.")
39+
sys.exit(1)
40+
else:
41+
print("PASS: no duplicate query IDs found in src folders.")
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)