From 0666108ebf215b12a43f99ba838f9e325c8903ea Mon Sep 17 00:00:00 2001 From: Rafael Camelo Date: Sun, 26 Oct 2025 17:20:28 -0400 Subject: [PATCH] api: move scripts to generate and validate swagger to api module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rafael Camelo revert api/swagger.yaml to undo formatting Signed-off-by: Rafael Camelo refactor(api): update Dockerfile and Makefile with newline at the EOF and use current best practices Signed-off-by: Rafael Camelo refactor validations and swagger generation flow Signed-off-by: Rafael Camelo shfmt Signed-off-by: Paweł Gronowski Signed-off-by: Rafael Camelo refactor generate-swagger-api.sh to use absolute path Signed-off-by: Rafael Camelo add validate-api-swagger job for GitHub Actions Signed-off-by: Rafael Camelo fix validate-api-swagger ci workflow Signed-off-by: Rafael Camelo update go version and redoc in api module Signed-off-by: Rafael Camelo Signed-off-by: Sebastiaan van Stijn --- .github/workflows/test.yml | 23 +++++++ Dockerfile | 17 ----- Makefile | 17 +---- api/Dockerfile | 25 ++++++++ api/Makefile | 64 +++++++++++++++++++ {hack => api/scripts}/generate-swagger-api.sh | 18 +++--- api/scripts/validate-swagger-gen.sh | 52 +++++++++++++++ api/scripts/validate-swagger.sh | 16 +++++ {hack => api}/validate/yamllint | 0 {hack => api}/validate/yamllint.yaml | 0 hack/validate/swagger | 18 ------ hack/validate/swagger-gen | 29 --------- 12 files changed, 193 insertions(+), 86 deletions(-) create mode 100644 api/Dockerfile create mode 100644 api/Makefile rename {hack => api/scripts}/generate-swagger-api.sh (82%) create mode 100755 api/scripts/validate-swagger-gen.sh create mode 100755 api/scripts/validate-swagger.sh rename {hack => api}/validate/yamllint (100%) rename {hack => api}/validate/yamllint.yaml (100%) delete mode 100755 hack/validate/swagger delete mode 100755 hack/validate/swagger-gen diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 009499508d..fe17da4417 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -153,6 +153,29 @@ jobs: run: | make -o build validate-${{ matrix.script }} + validate-api-swagger: + runs-on: ubuntu-24.04 + timeout-minutes: 10 # guardrails timeout for the whole job + defaults: + run: + working-directory: api + needs: + - validate-dco + name: validate (api-swagger) + steps: + - + name: Checkout + uses: actions/checkout@v4 + - + name: Build api module image + run: | + make build GO_VERSION=${{ env.GO_VERSION }} + - + name: Validate swagger + run: | + make validate-swagger + make validate-swagger-gen + smoke-prepare: runs-on: ubuntu-24.04 timeout-minutes: 10 # guardrails timeout for the whole job diff --git a/Dockerfile b/Dockerfile index 12322d6e5d..a01c1c86ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -83,22 +83,6 @@ RUN --mount=type=cache,sharing=locked,id=moby-criu-aptlib,target=/var/lib/apt \ FROM distribution/distribution:$REGISTRY_VERSION AS registry RUN mkdir /build && mv /bin/registry /build/registry -# go-swagger -FROM base AS swagger -WORKDIR /go/src/github.com/go-swagger/go-swagger -ARG TARGETPLATFORM -# GO_SWAGGER_VERSION specifies the version of the go-swagger binary to install. -# Go-swagger is used in CI for generating types from swagger.yaml in -# hack/validate/swagger-gen -ARG GO_SWAGGER_VERSION=v0.33.1 -RUN --mount=type=cache,target=/root/.cache/go-build,id=swagger-build-$TARGETPLATFORM \ - --mount=type=cache,target=/go/pkg/mod \ - --mount=type=tmpfs,target=/go/src/ < /dev/null || true + +echo "Generating swagger types in temporary folder..." +( + cd "${TMP_DIR}" + "${SCRIPT_DIR}/generate-swagger-api.sh" > /dev/null 2>&1 +) + +echo "Run diff for all generated files..." +DIFF_FOUND=false +for f in "${GEN_FILES[@]}"; do + REL="${f#${API_DIR}/}" + if ! diff -q "${TMP_DIR}/${REL}" "${API_DIR}/${REL}" > /dev/null 2>&1; then + echo "Difference found in ${REL}" + diff -u "${TMP_DIR}/${REL}" "${API_DIR}/${REL}" || true + DIFF_FOUND=true + fi +done + +if [ "$DIFF_FOUND" = true ]; then + echo + echo "Swagger validation failed. Please run:" + echo " ./scripts/generate-swagger-api.sh" + echo "and commit updated generated files." + exit 1 +fi + +echo "Swagger file is up to date." diff --git a/api/scripts/validate-swagger.sh b/api/scripts/validate-swagger.sh new file mode 100755 index 0000000000..b129be7ed6 --- /dev/null +++ b/api/scripts/validate-swagger.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -e + +# Expected to be in api directory +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +echo "Validating swagger.yaml..." + +yamllint -f parsable -c validate/yamllint.yaml swagger.yaml + +if out=$(swagger validate swagger.yaml); then + echo "Validation done! ${out}" +else + echo "${out}" >&2 + false +fi diff --git a/hack/validate/yamllint b/api/validate/yamllint similarity index 100% rename from hack/validate/yamllint rename to api/validate/yamllint diff --git a/hack/validate/yamllint.yaml b/api/validate/yamllint.yaml similarity index 100% rename from hack/validate/yamllint.yaml rename to api/validate/yamllint.yaml diff --git a/hack/validate/swagger b/hack/validate/swagger deleted file mode 100755 index 5313221b7b..0000000000 --- a/hack/validate/swagger +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -e -SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "${SCRIPTDIR}/.validate" - -IFS=$'\n' -files=($(validate_diff --diff-filter=ACMR --name-only -- 'api/swagger.yaml' || true)) -unset IFS - -if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ ${#files[@]} -gt 0 ]; then - yamllint -f parsable -c "${SCRIPTDIR}"/yamllint.yaml api/swagger.yaml - if out=$(swagger validate api/swagger.yaml); then - echo "Congratulations! ${out}" - else - echo "${out}" >&2 - false - fi -fi diff --git a/hack/validate/swagger-gen b/hack/validate/swagger-gen deleted file mode 100755 index 40631703cd..0000000000 --- a/hack/validate/swagger-gen +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env bash - -SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "${SCRIPTDIR}/.validate" - -IFS=$'\n' -files=($(validate_diff --diff-filter=ACMR --name-only -- 'api/types/' 'api/swagger.yaml' || true)) -unset IFS - -if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ ${#files[@]} -gt 0 ]; then - "${SCRIPTDIR}"/../generate-swagger-api.sh 2> /dev/null - # Let see if the working directory is clean - diffs="$(git diff -- api/types/)" - if [ "$diffs" ]; then - { - echo 'The result of hack/generate-swagger-api.sh differs' - echo - echo "$diffs" - echo - echo 'Please update api/swagger.yaml with any API changes, then ' - echo 'run hack/generate-swagger-api.sh.' - } >&2 - false - else - echo 'Congratulations! All API changes are done the right way.' - fi -else - echo 'No api/types/ or api/swagger.yaml changes in diff.' -fi