Pass draft/WIP pull requests by default; read draft state with labels
Test / Test (push) Successful in 17s
Test / Test (push) Successful in 17s
This commit is contained in:
7 files changed
+110
-41
No files matched your search
@@ -15,7 +15,9 @@ name: Required labels
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited]
|
||||
# ready_for_review/converted_to_draft matter because a draft PR passes by
|
||||
# default: without them, marking a PR ready would not re-run the check.
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited, ready_for_review, converted_to_draft]
|
||||
|
||||
jobs:
|
||||
required-labels:
|
||||
@@ -30,4 +32,5 @@ jobs:
|
||||
with:
|
||||
labels: ${{ vars.REQUIRED_PR_LABELS }}
|
||||
mode: ${{ vars.REQUIRED_PR_LABELS_MODE }}
|
||||
draft-passes: ${{ vars.REQUIRED_PR_LABELS_DRAFT_PASSES }}
|
||||
use-regex: "true"
|
||||
@@ -13,6 +13,10 @@ 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.
|
||||
|
||||
Draft ("WIP") pull requests pass by default, so the check only fails once a pull
|
||||
request is ready for review. Set `draft-passes: "false"` to enforce on drafts
|
||||
too.
|
||||
|
||||
### Inputs
|
||||
|
||||
| Input | Description | Default |
|
||||
@@ -22,6 +26,7 @@ would not notice a label being added.
|
||||
| `count` | Number of matches required when `mode` is `exactly`. | `1` |
|
||||
| `use-regex` | Treat each entry in `labels` as a regular expression. | `false` |
|
||||
| `message` | Extra text appended to the failure output. | `""` |
|
||||
| `draft-passes` | Pass draft ("WIP") pull requests instead of failing them. | `true` |
|
||||
|
||||
Outputs: `labels` — the required entries that matched, comma-separated.
|
||||
|
||||
@@ -54,6 +59,8 @@ Configuration is done with Actions variables (org default, per-repo override):
|
||||
- `REQUIRED_PR_LABELS` — the required labels/regexes. Org default:
|
||||
`^kredits(-[123]|/(small|medium|large))$`.
|
||||
- `REQUIRED_PR_LABELS_MODE` — `any`, `all`, or `exactly`. Default `any`.
|
||||
- `REQUIRED_PR_LABELS_DRAFT_PASSES` — set to `false` to enforce on drafts too.
|
||||
Default (unset) is `true`.
|
||||
|
||||
A repository opts out by setting `REQUIRED_PR_LABELS` to `disabled`.
|
||||
|
||||
@@ -67,7 +74,7 @@ noticed.
|
||||
name: Required labels
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited]
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited, ready_for_review, converted_to_draft]
|
||||
jobs:
|
||||
required-labels:
|
||||
name: Required labels
|
||||
|
||||
@@ -32,6 +32,12 @@ inputs:
|
||||
description: "Extra text appended to the failure output."
|
||||
required: false
|
||||
default: ""
|
||||
draft-passes:
|
||||
description: >-
|
||||
When true (default), draft ("WIP") pull requests always pass, so the check
|
||||
only fails once the pull request is ready for review.
|
||||
required: false
|
||||
default: "true"
|
||||
|
||||
outputs:
|
||||
labels:
|
||||
@@ -49,6 +55,7 @@ runs:
|
||||
RL_COUNT: ${{ inputs.count }}
|
||||
RL_USE_REGEX: ${{ inputs.use-regex }}
|
||||
RL_MESSAGE: ${{ inputs.message }}
|
||||
RL_DRAFT_PASSES: ${{ inputs.draft-passes }}
|
||||
# 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 }}
|
||||
|
||||
+51
-35
@@ -2,22 +2,24 @@
|
||||
#
|
||||
# required-labels — fail unless a pull request carries the required labels.
|
||||
#
|
||||
# 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.
|
||||
# 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_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.
|
||||
# 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
|
||||
|
||||
@@ -26,6 +28,7 @@ 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
|
||||
@@ -70,8 +73,10 @@ if [ "${#required[@]}" -eq 1 ] && [ "${required[0],,}" = "disabled" ]; then
|
||||
pass "'disabled' sentinel set"
|
||||
fi
|
||||
|
||||
# --- fetch the current labels from the forge API ---
|
||||
fetch_labels_api() {
|
||||
# --- 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
|
||||
@@ -84,11 +89,10 @@ fetch_labels_api() {
|
||||
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)
|
||||
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)
|
||||
@@ -100,56 +104,68 @@ 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:
|
||||
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
|
||||
}
|
||||
|
||||
# --- read the labels from the event payload (fallback) ---
|
||||
extract_labels_payload() {
|
||||
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 pull request labels." >&2
|
||||
echo "::error::GITHUB_EVENT_PATH is not set or does not exist; cannot read the pull request." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
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)
|
||||
for label in (data.get("pull_request") or {}).get("labels") or []:
|
||||
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 jq nor python3 is available to parse the event payload." >&2
|
||||
echo "::error::Neither python3 nor jq is available to read the pull request." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
pr_labels=()
|
||||
if api_labels="$(fetch_labels_api)"; then
|
||||
if pr_state="$(fetch_pr_state_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)
|
||||
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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"action": "opened",
|
||||
"number": 5,
|
||||
"pull_request": {
|
||||
"number": 5,
|
||||
"title": "WIP: Fix a thing",
|
||||
"draft": true,
|
||||
"labels": [
|
||||
{ "id": 1, "name": "kredits-1" }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"action": "opened",
|
||||
"number": 4,
|
||||
"pull_request": {
|
||||
"number": 4,
|
||||
"title": "WIP: Add a thing",
|
||||
"draft": true,
|
||||
"labels": [
|
||||
{ "id": 3, "name": "bug" }
|
||||
]
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -57,17 +57,29 @@ 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.
|
||||
# Draft ("WIP") pull requests pass by default.
|
||||
run_case "draft passes by default even without a required label" \
|
||||
draft-without.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
||||
run_case "draft with the label passes too" \
|
||||
draft-with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
||||
run_case "draft fails when draft-passes is false" \
|
||||
draft-without.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_DRAFT_PASSES=false
|
||||
|
||||
# API path: a file:// "API" returns the pull request. The event payload has no
|
||||
# labels, 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"
|
||||
mkdir -p "${api_dir}/repos/o/r/pulls" "${api_dir}/draft/repos/o/r/pulls"
|
||||
printf '{"draft":false,"labels":[{"name":"kredits-3"},{"name":"bug"}]}' > "${api_dir}/repos/o/r/pulls/1"
|
||||
printf '{"draft":true,"labels":[]}' > "${api_dir}/draft/repos/o/r/pulls/1"
|
||||
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 "api draft state is honoured" \
|
||||
no-labels.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
|
||||
RL_API_URL="file://${api_dir}/draft" 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'
|
||||
|
||||
Reference in new issue
Block a user