Files
docs/update_api_docs.sh
T
2026-08-13 22:18:25 +00:00

86 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
#
# Regenerates the swagger definitions served by this site.
#
# Usage: ./update_api_docs.sh [--latest-only] [sed -i suffix] [extra sed args...]
#
# --latest-only only refresh static/swagger-latest.json from gitea main,
# leaving the released static/swagger-<minor>.json untouched.
set -euo pipefail
LATEST_ONLY=0
if [ "${1:-}" = "--latest-only" ]; then
LATEST_ONLY=1
shift
fi
SED_INPLACE=(-i)
EXTRA_SED_ARGS=()
if [ "$#" -gt 0 ]; then
SED_INPLACE=(-i "$1")
shift
else
if sed --version >/dev/null 2>&1; then
SED_INPLACE=(-i)
else
SED_INPLACE=(-i '')
fi
fi
EXTRA_SED_ARGS=("$@")
inplace_sed() {
# ${arr[@]+...} keeps `set -u` happy with empty arrays on bash 3.2 (macOS)
sed "${SED_INPLACE[@]}" ${EXTRA_SED_ARGS[@]+"${EXTRA_SED_ARGS[@]}"} "$@"
}
# gitea >= 1.28 ships a pre-generated json, older versions ship a go template
SWAGGER_PATHS=(
'templates/swagger/v1-swagger.generated.json'
'templates/swagger/v1_json.tmpl'
)
# download_swagger <git ref> <output file>
download_swagger() {
local ref="$1" output="$2" path
for path in "${SWAGGER_PATHS[@]}"; do
if curl --silent --fail --location --output "$output" \
"https://raw.githubusercontent.com/go-gitea/gitea/${ref}/${path}"; then
return 0
fi
done
echo "unable to download the swagger definition of ${ref}" >&2
return 1
}
# rewrite_swagger <file> <version to display>
rewrite_swagger() {
local file="$1" version="$2"
# gitea >= 1.28
inplace_sed "s|\"version\": \"0.0.0+GITEA-API-APP-VERSION\"|\"version\": \"${version}\"|" "$file"
inplace_sed 's|"basePath": "/GITEA-API-APP-SUBURL/api/v1"|"basePath": "https://gitea.com/api/v1"|' "$file"
# gitea >= 1.24
inplace_sed "s|\"version\": \"{{.SwaggerAppVer}}\"|\"version\": \"${version}\"|" "$file"
inplace_sed 's|"basePath": "{{.SwaggerAppSubUrl}}/api/v1"|"basePath": "https://gitea.com/api/v1"|' "$file"
# gitea < 1.24
inplace_sed "s|\"version\": \"{{AppVer \| JSEscape}}\"|\"version\": \"${version}\"|" "$file"
inplace_sed "s#\"basePath\": \"{{AppSubUrl | JSEscape}}/api/v1\"#\"basePath\": \"https://gitea.com/api/v1\"#" "$file"
}
download_swagger 'refs/heads/main' v1_json.tmpl
rewrite_swagger v1_json.tmpl 'dev'
mv v1_json.tmpl static/swagger-latest.json
if [ "$LATEST_ONLY" -eq 1 ]; then
exit 0
fi
for ver in '1.27.2' '1.26.4' '1.25.5' '1.24.7' '1.23.8' '1.22.6'; do
download_swagger "refs/tags/v${ver}" v1_json.tmpl
rewrite_swagger v1_json.tmpl "${ver}"
minor=$(echo "$ver" | cut -d '.' -f 2)
mv v1_json.tmpl "static/swagger-$minor.json"
done