-
Notifications
You must be signed in to change notification settings - Fork 0
test(reviewer): guard the comment-selection filter that has broken twice #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+157
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # agy-review-selftest.sh -- guards the comment-selection logic in `agy-review.sh`. | ||
| # | ||
| # Why this exists: that filter decides which PR comments the bot DELETES, and it has been wrong | ||
| # twice, both times invisibly. | ||
| # | ||
| # 1. The just-posted comment was not reliably excluded. `new_comment_id` came from re-querying | ||
| # the comment list, which races GitHub's read replication; on a miss the exclusion became | ||
| # `select(.id != null)`, true for every id, and the run deleted the review it had just | ||
| # published. | ||
| # 2. jq's `--arg`/`--argjson` were handed to `gh api`, which has no such flags. It exited | ||
| # non-zero, `2>/dev/null` hid the message, and `set -o pipefail` + `set -e` killed the script | ||
| # AFTER posting — so stale comments silently accumulated and the job went red with nothing in | ||
| # the log explaining why. | ||
| # | ||
| # Neither was catchable by looking at the review the bot posted: both times it posted fine. So the | ||
| # filter is tested here directly, offline, against fixtures — no network, no `gh`, no runner. | ||
| # | ||
| # Run: bash scripts/agy-review-selftest.sh | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| # Source the constants out of the reviewer without running it. `agy-review.sh` does its work at | ||
| # top level, so it cannot simply be sourced; the two values under test are lifted by pattern | ||
| # instead. That coupling is deliberate: if either declaration is renamed or reshaped, this test | ||
| # fails loudly rather than silently checking a stale copy of the filter. | ||
| extract_marker() { | ||
| sed -n 's/^MARKER="\(.*\)"$/\1/p' "$SCRIPT_DIR/agy-review.sh" | head -n 1 | ||
| } | ||
| extract_filter() { | ||
| sed -n "/^SELECT_STALE_JQ='/,/'\$/p" "$SCRIPT_DIR/agy-review.sh" \ | ||
| | sed "1s/^SELECT_STALE_JQ='//; \$s/'\$//" | ||
| } | ||
|
|
||
| MARKER="$(extract_marker)" | ||
| FILTER="$(extract_filter)" | ||
|
|
||
| [ -n "$MARKER" ] || { echo "FAIL: could not extract MARKER from agy-review.sh" >&2; exit 1; } | ||
| [ -n "$FILTER" ] || { echo "FAIL: could not extract SELECT_STALE_JQ from agy-review.sh" >&2; exit 1; } | ||
|
|
||
| # A non-empty extraction is not the same as a COMPLETE one. The `sed` range above ends at the | ||
| # first line closing with a quote, so a filter whose body ever ends a line that way would be | ||
| # truncated — and a truncated jq program can still be valid and still return ids, which is the | ||
| # silent-wrong-answer this whole file exists to prevent. Two independent guards: | ||
| # | ||
| # 1. it must compile (a truncated program is usually, though not always, a syntax error); | ||
| # 2. it must END with the projection, which is what makes it a complete pipeline rather than a | ||
| # prefix of one. | ||
| # The named args must be supplied here too: the filter references `$marker`/`$new_id`, and jq | ||
| # rejects an undefined variable at COMPILE time — so omitting them fails a perfectly good program. | ||
| if ! printf '[]' | jq --arg marker x --argjson new_id 0 "$FILTER" >/dev/null 2>&1; then | ||
| echo "FAIL: extracted SELECT_STALE_JQ is not a valid jq program (truncated?):" >&2 | ||
| printf '%s\n' "$FILTER" >&2 | ||
| exit 1 | ||
| fi | ||
| case "$(printf '%s' "$FILTER" | tr -d '[:space:]')" in | ||
| *'|.id') : ;; | ||
| *) echo "FAIL: extracted SELECT_STALE_JQ does not end in '| .id'; extraction truncated" >&2 | ||
| printf '%s\n' "$FILTER" >&2 | ||
| exit 1 ;; | ||
| esac | ||
|
|
||
| fixture() { | ||
| cat <<JSON | ||
| [ | ||
| {"id": 111, "user": {"type": "Bot", "login": "github-actions[bot]"}, "body": "$MARKER\nold review"}, | ||
| {"id": 222, "user": {"type": "Bot", "login": "github-actions[bot]"}, "body": "$MARKER\nolder still"}, | ||
| {"id": 333, "user": {"type": "User", "login": "someone"}, "body": "$MARKER\nnot ours"}, | ||
| {"id": 444, "user": {"type": "Bot", "login": "other-bot"}, "body": "$MARKER\nwrong bot"}, | ||
| {"id": 555, "user": {"type": "Bot", "login": "github-actions[bot]"}, "body": "an ordinary bot comment"}, | ||
| {"id": 999, "user": {"type": "Bot", "login": "github-actions[bot]"}, "body": "$MARKER\nJUST POSTED"} | ||
| ] | ||
| JSON | ||
| } | ||
|
|
||
| # Ids as a single space-separated line, with no trailing space — so the expected values below read | ||
| # as what they are rather than carrying padding an assertion would have to mirror. | ||
| select_ids() { | ||
| fixture | jq -r --arg marker "$MARKER" --argjson new_id "$1" "$FILTER" | sort -n | paste -sd' ' - | ||
| } | ||
|
|
||
| fails=0 | ||
| check() { | ||
| local name="$1" want="$2" got="$3" | ||
| if [ "$got" = "$want" ]; then | ||
| echo " ok $name" | ||
| else | ||
| echo " FAIL $name" | ||
| echo " want: [$want]" | ||
| echo " got: [$got]" | ||
| fails=$((fails + 1)) | ||
| fi | ||
| } | ||
|
|
||
| echo "agy-review comment-selection self-test" | ||
|
|
||
| # The whole point: the comment just published is never selected for deletion. | ||
| check "excludes the just-posted comment" "111 222" "$(select_ids 999)" | ||
|
|
||
| # The author filter is a security control, not tidiness: without it any user could paste the | ||
| # marker into a comment and have the bot delete comments on the next run. | ||
| check "ignores other users and other bots" "111 222" "$(select_ids 999)" | ||
|
|
||
| # A bot comment without the marker is somebody else's feature (a CI summary, a deploy note). | ||
| check "ignores bot comments without the marker" "111 222" "$(select_ids 999)" | ||
|
|
||
| # Regression #1, pinned: an unknown id must not select everything. The caller now refuses to run | ||
| # the delete at all in this case, but the filter itself is checked so the two guards are | ||
| # independent rather than one relying on the other. | ||
| check "an id of 0 still excludes nothing real" "111 222 999" "$(select_ids 0)" | ||
|
|
||
| # A different id in the set behaves the same way, so the exclusion is genuinely by value. | ||
| check "excludes whichever id it is given" "222 999" "$(select_ids 111)" | ||
|
|
||
| # Regression #2, pinned: `--arg`/`--argjson` belong to jq. If they are ever moved onto `gh api` | ||
| # again, that command exits non-zero — assert the flags are not passed to `gh api` in the script. | ||
| # Line continuations are folded first: `--arg` moved onto a continuation line would otherwise sit | ||
| # on a different physical line from `gh api`, and a line-by-line grep would report a false pass on | ||
| # exactly the mistake this check exists to catch. | ||
| if sed -e ':a' -e '/\\$/{N;s/\\\n//;ba' -e '}' "$SCRIPT_DIR/agy-review.sh" \ | ||
| | grep -qE 'gh api[^|]*--(arg|argjson)'; then | ||
| echo " FAIL --arg/--argjson passed to \`gh api\` (jq flags; gh api rejects them)" | ||
| fails=$((fails + 1)) | ||
| else | ||
| echo " ok --arg/--argjson are not passed to \`gh api\`" | ||
| fi | ||
|
|
||
| if [ "$fails" -ne 0 ]; then | ||
| echo "$fails check(s) failed" >&2 | ||
| exit 1 | ||
| fi | ||
| echo "all checks passed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Security Misconfiguration (CWE-732): Incorrect Permission Assignment for Critical Resource
Declare least-privilege permissions for
lint.Line 105 runs PR-controlled code, but
linthas no job-levelpermissions:block. Its token privileges therefore inherit repository defaults; declare only the read access checkout needs, such ascontents: read.As per path instructions, jobs must have a least-privilege
permissions:block.🤖 Prompt for AI Agents
Source: Path instructions