-
Notifications
You must be signed in to change notification settings - Fork 3
Weblate outbound POST JSON schema + schema-driven bats #58
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
3 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
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
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,46 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "$id": "https://github.com/cppalliance/boost-docs-translation/docs/schemas/weblate-add-or-update.request.schema.json", | ||
| "title": "Weblate add-or-update request", | ||
| "description": "Outbound POST body built by trigger_weblate for WEBLATE_ENDPOINT_PATH.", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": ["organization", "version", "extensions", "add_or_update"], | ||
| "properties": { | ||
| "organization": { | ||
| "type": "string", | ||
| "minLength": 1, | ||
| "description": "GitHub org hosting library mirror repos (MODULE_ORG)." | ||
| }, | ||
| "version": { | ||
| "type": "string", | ||
| "minLength": 1, | ||
| "description": "Boost libs ref (e.g. develop or boost-1.90.0)." | ||
| }, | ||
| "extensions": { | ||
| "type": "array", | ||
| "description": "Dot-prefixed file extensions filter for Weblate; empty means no filter.", | ||
| "items": { | ||
| "type": "string", | ||
| "pattern": "^\\..+" | ||
| } | ||
| }, | ||
| "add_or_update": { | ||
| "type": "object", | ||
| "minProperties": 1, | ||
| "description": "Map of language code to non-empty arrays of submodule basenames.", | ||
| "propertyNames": { | ||
| "type": "string", | ||
| "pattern": "^[A-Za-z]{2,3}([_-][A-Za-z0-9]{2,8})*$" | ||
| }, | ||
| "additionalProperties": { | ||
| "type": "array", | ||
| "minItems": 1, | ||
| "items": { | ||
| "type": "string", | ||
| "pattern": "^[a-z][a-z0-9._-]*$" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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,57 @@ | ||
| #!/usr/bin/env bash | ||
| # Bootstrap pinned check-jsonschema into .cache/ (shared by lint.sh and test helpers). | ||
| # Safe to source: does not enable set -euo (callers own shell options). | ||
| # shellcheck shell=bash | ||
|
|
||
| CHECK_JSONSCHEMA_VERSION="${CHECK_JSONSCHEMA_VERSION:-0.37.4}" | ||
|
|
||
| ensure_check_jsonschema() { | ||
| local root version cache_dir venv_dir bin marker | ||
| root="${REPO_ROOT:-}" | ||
| if [[ -z "$root" ]]; then | ||
| root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| fi | ||
| version="$CHECK_JSONSCHEMA_VERSION" | ||
| # Reject path separators / .. so version cannot escape cache_dir via venv_dir. | ||
| case "$version" in | ||
| '' | */* | *\\* | *..*) | ||
| echo "ensure_check_jsonschema: invalid CHECK_JSONSCHEMA_VERSION: ${version}" >&2 | ||
| return 1 | ||
| ;; | ||
| esac | ||
| cache_dir="$root/.cache/check-jsonschema" | ||
| venv_dir="$cache_dir/${version}" | ||
| bin="$venv_dir/bin/check-jsonschema" | ||
| marker="$venv_dir/.installed" | ||
|
|
||
| if [[ -x "$bin" && -f "$marker" ]]; then | ||
| CHECK_JSONSCHEMA_BIN="$bin" | ||
| export CHECK_JSONSCHEMA_BIN | ||
| return 0 | ||
| fi | ||
|
|
||
| if ! command -v python3 >/dev/null 2>&1; then | ||
| echo "ensure_check_jsonschema: python3 is required" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| echo "ensure_check_jsonschema: installing check-jsonschema==${version} into ${venv_dir}..." >&2 | ||
| rm -rf "$venv_dir" | ||
| python3 -m venv "$venv_dir" || return 1 | ||
| "$venv_dir/bin/pip" install --disable-pip-version-check --quiet \ | ||
| "check-jsonschema==${version}" || return 1 | ||
| if [[ ! -x "$bin" ]]; then | ||
| echo "ensure_check_jsonschema: expected binary missing at $bin" >&2 | ||
| return 1 | ||
| fi | ||
| printf '%s\n' "$version" >"$marker" | ||
| CHECK_JSONSCHEMA_BIN="$bin" | ||
| export CHECK_JSONSCHEMA_BIN | ||
| } | ||
|
|
||
| # When sourced, define the function; when executed, run it and print the bin path. | ||
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | ||
| set -euo pipefail | ||
| ensure_check_jsonschema | ||
| printf '%s\n' "$CHECK_JSONSCHEMA_BIN" | ||
| fi | ||
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
8 changes: 8 additions & 0 deletions
8
tests/helpers/fixtures/weblate-add-or-update-request-valid.json
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,8 @@ | ||
| { | ||
| "organization": "testorg", | ||
| "version": "develop", | ||
| "extensions": [".adoc"], | ||
| "add_or_update": { | ||
| "en": ["algorithm", "system"] | ||
| } | ||
| } |
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,55 @@ | ||
| # shellcheck shell=bash | ||
| # JSON Schema helpers for bats (check-jsonschema + request/response extractors). | ||
| # shellcheck disable=SC1091 | ||
|
|
||
| SCHEMAS_DIR="${SCHEMAS_DIR:-$REPO_ROOT/docs/schemas}" | ||
|
|
||
| # shellcheck source=../../scripts/ensure_check_jsonschema.sh | ||
| source "$REPO_ROOT/scripts/ensure_check_jsonschema.sh" | ||
|
|
||
| # Validate a JSON string against a schema file. Writes a temp instance file. | ||
| validate_json_against_schema() { | ||
| local json="$1" schema_path="$2" tmp | ||
| ensure_check_jsonschema || return 1 | ||
| tmp="$(mktemp)" | ||
| printf '%s\n' "$json" >"$tmp" | ||
| "$CHECK_JSONSCHEMA_BIN" --schemafile "$schema_path" "$tmp" | ||
| local rc=$? | ||
| rm -f "$tmp" | ||
| return "$rc" | ||
| } | ||
|
|
||
| # Extract POST body from the Weblate mock request log (BODY_START … BODY_END). | ||
| extract_weblate_request_body_from_log() { | ||
| local log_file="$1" | ||
| sed -n '/^BODY_START$/,/^BODY_END$/p' "$log_file" | sed '1d;$d' | ||
| } | ||
|
|
||
| # Extract the last JSON object from a stderr/log file (pretty-printed or one-line). | ||
| extract_json_object_from_log() { | ||
| local log_file="$1" | ||
| python3 -c ' | ||
| import json, sys | ||
| text = open(sys.argv[1], encoding="utf-8", errors="replace").read() | ||
| decoder = json.JSONDecoder() | ||
| candidates = [] | ||
| i = 0 | ||
| n = len(text) | ||
| while i < n: | ||
| start = text.find("{", i) | ||
| if start < 0: | ||
| break | ||
| try: | ||
| obj, end = decoder.raw_decode(text, start) | ||
| except json.JSONDecodeError: | ||
| i = start + 1 | ||
| continue | ||
| if isinstance(obj, dict): | ||
| candidates.append(obj) | ||
| i = end | ||
| for obj in reversed(candidates): | ||
| print(json.dumps(obj)) | ||
| sys.exit(0) | ||
| sys.exit(1) | ||
| ' "$log_file" | ||
| } |
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
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.