-
Notifications
You must be signed in to change notification settings - Fork 0
chore: adds script and workflow to post docs #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| name: POST New Documents on Merge | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| post: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.11' | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install -r requirements.txt | ||
|
|
||
| - name: Get added files in latest commit | ||
| id: added | ||
| run: | | ||
| files=$(git diff --diff-filter=A HEAD~1 HEAD --name-only | grep -E '^(sources|claims|proofs)/' || true) | ||
| echo "Added files:" | ||
| echo "$files" | ||
| echo "ADDED_FILES<<EOF" >> "$GITHUB_ENV" | ||
| echo "$files" >> "$GITHUB_ENV" | ||
| echo "EOF" >> "$GITHUB_ENV" | ||
|
|
||
| - name: POST new documents | ||
| if: env.ADDED_FILES != '' | ||
| run: python scripts/post_requests.py | ||
| env: | ||
| API_BASE_URL: ${{ secrets.API_BASE_URL }} | ||
| API_KEY: ${{ secrets.API_KEY }} | ||
|
|
||
| - name: Nothing to POST | ||
| if: env.ADDED_FILES == '' | ||
| run: echo "No new request documents to POST." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| **/__pycache__/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env python3 | ||
| """Shared utilities for scripts in this folder. | ||
|
|
||
| Exports: | ||
| - load_oapi(path) -> dict | ||
| - load_doc(path) -> dict (YAML with JSON fallback) | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| import json | ||
| import yaml | ||
| from typing import Any, Dict | ||
|
|
||
|
|
||
| def load_oapi(path: str) -> Dict[str, Any]: | ||
| with open(path) as f: | ||
| return yaml.safe_load(f) | ||
|
|
||
|
|
||
| def load_doc(path: str) -> Dict[str, Any]: | ||
| with open(path) as f: | ||
| content = f.read() | ||
| try: | ||
| return yaml.safe_load(content) | ||
| except yaml.YAMLError: | ||
| return json.loads(content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| #!/usr/bin/env python3 | ||
| """POST newly added request documents to their respective APIs. | ||
|
|
||
| Requires environment variables: | ||
| API_BASE_URL API server base URL | ||
| API_KEY API token for authentication | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import sys | ||
| import urllib.error | ||
| import urllib.request | ||
| from pathlib import Path | ||
|
|
||
| # Shared utilities (try package import first, fallback to local module) | ||
| try: | ||
| from scripts.common import load_oapi, load_doc | ||
| except ImportError: | ||
| sys.path.insert(0, os.path.dirname(__file__)) | ||
| from common import load_oapi, load_doc | ||
|
|
||
| # folder -> schema name | ||
| SCHEMA_MAP = { | ||
|
semmet95 marked this conversation as resolved.
|
||
| "sources": "SourceInput", | ||
| "claims": "ClaimInput", | ||
| "proofs": "ProofInput", | ||
| } | ||
|
|
||
| def extract_post_paths(spec: dict) -> dict[str, str]: | ||
| """Map schema names to path suffixes from the OpenAPI spec.""" | ||
| paths = {} | ||
| for path, methods in spec.get("paths", {}).items(): | ||
| post = methods.get("post") | ||
| if not post: | ||
| continue | ||
|
|
||
| content = post.get("requestBody", {}).get("content", {}) | ||
| json_schema = content.get("application/json", {}).get("schema", {}) | ||
| ref = json_schema.get("$ref", "") | ||
|
|
||
| if ref.startswith("#/components/schemas/"): | ||
| schema_name = ref.split("/")[-1] | ||
| paths[schema_name] = path | ||
|
|
||
| return paths | ||
|
|
||
| def post(url: str, data: dict, api_key: str) -> tuple[int, str]: | ||
| payload = json.dumps(data).encode() | ||
| headers = { | ||
|
semmet95 marked this conversation as resolved.
|
||
| "Content-Type": "application/json", | ||
| "X-API-Key": f"{api_key}", | ||
| } | ||
|
|
||
| req = urllib.request.Request(url, data=payload, headers=headers, method="POST") | ||
| try: | ||
| with urllib.request.urlopen(req) as resp: | ||
| return resp.status, resp.read().decode() | ||
| except urllib.error.HTTPError as e: | ||
| return e.code, e.read().decode() | ||
|
|
||
|
|
||
| def main() -> int: | ||
| base_url = os.environ.get("API_BASE_URL", "").rstrip("/") | ||
|
semmet95 marked this conversation as resolved.
|
||
| api_key = os.environ.get("API_KEY", "") | ||
|
|
||
| if not base_url: | ||
| print("API_BASE_URL environment variable is not set", file=sys.stderr) | ||
| return 1 | ||
| if not api_key: | ||
| print("API_KEY environment variable is not set", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| files = [f for f in os.environ.get("ADDED_FILES", "").splitlines() if f.strip()] | ||
| if not files: | ||
| print("No added files to process.") | ||
| return 0 | ||
|
|
||
| spec = load_oapi("oapi.yaml") | ||
| schema_paths = extract_post_paths(spec) | ||
|
|
||
| failed = False | ||
| for f in files: | ||
| f = f.strip() | ||
| parts = Path(f).parts | ||
| if not parts or parts[0] not in SCHEMA_MAP: | ||
| continue | ||
|
|
||
| folder = parts[0] | ||
| schema_name = SCHEMA_MAP[folder] | ||
| path = schema_paths.get(schema_name) | ||
| if not path: | ||
| print(f"No POST path found for schema {schema_name}, skipping {f}") | ||
| failed = True | ||
| continue | ||
|
|
||
| url = f"{base_url}{path}" | ||
|
|
||
| if not Path(f).exists(): | ||
| print(f"{f}: File not found") | ||
| failed = True | ||
| continue | ||
|
|
||
| try: | ||
| data = load_doc(f) | ||
| except Exception as e: | ||
| print(f"{f}: Failed to parse: {e}") | ||
| failed = True | ||
| continue | ||
|
|
||
| status, body = post(url, data, api_key) | ||
| if 200 <= status < 300: | ||
| print(f"{f} → {url} ({status})") | ||
| else: | ||
| print(f"{f} → {url} ({status}): {body[:200]}") | ||
| failed = True | ||
|
|
||
| return 1 if failed else 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.