Skip to content

fix: post-merge guard jq 正则转义(.github #91,ADR-0041) - #73

Merged
randypanding merged 2 commits into
mainfrom
fix/jq-escape
Aug 21, 2026
Merged

randypanding merged 2 commits into
mainfrom
fix/jq-escape

Conversation

@randypanding

Copy link
Copy Markdown
Contributor

jq 字符串中 [auto-revert] 正则须双反斜杠转义——实测单反斜杠 invalid escape 使 guard 步骤 bash -e 误红、revert 链路中断为 P0 告警。

Copilot AI lite review requested due to automatic review settings August 21, 2026 12:03
@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@randypanding, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Limit details: You’ve used all 10 included reviews currently available.

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5baa00fc-5439-48d0-960e-9cf1a41ec39a

📥 Commits

Reviewing files that changed from the base of the PR and between d471990 and 4873761.

📒 Files selected for processing (1)
  • .github/workflows/post-merge-verify.yml

Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Fix jq regex escaping for [auto-revert] detection in post-merge guard

🐞 Bug fix ⚙️ Configuration changes 🕐 10-20 Minutes

Grey Divider

AI Description

• Fix jq test() regex escaping so [auto-revert] titles are matched correctly.
• Prevent post-merge guard step from failing under bash -e due to invalid escape.
• Restore revert-chain signal checks (recent reverts / 24h reverts) without false red alarms.
Diagram

graph TD
  A["post-merge-verify.yml"] --> B["Guard bash step"] --> C["gh api"] --> D[("GitHub PR API")]
  B --> E["jq filter"] --> F["count output"]

  subgraph Legend
    direction LR
    _wf["Workflow"] ~~~ _step["Step"] ~~~ _ext[("External API")]
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Avoid regex: use jq `contains("[auto-revert]")`
  • ➕ No regex escaping pitfalls across YAML → bash → jq string layers
  • ➕ More readable intent for a literal marker match
  • ➖ Semantics differ if you relied on regex features (e.g., case-insensitive, boundaries)
  • ➖ Still requires careful quoting depending on shell/YAML context
2. Use GitHub search API / `gh pr list --search`
  • ➕ Moves filtering server-side; reduces client jq complexity
  • ➕ Potentially faster/less data pulled
  • ➖ Search syntax and indexing can be less predictable than direct JSON filtering
  • ➖ May require auth/permissions tweaks and different pagination handling

Recommendation: Current approach (keep jq test() but correctly double-escape backslashes) is the lowest-risk fix: it preserves existing behavior and only addresses the observed invalid escape failure under bash -e. If future guard logic remains a literal marker match, switching to contains("[auto-revert]") would further reduce escaping complexity.

Files changed (1) +2 / -2

Bug fix (1) +2 / -2
post-merge-verify.ymlDouble-escape jq regex for '[auto-revert]' title matching +2/-2

Double-escape jq regex for '[auto-revert]' title matching

• Updates the jq 'test()' pattern from single-escaped bracket literals to double-escaped sequences so jq receives a valid regex. This prevents 'invalid escape' errors and ensures the workflow correctly counts recent auto-revert PRs and 24h merged reverts.

.github/workflows/post-merge-verify.yml

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes jq regex escaping in the post-merge guard so [auto-revert] detection works correctly.

Changes:

  • Double-escapes brackets in both jq expressions.
  • Requires a human-authorized PR under repository workflow ownership rules.
Suppressed comments (1)

.github/workflows/post-merge-verify.yml:60

  • AGENTS.md:17 requires a bug fix to include a reproducing failing test first. This change has no regression test that exercises the shell-to-jq escaping and fails with the old single-backslash filter, so the reported P0 guard failure is not reproducibly covered. Add that test before applying the workflow update.
          RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
          echo "recent=$RECENT" >> "$GITHUB_OUTPUT"
          RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

echo "nested=false" >> "$GITHUB_OUTPUT"
fi
RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30" --jq '[.[] | select(.title | test("\[auto-revert\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30" --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
@randypanding
randypanding merged commit da8bf7e into main Aug 21, 2026
22 checks passed
@randypanding
randypanding deleted the fix/jq-escape branch August 21, 2026 12:06
@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (1) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Silent gh/jq failure risk 🐞 Bug ☼ Reliability
Description
In the guard step, RECENT=$(gh api ... --jq ...) and RCNT=$(gh api ... --jq ...) can fail inside
command substitution without failing the step under bash -e unless errexit is inherited, resulting
in empty/incorrect outputs and mis-routing the revert vs. alert logic.
Code

.github/workflows/post-merge-verify.yml[R58-60]

+          RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
          echo "recent=$RECENT" >> "$GITHUB_OUTPUT"
-          RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50"             --jq '[.[] | select(.title | test("\[auto-revert\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')
+          RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')
Relevance

●●● Strong

Recent workflow precedent accepted fail-closed handling when jq extraction can produce empty gating
variables.

PR-#29

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The PR-modified guard step uses command substitution to run gh api ... --jq ... and then writes
the results to GITHUB_OUTPUT. Bash errexit is not reliably inherited into subshell contexts
(including command substitutions) unless inherit_errexit is enabled, so failures may not stop the
step and can yield empty outputs.

.github/workflows/post-merge-verify.yml[53-61]
🌐 Demonstrates that set -o errexit may appear set in a subshell but does not cause exit on false, i.e., errexit is not effectively inherited in subshell contexts.
🌐 Explains and shows shopt -s inherit_errexit is needed for set -e to propagate into command substitutions so failures are not ignored.
PR-#29

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The guard step computes `recent` and `reverts_24h` via command substitution. In Bash, `set -e`/errexit is not reliably inherited into subshell contexts like command substitutions unless `inherit_errexit` is enabled, so a failing `gh api --jq` can leave `RECENT`/`RCNT` empty while the step continues.

## Issue Context
This logic gates whether to auto-revert or to create a P0 issue. When the API/jq path is unhealthy (rate limits, auth, transient failures), we should make the failure explicit and deterministic.

## Fix Focus Areas
- .github/workflows/post-merge-verify.yml[53-61]

## Suggested change
Wrap each `gh api` invocation with an explicit failure check, e.g.:

```bash
if ! RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30" \
 --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length'); then
 echo "::error::guard: failed to compute RECENT via gh/jq"
 exit 1
fi

if ! RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50" \
 --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length'); then
 echo "::error::guard: failed to compute RCNT via gh/jq"
 exit 1
fi
```

Optionally also add:

```bash
shopt -s inherit_errexit
set -euo pipefail
```

and validate outputs are numeric before writing them to `$GITHUB_OUTPUT`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational

2. post-merge-verify lacks regression test 📘 Rule violation ▣ Testability
Description
This PR is a bug fix but only changes the workflow jq escaping and does not add/modify any automated
test that would fail under the previous invalid jq escaping. Without a regression test, the guard
logic can regress and break the post-merge auto-revert chain again.
Code

.github/workflows/post-merge-verify.yml[R58-60]

+          RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
          echo "recent=$RECENT" >> "$GITHUB_OUTPUT"
-          RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50"             --jq '[.[] | select(.title | test("\[auto-revert\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')
+          RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50"             --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')
Relevance

● Weak

Recent workflow precedent explicitly rejected adding regression tests for workflow-only gate fixes.

PR-#29

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
PR Compliance ID 2787558 requires bug-fix PRs to include at least one new/modified automated
regression test; the diff shows the fix is implemented by changing the jq regex escaping in the
workflow, but no test changes are included alongside it.

Rule 2787558: Bug fix PRs must add a failing regression test
.github/workflows/post-merge-verify.yml[58-60]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
This bug-fix PR updates the jq `test(...)` pattern escaping in `.github/workflows/post-merge-verify.yml`, but it does not add a regression test that fails with the prior (buggy) jq string and passes with the fixed one.

## Issue Context
The failure mode described is `jq: invalid escape` during the guard step, which can be regression-tested by evaluating the jq filter (or an equivalent) against representative PR JSON and asserting it compiles/executes successfully.

## Fix Focus Areas
- .github/workflows/post-merge-verify.yml[58-60]
- Makefile[1-16]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
✅ Compliance rules (platform): 5 rules
✅ Web pages:
  +5 more
Review mode: ⚖️ Balanced: This is a small, localized change, but it alters a CI post-merge/revert guard on a P0 operational path, so a complete review is warranted.
ⓘ  1 issues published inline · 2 in summary

Grey Divider

Tip of the day
💡 Did you know, you can tweak Display preferences with a live preview to see your comment before it ships

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment on lines +58 to +60
RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30" --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length')
echo "recent=$RECENT" >> "$GITHUB_OUTPUT"
RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50" --jq '[.[] | select(.title | test("\[auto-revert\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')
RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50" --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

2. Silent gh/jq failure risk 🐞 Bug ☼ Reliability

In the guard step, RECENT=$(gh api ... --jq ...) and RCNT=$(gh api ... --jq ...) can fail inside
command substitution without failing the step under bash -e unless errexit is inherited, resulting
in empty/incorrect outputs and mis-routing the revert vs. alert logic.
Agent Prompt
## Issue description
The guard step computes `recent` and `reverts_24h` via command substitution. In Bash, `set -e`/errexit is not reliably inherited into subshell contexts like command substitutions unless `inherit_errexit` is enabled, so a failing `gh api --jq` can leave `RECENT`/`RCNT` empty while the step continues.

## Issue Context
This logic gates whether to auto-revert or to create a P0 issue. When the API/jq path is unhealthy (rate limits, auth, transient failures), we should make the failure explicit and deterministic.

## Fix Focus Areas
- .github/workflows/post-merge-verify.yml[53-61]

## Suggested change
Wrap each `gh api` invocation with an explicit failure check, e.g.:

```bash
if ! RECENT=$(gh api "repos/$REPO/pulls?state=all&sort=created&direction=desc&per_page=30" \
  --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.created_at > (now - 3600 | todateiso8601))] | length'); then
  echo "::error::guard: failed to compute RECENT via gh/jq"
  exit 1
fi

if ! RCNT=$(gh api "repos/$REPO/pulls?state=all&sort=updated&direction=desc&per_page=50" \
  --jq '[.[] | select(.title | test("\\[auto-revert\\]")) | select(.merged_at != null) | select(.merged_at > (now - 86400 | todateiso8601))] | length'); then
  echo "::error::guard: failed to compute RCNT via gh/jq"
  exit 1
fi
```

Optionally also add:

```bash
shopt -s inherit_errexit
set -euo pipefail
```

and validate outputs are numeric before writing them to `$GITHUB_OUTPUT`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants