Skip to content

Commit

Permalink
Makes the OpenAPI docs generation more robust (#14175)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguidry committed Jun 20, 2024
1 parent c1dfa50 commit be8cbda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ repos:
name: Generating OpenAPI docs for Mintlify
language: system
entry: ./scripts/generate_mintlify_openapi_docs.py
pass_filenames: false
files: |
(?x)^(
.pre-commit-config.yaml|
src/prefect/server/.*|
src/prefect/server/api/.*|
src/prefect/server/schemas/.*|
scripts/generate_mintlify_openapi_docs.py
)$
30 changes: 15 additions & 15 deletions scripts/generate_mintlify_openapi_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
SinglePage = str
PageGroup = dict[str, Any]

MINTLIFY_SCRAPE = ["npx", "--yes", "@mintlify/scraping@3.0.123"]


def docs_path() -> Path:
return Path(__file__).parent.parent / "docs"
Expand All @@ -30,7 +32,7 @@ def current_version() -> str:


def main():
ensure_mintlify_installed()
ensure_npx_environment()

version = current_version()
server_docs_path = docs_path() / f"{version}/api-ref/server/"
Expand All @@ -50,23 +52,16 @@ def main():
write_mint(mint_json)


def ensure_mintlify_installed():
result = subprocess.run(["which", "mintlify-scrape"], capture_output=True)
if result.returncode == 0:
return

result = subprocess.run(["which", "npm"], capture_output=True)
def ensure_npx_environment():
result = subprocess.run(["which", "npx"], capture_output=True)
if result.returncode != 0:
print(
"Neither `@mintlify/scraping` nor `npm` are installed. Please make sure "
"you have a working `npm` installation before generating API docs.",
"`npx` is not installed. Please make sure you have a working `npm` "
"installation before generating API docs.",
file=sys.stderr,
)
sys.exit(1)

print("@mintlify/scraping is not installed. Installing...")
subprocess.check_call(["npm", "install", "-g", "@mintlify/scraping"])


def generate_schema_documentation(version: str, server_docs_path: Path) -> Navigation:
"""Write the current OpenAPI schema to the given path, generates documentation files
Expand All @@ -80,8 +75,8 @@ def generate_schema_documentation(version: str, server_docs_path: Path) -> Navig
f.flush()

result = subprocess.run(
[
"mintlify-scrape",
MINTLIFY_SCRAPE
+ [
"openapi-file",
schema_path,
"-o",
Expand All @@ -96,7 +91,12 @@ def generate_schema_documentation(version: str, server_docs_path: Path) -> Navig
# mintlify-scrape will output a list of suggestions for navigation objects, prefixed
# with a header "navigation object suggestion:"
output = result.stdout.replace("navigation object suggestion:\n", "")
suggestions = json.loads(output)
try:
suggestions = json.loads(output)
except Exception:
print("Couldn't understand the output of mintlify-scrape:", file=sys.stderr)
print(output, file=sys.stderr)
raise
return suggestions


Expand Down

0 comments on commit be8cbda

Please sign in to comment.