Skip to content

Commit 8b1c620

Browse files
authored
tmp: rebase PR 1633 onto dev (#12) (#13) (#1633)
* fix(pr-quality): break CodeRabbit review feedback loop in gate comment The readiness gate wrote a literal '@coderabbitai review' command into its status comment, and it rewrote an unchanged comment body on every wake. - Remove the inline CodeRabbit command from the ready notice. - Skip updateComment when the rebuilt body is unchanged. - Add regression tests for both behaviors. * test(pr-quality): cover unchanged CodeRabbit status wake * test(pr-quality): keep status-wake regression focused * test(pr-quality): cover legacy migration on no-op wake * test(pr-quality): reproduce stale gate comment body * fix(pr-quality): track gate comment body after writes * test(pr-quality): restore trust-boundary context * test(pr-quality): pin repeated failure comment target * test: fix gate body helper
1 parent dffa373 commit 8b1c620

3 files changed

Lines changed: 205 additions & 2 deletions

File tree

‎.github/scripts/enforce-pr-target.test.cjs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ describe("enforce-pr-target workflow", () => {
112112
assert.match(workflow, /reviewReadyDesired/);
113113
});
114114

115+
it("does not embed a literal CodeRabbit review command in the ready notice", () => {
116+
// A literal "@coderabbitai review" inside the gate comment is executed by
117+
// CodeRabbit as a review command even when rendered as inline code. Its
118+
// success status then wakes this workflow again, which rewrites the same
119+
// comment, which CodeRabbit reads as a new command -- a self-sustaining
120+
// loop that only stops on CodeRabbit's per-hour rate limit. The ready
121+
// notice must describe the label without issuing a command (PR #1630).
122+
assert.doesNotMatch(workflow, /coderabbitai review/);
123+
});
124+
125+
it("does not rewrite the gate comment when the rebuilt body is unchanged", () => {
126+
// The ready-path rebuild is deterministic: on a CodeRabbit status wake the
127+
// gate recomputes the same READY body and would call updateComment on it.
128+
// That no-op edit is still a mutation event to review bots and restarts the
129+
// loop above, so the upsert must skip the write when body equals the posted
130+
// comment body (PR #1630).
131+
assert.match(workflow, /if \(gateComment\?\.body === body\)/);
132+
assert.match(workflow, /let body = buildGateCommentBody/);
133+
});
134+
115135
it("keeps CodeRabbit auto-review unfiltered so maintainer PRs are not starved", () => {
116136
// A positive `labels:` filter under `reviews.auto_review` in
117137
// `.coderabbit.yaml` would restrict ALL automatic reviews to PRs carrying

‎.github/workflows/enforce-pr-target.yml‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ jobs:
300300
// One consolidated comment. The gate finds its own comment by the
301301
// single GATE_MARKER; the legacy enforcer/readiness markers are
302302
// matched only to migrate pre-consolidation PRs.
303-
const gateComment = comments.find(
303+
let gateComment = comments.find(
304304
comment =>
305305
comment.user?.login === "github-actions[bot]" &&
306306
comment.body?.includes(GATE_MARKER)
@@ -406,12 +406,22 @@ jobs:
406406
);
407407
}
408408
if (gateCommentId) {
409+
// Skip the write when the rebuilt body matches what is already
410+
// posted. A no-op comment edit is still a mutation event to
411+
// review bots, so updating an identical body would re-wake the
412+
// CodeRabbit status signal that triggered this run and create
413+
// a self-sustaining loop.
414+
if (gateComment?.body === body) {
415+
await migrateLegacyCommentsIfNeeded();
416+
return;
417+
}
409418
await github.rest.issues.updateComment({
410419
owner,
411420
repo,
412421
comment_id: gateCommentId,
413422
body
414423
});
424+
gateComment.body = body;
415425
await migrateLegacyCommentsIfNeeded();
416426
return;
417427
}
@@ -422,6 +432,7 @@ jobs:
422432
body
423433
});
424434
gateCommentId = created.data.id;
435+
gateComment = { id: gateCommentId, body };
425436
await migrateLegacyCommentsIfNeeded();
426437
}
427438
@@ -1351,7 +1362,7 @@ jobs:
13511362
? "This pull request has been marked Ready for Review."
13521363
: "This pull request is already Ready for Review.",
13531364
readyMoment
1354-
? `The ${inlineCode(REVIEW_READY_LABEL)} label marks this PR as ready; review automation runs independently. If no CodeRabbit review appears, comment ${inlineCode("@coderabbitai review")} to request one.`
1365+
? `The ${inlineCode(REVIEW_READY_LABEL)} label marks this PR as ready; review automation runs independently.`
13551366
: "",
13561367
notified && maintainers.length > 0
13571368
? `Maintainers notified: ${maintainers

‎tests/zz-pr-coderabbit-readiness-revalidation.test.ts‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { describe, expect, test } from "bun:test";
2+
import {
3+
callsTo,
4+
runEnforcePrTarget,
5+
} from "./helpers/enforce-pr-target-harness";
26

37
type WorkflowJob = {
48
if?: string;
@@ -23,6 +27,72 @@ type Workflow = {
2327
jobs?: Record<string, WorkflowJob>;
2428
};
2529

30+
const GATE_MARKER = "<!-- opencodex-pr-gate -->";
31+
const CHECKLIST_START = "<!-- pr-quality-readiness-checklist:start -->";
32+
const CHECKLIST_END = "<!-- pr-quality-readiness-checklist:end -->";
33+
const CHECKLIST_ITEMS = [
34+
"All CI tests are green on my local testing.",
35+
"I pushed my PR to the latest dev commit.",
36+
"I resolved all correct Codex and CodeRabbit findings.",
37+
"My PR is ready for review.",
38+
];
39+
40+
const MAINTAINERS_FIXTURE = [
41+
"## Current maintainers",
42+
"",
43+
"| GitHub account | Project role | Responsibilities |",
44+
"| --- | --- | --- |",
45+
"| [@lidge-jun](https://github.com/lidge-jun) | Project owner | x |",
46+
"| [@Ingwannu](https://github.com/Ingwannu) | Maintainer | x |",
47+
"| [@Wibias](https://github.com/Wibias) | Maintainer | x |",
48+
].join("\n");
49+
50+
function completedChecklistBody(): string {
51+
const description = [
52+
"## Summary",
53+
"",
54+
"Fix the PR-quality gate so an unchanged CodeRabbit status wake cannot rewrite its own READY comment.",
55+
"",
56+
"## Test plan",
57+
"",
58+
"- Run the PR-quality regression tests.",
59+
].join("\n");
60+
return [
61+
description,
62+
CHECKLIST_START,
63+
"## Review readiness checklist",
64+
"",
65+
...CHECKLIST_ITEMS.map(item => `- [x] ${item}`),
66+
CHECKLIST_END,
67+
].join("\n");
68+
}
69+
70+
async function readGateScript(): Promise<string> {
71+
const text = await Bun.file(
72+
new URL("../.github/workflows/enforce-pr-target.yml", import.meta.url),
73+
).text();
74+
const workflow = Bun.YAML.parse(text) as Workflow;
75+
const script = workflow.jobs?.["enforce-target"]?.steps?.find(
76+
step => step.name === "Enforce PR target, ancestry, and description",
77+
)?.with?.script;
78+
if (typeof script !== "string") {
79+
throw new Error("enforce-target step has no inline script");
80+
}
81+
return script;
82+
}
83+
84+
function gateBodyFrom(
85+
result: Awaited<ReturnType<typeof runEnforcePrTarget>>,
86+
): string {
87+
const updates = callsTo(result, "issues.updateComment") as Array<{ body: string }>;
88+
const creates = callsTo(result, "issues.createComment") as Array<{ body: string }>;
89+
const body = updates.at(-1)?.body ?? creates.at(-1)?.body;
90+
if (!body?.includes(GATE_MARKER)) {
91+
throw new Error("scenario recorded no PR gate comment body");
92+
}
93+
return body;
94+
}
95+
2696
describe("workflow comment-spam hardening", () => {
2797
test("PR gate consumes CodeRabbit commit status from the trusted default branch", async () => {
2898
const text = await Bun.file(
@@ -85,6 +155,108 @@ describe("workflow comment-spam hardening", () => {
85155
expect(script).toContain("unresolvedFindingsClaim");
86156
});
87157

158+
test("an unchanged READY comment is not rewritten on a CodeRabbit status wake", async () => {
159+
const script = await readGateScript();
160+
const body = completedChecklistBody();
161+
162+
const initial = await runEnforcePrTarget(script, {
163+
pr: { base: { ref: "dev" }, draft: true, body },
164+
maintainersFile: MAINTAINERS_FIXTURE,
165+
});
166+
const transitionBody = gateBodyFrom(initial);
167+
168+
const steady = await runEnforcePrTarget(script, {
169+
pr: { base: { ref: "dev" }, draft: false, body },
170+
maintainersFile: MAINTAINERS_FIXTURE,
171+
labels: ["review-ready"],
172+
comments: [{
173+
id: 7,
174+
user: { login: "github-actions[bot]" },
175+
body: transitionBody,
176+
}],
177+
});
178+
const steadyBody = gateBodyFrom(steady);
179+
expect(steadyBody).toContain("This pull request is already Ready for Review.");
180+
expect(steadyBody).not.toContain("@coderabbitai review");
181+
182+
const statusWake = await runEnforcePrTarget(script, {
183+
pr: { base: { ref: "dev" }, draft: false, body },
184+
eventName: "status",
185+
maintainersFile: MAINTAINERS_FIXTURE,
186+
labels: ["review-ready"],
187+
comments: [
188+
{
189+
id: 7,
190+
user: { login: "github-actions[bot]" },
191+
body: steadyBody,
192+
},
193+
{
194+
id: 8,
195+
user: { login: "github-actions[bot]" },
196+
body: "<!-- pr-quality-enforcer -->\nlegacy enforcer comment",
197+
},
198+
{
199+
id: 9,
200+
user: { login: "github-actions[bot]" },
201+
body: "<!-- pr-quality-readiness -->\nlegacy readiness comment",
202+
},
203+
],
204+
});
205+
206+
expect(callsTo(statusWake, "issues.updateComment")).toEqual([]);
207+
expect(callsTo(statusWake, "issues.createComment")).toEqual([]);
208+
expect(callsTo(statusWake, "issues.addLabels")).toEqual([]);
209+
expect(callsTo(statusWake, "issues.removeLabel")).toEqual([]);
210+
expect(callsTo(statusWake, "pulls.update")).toEqual([]);
211+
expect(callsTo(statusWake, "issues.deleteComment")).toEqual([
212+
{ owner: "lidge-jun", repo: "opencodex", comment_id: 8 },
213+
{ owner: "lidge-jun", repo: "opencodex", comment_id: 9 },
214+
]);
215+
216+
const graphqlCalls = callsTo(statusWake, "graphql") as Array<{ query: string }>;
217+
expect(graphqlCalls.some(call => call.query.includes("convertPullRequestToDraft"))).toBe(false);
218+
expect(graphqlCalls.some(call => call.query.includes("markPullRequestReadyForReview"))).toBe(false);
219+
});
220+
221+
test("a repeated draft-conversion failure restores the failure body after its checkpoint write", async () => {
222+
const script = await readGateScript();
223+
const body = completedChecklistBody().replace(
224+
"- [x] My PR is ready for review.",
225+
"- [ ] My PR is ready for review.",
226+
);
227+
228+
const firstFailure = await runEnforcePrTarget(script, {
229+
pr: { base: { ref: "dev" }, draft: false, body },
230+
maintainersFile: MAINTAINERS_FIXTURE,
231+
failGraphqlOn: ["convertPullRequestToDraft"],
232+
});
233+
const previousFailureBody = gateBodyFrom(firstFailure);
234+
expect(previousFailureBody).toContain('"autoDraftedByBot":false');
235+
expect(previousFailureBody).toContain("Automatic draft conversion failed");
236+
237+
const repeatedFailure = await runEnforcePrTarget(script, {
238+
pr: { base: { ref: "dev" }, draft: false, body },
239+
maintainersFile: MAINTAINERS_FIXTURE,
240+
failGraphqlOn: ["convertPullRequestToDraft"],
241+
comments: [{
242+
id: 7,
243+
user: { login: "github-actions[bot]" },
244+
body: previousFailureBody,
245+
}],
246+
});
247+
248+
const updates = callsTo(repeatedFailure, "issues.updateComment") as Array<{
249+
body: string;
250+
comment_id: number;
251+
}>;
252+
expect(callsTo(repeatedFailure, "issues.createComment")).toEqual([]);
253+
expect(updates).toHaveLength(2);
254+
expect(updates.map(update => update.comment_id)).toEqual([7, 7]);
255+
expect(updates[0]?.body).toContain('"autoDraftedByBot":true');
256+
expect(updates[1]?.body).toContain('"autoDraftedByBot":false');
257+
expect(updates[1]?.body).toContain("Automatic draft conversion failed");
258+
});
259+
88260
test("issue-comment translation rejects PR and bot comments before runner allocation", async () => {
89261
const text = await Bun.file(
90262
new URL("../.github/workflows/enforce-issue-quality.yml", import.meta.url),

0 commit comments

Comments
 (0)