Skip to content

Commit a846084

Browse files
fix(server): Forgejo required-check globs no longer hang the event loop (pingdotgg#26183)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7e884b2 commit a846084

2 files changed

Lines changed: 74 additions & 11 deletions

File tree

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSour
1717
import * as ForgejoCli from "./ForgejoCli.ts";
1818
import * as ForgejoPullRequestProvider from "./ForgejoPullRequestProvider.ts";
1919
import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts";
20+
import { markRequiredForgejoChecks } from "./forgejoRequiredChecks.ts";
2021

2122
const processOutput = TestSourceControlHost.processOutput;
2223

@@ -140,6 +141,47 @@ it.effect.each([
140141
),
141142
);
142143

144+
it("matches many-wildcard required patterns against long check names without backtracking", () => {
145+
const checks = ["a".repeat(200), `${"a".repeat(199)}b`, "x?y\nz"].map((name) => ({
146+
name,
147+
status: "success" as const,
148+
description: null,
149+
url: null,
150+
}));
151+
const marked = markRequiredForgejoChecks(checks, {
152+
enable_status_check: true,
153+
status_check_contexts: [`${"*a".repeat(12)}b`, "x*?y?z"],
154+
});
155+
assert.deepStrictEqual(
156+
marked.map((check) => [check.name.slice(-3), check.required]),
157+
[
158+
["aaa", undefined],
159+
["aab", true],
160+
["y\nz", true],
161+
],
162+
);
163+
});
164+
165+
it("treats a required pattern's `*` as a wildcard even when the check name has one", () => {
166+
const checks = ["*build", "ci/*lint"].map((name) => ({
167+
name,
168+
status: "success" as const,
169+
description: null,
170+
url: null,
171+
}));
172+
const marked = markRequiredForgejoChecks(checks, {
173+
enable_status_check: true,
174+
status_check_contexts: ["*build", "ci/*"],
175+
});
176+
assert.deepStrictEqual(
177+
marked.map((check) => [check.name, check.required]),
178+
[
179+
["*build", true],
180+
["ci/*lint", true],
181+
],
182+
);
183+
});
184+
143185
const forgejoMergeProvider = (repository: Effect.Effect<string, ForgejoCli.ForgejoCliError>) => {
144186
const writes: Array<ForgejoCli.ForgejoApiInput> = [];
145187
const provider = ForgejoPullRequestProvider.make.pipe(

‎packages/source-control-forgejo/src/server/forgejoRequiredChecks.ts‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@ export const ForgejoBranchStatusChecks = Schema.Struct({
1111
// run of characters, `/` included, and `?` matches one character. Classes, alternation and escapes
1212
// are not handled.
1313
const UNSUPPORTED_GLOB = /[[\]{}\\]/;
14-
const contextPattern = (glob: string) =>
15-
new RegExp(
16-
`^${Array.from(glob, (char) =>
17-
char === "*" ? ".*" : char === "?" ? "." : char.replace(/[.+^$()|]/g, "\\$&"),
18-
).join("")}$`,
19-
"su",
20-
);
14+
// Linear two-pointer match: a regex built from the glob backtracks polynomially per `*`.
15+
const matchesContext = (glob: string, name: string) => {
16+
const pattern = Array.from(glob);
17+
const text = Array.from(name);
18+
let p = 0;
19+
let t = 0;
20+
let star = -1;
21+
let resume = 0;
22+
while (t < text.length) {
23+
// Wildcard first, so a literal `*` in the name never consumes the pattern's `*`.
24+
if (pattern[p] === "*") {
25+
star = p++;
26+
resume = t;
27+
} else if (p < pattern.length && (pattern[p] === "?" || pattern[p] === text[t])) {
28+
p++;
29+
t++;
30+
} else if (star >= 0) {
31+
p = star + 1;
32+
t = ++resume;
33+
} else {
34+
return false;
35+
}
36+
}
37+
while (pattern[p] === "*") p++;
38+
return p === pattern.length;
39+
};
2140

2241
export function markRequiredForgejoChecks(
2342
checks: ReadonlyArray<PullRequestCheck>,
@@ -38,13 +57,15 @@ export function markRequiredForgejoChecks(
3857
})),
3958
];
4059
}
41-
const patterns = [...new Set(contexts)].map((name) => ({ name, pattern: contextPattern(name) }));
60+
const patterns = [...new Set(contexts)];
4261
const marked = checks.map((check) =>
43-
patterns.some(({ pattern }) => pattern.test(check.name)) ? { ...check, required: true } : check,
62+
patterns.some((pattern) => matchesContext(pattern, check.name))
63+
? { ...check, required: true }
64+
: check,
4465
);
4566
// Forgejo keeps an unmatched required pattern pending, including wildcards.
46-
const missing = patterns.flatMap(({ name, pattern }) =>
47-
!name.trim() || checks.some((check) => pattern.test(check.name))
67+
const missing = patterns.flatMap((name) =>
68+
!name.trim() || checks.some((check) => matchesContext(name, check.name))
4869
? []
4970
: [{ name, status: "pending", description: null, url: null, required: true } as const],
5071
);

0 commit comments

Comments
 (0)