Fix links in OpenAPI description nodes (#3598)

Signed-off-by: Alexandre Franke <alexandre.franke@matrix.org>
pull/977/head
Alexandre Franke 3 years ago committed by GitHub
parent 00ee4d7010
commit b55cc15772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -38,19 +38,46 @@ jobs:
- name: "🔎 Run validator" - name: "🔎 Run validator"
run: | run: |
/env/bin/python scripts/check-event-schema-examples.py /env/bin/python scripts/check-event-schema-examples.py
calculate-baseurl:
name: "⚙️ Calculate baseURL for later jobs"
runs-on: ubuntu-latest
outputs:
baseURL: "${{ steps.set-baseurl.outputs.baseURL }}"
steps:
# For PRs, set the baseURL to `/`.
# For releases, set the baseURL to `/$tag` (eg: `/v1.2`).
# Otherwise, set it to `/unstable`.
- name: "⚙️ Calculate baseURL"
id: set-baseurl
# Double brackets on the elif to avoid auto-escaping refs/tags/* because we need
# the asterisk matching behaviour, not the literal string.
run: |
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ]; then
echo ::set-output name=baseURL::/
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
echo ::set-output name=baseURL::"/${GITHUB_REF/refs\/tags\//}"
else
echo ::set-output name=baseURL::/unstable
fi
build-openapi: build-openapi:
name: "🐍 Build OpenAPI definitions" name: "🐍 Build OpenAPI definitions"
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: "python:3.9" container: "python:3.9"
needs: [calculate-baseurl]
steps: steps:
- name: "📥 Source checkout" - name: "📥 Source checkout"
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: "📦 Asset creation" - name: "📦 Asset creation"
run: | run: |
mkdir -p assets
python3 -m venv env && . env/bin/activate python3 -m venv env && . env/bin/activate
pip install -r scripts/requirements.txt pip install -r scripts/requirements.txt
scripts/generate-matrix-org-assets scripts/dump-swagger.py \
--base-url "${{ needs.calculate-baseurl.outputs.baseURL }}" \
-o assets/spec/client_server/api.json
tar -czf assets.tar.gz assets
- name: "📤 Artifact upload" - name: "📤 Artifact upload"
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
with: with:
@ -60,7 +87,7 @@ jobs:
build-spec: build-spec:
name: "📖 Build the spec" name: "📖 Build the spec"
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: [build-openapi] needs: [calculate-baseurl, build-openapi]
steps: steps:
- name: " Setup Node" - name: " Setup Node"
uses: actions/setup-node@v2 uses: actions/setup-node@v2
@ -79,25 +106,8 @@ jobs:
run: | run: |
npm i npm i
npm run get-proposals npm run get-proposals
# For PRs, set the baseURL to `/`.
# For releases, set the baseURL to `/$tag` (eg: `/v1.2`).
# Otherwise, set it to `/unstable`.
- name: "⚙️ Calculate baseURL"
id: set-baseurl
# Double brackets on the elif to avoid auto-escaping refs/tags/* because we need
# the asterisk matching behaviour, not the literal string.
run: |
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ]; then
echo ::set-output name=baseURL::/
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
echo ::set-output name=baseURL::"/${GITHUB_REF/refs\/tags\//}"
else
echo ::set-output name=baseURL::/unstable
fi
- name: "⚙️ hugo" - name: "⚙️ hugo"
run: hugo --baseURL "${{ steps.set-baseurl.outputs.baseURL }}" -d "spec" run: hugo --baseURL "${{ needs.calculate-baseurl.outputs.baseURL }}" -d "spec"
# We manually copy the spec OpenAPI definition JSON to the website tree # We manually copy the spec OpenAPI definition JSON to the website tree
# to make it available to the world in a canonical place: # to make it available to the world in a canonical place:

@ -53,10 +53,39 @@ def resolve_references(path, schema):
else: else:
return schema return schema
def prefix_absolute_path_references(text, base_url):
"""Adds base_url to absolute-path references.
Markdown links in descriptions may be absolute-path references.
These wont work when the spec is not hosted at the root, such as
https://spec.matrix.org/latest/
This turns all `[foo](/bar)` found in text into
`[foo](https://spec.matrix.org/latest/bar)`, with
base_url = 'https://spec.matrix.org/latest/'
"""
return text.replace("](/", "]({}/".format(base_url))
def edit_links(node, base_url):
"""Finds description nodes and makes any links in them absolute."""
if isinstance(node, dict):
for key in node:
if isinstance(node[key], str):
node[key] = prefix_absolute_path_references(node[key], base_url)
else:
edit_links(node[key], base_url)
elif isinstance(node, list):
for item in node:
edit_links(item, base_url)
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
"dump-swagger.py - assemble the Swagger specs into a single JSON file" "dump-swagger.py - assemble the Swagger specs into a single JSON file"
) )
parser.add_argument(
"--base-url", "-b",
default="https://spec.matrix.org/unstable/",
help="""The base URL to prepend to links in descriptions. Default:
%(default)s""",
)
parser.add_argument( parser.add_argument(
"--client_release", "-c", metavar="LABEL", "--client_release", "-c", metavar="LABEL",
default="unstable", default="unstable",
@ -78,6 +107,8 @@ match = re.match("^(r\d+)(\.\d+)*$", major_version)
if match: if match:
major_version = match.group(1) major_version = match.group(1)
base_url = args.base_url.rstrip("/")
logging.basicConfig() logging.basicConfig()
output = { output = {
@ -130,6 +161,8 @@ for filename in os.listdir(cs_api_dir):
output["paths"][path] = {} output["paths"][path] = {}
output["paths"][path][method] = spec output["paths"][path][method] = spec
edit_links(output, base_url)
print("Generating %s" % output_file) print("Generating %s" % output_file)
try: try:

@ -1,15 +0,0 @@
#!/bin/sh
#
# generate a tarball of assets suitable for the matrix.org site
set -e
cd `dirname $0`/..
mkdir -p assets
# and the swagger
./scripts/dump-swagger.py -o assets/spec/client_server/api.json
# create a tarball of the assets.
tar -czf assets.tar.gz assets
Loading…
Cancel
Save