This commit is contained in:
10 files changed
+434
No files matched your search
@@ -0,0 +1,30 @@
|
||||
# Org-wide required-label gate.
|
||||
#
|
||||
# This file lives under SCOPED_WORKFLOW_DIRS (.gitea/scoped_workflows) and only
|
||||
# runs once this repository is registered as a scoped-workflow source at the
|
||||
# owner (org) level. See README.md for the rollout steps.
|
||||
#
|
||||
# The requirement is configured with Actions variables so repositories can
|
||||
# override or opt out without editing any file:
|
||||
# REQUIRED_PR_LABELS regex(es) or names, newline/comma separated
|
||||
# REQUIRED_PR_LABELS_MODE any | all | exactly (default: any)
|
||||
#
|
||||
# Both `labeled` and `unlabeled` are listed because Gitea delivers label
|
||||
# removals as `labeled`; the action always reads the full labels array.
|
||||
name: Required labels
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited]
|
||||
|
||||
jobs:
|
||||
required-labels:
|
||||
name: Required labels
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check required PR labels
|
||||
uses: self:kosmos/actions/required-labels@v1
|
||||
with:
|
||||
labels: ${{ vars.REQUIRED_PR_LABELS }}
|
||||
mode: ${{ vars.REQUIRED_PR_LABELS_MODE }}
|
||||
use-regex: "true"
|
||||
@@ -0,0 +1,14 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Run check tests
|
||||
run: bash test/run-tests.sh
|
||||
@@ -0,0 +1,17 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
# This repository is mirrored from Gitea, which also reads .github/workflows.
|
||||
# Run this copy only on github.com; Gitea uses .gitea/workflows/test.yml.
|
||||
if: ${{ github.server_url == 'https://github.com' }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Run check tests
|
||||
run: bash test/run-tests.sh
|
||||
@@ -0,0 +1,69 @@
|
||||
# Kosmos Actions
|
||||
|
||||
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.
|
||||
|
||||
### Inputs
|
||||
|
||||
| Input | Description | Default |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------ | ------- |
|
||||
| `labels` | Required labels, one per line or comma-separated. Each entry is a regular expression when `use-regex` is `true`. | `""` |
|
||||
| `mode` | `any` (at least one), `all`, or `exactly`. | `any` |
|
||||
| `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. | `""` |
|
||||
|
||||
Outputs: `labels` — the required entries that matched, comma-separated.
|
||||
|
||||
An empty `labels` input, or the literal `disabled`, makes the check pass. Matching
|
||||
is case-insensitive. Anchor regexes (`^...$`) when you need an exact match.
|
||||
|
||||
### Gitea (org-wide, recommended)
|
||||
|
||||
This repository is registered as a **scoped workflow source** for the `kosmos`
|
||||
org. `.gitea/scoped_workflows/required-labels.yaml` then runs on every org
|
||||
repository and, once marked **required**, gates merges.
|
||||
|
||||
Configuration is done with Actions variables (org default, per-repo override):
|
||||
|
||||
- `REQUIRED_PR_LABELS` — the required labels/regexes. Org default:
|
||||
`^kredits-[0-9]$`.
|
||||
- `REQUIRED_PR_LABELS_MODE` — `any`, `all`, or `exactly`. Default `any`.
|
||||
|
||||
A repository opts out by setting `REQUIRED_PR_LABELS` to `disabled`.
|
||||
|
||||
### GitHub (per-repo)
|
||||
|
||||
```yaml
|
||||
name: Required labels
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, labeled, unlabeled, synchronize, edited]
|
||||
jobs:
|
||||
required-labels:
|
||||
name: Required labels
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: 67P/actions/required-labels@v1
|
||||
with:
|
||||
labels: '^kredits-[0-9]$'
|
||||
use-regex: "true"
|
||||
mode: any
|
||||
```
|
||||
|
||||
Then require the `Required labels / required-labels` check in the repository's
|
||||
branch protection or ruleset.
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
bash test/run-tests.sh
|
||||
```
|
||||
|
||||
`kosmos/actions` (Gitea) is canonical; `67P/actions` (GitHub) is a mirror. Tag a
|
||||
release on both so `@v1` resolves.
|
||||
@@ -0,0 +1,53 @@
|
||||
name: Required labels
|
||||
description: >-
|
||||
Fail unless a pull request carries the required labels (names or regular
|
||||
expressions). 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.
|
||||
author: Kosmos
|
||||
branding:
|
||||
icon: tag
|
||||
color: blue
|
||||
|
||||
inputs:
|
||||
labels:
|
||||
description: >-
|
||||
Required labels, one per line or comma-separated. Each entry is matched
|
||||
against the pull request's labels; with `use-regex: true` each entry is a
|
||||
regular expression. Empty, or the literal "disabled", makes the check pass.
|
||||
required: false
|
||||
default: ""
|
||||
mode:
|
||||
description: "How to compare: any (at least one), all, or exactly."
|
||||
required: false
|
||||
default: "any"
|
||||
count:
|
||||
description: "Number of labels required when mode is exactly."
|
||||
required: false
|
||||
default: "1"
|
||||
use-regex:
|
||||
description: "Treat each entry in `labels` as a regular expression."
|
||||
required: false
|
||||
default: "false"
|
||||
message:
|
||||
description: "Extra text appended to the failure output."
|
||||
required: false
|
||||
default: ""
|
||||
|
||||
outputs:
|
||||
labels:
|
||||
description: "The required entries that matched, comma-separated."
|
||||
value: ${{ steps.check.outputs.labels }}
|
||||
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- id: check
|
||||
shell: bash
|
||||
env:
|
||||
RL_LABELS: ${{ inputs.labels }}
|
||||
RL_MODE: ${{ inputs.mode }}
|
||||
RL_COUNT: ${{ inputs.count }}
|
||||
RL_USE_REGEX: ${{ inputs.use-regex }}
|
||||
RL_MESSAGE: ${{ inputs.message }}
|
||||
run: |
|
||||
"${{ github.action_path }}/check.sh"
|
||||
Executable
+154
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
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:-}"
|
||||
|
||||
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: ${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 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
|
||||
|
||||
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
|
||||
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 []:
|
||||
name = label.get("name") if isinstance(label, dict) else label
|
||||
if name:
|
||||
print(name)
|
||||
PY
|
||||
else
|
||||
echo "::error::Neither jq nor python3 is available to parse the event payload." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
pr_labels=()
|
||||
while IFS= read -r label; do
|
||||
[ -n "$label" ] && pr_labels+=("$label")
|
||||
done < <(extract_labels)
|
||||
|
||||
pr_csv="$(IFS=,; echo "${pr_labels[*]:-}")"
|
||||
|
||||
# --- 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
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"action": "opened",
|
||||
"number": 3,
|
||||
"pull_request": {
|
||||
"number": 3,
|
||||
"title": "No labels yet",
|
||||
"labels": []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"action": "opened",
|
||||
"number": 1,
|
||||
"pull_request": {
|
||||
"number": 1,
|
||||
"title": "Add a thing",
|
||||
"labels": [
|
||||
{ "id": 1, "name": "kredits-2" },
|
||||
{ "id": 2, "name": "bug" }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"action": "opened",
|
||||
"number": 2,
|
||||
"pull_request": {
|
||||
"number": 2,
|
||||
"title": "Fix a thing",
|
||||
"labels": [
|
||||
{ "id": 3, "name": "bug" },
|
||||
{ "id": 4, "name": "docs" }
|
||||
]
|
||||
}
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Unit tests for required-labels/check.sh. Runs on GitHub and Gitea.
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
check="${here}/../required-labels/check.sh"
|
||||
events="${here}/events"
|
||||
|
||||
failures=0
|
||||
|
||||
# run_case <name> <event-file> <expected-exit> <KEY=value...>
|
||||
run_case() {
|
||||
local name="$1" event="$2" expected="$3"
|
||||
shift 3
|
||||
local status=0
|
||||
env GITHUB_EVENT_PATH="${events}/${event}" GITHUB_OUTPUT=/dev/null "$@" \
|
||||
bash "$check" >/dev/null 2>&1 || status=$?
|
||||
if [ "$status" -eq "$expected" ]; then
|
||||
echo "ok - ${name}"
|
||||
else
|
||||
echo "FAIL - ${name} (expected exit ${expected}, got ${status})"
|
||||
failures=$((failures + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# kredits regex, at-least-one
|
||||
run_case "regex/any passes when a kredits label is present" \
|
||||
with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_MODE=any
|
||||
run_case "regex/any fails when no kredits label is present" \
|
||||
without.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_MODE=any
|
||||
|
||||
# opt-out
|
||||
run_case "empty labels pass" without.json 0 RL_LABELS=''
|
||||
run_case "disabled sentinel passes" without.json 0 RL_LABELS='disabled'
|
||||
|
||||
# all
|
||||
run_case "all passes when every required label is present" \
|
||||
with-kredits.json 0 RL_LABELS='bug,kredits-2' RL_MODE=all
|
||||
run_case "all fails when a required label is missing" \
|
||||
without.json 1 RL_LABELS='bug,kredits-2' RL_MODE=all
|
||||
|
||||
# exactly
|
||||
run_case "exactly passes on the expected count" \
|
||||
with-kredits.json 0 RL_LABELS='kredits-2,bug' RL_MODE=exactly RL_COUNT=2
|
||||
run_case "exactly fails on a different count" \
|
||||
with-kredits.json 1 RL_LABELS='kredits-2,bug' RL_MODE=exactly RL_COUNT=1
|
||||
|
||||
# exact (non-regex) matching, and multi-line input
|
||||
run_case "exact match is case-insensitive" \
|
||||
with-kredits.json 0 RL_LABELS='BUG' RL_MODE=all
|
||||
run_case "multi-line labels are parsed" \
|
||||
with-kredits.json 0 RL_LABELS=$'bug\nkredits-2' RL_MODE=all
|
||||
|
||||
# no labels on the PR
|
||||
run_case "regex/any fails when the PR has no labels" \
|
||||
no-labels.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
||||
|
||||
if [ "$failures" -ne 0 ]; then
|
||||
echo "${failures} test(s) failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "all tests passed"
|
||||
Reference in new issue
Block a user