From 5abfa6b226c94a41844f46a7ec87f0c8d892ba8a Mon Sep 17 00:00:00 2001 From: ggman12 Date: Thu, 12 Feb 2026 12:13:54 -0500 Subject: [PATCH] update submission validation --- .../validate-community-submission.yaml | 8 +++++- src/contributions/schema.py | 27 +++++++++++++++---- src/contributions/validate_submission.py | 16 +++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/.github/workflows/validate-community-submission.yaml b/.github/workflows/validate-community-submission.yaml index dbb1a34..ed2973d 100644 --- a/.github/workflows/validate-community-submission.yaml +++ b/.github/workflows/validate-community-submission.yaml @@ -30,11 +30,17 @@ jobs: ${{ github.event.issue.body }} 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 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_REPOSITORY: ${{ github.repository }} run: | 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 }} diff --git a/src/contributions/schema.py b/src/contributions/schema.py index 3bc9539..8d9994b 100644 --- a/src/contributions/schema.py +++ b/src/contributions/schema.py @@ -54,7 +54,9 @@ def extract_json_from_issue_body(body: str) -> str | None: """ 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: body: The issue body text @@ -62,13 +64,28 @@ def extract_json_from_issue_body(body: str) -> str | None: Returns: Extracted JSON string or None if not found """ - # Match JSON in "### Submission JSON" section - pattern = r"### Submission JSON\s*\n\s*```(?:json)?\s*\n([\s\S]*?)\n\s*```" - match = re.search(pattern, body) - + # Try: JSON in code blocks after "### Submission JSON" + pattern_codeblock = r"### Submission JSON\s*\n\s*```(?:json)?\s*\n([\s\S]*?)\n\s*```" + match = re.search(pattern_codeblock, body) if match: 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 diff --git a/src/contributions/validate_submission.py b/src/contributions/validate_submission.py index e4d45b6..8b19732 100644 --- a/src/contributions/validate_submission.py +++ b/src/contributions/validate_submission.py @@ -7,6 +7,7 @@ submissions when issues are opened or edited. Usage: 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 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") 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-file", help="File containing issue body text") source_group.add_argument("--file", help="JSON file to validate") 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." ) 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: with open(args.file) as f: json_str = f.read()