Files
docs/scripts/check-upstream-deps.sh
T

102 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
#
# Fails when a pull request depends on a gitea change that is not merged yet.
#
# Usage: ./scripts/check-upstream-deps.sh <pull request json>
#
# The json is what the gitea api returns for a pull request; the script reads
# the description and the labels from it. A dependency is declared in the
# description as
#
# Depends on: gitea#35851
# Depends on: https://github.com/go-gitea/gitea/pull/35851
#
# and may appear several times. `GITHUB_COM_TOKEN` is optional and only lifts
# the rate limit of the unauthenticated github api, `UPSTREAM_LABEL` overrides
# the label the check looks for.
set -euo pipefail
LABEL="${UPSTREAM_LABEL:-depends-on-upstream}"
PULL_JSON="${1:-}"
if [ -z "$PULL_JSON" ] || [ ! -f "$PULL_JSON" ]; then
echo "usage: $0 <pull request json>" >&2
exit 2
fi
body="$(jq -r '.body // ""' "$PULL_JSON")"
labelled="$(jq -r --arg label "$LABEL" '[.labels[]?.name] | index($label) != null' "$PULL_JSON")"
# every gitea#<id> and every pull request url, deduplicated
refs="$(printf '%s\n' "$body" \
| grep -oiE 'gitea#[0-9]+|github\.com/go-gitea/gitea/pull/[0-9]+' \
| grep -oE '[0-9]+' \
| sort -un || true)"
if [ -z "$refs" ]; then
if [ "$labelled" = 'true' ]; then
echo "This pull request carries the \"$LABEL\" label but does not say what it"
echo "depends on. Add a line to the description, for example:"
echo
echo " Depends on: gitea#35851"
exit 1
fi
echo "no upstream dependency declared"
exit 0
fi
github_api() {
local url="$1"
if [ -n "${GITHUB_COM_TOKEN:-}" ]; then
curl --silent --show-error --write-out '\n%{http_code}' \
-H "Authorization: Bearer $GITHUB_COM_TOKEN" "$url"
else
curl --silent --show-error --write-out '\n%{http_code}' "$url"
fi
}
status=0
for ref in $refs; do
response="$(github_api "https://api.github.com/repos/go-gitea/gitea/pulls/$ref")"
code="$(printf '%s' "$response" | tail -n 1)"
payload="$(printf '%s' "$response" | sed '$d')"
case "$code" in
200)
if [ "$(printf '%s' "$payload" | jq -r '.merged')" = 'true' ]; then
echo "gitea#$ref is merged"
else
echo "gitea#$ref is not merged yet ($(printf '%s' "$payload" | jq -r '.state'))"
status=1
fi
;;
404)
echo "gitea#$ref is not a pull request of go-gitea/gitea, reference the pull request that adds the feature"
status=1
;;
403 | 429)
echo "the github api refused the request for gitea#$ref (rate limit?), cannot tell whether it is merged"
status=1
;;
*)
echo "unexpected response $code from the github api for gitea#$ref"
status=1
;;
esac
done
if [ "$status" -ne 0 ]; then
echo
echo "The documentation of an unreleased feature can only be merged once the"
echo "feature itself is in gitea main. Keep the \"$LABEL\" label on this pull"
echo "request; the scheduled job removes it and reruns this check as soon as"
echo "every referenced pull request is merged."
exit 1
fi
if [ "$labelled" = 'true' ]; then
echo
echo "every referenced pull request is merged, the \"$LABEL\" label can be removed"
fi