Skip to content

Commit fa51d42

Browse files
euxaristbeardthelion
authored andcommitted
fix(ci): stop bounded scan from aborting triage, use blob API, tighten matching
Addresses jatmn's second round of review on pr-triage's needs-tests check: hitting the per-PR fetch cap now logs and stops scanning instead of throwing (which was skipping label reconciliation entirely), file content comes from the Git blob API instead of the Contents API to avoid the encoding: none gap for 1-100MB files, the added-line check now requires a non-whitespace attribute character (not just added whitespace) on the added line, stripped literals leave a placeholder so they can't glue adjacent tokens into a false match, and the namespaced-test regex now accepts a leading :: for absolute-path attributes.
1 parent 24952b1 commit fa51d42

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

‎.github/workflows/pr-triage.yml‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ jobs:
7575
// #[test]. Anchor to a leading #[ (horizontal whitespace only, so the attribute
7676
// itself must be on the added line) so ::test in ordinary code (indexing,
7777
// comments, strings) doesn't count, require cfg(test) to close before accepting
78-
// it as a test gate, and allow the whitespace Rust permits around :: and cfg(.
78+
// it as a test gate, allow the whitespace Rust permits around :: and cfg(, and
79+
// allow an optional leading :: for absolute-path attributes (::tokio::test).
7980
const testAttrRe =
80-
/#[ \s]*\[[ \s]*(?:cfg[ \s]*\([ \s]*test[ \s]*\)|(?:[\w-]+(?:[ \s]*::[ \s]*[\w-]+)*[ \s]*::[ \s]*)?test\b[ \s]*[\]\()])/g;
81+
/#[ \s]*\[[ \s]*(?:cfg[ \s]*\([ \s]*test[ \s]*\)|(?:[ \s]*::[ \s]*)?(?:[\w-]+(?:[ \s]*::[ \s]*[\w-]+)*[ \s]*::[ \s]*)?test\b[ \s]*[\]\()])/g;
8182
8283
// Strip comments and string contents from a file of Rust, carrying lexical
8384
// state (open block comment / raw string / normal string) and tracking
84-
// original line mapping.
85+
// original line mapping. Each skipped comment/string/char-literal leaves a single
86+
// non-whitespace placeholder behind so tokens on either side of it can't glue
87+
// together into a false attribute match (e.g. a macro arg list containing a
88+
// stray "#" and a string literal must not read as "#[test]" once stripped).
8589
function stripRustFile(fileContent) {
8690
let stripped = "";
8791
const lineMap = [];
@@ -125,22 +129,30 @@ jobs:
125129
}
126130
if (content.slice(i, i + 2) === "/*") {
127131
state.blockCommentDepth = 1;
132+
stripped += "\0";
133+
lineMap.push(lineNo);
128134
i += 2;
129135
continue;
130136
}
131137
const charMatch = content.slice(i).match(/^'(?:[^'\\]|\\.)'/);
132138
if (charMatch) {
139+
stripped += "\0";
140+
lineMap.push(lineNo);
133141
i += charMatch[0].length;
134142
continue;
135143
}
136144
const rawMatch = content.slice(i).match(/^r(#*)"/);
137145
if (rawMatch) {
138146
state.rawStringDelim = '"' + rawMatch[1];
147+
stripped += "\0";
148+
lineMap.push(lineNo);
139149
i += rawMatch[0].length;
140150
continue;
141151
}
142152
if (content[i] === '"') {
143153
state.inNormalString = true;
154+
stripped += "\0";
155+
lineMap.push(lineNo);
144156
i += 1;
145157
continue;
146158
}
@@ -196,16 +208,28 @@ jobs:
196208
);
197209
if (!patchHasPotentialTest) continue;
198210
211+
// Bound the API work, but don't abort the whole job when a large PR exceeds it:
212+
// that would skip label reconciliation and the guidance comment for every
213+
// managed label, not just needs-tests. Stop scanning further candidates and
214+
// fall through with whatever inline-test evidence we've already found.
199215
if (contentFetchCount >= 15) {
200-
throw new Error("PR is too large: exceeded maximum number of file content fetches (15) for inline test analysis.");
216+
core.warning(
217+
"More than 15 candidate Rust files touch tests/cfg; stopping inline-test analysis early."
218+
);
219+
break;
201220
}
202221
contentFetchCount++;
203222
223+
// Use the Git Data API (blob sha from the diff) rather than the Contents API:
224+
// the Contents API returns encoding "none" (no content) for files between 1-100MB,
225+
// which the Git Data API supports directly.
204226
let fileContent;
205227
try {
206-
const res = await github.rest.repos.getContent({
207-
owner, repo, path: f.filename, ref: pr.head.sha,
208-
});
228+
const res = await github.rest.git.getBlob({ owner, repo, file_sha: f.sha });
229+
if (res.data.encoding !== "base64") {
230+
core.warning(`Unsupported content encoding for ${f.filename}; skipping inline-test analysis for this file.`);
231+
continue;
232+
}
209233
fileContent = Buffer.from(res.data.content, "base64").toString("utf8");
210234
} catch (e) {
211235
if (e.status === 404) {
@@ -224,7 +248,10 @@ jobs:
224248
const start = match.index;
225249
const end = testAttrRe.lastIndex;
226250
for (let k = start; k < end; k++) {
227-
if (added.has(lineMap[k])) {
251+
// Require an actual attribute token (not just whitespace the match
252+
// spans, e.g. a blank line inserted inside an otherwise unchanged
253+
// multiline attribute) to be on an added line.
254+
if (added.has(lineMap[k]) && !/\s/.test(stripped[k])) {
228255
addsInlineTest = true;
229256
break;
230257
}

0 commit comments

Comments
 (0)