Skip to content

Commit 9a495ce

Browse files
authored
Merge pull request #1303 from cloudbees-oss/feat/AIENG-420-schema
feat(AIENG:420): Add command to download OpenAPI schema for Smart Tes…
2 parents b6cbdef + 2ae8825 commit 9a495ce

5 files changed

Lines changed: 2963 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ test = "python -m unittest"
5454
test-xml = "python -m test-runner"
5555
type = "mypy smart_tests tests"
5656
update-docs = "bash scripts/update_docs.sh"
57+
update-schema = "bash scripts/update_schema.sh"
5758

5859
[build-system]
5960
requires = ["setuptools>=45", "wheel", "setuptools_scm"]
@@ -63,6 +64,6 @@ build-backend = "setuptools.build_meta"
6364
packages = ["smart_tests"]
6465

6566
[tool.setuptools.package-data]
66-
smart_tests = ["jar/exe_deploy.jar", "docs/**/*"]
67+
smart_tests = ["jar/exe_deploy.jar", "docs/**/*", "schema/**/*"]
6768

6869
[tool.setuptools_scm]

scripts/update_schema.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
# Update the bundled OpenAPI schema from mothership dev server
3+
set -e
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
CLI_DIR="$(dirname "$SCRIPT_DIR")"
7+
SCHEMA_FILE="$CLI_DIR/smart_tests/schema/openapi-schema.json"
8+
9+
echo "Fetching OpenAPI schema from local dev server..."
10+
11+
# Check if mothership is running
12+
if ! curl -s -f http://localhost:8080/intake/v3/api-docs > /dev/null 2>&1; then
13+
echo "Error: Mothership dev server not running at localhost:8080"
14+
echo "Please start it with: cd mothership && bazel run //src/main/java/com/launchableinc/mercury/intake"
15+
exit 1
16+
fi
17+
18+
# Create schema directory if it doesn't exist
19+
mkdir -p "$(dirname "$SCHEMA_FILE")"
20+
21+
# Fetch and save schema
22+
curl -s http://localhost:8080/intake/v3/api-docs > "$SCHEMA_FILE"
23+
24+
# Validate and format the JSON with proper indentation and trailing newline
25+
python3 << PYTHON_EOF
26+
import json
27+
28+
with open("$SCHEMA_FILE", 'r') as f:
29+
schema = json.load(f)
30+
31+
# Write back with proper formatting
32+
with open("$SCHEMA_FILE", 'w') as f:
33+
json.dump(schema, f, indent=2)
34+
f.write('\n') # Add trailing newline
35+
PYTHON_EOF
36+
37+
echo "✓ Schema updated successfully: $SCHEMA_FILE"
38+
echo ""
39+
40+
# Show what changed
41+
if git diff --quiet "$SCHEMA_FILE" 2>/dev/null; then
42+
echo "No changes detected in schema"
43+
else
44+
echo "Changes detected:"
45+
git diff --stat "$SCHEMA_FILE" 2>/dev/null || echo "(Not a git repository)"
46+
echo ""
47+
echo "Review with: git diff $SCHEMA_FILE"
48+
echo "Commit with: git add $SCHEMA_FILE && git commit -m 'Update OpenAPI schema'"
49+
fi

smart_tests/commands/get/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ... import args4p
22
from ...app import Application
3+
from .api_schema import api_schema
34
from .docs import docs
45

56

@@ -8,4 +9,5 @@ def get(app: Application):
89
return app
910

1011

12+
get.add_command(api_schema)
1113
get.add_command(docs)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import importlib.resources
2+
import shutil
3+
import sys
4+
from pathlib import Path
5+
6+
import click
7+
8+
from smart_tests import args4p
9+
from smart_tests.app import Application
10+
11+
OUTPUT_FILE = 'openapi-schema.json'
12+
13+
14+
@args4p.command(help="Copy OpenAPI schema to ./openapi-schema.json")
15+
def api_schema(app: Application):
16+
output = Path(OUTPUT_FILE)
17+
if output.exists():
18+
click.secho(
19+
f"'{OUTPUT_FILE}' already exists. Please delete it first, then re-run this command.",
20+
fg='red', err=True,
21+
)
22+
sys.exit(1)
23+
24+
# Copy bundled schema file to current directory
25+
schema_src = Path(str(importlib.resources.files('smart_tests') / 'schema' / 'openapi-schema.json'))
26+
click.echo(f"Copying OpenAPI schema to ./{OUTPUT_FILE} ...")
27+
shutil.copy(schema_src, output)
28+
click.echo(f"Done. OpenAPI schema is in ./{OUTPUT_FILE}")

0 commit comments

Comments
 (0)