update submission validation

This commit is contained in:
ggman12
2026-02-12 12:13:54 -05:00
parent a743b74ae5
commit 5abfa6b226
3 changed files with 45 additions and 6 deletions
@@ -30,11 +30,17 @@ jobs:
${{ github.event.issue.body }} ${{ github.event.issue.body }}
ISSUE_BODY_EOF ISSUE_BODY_EOF
- name: Save issue body to file
run: |
cat << 'ISSUE_BODY_EOF' > /tmp/issue_body.txt
${{ github.event.issue.body }}
ISSUE_BODY_EOF
- name: Validate submission - name: Validate submission
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_REPOSITORY: ${{ github.repository }}
run: | run: |
python -m src.contributions.validate_submission \ python -m src.contributions.validate_submission \
--issue-body "${{ github.event.issue.body }}" \ --issue-body-file /tmp/issue_body.txt \
--issue-number ${{ github.event.issue.number }} --issue-number ${{ github.event.issue.number }}
+22 -5
View File
@@ -54,7 +54,9 @@ def extract_json_from_issue_body(body: str) -> str | None:
""" """
Extract JSON from GitHub issue body. Extract JSON from GitHub issue body.
Looks for JSON in the 'Submission JSON' section wrapped in code blocks. Looks for JSON in the 'Submission JSON' section, either:
- Wrapped in code blocks (```json ... ``` or ``` ... ```)
- Or raw JSON after the header
Args: Args:
body: The issue body text body: The issue body text
@@ -62,13 +64,28 @@ def extract_json_from_issue_body(body: str) -> str | None:
Returns: Returns:
Extracted JSON string or None if not found Extracted JSON string or None if not found
""" """
# Match JSON in "### Submission JSON" section # Try: JSON in code blocks after "### Submission JSON"
pattern = r"### Submission JSON\s*\n\s*```(?:json)?\s*\n([\s\S]*?)\n\s*```" pattern_codeblock = r"### Submission JSON\s*\n\s*```(?:json)?\s*\n([\s\S]*?)\n\s*```"
match = re.search(pattern, body) match = re.search(pattern_codeblock, body)
if match: if match:
return match.group(1).strip() return match.group(1).strip()
# Try: Raw JSON after "### Submission JSON" until next section or end
pattern_raw = r"### Submission JSON\s*\n\s*([\[{][\s\S]*?[\]}])(?=\n###|\n\n###|$)"
match = re.search(pattern_raw, body)
if match:
return match.group(1).strip()
# Try: Any JSON object/array in the body (fallback)
pattern_any = r"([\[{][\s\S]*?[\]}])"
for match in re.finditer(pattern_any, body):
candidate = match.group(1).strip()
# Validate it looks like JSON
if candidate.startswith('{') and candidate.endswith('}'):
return candidate
if candidate.startswith('[') and candidate.endswith(']'):
return candidate
return None return None
+16
View File
@@ -7,6 +7,7 @@ submissions when issues are opened or edited.
Usage: Usage:
python -m src.contributions.validate_submission --issue-body "..." python -m src.contributions.validate_submission --issue-body "..."
python -m src.contributions.validate_submission --issue-body-file /path/to/body.txt
python -m src.contributions.validate_submission --file submission.json python -m src.contributions.validate_submission --file submission.json
echo '{"registration_number": "N12345"}' | python -m src.contributions.validate_submission --stdin echo '{"registration_number": "N12345"}' | python -m src.contributions.validate_submission --stdin
@@ -106,6 +107,7 @@ def main():
parser = argparse.ArgumentParser(description="Validate community submission JSON") parser = argparse.ArgumentParser(description="Validate community submission JSON")
source_group = parser.add_mutually_exclusive_group(required=True) source_group = parser.add_mutually_exclusive_group(required=True)
source_group.add_argument("--issue-body", help="Issue body text containing JSON") source_group.add_argument("--issue-body", help="Issue body text containing JSON")
source_group.add_argument("--issue-body-file", help="File containing issue body text")
source_group.add_argument("--file", help="JSON file to validate") source_group.add_argument("--file", help="JSON file to validate")
source_group.add_argument("--stdin", action="store_true", help="Read JSON from stdin") source_group.add_argument("--stdin", action="store_true", help="Read JSON from stdin")
@@ -125,6 +127,20 @@ def main():
"Please ensure your JSON is in the 'Submission JSON' field wrapped in code blocks." "Please ensure your JSON is in the 'Submission JSON' field wrapped in code blocks."
) )
sys.exit(1) sys.exit(1)
elif args.issue_body_file:
with open(args.issue_body_file) as f:
issue_body = f.read()
json_str = extract_json_from_issue_body(issue_body)
if not json_str:
print("❌ Could not extract JSON from issue body", file=sys.stderr)
print(f"Issue body:\n{issue_body}", file=sys.stderr)
if args.issue_number:
add_issue_comment(
args.issue_number,
"❌ **Validation Failed**\n\nCould not extract JSON from submission. "
"Please ensure your JSON is in the 'Submission JSON' field."
)
sys.exit(1)
elif args.file: elif args.file:
with open(args.file) as f: with open(args.file) as f:
json_str = f.read() json_str = f.read()