Read PR labels from the forge API (payload is empty on Gitea label events)
Test / Test (push) Successful in 17s

This commit is contained in:
raucao committed 2026-12-04 16:42:09 +04:00
1 parent 3e7f615b91
commit e87cca4118
4 files changed
+94 -16

No files matched your search

+8 -3
View File
@@ -4,9 +4,14 @@ Reusable Actions for Kosmos repositories. Currently one:
## `required-labels`
Fail a pull request unless it carries the required labels. It reads the pull
request's labels from the event payload, so it needs no API token and behaves the
same on GitHub Actions and Gitea Actions.
Fail a pull request unless it carries the required labels. It reads the current
labels from the forge API with the workflow token, falling back to the event
payload if the API is unavailable. It needs no special permissions and behaves
the same on GitHub Actions and Gitea Actions.
Reading via the API matters on Gitea: the `label_updated` payload (fired for
label add/remove) leaves `pull_request.labels` empty, so a payload-only check
would not notice a label being added.
### Inputs
+6
View File
@@ -49,5 +49,11 @@ runs:
RL_COUNT: ${{ inputs.count }}
RL_USE_REGEX: ${{ inputs.use-regex }}
RL_MESSAGE: ${{ inputs.message }}
# Used to read the current labels from the forge API. The event payload
# is unreliable for label add/remove events on Gitea.
RL_API_URL: ${{ github.api_url }}
RL_REPOSITORY: ${{ github.repository }}
RL_PR_NUMBER: ${{ github.event.pull_request.number }}
RL_TOKEN: ${{ github.token }}
run: |
"${{ github.action_path }}/check.sh"
+64 -13
View File
@@ -2,8 +2,10 @@
#
# required-labels — fail unless a pull request carries the required labels.
#
# The pull request's labels are read from the event payload, so this needs no
# API token and behaves the same on GitHub Actions and Gitea Actions.
# The current labels 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. It needs no special
# permissions and behaves the same on GitHub Actions and Gitea Actions.
#
# Inputs (environment):
# RL_LABELS Required labels, one per line or comma-separated. Empty, or the
@@ -12,6 +14,10 @@
# 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_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
@@ -35,7 +41,7 @@ pass() {
fail() {
echo "Required labels check: FAIL - $1" >&2
echo "Pull request labels: ${pr_csv:-<none>}" >&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
@@ -64,14 +70,51 @@ if [ "${#required[@]}" -eq 1 ] && [ "${required[0],,}" = "disabled" ]; then
pass "'disabled' sentinel set"
fi
# --- read the pull request's labels from the event payload ---
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 pull request labels." >&2
exit 1
fi
# --- fetch the current labels from the forge API ---
fetch_labels_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.error
import urllib.request
api_url, repo, number, token = sys.argv[1:5]
url = "{}/repos/{}/issues/{}/labels".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)
for label in data:
name = label.get("name") if isinstance(label, dict) else label
if name:
print(name)
PY
}
# --- read the labels from the event payload (fallback) ---
extract_labels_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 pull request labels." >&2
exit 1
fi
extract_labels() {
if command -v jq >/dev/null 2>&1; then
jq -r '(.pull_request.labels // [])[] | if type == "object" then .name else . end' "$event_path"
elif command -v python3 >/dev/null 2>&1; then
@@ -93,9 +136,17 @@ PY
}
pr_labels=()
while IFS= read -r label; do
[ -n "$label" ] && pr_labels+=("$label")
done < <(extract_labels)
if api_labels="$(fetch_labels_api)"; then
label_source="api"
while IFS= read -r label; do
[ -n "$label" ] && pr_labels+=("$label")
done <<< "$api_labels"
else
label_source="event payload"
while IFS= read -r label; do
[ -n "$label" ] && pr_labels+=("$label")
done < <(extract_labels_payload)
fi
pr_csv="$(IFS=,; echo "${pr_labels[*]:-}")"
+16
View File
@@ -57,6 +57,22 @@ run_case "multi-line labels are parsed" \
run_case "regex/any fails when the PR has no labels" \
no-labels.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
# API path: a file:// "API" returns the current labels. The event payload has
# none, so passing proves the API result was used.
api_dir="$(mktemp -d)"
mkdir -p "${api_dir}/repos/o/r/issues/1"
printf '[{"name":"kredits-3"},{"name":"bug"}]' > "${api_dir}/repos/o/r/issues/1/labels"
run_case "api labels are used when available (payload has none)" \
no-labels.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
RL_API_URL="file://${api_dir}" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
run_case "api labels can also fail the check" \
no-labels.json 1 RL_LABELS='^feature-[0-9]$' RL_USE_REGEX=true \
RL_API_URL="file://${api_dir}" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
run_case "falls back to the payload when the API call fails" \
with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
RL_API_URL="file://${api_dir}/does-not-exist" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
rm -rf "$api_dir"
if [ "$failures" -ne 0 ]; then
echo "${failures} test(s) failed"
exit 1