Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

ci(test-scope): read the trusted classifier in one GraphQL query, not a REST call per file (#9180)

Cindy Zhang0eb4eff9036f
diff --git a/.github/actions/test-scope/action.yml b/.github/actions/test-scope/action.yml
index b583b0b315..c3146cf567 100644
--- a/.github/actions/test-scope/action.yml
+++ b/.github/actions/test-scope/action.yml
@@ -10,7 +10,9 @@
# base, so a long-lived branch does not inherit main's changes (#8812) and
# no history is fetched or deepened (#8826).
# - The classifier (change-scope.cjs and its two helpers) is read from the PR's
-# base commit, so a pull request cannot loosen its own classification.
+# base commit, so a pull request cannot loosen its own classification. It is
+# read in one GraphQL query, not a REST call per file: every job runs this
+# step, and every workflow shares the repository's REST rate limit.
# - Anything this step cannot decide (not a pull request, a missing trusted
# classifier, an API error, an empty file list or one GitHub truncates)
# puts everything in scope (spec:AST-030 FR3, FR4). A classifier that rejects the change (a spec-only PR with a
@@ -180,15 +182,28 @@ runs:
if (files.length >= 3000) throw new Error('GitHub truncated the file list at 3000');
if (files.length === 0) throw new Error('GitHub listed no changed files');
root = fs.mkdtempSync(path.join(os.tmpdir(), 'test-scope-'));
- for (const file of [
+ // The trusted policy comes from the base commit in one GraphQL
+ // query: every job in every run reads it, and one REST call per
+ // file counted against the repository's shared REST rate limit.
+ const policy = [
'.github/scripts/change-scope.cjs',
'.github/scripts/knowledge-paths.cjs',
'scripts/component-packages.cjs',
...(audit ? ['.github/scripts/component-audit-scope.cjs'] : []),
- ]) {
- const {data} = await github.rest.repos.getContent({...context.repo, path: file, ref: pr.base.sha});
+ ];
+ const {repository} = await github.graphql(
+ `query($owner: String!, $repo: String!, ${policy.map((_, i) => `$f${i}: String!`).join(', ')}) {
+ repository(owner: $owner, name: $repo) {
+ ${policy.map((_, i) => `f${i}: object(expression: $f${i}) { ... on Blob { text isTruncated } }`).join('\n')}
+ }
+ }`,
+ {...context.repo, ...Object.fromEntries(policy.map((file, i) => [`f${i}`, `${pr.base.sha}:${file}`]))},
+ );
+ for (const [i, file] of policy.entries()) {
+ const blob = repository?.[`f${i}`];
+ if (typeof blob?.text !== 'string' || blob.isTruncated) throw new Error(`the base has no readable ${file}`);
fs.mkdirSync(path.join(root, path.dirname(file)), {recursive: true});
- fs.writeFileSync(path.join(root, file), Buffer.from(data.content, 'base64'));
+ fs.writeFileSync(path.join(root, file), blob.text);
}
} catch (error) {
core.warning(`Scope could not be decided (${error.message}); everything is in scope.`);
diff --git a/.github/scripts/ci-tooling-routing.test.mjs b/.github/scripts/ci-tooling-routing.test.mjs
index 9267e4f894..38120f7f7a 100644
--- a/.github/scripts/ci-tooling-routing.test.mjs
+++ b/.github/scripts/ci-tooling-routing.test.mjs
@@ -37,8 +37,8 @@ function step(job, name) {
describe('Node-tooling CI routing', () => {
it('publishes the tooling output from the trusted-base classifier', () => {
// ci.yml jobs each run the scope action, which reads the classifier and
- // both dependencies from the PR's base commit.
- expect(scopeAction).toContain('ref: pr.base.sha');
+ // both dependencies from the PR's base commit, in one GraphQL query.
+ expect(scopeAction).toContain('`${pr.base.sha}:${file}`');
for (const file of [
'.github/scripts/change-scope.cjs',
'.github/scripts/knowledge-paths.cjs',
diff --git a/.github/scripts/spec-owner-workflow.test.mjs b/.github/scripts/spec-owner-workflow.test.mjs
index 9fab31bd64..b2607e5ca0 100644
--- a/.github/scripts/spec-owner-workflow.test.mjs
+++ b/.github/scripts/spec-owner-workflow.test.mjs
@@ -41,7 +41,8 @@ describe('spec-only workflow contract', () => {
]) {
expect(action).toContain(`'${file}'`);
}
- expect(action).toContain('ref: pr.base.sha');
+ // Read at the base commit, in one GraphQL query.
+ expect(action).toContain('`${pr.base.sha}:${file}`');
for (const workflow of [
'.github/workflows/lint.yml',
'.github/workflows/cli-smoke-test.yml',
diff --git a/.github/scripts/test-scope-action.test.mjs b/.github/scripts/test-scope-action.test.mjs
index d87010c59f..4dd0ea4ad6 100644
--- a/.github/scripts/test-scope-action.test.mjs
+++ b/.github/scripts/test-scope-action.test.mjs
@@ -68,6 +68,8 @@ async function decide({
headError,
missingBase = [],
baseOverrides = {},
+ truncated = [],
+ policyError,
} = {}) {
const outputs = {};
const messages = {info: [], warning: [], notice: [], failed: []};
@@ -82,7 +84,29 @@ async function decide({
setFailed: message => messages.failed.push(message),
};
const notFound = () => Object.assign(new Error('Not Found'), {status: 404});
+ const policyText = file => {
+ if (missingBase.includes(file)) return null;
+ if (file in baseOverrides) return baseOverrides[file];
+ return fs.readFileSync(path.join(root, file), 'utf8');
+ };
+ const graphqlCalls = [];
const github = {
+ // The trusted policy: one aliased Blob per `<sha>:<path>` expression.
+ graphql: async (query, variables) => {
+ graphqlCalls.push({query, variables});
+ if (policyError) throw policyError;
+ const repository = {};
+ for (const [key, expression] of Object.entries(variables)) {
+ if (!/^f\d+$/.test(key)) continue;
+ const [ref, file] = [expression.slice(0, 40), expression.slice(41)];
+ contentCalls.push({file, ref});
+ const known = CLASSIFIER_FILES.has(file) || file === AUDIT_FILE;
+ const text = ref === BASE_SHA && known ? policyText(file) : null;
+ repository[key] =
+ text == null ? null : {text, isTruncated: truncated.includes(file)};
+ }
+ return {repository};
+ },
paginate: async () => {
if (files instanceof Error) throw files;
return files;
@@ -91,27 +115,7 @@ async function decide({
pulls: {listFiles: () => {}},
repos: {
getContent: async ({path: file, ref}) => {
- contentCalls.push({file, ref});
- if (
- ref === BASE_SHA &&
- (CLASSIFIER_FILES.has(file) || file === AUDIT_FILE)
- ) {
- if (missingBase.includes(file)) throw notFound();
- if (file in baseOverrides) {
- return {
- data: {
- content: Buffer.from(baseOverrides[file]).toString('base64'),
- },
- };
- }
- return {
- data: {
- content: fs
- .readFileSync(path.join(root, file))
- .toString('base64'),
- },
- };
- }
+ contentCalls.push({file, ref, rest: true});
if (ref === HEAD_SHA && headError) {
throw Object.assign(new Error('Server Error'), {status: headError});
}
@@ -145,7 +149,7 @@ async function decide({
else delete process.env[key];
}
}
- return {outputs, messages, contentCalls};
+ return {outputs, messages, contentCalls, graphqlCalls};
}
const changed = (...names) =>
@@ -221,6 +225,46 @@ describe('per-job scope step', () => {
).toEqual([...CLASSIFIER_FILES].map(file => ({file, ref: BASE_SHA})));
});
+ it('reads the trusted policy in one GraphQL query and no REST call', async () => {
+ const {graphqlCalls, contentCalls} = await decide({
+ files: changed('packages/core/src/Button/Button.tsx'),
+ inputs: {'component-audit': 'true'},
+ });
+ expect(graphqlCalls).toHaveLength(1);
+ expect(graphqlCalls[0].variables).toEqual({
+ owner: 'o',
+ repo: 'r',
+ f0: `${BASE_SHA}:.github/scripts/change-scope.cjs`,
+ f1: `${BASE_SHA}:.github/scripts/knowledge-paths.cjs`,
+ f2: `${BASE_SHA}:scripts/component-packages.cjs`,
+ f3: `${BASE_SHA}:${AUDIT_FILE}`,
+ });
+ expect(contentCalls.filter(call => call.rest)).toEqual([]);
+ });
+
+ it.each([
+ ['the query fails', {policyError: new Error('Something went wrong')}],
+ [
+ 'a policy file is truncated',
+ {truncated: ['.github/scripts/change-scope.cjs']},
+ ],
+ ])('puts everything in scope when %s', async (_, options) => {
+ const {outputs, messages} = await decide({
+ files: changed('docs/specs/AST-999-example/spec.md'),
+ ...options,
+ });
+ expect(outputs).toMatchObject({
+ spec_only: 'false',
+ full_suite: 'true',
+ decided: 'false',
+ });
+ expect(messages.warning).toEqual([
+ expect.stringMatching(
+ /^Scope could not be decided \(.+\); everything is in scope\.$/,
+ ),
+ ]);
+ });
+
it('does not apply when no changed path is in the declared scope', async () => {
const {outputs} = await decide({
files: changed('packages/cli/assets/codemods/registry.json'),