|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Verify that the checked-in Python and Julia API clients match what |
| 5 | +# openapi-generator would produce from the current api/openapi.yaml. |
| 6 | +# Exits non-zero and prints a diff when drift is detected. |
| 7 | + |
| 8 | +OPENAPI_CLI_VERSION="${OPENAPI_CLI_VERSION:-v7.16.0}" |
| 9 | +OPENAPI_CLI_DIGEST="sha256:e56372add5e038753fb91aa1bbb470724ef58382fdfc35082bf1b3e079ce353c" |
| 10 | +CONTAINER_EXEC="${CONTAINER_EXEC:-docker}" |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 13 | +SPEC_PATH="${SCRIPT_DIR}/openapi.yaml" |
| 14 | +API_VERSION="$( |
| 15 | + awk -F'"' '/pub const HTTP_API_VERSION:/ { print $2; exit }' "${REPO_ROOT}/src/api_version.rs" |
| 16 | +)" |
| 17 | + |
| 18 | +if [[ -z "${API_VERSION}" ]]; then |
| 19 | + echo "Failed to read HTTP API version from src/api_version.rs" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +SPEC_DIR="$(cd "$(dirname "${SPEC_PATH}")" && pwd)" |
| 24 | +SPEC_FILE="$(basename "${SPEC_PATH}")" |
| 25 | + |
| 26 | +TMP_PYTHON="$(mktemp -d "${TMPDIR:-/tmp}/torc-py-check.XXXXXX")" |
| 27 | +TMP_JULIA="$(mktemp -d "${TMPDIR:-/tmp}/torc-jl-check.XXXXXX")" |
| 28 | + |
| 29 | +# shellcheck disable=SC2329,SC2317 # invoked indirectly via trap |
| 30 | +cleanup_tmp() { |
| 31 | + # Docker may create root-owned files that the CI runner cannot delete directly. |
| 32 | + # Use a container to remove them, then clean up the (now-empty) temp dirs. |
| 33 | + for d in "${TMP_PYTHON}" "${TMP_JULIA}"; do |
| 34 | + "${CONTAINER_EXEC}" run --rm -v "${d}":/tmp_clean alpine rm -rf /tmp_clean/* 2>/dev/null || true |
| 35 | + rm -rf "${d}" 2>/dev/null || true |
| 36 | + done |
| 37 | +} |
| 38 | +trap cleanup_tmp EXIT |
| 39 | + |
| 40 | +docker_run() { |
| 41 | + case "${OSTYPE:-}" in |
| 42 | + msys*|cygwin*) |
| 43 | + MSYS_NO_PATHCONV=1 "${CONTAINER_EXEC}" "$@" |
| 44 | + ;; |
| 45 | + *) |
| 46 | + "${CONTAINER_EXEC}" "$@" |
| 47 | + ;; |
| 48 | + esac |
| 49 | +} |
| 50 | + |
| 51 | +echo "Generating Python client from ${SPEC_FILE}…" |
| 52 | +docker_run run \ |
| 53 | + -v "${SCRIPT_DIR}":/data \ |
| 54 | + -v "${SPEC_DIR}":/spec \ |
| 55 | + -v "${TMP_PYTHON}":/python_client \ |
| 56 | + "docker.io/openapitools/openapi-generator-cli:${OPENAPI_CLI_VERSION}@${OPENAPI_CLI_DIGEST}" \ |
| 57 | + generate -g python --input-spec="/spec/${SPEC_FILE}" -o /python_client -c /data/config.json \ |
| 58 | + --additional-properties=packageVersion="${API_VERSION}" |
| 59 | + |
| 60 | +echo "Generating Julia client from ${SPEC_FILE}…" |
| 61 | +docker_run run \ |
| 62 | + -v "${SCRIPT_DIR}":/data \ |
| 63 | + -v "${SPEC_DIR}":/spec \ |
| 64 | + -v "${TMP_JULIA}":/julia_client \ |
| 65 | + "docker.io/openapitools/openapi-generator-cli:${OPENAPI_CLI_VERSION}@${OPENAPI_CLI_DIGEST}" \ |
| 66 | + generate -g julia-client --input-spec="/spec/${SPEC_FILE}" -o /julia_client \ |
| 67 | + --additional-properties=packageVersion="${API_VERSION}" |
| 68 | + |
| 69 | +RC=0 |
| 70 | + |
| 71 | +echo "Checking Python client parity…" |
| 72 | +if ! diff -rq "${TMP_PYTHON}/torc/openapi_client" \ |
| 73 | + "${REPO_ROOT}/python_client/src/torc/openapi_client" >/dev/null 2>&1; then |
| 74 | + echo "Python client is out of date with ${SPEC_FILE}" >&2 |
| 75 | + diff -ru "${REPO_ROOT}/python_client/src/torc/openapi_client" \ |
| 76 | + "${TMP_PYTHON}/torc/openapi_client" || true |
| 77 | + RC=1 |
| 78 | +else |
| 79 | + echo "Python client is up to date." |
| 80 | +fi |
| 81 | + |
| 82 | +echo "Checking Julia client parity…" |
| 83 | +JULIA_DRIFT=0 |
| 84 | +if ! diff -rq "${TMP_JULIA}/src" \ |
| 85 | + "${REPO_ROOT}/julia_client/Torc/src/api" >/dev/null 2>&1; then |
| 86 | + echo "Julia client API sources are out of date with ${SPEC_FILE}" >&2 |
| 87 | + diff -ru "${REPO_ROOT}/julia_client/Torc/src/api" \ |
| 88 | + "${TMP_JULIA}/src" || true |
| 89 | + JULIA_DRIFT=1 |
| 90 | +fi |
| 91 | +if ! diff -rq "${TMP_JULIA}/docs" \ |
| 92 | + "${REPO_ROOT}/julia_client/julia_client/docs" >/dev/null 2>&1; then |
| 93 | + echo "Julia client docs are out of date with ${SPEC_FILE}" >&2 |
| 94 | + diff -ru "${REPO_ROOT}/julia_client/julia_client/docs" \ |
| 95 | + "${TMP_JULIA}/docs" || true |
| 96 | + JULIA_DRIFT=1 |
| 97 | +fi |
| 98 | +if ! diff -q "${TMP_JULIA}/README.md" \ |
| 99 | + "${REPO_ROOT}/julia_client/julia_client/README.md" >/dev/null 2>&1; then |
| 100 | + echo "Julia client README is out of date with ${SPEC_FILE}" >&2 |
| 101 | + diff -u "${REPO_ROOT}/julia_client/julia_client/README.md" \ |
| 102 | + "${TMP_JULIA}/README.md" || true |
| 103 | + JULIA_DRIFT=1 |
| 104 | +fi |
| 105 | + |
| 106 | +if [[ "${JULIA_DRIFT}" -eq 1 ]]; then |
| 107 | + RC=1 |
| 108 | +else |
| 109 | + echo "Julia client is up to date." |
| 110 | +fi |
| 111 | + |
| 112 | +if [[ "${RC}" -ne 0 ]]; then |
| 113 | + echo "" >&2 |
| 114 | + echo "Run 'bash api/sync_openapi.sh clients' to regenerate." >&2 |
| 115 | +fi |
| 116 | + |
| 117 | +exit "${RC}" |
0 commit comments