Skip to content

Commit 6988ff0

Browse files
Add validation job
1 parent be195a0 commit 6988ff0

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

.github/workflows/update-data.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ on:
66
branches: ['main']
77

88
jobs:
9-
update-data:
9+
validate-questions:
1010
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
15+
- name: Validate questions
16+
run: python scripts/validate_questions.py
17+
shell: sh
1118

19+
update-data:
20+
runs-on: ubuntu-latest
1221
steps:
1322
- name: Call webhook
1423
run: curl -X POST ${{secrets.DEPLOY_WEBHOOK}}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Validate questions
2+
3+
# Configures this workflow to run every time a change is pushed to the branch called `release`.
4+
on: [pull_request]
5+
6+
jobs:
7+
validate-questions:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v4
12+
13+
- name: Validate questions
14+
run: python scripts/validate_questions.py
15+
shell: sh

scripts/validate_questions.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
import json
3+
import logging
4+
5+
logging.basicConfig(level=logging.DEBUG)
6+
7+
for filename in os.listdir("data/questions"):
8+
logging.info("Analyzing " + filename)
9+
with open(os.path.join("data/questions", filename), 'r') as f:
10+
text = f.read()
11+
try:
12+
data = json.loads(text)
13+
14+
for q in data:
15+
if q["quest"] == "" and q["image"] == "":
16+
raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.")
17+
if q["quest"] is None or q["image"] is None or q["answers"] is None or q["correct"] is None:
18+
raise Exception(str(data.index(q)) + ") Some parameters are null or missing.")
19+
20+
if len(q["answers"]) == 0:
21+
raise Exception(str(data.index(q)) + ") Question has no answers.")
22+
23+
for a in q["answers"]:
24+
if a["text"] == "" and a["image"] == "":
25+
raise Exception(str(data.index(q)) + ") Answer's text and image cannot both be empty.")
26+
if a["text"] is None or a["image"] is None:
27+
raise Exception(str(data.index(q)) + ") Some answer's parameters are null or missing.")
28+
29+
except Exception as e:
30+
logging.error(getattr(e, 'message', repr(e)))
31+
logging.fatal(filename + " is invalid. Aborting.")
32+
33+
logging.info("Parsing successful!")

0 commit comments

Comments
 (0)