...
1#!/usr/bin/env bash
2
3# Copyright 2023 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# This script checks the documentation (description) from the OpenAPI specification for URLs, and
18# verifies those URLs are valid.
19# Usage: `hack/verify-openapi-docs-links.sh`.
20
21set -o errexit
22set -o nounset
23set -o pipefail
24
25KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26source "${KUBE_ROOT}/hack/lib/init.sh"
27
28SPECROOT="${KUBE_ROOT}/api/openapi-spec"
29SPECV3PATH="${SPECROOT}/v3"
30
31_tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
32mkdir -p "${_tmpdir}"
33trap 'rm -rf ${_tmpdir}' EXIT SIGINT
34trap "echo Aborted; exit;" SIGINT SIGTERM
35
36TMP_URLS="${_tmpdir}/docs_urls.txt"
37touch "${TMP_URLS}"
38
39
40for full_repo_path in "${SPECV3PATH}"/*.json; do
41 grep -oE '"description": ".*",?$' "$full_repo_path" | grep -oE 'https?:\/\/[-a-zA-Z0-9._+]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9:%_+.~&/=]*[a-zA-Z0-9])' >> "${TMP_URLS}" || true
42done
43sort -u "${TMP_URLS}" -o "${TMP_URLS}"
44
45RESULT=0
46while read -r URL; do
47 if ! curl --head --location --fail --silent "$URL" > /dev/null; then
48 echo "$URL not found"
49 RESULT=1
50 fi
51done < "${TMP_URLS}"
52
53exit $RESULT
54
55# ex: ts=2 sw=2 et filetype=sh
View as plain text