222 lines
6.8 KiB
Bash
Executable File
222 lines
6.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# required-labels — fail unless a pull request carries the required labels.
|
|
#
|
|
# The current labels and draft state are read from the forge API (authoritative,
|
|
# and correct for label add/remove events, whose payloads are unreliable on
|
|
# Gitea). If no API access is available, it falls back to the event payload.
|
|
#
|
|
# Inputs (environment):
|
|
# RL_LABELS Required labels, one per line or comma-separated. Empty, or
|
|
# the literal "disabled", makes the check pass.
|
|
# RL_MODE any | all | exactly (default: any)
|
|
# RL_COUNT Number of matches for mode=exactly (default: 1)
|
|
# RL_USE_REGEX true | false (default: false)
|
|
# RL_MESSAGE Extra text appended to the failure output.
|
|
# RL_DRAFT_PASSES true | false (default: true)
|
|
# When true, draft ("WIP") pull requests always pass, so the
|
|
# check only fails once the pull request is ready for review.
|
|
# RL_API_URL API base URL (e.g. https://gitea.example/api/v1).
|
|
# RL_REPOSITORY owner/repo.
|
|
# RL_PR_NUMBER Pull request number.
|
|
# RL_TOKEN Token for the API call.
|
|
#
|
|
set -euo pipefail
|
|
|
|
mode="${RL_MODE:-any}"
|
|
count="${RL_COUNT:-1}"
|
|
use_regex="${RL_USE_REGEX:-false}"
|
|
custom_message="${RL_MESSAGE:-}"
|
|
required_raw="${RL_LABELS:-}"
|
|
draft_passes="${RL_DRAFT_PASSES:-true}"
|
|
|
|
emit_output() {
|
|
if [ -n "${GITHUB_OUTPUT:-}" ]; then
|
|
printf 'labels=%s\n' "$1" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
}
|
|
|
|
pass() {
|
|
echo "Required labels check: PASS - $1"
|
|
emit_output "${matched_csv:-}"
|
|
exit 0
|
|
}
|
|
|
|
fail() {
|
|
echo "Required labels check: FAIL - $1" >&2
|
|
echo "Pull request labels (${label_source:-unknown}): ${pr_csv:-<none>}" >&2
|
|
echo "Required (mode=${mode}): ${required_csv:-<none>}" >&2
|
|
if [ -n "$custom_message" ]; then
|
|
echo "$custom_message" >&2
|
|
fi
|
|
echo "::error::$1" >&2
|
|
exit 1
|
|
}
|
|
|
|
# --- parse the required labels (newline- and comma-separated) ---
|
|
required=()
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
IFS=',' read -r -a parts <<< "$line"
|
|
for part in "${parts[@]}"; do
|
|
part="$(printf '%s' "$part" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
|
|
[ -n "$part" ] && required+=("$part")
|
|
done
|
|
done <<< "$required_raw"
|
|
|
|
required_csv="$(IFS=,; echo "${required[*]:-}")"
|
|
|
|
# Nothing configured, or an explicit opt-out -> pass (fail-open, documented).
|
|
if [ "${#required[@]}" -eq 0 ]; then
|
|
pass "no required labels configured"
|
|
fi
|
|
if [ "${#required[@]}" -eq 1 ] && [ "${required[0],,}" = "disabled" ]; then
|
|
pass "'disabled' sentinel set"
|
|
fi
|
|
|
|
# --- read the pull request's draft state and labels ---
|
|
# Both functions print the draft state on the first line and one label name per
|
|
# line after it.
|
|
fetch_pr_state_api() {
|
|
local api_url="${RL_API_URL:-}" repo="${RL_REPOSITORY:-}"
|
|
local number="${RL_PR_NUMBER:-}" token="${RL_TOKEN:-}"
|
|
if [ -z "$api_url" ] || [ -z "$repo" ] || [ -z "$number" ]; then
|
|
return 1
|
|
fi
|
|
if ! command -v python3 >/dev/null 2>&1; then
|
|
return 1
|
|
fi
|
|
|
|
python3 - "$api_url" "$repo" "$number" "$token" <<'PY'
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
|
|
api_url, repo, number, token = sys.argv[1:5]
|
|
url = "{}/repos/{}/pulls/{}".format(api_url.rstrip("/"), repo, number)
|
|
request = urllib.request.Request(url, headers={"Accept": "application/json"})
|
|
if token:
|
|
request.add_header("Authorization", "Bearer " + token)
|
|
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=20) as response:
|
|
data = json.load(response)
|
|
except Exception as exc: # noqa: BLE001 - any failure falls back to the payload
|
|
print("api error: {}".format(exc), file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
print("true" if data.get("draft") else "false")
|
|
for label in data.get("labels") or []:
|
|
name = label.get("name") if isinstance(label, dict) else label
|
|
if name:
|
|
print(name)
|
|
PY
|
|
}
|
|
|
|
fetch_pr_state_payload() {
|
|
local event_path="${GITHUB_EVENT_PATH:-}"
|
|
if [ -z "$event_path" ] || [ ! -f "$event_path" ]; then
|
|
echo "::error::GITHUB_EVENT_PATH is not set or does not exist; cannot read the pull request." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
python3 - "$event_path" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
with open(sys.argv[1], encoding="utf-8") as handle:
|
|
data = json.load(handle)
|
|
pr = data.get("pull_request") or {}
|
|
print("true" if pr.get("draft") else "false")
|
|
for label in pr.get("labels") or []:
|
|
name = label.get("name") if isinstance(label, dict) else label
|
|
if name:
|
|
print(name)
|
|
PY
|
|
elif command -v jq >/dev/null 2>&1; then
|
|
jq -r 'if (.pull_request.draft // false) then "true" else "false" end, ((.pull_request.labels // [])[] | if type == "object" then .name else . end)' "$event_path"
|
|
else
|
|
echo "::error::Neither python3 nor jq is available to read the pull request." >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
if pr_state="$(fetch_pr_state_api)"; then
|
|
label_source="api"
|
|
else
|
|
label_source="event payload"
|
|
pr_state="$(fetch_pr_state_payload)"
|
|
fi
|
|
|
|
pr_draft="false"
|
|
pr_labels=()
|
|
if [ -n "$pr_state" ]; then
|
|
mapfile -t pr_lines <<< "$pr_state"
|
|
pr_draft="${pr_lines[0]:-false}"
|
|
for pr_line in "${pr_lines[@]:1}"; do
|
|
[ -n "$pr_line" ] && pr_labels+=("$pr_line")
|
|
done
|
|
fi
|
|
|
|
pr_csv="$(IFS=,; echo "${pr_labels[*]:-}")"
|
|
|
|
# Draft ("WIP") pull requests pass by default: the check only applies once the
|
|
# pull request is ready for review.
|
|
if [ "$draft_passes" != "false" ] && [ "$pr_draft" = "true" ]; then
|
|
pass "pull request is a draft; required labels are not enforced yet"
|
|
fi
|
|
|
|
# --- match required entries against the PR labels (case-insensitive) ---
|
|
matched=()
|
|
if [ "$use_regex" = "true" ]; then
|
|
for req in "${required[@]}"; do
|
|
for label in "${pr_labels[@]}"; do
|
|
if printf '%s' "$label" | grep -Eiq -- "$req"; then
|
|
matched+=("$req")
|
|
break
|
|
fi
|
|
done
|
|
done
|
|
else
|
|
for req in "${required[@]}"; do
|
|
for label in "${pr_labels[@]}"; do
|
|
if [ "${label,,}" = "${req,,}" ]; then
|
|
matched+=("$req")
|
|
break
|
|
fi
|
|
done
|
|
done
|
|
fi
|
|
|
|
matched_csv="$(IFS=,; echo "${matched[*]:-}")"
|
|
|
|
# --- decide ---
|
|
case "$mode" in
|
|
any)
|
|
if [ "${#matched[@]}" -ge 1 ]; then
|
|
pass "at least one required label is present (${matched_csv})"
|
|
fi
|
|
fail "none of the required labels are present"
|
|
;;
|
|
all)
|
|
if [ "${#matched[@]}" -eq "${#required[@]}" ]; then
|
|
pass "all required labels are present (${matched_csv})"
|
|
fi
|
|
fail "not all required labels are present (matched ${#matched[@]}/${#required[@]})"
|
|
;;
|
|
exactly)
|
|
if ! [[ "$count" =~ ^[0-9]+$ ]]; then
|
|
echo "::error::mode=exactly needs a numeric count, got '$count'." >&2
|
|
exit 1
|
|
fi
|
|
if [ "${#matched[@]}" -eq "$count" ]; then
|
|
pass "exactly ${count} required label(s) present (${matched_csv})"
|
|
fi
|
|
fail "expected exactly ${count} required label(s), found ${#matched[@]}"
|
|
;;
|
|
*)
|
|
echo "::error::Unknown mode '$mode' (expected any, all or exactly)." >&2
|
|
exit 1
|
|
;;
|
|
esac
|