Skip to content

Commit bdb26de

Browse files
committed
update for openapi scripts
1 parent caa2f3c commit bdb26de

4 files changed

Lines changed: 91 additions & 6 deletions

File tree

scripts/check-openapi-security.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
2727

2828
OPENAPI_PATH="openapi"
2929

30+
resolve_repo_root() {
31+
# Prefer git root for local execution and subdirectory calls.
32+
if root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
33+
printf '%s\n' "${root}"
34+
return 0
35+
fi
36+
37+
# Fallback for checked-out scripts under a conventional ./scripts layout.
38+
if [ -d "${SCRIPT_DIR}/../.git" ]; then
39+
(
40+
cd -- "${SCRIPT_DIR}/.."
41+
pwd
42+
)
43+
return 0
44+
fi
45+
46+
# Last resort for piped execution (for example: curl | bash).
47+
pwd
48+
}
49+
3050
usage() {
3151
cat >&2 <<EOF
3252
Usage: $0 [-f openapi_path]
@@ -53,8 +73,9 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
5373
# Absolute paths are used as-is.
5474
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
5575
else
56-
# Relative paths are resolved from this script directory.
57-
OPENAPI_ABS_PATH="${SCRIPT_DIR}/${OPENAPI_PATH}"
76+
# Relative paths are resolved from repository root.
77+
REPO_ROOT="$(resolve_repo_root)"
78+
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
5879
fi
5980

6081
# Allow extension fallback between .yml and .yaml.

scripts/check-openapi-validation.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
2929

3030
OPENAPI_PATH="openapi"
3131

32+
resolve_repo_root() {
33+
# Prefer git root for local execution and subdirectory calls.
34+
if root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
35+
printf '%s\n' "${root}"
36+
return 0
37+
fi
38+
39+
# Fallback for checked-out scripts under a conventional ./scripts layout.
40+
if [ -d "${SCRIPT_DIR}/../.git" ]; then
41+
(
42+
cd -- "${SCRIPT_DIR}/.."
43+
pwd
44+
)
45+
return 0
46+
fi
47+
48+
# Last resort for piped execution (for example: curl | bash).
49+
pwd
50+
}
51+
3252
usage() {
3353
cat >&2 <<EOF
3454
Usage: $0 [-f openapi_path]
@@ -55,8 +75,9 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
5575
# Absolute paths are used as-is.
5676
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
5777
else
58-
# Relative paths are resolved from this script directory.
59-
OPENAPI_ABS_PATH="${SCRIPT_DIR}/${OPENAPI_PATH}"
78+
# Relative paths are resolved from repository root.
79+
REPO_ROOT="$(resolve_repo_root)"
80+
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
6081
fi
6182

6283
# Allow extension fallback between .yml and .yaml.

scripts/run-openapi-docker.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ PORT="8888:80"
3838
# If a file is provided, its parent directory will be mounted.
3939
OPENAPI_PATH="openapi"
4040

41+
resolve_repo_root() {
42+
# Prefer git root for local execution and subdirectory calls.
43+
if root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
44+
printf '%s\n' "${root}"
45+
return 0
46+
fi
47+
48+
# Fallback for checked-out scripts under a conventional ./scripts layout.
49+
if [ -d "${SCRIPT_DIR}/../.git" ]; then
50+
(
51+
cd -- "${SCRIPT_DIR}/.."
52+
pwd
53+
)
54+
return 0
55+
fi
56+
57+
# Last resort for piped execution (for example: curl | bash).
58+
pwd
59+
}
60+
4161
usage() {
4262
cat >&2 <<EOF
4363
Usage: $0 [-n name] [-p host_port:container_port] [-f openapi_path]
@@ -74,8 +94,9 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
7494
# Absolute paths are used as-is.
7595
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
7696
else
77-
# Relative paths are resolved from this script directory.
78-
OPENAPI_ABS_PATH="${SCRIPT_DIR}/${OPENAPI_PATH}"
97+
# Relative paths are resolved from repository root.
98+
REPO_ROOT="$(resolve_repo_root)"
99+
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
79100
fi
80101

81102
# Allow extension fallback between .yml and .yaml.

tests/bats/openapi-scripts.bats

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,25 @@ YAML
123123
docker_call="$(cat "$DOCKER_LOG")"
124124
[[ "$docker_call" == *"zap-api-scan.py"* ]]
125125
}
126+
127+
@test "check-openapi-validation resolves default openapi path from repo root when run from subdirectory" {
128+
repo="$(make_temp_repo)"
129+
copy_openapi_scripts_into_repo "$repo"
130+
install_docker_stub "$repo"
131+
132+
mkdir -p "$repo/openapi" "$repo/subdir"
133+
cat >"$repo/openapi/openapi.yaml" <<'YAML'
134+
openapi: 3.0.0
135+
info:
136+
title: Root API
137+
version: 1.0.0
138+
paths: {}
139+
YAML
140+
141+
export DOCKER_LOG="$repo/docker.log"
142+
run bash -c "cd '$repo/subdir' && bash ../scripts/check-openapi-validation.sh"
143+
144+
[ "$status" -eq 0 ]
145+
docker_call="$(cat "$DOCKER_LOG")"
146+
[[ "$docker_call" == *"/openapi/openapi.yaml:/openapi.yaml"* ]]
147+
}

0 commit comments

Comments
 (0)