Skip to content

Commit a846012

Browse files
committed
test(ci): pin the label-writing workflows with contract tests
pr-labeler.yaml and stale.yaml decide which labels land on issues and PRs, and neither had a test behind it. A mistyped label name is not an error, it is a rule that silently stops matching. hack/label-workflows-contract.bats pins: - every label either workflow can write or match resolves against a name: in .github/labels.yml (typeToKind, scopeToArea, the three literal adds, stale/close labels, both exempt lists) - the typeToKind mappings are exactly the rows of the type table in docs/agents/contributing.md, in both directions, and a type the table lists under "(no kind label)" has no key in the workflow - the trigger list stays [opened, reopened, synchronize] and 'edited' does not come back in either YAML spelling - the labeler stays additive-only (addLabels only, no removeLabel or setLabels) with the area/uncategorized fallback - stale policy values: label choices, 12 issue and 4 PR exemptions, days-before-stale 60, days-before-close 14, operations-per-run 100 - stale runs from its daily cron and workflow_dispatch only JS // and YAML # comments are stripped before matching, so a mapping or a gate demoted to a comment cannot satisfy the contract. Negative checks use the grep -c / -eq 0 form of the sibling contract files: a negated pipeline is ignored by set -e, so it cannot fail a test under hack/cozytest.sh and fails under bats only as the last command. Every assertion was mutation-checked under both bats and hack/cozytest.sh. POSIX-only constructs; picked up automatically by BATS_UNIT_FILES. Closes #3589 Signed-off-by: shaurya703 <[email protected]>
1 parent a5d12e7 commit a846012

1 file changed

Lines changed: 240 additions & 0 deletions

File tree

‎hack/label-workflows-contract.bats‎

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#!/usr/bin/env bats
2+
3+
# Contract for the two label-writing workflows: pr-labeler.yaml (which labels
4+
# it may apply and from what input) and stale.yaml (which labels drive the
5+
# auto-close policy and who is exempt). Every label name either workflow can
6+
# write or match must resolve against .github/labels.yml — a mistyped name is
7+
# not an error at runtime, it is a rule that silently stops matching.
8+
#
9+
# These tests intentionally pin executable/structural lines, not prose: a
10+
# mapping demoted to a YAML `#` or JavaScript `//` comment must never satisfy
11+
# the contract. See hack/promote-gate-contract.bats for the pattern.
12+
13+
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
14+
PR_LABELER="$REPO_ROOT/.github/workflows/pr-labeler.yaml"
15+
STALE="$REPO_ROOT/.github/workflows/stale.yaml"
16+
LABELS="$REPO_ROOT/.github/labels.yml"
17+
DOCS="$REPO_ROOT/docs/agents/contributing.md"
18+
19+
# Comment-stripping filters, one per language in these files. grep exits 1 on
20+
# an empty selection (legitimate for an empty block) and 2 on a real error, so
21+
# only the latter propagates.
22+
code_lines() {
23+
local rc=0
24+
grep -v '^[[:space:]]*#' || rc=$?
25+
[ "$rc" -le 1 ]
26+
}
27+
28+
script_lines() {
29+
local rc=0
30+
grep -v '^[[:space:]]*//' || rc=$?
31+
[ "$rc" -le 1 ]
32+
}
33+
34+
# Body of a `const <name> = {` ... `};` object literal inside the
35+
# github-script block. Comments are stripped BEFORE block detection so a
36+
# commented-out `// const typeToKind = {` can never open the block.
37+
js_object_block() {
38+
script_lines < "$2" | awk -v name="const $1 = {" '
39+
index($0, name) { inside = 1; next }
40+
inside && /^[[:space:]]*};[[:space:]]*$/ { exit }
41+
inside'
42+
}
43+
44+
# The type -> kind/* table under "Type → `kind/*`" in docs/agents/contributing.md,
45+
# as "<type> <label>" lines. A row listing several types yields one line per
46+
# type; a "(no kind label)" row yields "-" as the label, so a type documented
47+
# as unmapped is distinguishable from one the table does not mention.
48+
doc_type_table() {
49+
awk '
50+
/^\*\*Type .*kind\/\*.*:\*\*/ { inside = 1; next }
51+
inside && /^\|/ {
52+
started = 1
53+
if ($0 ~ /^\|[[:space:]]*type[[:space:]]*\|/ || $0 ~ /^\|[[:space:]-]*\|[[:space:]-]*\|/) next
54+
line = $0
55+
sub(/^\|[[:space:]]*/, "", line)
56+
sub(/[[:space:]]*\|[[:space:]]*$/, "", line)
57+
split(line, cell, /[[:space:]]*\|[[:space:]]*/)
58+
label = cell[2]
59+
if (label == "(no kind label)") label = "-"
60+
n = split(cell[1], types, /[[:space:]]*,[[:space:]]*/)
61+
for (i = 1; i <= n; i++) print types[i], label
62+
next
63+
}
64+
inside && started { exit }
65+
' "$DOCS"
66+
}
67+
68+
# The executable typeToKind mappings in pr-labeler.yaml, as "<type> <label>"
69+
# lines. Comment-stripped by js_object_block, so a mapping demoted to a
70+
# `//` comment is not listed.
71+
workflow_type_table() {
72+
js_object_block typeToKind "$PR_LABELER" \
73+
| sed -nE "s/^[[:space:]]*([A-Za-z]+):[[:space:]]*'([^']*)',?[[:space:]]*$/\1 \2/p"
74+
}
75+
76+
# Every label defined in .github/labels.yml, one per line. Parses the value
77+
# after `- name: ` verbatim (labels may contain spaces, e.g. "help wanted"),
78+
# stripping optional quotes and trailing inline comments.
79+
defined_labels() {
80+
code_lines < "$LABELS" | awk '/^- name: / {
81+
sub(/^- name: /, "")
82+
sub(/[[:space:]]+#.*$/, "")
83+
sub(/[[:space:]]+$/, "")
84+
gsub(/^"|"$/, "")
85+
print
86+
}'
87+
}
88+
89+
# `key: value` inputs of the stale action; whole-line and trailing inline
90+
# comments are both stripped from the value.
91+
stale_input() {
92+
code_lines < "$STALE" | awk -v key=" $1: " '
93+
index($0, key) == 1 {
94+
sub(key, "")
95+
sub(/[[:space:]]+#.*$/, "")
96+
sub(/[[:space:]]+$/, "")
97+
print
98+
exit
99+
}'
100+
}
101+
102+
# A `>-` folded list input of the stale action, flattened to one label per line.
103+
stale_exempt_list() {
104+
code_lines < "$STALE" | awk -v key=" $1: >-" '
105+
$0 == key { inside = 1; next }
106+
inside && !/^ / { exit }
107+
inside { print }' | tr -d ' ' | tr ',' '\n' | grep -v '^$'
108+
}
109+
110+
assert_labels_defined() {
111+
# $1: newline-separated label names. Fails naming the missing ones.
112+
# No process substitution here: hack/cozytest.sh runs these files in plain
113+
# sh, which lacks it. (`local` is fine — dash/ash/bash all support it, and
114+
# the other contract files in hack/ already use it.)
115+
local all missing
116+
all="$(defined_labels)"
117+
missing="$(printf '%s\n' "$1" | sort -u | while read -r l; do
118+
[ -n "$l" ] || continue
119+
printf '%s\n' "$all" | grep -qxF "$l" || printf '%s\n' "$l"
120+
done)"
121+
if [ -n "$missing" ]; then
122+
echo "labels not defined in .github/labels.yml: $missing" >&2
123+
return 1
124+
fi
125+
}
126+
127+
@test "every label pr-labeler can apply resolves against labels.yml" {
128+
kind_labels="$(js_object_block typeToKind "$PR_LABELER" | grep -o "'kind/[^']*'" | tr -d "'")"
129+
[ -n "$kind_labels" ]
130+
assert_labels_defined "$kind_labels"
131+
132+
area_labels="$(js_object_block scopeToArea "$PR_LABELER" | grep -o "'area/[^']*'" | tr -d "'")"
133+
[ -n "$area_labels" ]
134+
assert_labels_defined "$area_labels"
135+
136+
# Labels added outside the two tables: backport prefix, breaking-change
137+
# footer, and the no-area fallback.
138+
literal_labels="$(code_lines < "$PR_LABELER" | script_lines \
139+
| grep -o "toAdd.add('[^']*')" | sed "s/toAdd.add('//; s/')//" || true)"
140+
for l in area/release kind/breaking-change area/uncategorized; do
141+
printf '%s\n' "$literal_labels" | grep -qxF "$l"
142+
done
143+
assert_labels_defined "$literal_labels"
144+
}
145+
146+
@test "pr-labeler's type mappings are the table in docs/agents/contributing.md" {
147+
documented="$(doc_type_table)"
148+
[ -n "$documented" ]
149+
mapped="$(workflow_type_table)"
150+
[ -n "$mapped" ]
151+
152+
# Mapped rows: the same set in both files, in both directions, so a row
153+
# added, dropped, retyped or demoted to a `//` comment on either side fails.
154+
expected="$(printf '%s\n' "$documented" | grep -v ' -$' | sort)"
155+
actual="$(printf '%s\n' "$mapped" | sort)"
156+
if [ "$expected" != "$actual" ]; then
157+
printf 'docs table:\n%s\nworkflow typeToKind:\n%s\n' "$expected" "$actual" >&2
158+
return 1
159+
fi
160+
161+
# Rows the table documents as unmapped must have no key in typeToKind.
162+
unmapped="$(printf '%s\n' "$documented" | awk '$2 == "-" { print $1 }')"
163+
[ -n "$unmapped" ]
164+
for t in $unmapped; do
165+
count="$(printf '%s\n' "$actual" | grep -c "^$t " || true)"
166+
[ "${count:-0}" -eq 0 ]
167+
done
168+
}
169+
170+
@test "pr-labeler subscribes to opened/reopened/synchronize and not edited" {
171+
count="$(code_lines < "$PR_LABELER" | grep -cF ' types: [opened, reopened, synchronize]' || true)"
172+
[ "${count:-0}" -eq 1 ]
173+
# 'edited' is deliberately omitted (see the comment above the trigger); it
174+
# must not creep back in as an executable line — in either the flow-sequence
175+
# or block-sequence spelling. Scan the whole trigger section (before jobs:).
176+
count="$(code_lines < "$PR_LABELER" | awk '/^jobs:/ { exit } { print }' | grep -cw 'edited' || true)"
177+
[ "${count:-0}" -eq 0 ]
178+
}
179+
180+
@test "pr-labeler is additive only and falls back to area/uncategorized" {
181+
lines="$(code_lines < "$PR_LABELER" | script_lines)"
182+
183+
count="$(printf '%s\n' "$lines" | grep -cF "toAdd.add('area/uncategorized')" || true)"
184+
[ "${count:-0}" -eq 1 ]
185+
186+
# Only labels not already present are sent, and only via addLabels: a
187+
# labeler that starts removing or replacing labels is a different tool.
188+
count="$(printf '%s\n' "$lines" | grep -cF '!existing.has(l)' || true)"
189+
[ "${count:-0}" -eq 1 ]
190+
count="$(printf '%s\n' "$lines" | grep -cF 'addLabels' || true)"
191+
[ "${count:-0}" -eq 1 ]
192+
count="$(printf '%s\n' "$lines" | grep -cE 'removeLabel|setLabels' || true)"
193+
[ "${count:-0}" -eq 0 ]
194+
}
195+
196+
@test "every label stale.yaml writes or matches resolves against labels.yml" {
197+
for key in stale-issue-label stale-pr-label close-issue-label close-pr-label; do
198+
value="$(stale_input "$key")"
199+
[ -n "$value" ]
200+
assert_labels_defined "$value"
201+
done
202+
assert_labels_defined "$(stale_exempt_list exempt-issue-labels)"
203+
assert_labels_defined "$(stale_exempt_list exempt-pr-labels)"
204+
}
205+
206+
@test "stale policy pins: label choices, exempt list sizes, day counts, write cap" {
207+
[ "$(stale_input stale-issue-label)" = "lifecycle/stale" ]
208+
[ "$(stale_input stale-pr-label)" = "lifecycle/stale" ]
209+
[ "$(stale_input close-issue-label)" = "lifecycle/rotten" ]
210+
[ "$(stale_input close-pr-label)" = "lifecycle/rotten" ]
211+
212+
count="$(stale_exempt_list exempt-issue-labels | wc -l | tr -d ' ')"
213+
[ "$count" -eq 12 ]
214+
count="$(stale_exempt_list exempt-pr-labels | wc -l | tr -d ' ')"
215+
[ "$count" -eq 4 ]
216+
217+
# The renewal path: frozen exempts both; an accepted issue or a held PR is
218+
# deliberate, not stale.
219+
stale_exempt_list exempt-issue-labels | grep -qxF 'lifecycle/frozen'
220+
stale_exempt_list exempt-issue-labels | grep -qxF 'triage/accepted'
221+
stale_exempt_list exempt-pr-labels | grep -qxF 'lifecycle/frozen'
222+
stale_exempt_list exempt-pr-labels | grep -qxF 'do-not-merge/hold'
223+
224+
[ "$(stale_input days-before-stale)" = "60" ]
225+
[ "$(stale_input days-before-close)" = "14" ]
226+
[ "$(stale_input operations-per-run)" = "100" ]
227+
[ "$(stale_input remove-stale-when-updated)" = "true" ]
228+
}
229+
230+
@test "stale runs only on a daily schedule and manual dispatch" {
231+
triggers="$(code_lines < "$STALE" | awk '/^on:/ { inside = 1; next }
232+
inside && /^[^[:space:]]/ { exit }
233+
inside')"
234+
[ -n "$triggers" ]
235+
printf '%s\n' "$triggers" | grep -qF " - cron: '37 4 * * *'"
236+
printf '%s\n' "$triggers" | grep -qE '^ workflow_dispatch:'
237+
# No event trigger may run the write policy on user activity.
238+
count="$(printf '%s\n' "$triggers" | grep -cE '^ (push|pull_request|pull_request_target|issues|issue_comment):' || true)"
239+
[ "${count:-0}" -eq 0 ]
240+
}

0 commit comments

Comments
 (0)