Skip to content

Commit 999fe48

Browse files
committed
address review: close comment-order and parsing holes, pin stale triggers
- js_object_block strips // comments before block detection, so a commented-out 'const typeToKind = {' can never open the block - defined_labels parses the name: value verbatim (labels with spaces, quotes, inline comments) instead of awk $3 - stale_input strips trailing inline comments from values - the 'edited' guard scans the whole trigger section, catching the block-sequence spelling too - removed the dead grep -v filter on literal labels - reworded the sh-compat comment: the real constraint is no process substitution; local is supported by the runner and used by the other contract files - new test pins stale.yaml triggers: daily cron + workflow_dispatch only, no event triggers All previous mutation checks still go red, plus three new ones for the holes closed here (block-sequence 'edited', cron change, commented-out typeToKind).
1 parent 035967a commit 999fe48

1 file changed

Lines changed: 41 additions & 12 deletions

File tree

‎hack/label-workflows-contract.bats‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,39 @@ script_lines() {
3131
}
3232

3333
# Body of a `const <name> = {` ... `};` object literal inside the
34-
# github-script block, comment lines already stripped.
34+
# github-script block. Comments are stripped BEFORE block detection so a
35+
# commented-out `// const typeToKind = {` can never open the block.
3536
js_object_block() {
36-
awk -v name="const $1 = {" '
37+
script_lines < "$2" | awk -v name="const $1 = {" '
3738
index($0, name) { inside = 1; next }
3839
inside && /^[[:space:]]*};[[:space:]]*$/ { exit }
39-
inside' "$2" | script_lines
40+
inside'
4041
}
4142

42-
# Every label defined in .github/labels.yml, one per line.
43+
# Every label defined in .github/labels.yml, one per line. Parses the value
44+
# after `- name: ` verbatim (labels may contain spaces, e.g. "help wanted"),
45+
# stripping optional quotes and trailing inline comments.
4346
defined_labels() {
44-
code_lines < "$LABELS" | awk '/^- name: / { print $3 }'
47+
code_lines < "$LABELS" | awk '/^- name: / {
48+
sub(/^- name: /, "")
49+
sub(/[[:space:]]+#.*$/, "")
50+
sub(/[[:space:]]+$/, "")
51+
gsub(/^"|"$/, "")
52+
print
53+
}'
4554
}
4655

47-
# `key: value` inputs of the stale action, comment lines stripped.
56+
# `key: value` inputs of the stale action; whole-line and trailing inline
57+
# comments are both stripped from the value.
4858
stale_input() {
4959
code_lines < "$STALE" | awk -v key=" $1: " '
50-
index($0, key) == 1 { sub(key, ""); print; exit }'
60+
index($0, key) == 1 {
61+
sub(key, "")
62+
sub(/[[:space:]]+#.*$/, "")
63+
sub(/[[:space:]]+$/, "")
64+
print
65+
exit
66+
}'
5167
}
5268

5369
# A `>-` folded list input of the stale action, flattened to one label per line.
@@ -60,7 +76,9 @@ stale_exempt_list() {
6076

6177
assert_labels_defined() {
6278
# $1: newline-separated label names. Fails naming the missing ones.
63-
# POSIX-only on purpose: hack/cozytest.sh runs these files in plain sh.
79+
# No process substitution here: hack/cozytest.sh runs these files in plain
80+
# sh, which lacks it. (`local` is fine — dash/ash/bash all support it, and
81+
# the other contract files in hack/ already use it.)
6482
local all missing
6583
all="$(defined_labels)"
6684
missing="$(printf '%s\n' "$1" | sort -u | while read -r l; do
@@ -85,8 +103,7 @@ assert_labels_defined() {
85103
# Labels added outside the two tables: backport prefix, breaking-change
86104
# footer, and the no-area fallback.
87105
literal_labels="$(code_lines < "$PR_LABELER" | script_lines \
88-
| grep -o "toAdd.add('[^']*')" | sed "s/toAdd.add('//; s/')//" \
89-
| grep -v '^typeToKind\|^scopeToArea' || true)"
106+
| grep -o "toAdd.add('[^']*')" | sed "s/toAdd.add('//; s/')//" || true)"
90107
for l in area/release kind/breaking-change area/uncategorized; do
91108
printf '%s\n' "$literal_labels" | grep -qxF "$l"
92109
done
@@ -110,8 +127,9 @@ assert_labels_defined() {
110127
count="$(code_lines < "$PR_LABELER" | grep -cF ' types: [opened, reopened, synchronize]' || true)"
111128
[ "${count:-0}" -eq 1 ]
112129
# 'edited' is deliberately omitted (see the comment above the trigger); it
113-
# must not creep back in as an executable line.
114-
! code_lines < "$PR_LABELER" | grep -E '^[[:space:]]*types:' | grep -q 'edited'
130+
# must not creep back in as an executable line — in either the flow-sequence
131+
# or block-sequence spelling. Scan the whole trigger section (before jobs:).
132+
! code_lines < "$PR_LABELER" | awk '/^jobs:/ { exit } { print }' | grep -qw 'edited'
115133
}
116134
117135
@test "pr-labeler is additive only and falls back to area/uncategorized" {
@@ -162,3 +180,14 @@ assert_labels_defined() {
162180
[ "$(stale_input operations-per-run)" = "100" ]
163181
[ "$(stale_input remove-stale-when-updated)" = "true" ]
164182
}
183+
184+
@test "stale runs only on a daily schedule and manual dispatch" {
185+
triggers="$(code_lines < "$STALE" | awk '/^on:/ { inside = 1; next }
186+
inside && /^[^[:space:]]/ { exit }
187+
inside')"
188+
[ -n "$triggers" ]
189+
printf '%s\n' "$triggers" | grep -qF " - cron: '37 4 * * *'"
190+
printf '%s\n' "$triggers" | grep -qE '^ workflow_dispatch:'
191+
# No event trigger may run the write policy on user activity.
192+
! printf '%s\n' "$triggers" | grep -qE '^ (push|pull_request|pull_request_target|issues|issue_comment):'
193+
}

0 commit comments

Comments
 (0)