An open source design system that's fully customizable and agent ready
TypeScript
ci: remove the scope step's and the PR report's temporary folders when they finish (#9774)
Cindy Zhang7ba6d800927b
diff --git a/.github/actions/test-scope/action.yml b/.github/actions/test-scope/action.ymlindex d07b17728d..e38cc692bf 100644--- a/.github/actions/test-scope/action.yml+++ b/.github/actions/test-scope/action.yml@@ -185,120 +185,126 @@ runs:let files;let root;+ // The base's policy files are copied to a temporary folder that+ // lives only while this step decides the scope.try {- files = await github.paginate(github.rest.pulls.listFiles, {- ...context.repo,- pull_number: pr.number,- per_page: 100,- });- // GitHub lists at most 3000 files for a pull request.- 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-'));- // 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 {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), blob.text);+ try {+ files = await github.paginate(github.rest.pulls.listFiles, {+ ...context.repo,+ pull_number: pr.number,+ per_page: 100,+ });+ // GitHub lists at most 3000 files for a pull request.+ 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-'));+ // 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 {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), blob.text);+ }+ } catch (error) {+ core.warning(`Scope could not be decided (${error.message}); everything is in scope.`);+ report({...everything, decided: 'false'});+ return;}- } catch (error) {- core.warning(`Scope could not be decided (${error.message}); everything is in scope.`);- report({...everything, decided: 'false'});- return;- }- const STATUS = {added: 'A', removed: 'D', modified: 'M', changed: 'M', copied: 'C', unchanged: 'M'};- const nameStatus = files- .map(f => f.status === 'renamed'- ? `R100\t${f.previous_filename}\t${f.filename}`- : `${STATUS[f.status] ?? 'M'}\t${f.filename}`)- .join('\n');- const outputFile = path.join(root, 'classification');- fs.writeFileSync(outputFile, '');- try {- execFileSync('node', [path.join(root, '.github/scripts/change-scope.cjs'), '--github-output'], {- input: `${nameStatus}\n`,- env: {...process.env, GITHUB_OUTPUT: outputFile},- stdio: ['pipe', 'inherit', 'inherit'],- });- } catch (error) {- core.setFailed(`The trusted classifier rejected this change: ${error.message}`);- return;- }- const classified = Object.fromEntries(- fs.readFileSync(outputFile, 'utf8').split('\n').filter(Boolean).map(line => line.split('=')),- );-- let touches = 'true';- if (process.env.SCOPE_PATHS) {- const pattern = new RegExp(process.env.SCOPE_PATHS);- touches = String(files.some(f => pattern.test(f.filename) || (f.previous_filename != null && pattern.test(f.previous_filename))));- }- // A base whose classifier predates lab_only reports none: false.- const specialized = ['spec_only', 'docsite_only', 'tooling_only', 'lab_only'].some(key => classified[key] === 'true');- let applies = String(!specialized && touches === 'true');- // Whether the head has the file the work needs, decided whether or- // not `touches` holds; a diff that touches the file keeps it.- let headFilePresent = 'true';- const headFile = process.env.SCOPE_HEAD_FILE;- if (!specialized && headFile && !files.some(f => f.filename === headFile || f.previous_filename === headFile)) {+ const STATUS = {added: 'A', removed: 'D', modified: 'M', changed: 'M', copied: 'C', unchanged: 'M'};+ const nameStatus = files+ .map(f => f.status === 'renamed'+ ? `R100\t${f.previous_filename}\t${f.filename}`+ : `${STATUS[f.status] ?? 'M'}\t${f.filename}`)+ .join('\n');+ const outputFile = path.join(root, 'classification');+ fs.writeFileSync(outputFile, '');try {- await github.rest.repos.getContent({...context.repo, path: headFile, ref: pr.head.sha});+ execFileSync('node', [path.join(root, '.github/scripts/change-scope.cjs'), '--github-output'], {+ input: `${nameStatus}\n`,+ env: {...process.env, GITHUB_OUTPUT: outputFile},+ stdio: ['pipe', 'inherit', 'inherit'],+ });} catch (error) {- if (error.status === 404) {- core.notice(`This pull request's head has no ${headFile}, so this job skips what needs it; merging main brings it in.`);- headFilePresent = 'false';- }+ core.setFailed(`The trusted classifier rejected this change: ${error.message}`);+ return;}- }- if (headFilePresent === 'false') applies = 'false';- let componentScope = {};- if (audit) {- // A Lab-only change keeps its component audits: the default- // touched check skips it, but a maintainer's label still runs its- // suite on Lab's components (spec:AST-030 FR12, FR26).- if (specialized && classified.lab_only !== 'true') {- componentScope = auditNone;- } else {- const auditFile = path.join(root, 'component-audit');- fs.writeFileSync(auditFile, '');+ const classified = Object.fromEntries(+ fs.readFileSync(outputFile, 'utf8').split('\n').filter(Boolean).map(line => line.split('=')),+ );++ let touches = 'true';+ if (process.env.SCOPE_PATHS) {+ const pattern = new RegExp(process.env.SCOPE_PATHS);+ touches = String(files.some(f => pattern.test(f.filename) || (f.previous_filename != null && pattern.test(f.previous_filename))));+ }+ // A base whose classifier predates lab_only reports none: false.+ const specialized = ['spec_only', 'docsite_only', 'tooling_only', 'lab_only'].some(key => classified[key] === 'true');+ let applies = String(!specialized && touches === 'true');+ // Whether the head has the file the work needs, decided whether or+ // not `touches` holds; a diff that touches the file keeps it.+ let headFilePresent = 'true';+ const headFile = process.env.SCOPE_HEAD_FILE;+ if (!specialized && headFile && !files.some(f => f.filename === headFile || f.previous_filename === headFile)) {try {- execFileSync('node', [- path.join(root, '.github/scripts/component-audit-scope.cjs'),- '--registry',- path.join(root, 'scripts/component-packages.cjs'),- '--github-output',- ], {input: `${nameStatus}\n`, env: {...process.env, GITHUB_OUTPUT: auditFile}, stdio: ['pipe', 'inherit', 'inherit']});- componentScope = Object.fromEntries(- fs.readFileSync(auditFile, 'utf8').split('\n').filter(Boolean).map(line => line.split('=')),- );+ await github.rest.repos.getContent({...context.repo, path: headFile, ref: pr.head.sha});} catch (error) {- core.warning(`Component audit scope could not be decided (${error.message}); every audit runs in full.`);- componentScope = auditAll;+ if (error.status === 404) {+ core.notice(`This pull request's head has no ${headFile}, so this job skips what needs it; merging main brings it in.`);+ headFilePresent = 'false';+ }+ }+ }+ if (headFilePresent === 'false') applies = 'false';+ let componentScope = {};+ if (audit) {+ // A Lab-only change keeps its component audits: the default+ // touched check skips it, but a maintainer's label still runs its+ // suite on Lab's components (spec:AST-030 FR12, FR26).+ if (specialized && classified.lab_only !== 'true') {+ componentScope = auditNone;+ } else {+ const auditFile = path.join(root, 'component-audit');+ fs.writeFileSync(auditFile, '');+ try {+ execFileSync('node', [+ path.join(root, '.github/scripts/component-audit-scope.cjs'),+ '--registry',+ path.join(root, 'scripts/component-packages.cjs'),+ '--github-output',+ ], {input: `${nameStatus}\n`, env: {...process.env, GITHUB_OUTPUT: auditFile}, stdio: ['pipe', 'inherit', 'inherit']});+ componentScope = Object.fromEntries(+ fs.readFileSync(auditFile, 'utf8').split('\n').filter(Boolean).map(line => line.split('=')),+ );+ } catch (error) {+ core.warning(`Component audit scope could not be decided (${error.message}); every audit runs in full.`);+ componentScope = auditAll;+ }}+ // A file, not an output value: a large pull request's list would+ // not fit an environment variable.+ const changedFilesPath = path.join(process.env.RUNNER_TEMP || os.tmpdir(), 'test-scope-changed-files.txt');+ const changed = [...new Set(files.flatMap(f => [f.filename, f.previous_filename].filter(Boolean)))];+ fs.writeFileSync(changedFilesPath, `${changed.join('\n')}\n`);+ componentScope.changed_files_path = changedFilesPath;}- // A file, not an output value: a large pull request's list would- // not fit an environment variable.- const changedFilesPath = path.join(process.env.RUNNER_TEMP || os.tmpdir(), 'test-scope-changed-files.txt');- const changed = [...new Set(files.flatMap(f => [f.filename, f.previous_filename].filter(Boolean)))];- fs.writeFileSync(changedFilesPath, `${changed.join('\n')}\n`);- componentScope.changed_files_path = changedFilesPath;+ report({...everything, ...classified, ...componentScope, release_bump: 'false', touches, applies, head_file_present: headFilePresent});+ } finally {+ if (root) fs.rmSync(root, {recursive: true, force: true});}- report({...everything, ...classified, ...componentScope, release_bump: 'false', touches, applies, head_file_present: headFilePresent});diff --git a/.github/scripts/lib/pr-preview.mjs b/.github/scripts/lib/pr-preview.mjsindex dd6acdd3c1..a7492b46c0 100644--- a/.github/scripts/lib/pr-preview.mjs+++ b/.github/scripts/lib/pr-preview.mjs@@ -877,85 +877,100 @@ export async function reconcilePrComment({}): null;let body;- if (analysisReady) {- // The comment job's own reads, handed to the generator as files.- const inputs = fs.mkdtempSync(path.join(os.tmpdir(), 'pr-report-'));- const jobs = await sourceRunJobs({- github,- owner,- repo,- runId: identity.sourceRunId,- });- const impact = await gatherCiImpact({- github,- owner,- repo,- identity,- jobs,- historyPath: ciHistoryPath,- });- const impactPath = path.join(inputs, 'ci-impact.json');- if (impact) fs.writeFileSync(impactPath, JSON.stringify(impact));- // Frames only from this PR's assets branch for this exact head.- const framesPath = trustedFrames({frames: visualFrames, identity, inputs});- const previewPath = path.join(inputs, 'preview.json');- const jobsPath = path.join(inputs, 'jobs.json');- if (preview) fs.writeFileSync(previewPath, JSON.stringify(preview));- if (jobs) fs.writeFileSync(jobsPath, JSON.stringify(jobs));- const args = [- generator,- '--analysis',- analysisPath,- ...(trustedHeadReport({- reportPath: baseStatsPath,- label: 'base stats',- identity,- core,- })- ? ['--base-stats', baseStatsPath]- : []),- ...(trustedHeadReport({- reportPath: packageSizePath,- label: 'package size report',+ // The generator's input files live only while the report renders.+ let inputs = null;+ try {+ if (analysisReady) {+ // The comment job's own reads, handed to the generator as files.+ inputs = fs.mkdtempSync(path.join(os.tmpdir(), 'pr-report-'));+ const jobs = await sourceRunJobs({+ github,+ owner,+ repo,+ runId: identity.sourceRunId,+ });+ const impact = await gatherCiImpact({+ github,+ owner,+ repo,identity,- core,- })- ? ['--package-size', packageSizePath]- : []),- // Part of the analysis artifact, which pr-meta.json ties to this head.- ...(fs.existsSync(apiChangesPath)- ? ['--api-changes', apiChangesPath]- : []),- // No report means the audit did not run (it runs on main and release- // checks, or with the ci:run-a11y label); the comment says so.- ...(fs.existsSync(a11yPath) ? ['--a11y', a11yPath] : []),- ...(fs.existsSync(visualPath) ? ['--visual', visualPath] : []),- ...(framesPath ? ['--visual-frames', framesPath] : []),- ...(trustedHeadReport({- reportPath: touchedPath,- label: 'touched-components report',+ jobs,+ historyPath: ciHistoryPath,+ });+ const impactPath = path.join(inputs, 'ci-impact.json');+ if (impact) fs.writeFileSync(impactPath, JSON.stringify(impact));+ // Frames only from this PR's assets branch for this exact head.+ const framesPath = trustedFrames({+ frames: visualFrames,identity,- core,- })- ? ['--touched', touchedPath]- : []),- ...(preview ? ['--preview', previewPath] : []),- ...(jobs ? ['--jobs', jobsPath] : []),- ...(impact ? ['--ci-impact', impactPath] : []),- '--source-conclusion',- identity.sourceConclusion,- '--run-url',- runUrl,- '--pr-number',- String(identity.prNumber),- ];- try {- body = execute(process.execPath, args, {encoding: 'utf8'}).replace(- PR_ANALYSIS_MARKER,- `${PR_ANALYSIS_MARKER}\n${headMarker(identity.headSha)}`,- );- } catch (error) {- core.warning(`Could not render current analysis: ${error.message}`);+ inputs,+ });+ const previewPath = path.join(inputs, 'preview.json');+ const jobsPath = path.join(inputs, 'jobs.json');+ if (preview) fs.writeFileSync(previewPath, JSON.stringify(preview));+ if (jobs) fs.writeFileSync(jobsPath, JSON.stringify(jobs));+ const args = [+ generator,+ '--analysis',+ analysisPath,+ ...(trustedHeadReport({+ reportPath: baseStatsPath,+ label: 'base stats',+ identity,+ core,+ })+ ? ['--base-stats', baseStatsPath]+ : []),+ ...(trustedHeadReport({+ reportPath: packageSizePath,+ label: 'package size report',+ identity,+ core,+ })+ ? ['--package-size', packageSizePath]+ : []),+ // Part of the analysis artifact, which pr-meta.json ties to this head.+ ...(fs.existsSync(apiChangesPath)+ ? ['--api-changes', apiChangesPath]+ : []),+ // No report means the audit did not run (it runs on main and release+ // checks, or with the ci:run-a11y label); the comment says so.+ ...(fs.existsSync(a11yPath) ? ['--a11y', a11yPath] : []),+ ...(fs.existsSync(visualPath) ? ['--visual', visualPath] : []),+ ...(framesPath ? ['--visual-frames', framesPath] : []),+ ...(trustedHeadReport({+ reportPath: touchedPath,+ label: 'touched-components report',+ identity,+ core,+ })+ ? ['--touched', touchedPath]+ : []),+ ...(preview ? ['--preview', previewPath] : []),+ ...(jobs ? ['--jobs', jobsPath] : []),+ ...(impact ? ['--ci-impact', impactPath] : []),+ '--source-conclusion',+ identity.sourceConclusion,+ '--run-url',+ runUrl,+ '--pr-number',+ String(identity.prNumber),+ ];+ try {+ body = execute(process.execPath, args, {encoding: 'utf8'}).replace(+ PR_ANALYSIS_MARKER,+ `${PR_ANALYSIS_MARKER}\n${headMarker(identity.headSha)}`,+ );+ } catch (error) {+ core.warning(`Could not render current analysis: ${error.message}`);+ body = safeCurrentBody({+ identity,+ runUrl,+ message: fallbackMessage,+ preview,+ });+ }+ } else {body = safeCurrentBody({identity,runUrl,@@ -963,13 +978,8 @@ export async function reconcilePrComment({preview,});}- } else {- body = safeCurrentBody({- identity,- runUrl,- message: fallbackMessage,- preview,- });+ } finally {+ if (inputs != null) fs.rmSync(inputs, {recursive: true, force: true});}// Rendering takes a moment. Confirm the reported attempt's identity again,diff --git a/.github/scripts/lib/pr-preview.test.mjs b/.github/scripts/lib/pr-preview.test.mjsindex b675fb98be..5748d54b0c 100644--- a/.github/scripts/lib/pr-preview.test.mjs+++ b/.github/scripts/lib/pr-preview.test.mjs@@ -1544,18 +1544,19 @@ describe('CI impact in the report', () => {it('reaches the comment for a pull request that changes CI, with the merge base as before', async () => {let seen;+ let impactText;const {github} = await reconcile({extendGithub: ciChange,execute: (_node, args) => {seen = args;const file = args[args.indexOf('--ci-impact') + 1];- return `## PR Analysis Report\n${fs.readFileSync(file, 'utf8')}`;+ impactText = fs.readFileSync(file, 'utf8');+ return `## PR Analysis Report\n${impactText}`;},});expect(seen).toContain('--ci-impact');- const impact = JSON.parse(- fs.readFileSync(seen[seen.indexOf('--ci-impact') + 1], 'utf8'),- );+ // The input folder is removed once the report renders.+ const impact = JSON.parse(impactText);expect(impact.flags).toContain('extra moves onto a non-standard runner');expect(github.rest.repos.getContent).toHaveBeenCalledWith(expect.objectContaining({ref: 'c'.repeat(40)}),@@ -1563,7 +1564,7 @@ describe('CI impact in the report', () => {});it('names a composite-action-only change as not measured', async () => {- let seen;+ let impactText;await reconcile({extendGithub: github => {ciChange(github);@@ -1572,18 +1573,49 @@ describe('CI impact in the report', () => {}));},execute: (_node, args) => {- seen = args;+ impactText = fs.readFileSync(+ args[args.indexOf('--ci-impact') + 1],+ 'utf8',+ );return '## PR Analysis Report\n';},});- const impact = JSON.parse(- fs.readFileSync(seen[seen.indexOf('--ci-impact') + 1], 'utf8'),- );+ // The input folder is removed once the report renders.+ const impact = JSON.parse(impactText);expect(impact.unmeasuredFiles).toEqual(['.github/actions/setup/action.yml',]);});+ it("removes the generator's input folder after the report renders", async () => {+ let folder;+ await reconcile({+ extendGithub: ciChange,+ execute: (_node, args) => {+ folder = path.dirname(args[args.indexOf('--ci-impact') + 1]);+ expect(fs.existsSync(folder)).toBe(true);+ return '## PR Analysis Report\n';+ },+ });+ expect(folder).toMatch(/pr-report-/);+ expect(fs.existsSync(folder)).toBe(false);+ });++ it("removes the generator's input folder when the generator fails", async () => {+ let folder;+ const {result} = await reconcile({+ extendGithub: ciChange,+ createIfMissing: true,+ execute: (_node, args) => {+ folder = path.dirname(args[args.indexOf('--ci-impact') + 1]);+ throw new Error('malformed analysis');+ },+ });+ expect(result.body).toContain('trusted analysis is unavailable');+ expect(folder).toMatch(/pr-report-/);+ expect(fs.existsSync(folder)).toBe(false);+ });+it('passes no impact for a pull request that does not change CI', async () => {let seen;await reconcile({diff --git a/.github/scripts/test-scope-action.test.mjs b/.github/scripts/test-scope-action.test.mjsindex ee90e37556..61aaf887fc 100644--- a/.github/scripts/test-scope-action.test.mjs+++ b/.github/scripts/test-scope-action.test.mjs@@ -11,6 +11,7 @@*/import fs from 'node:fs';+import os from 'node:os';import {createRequire} from 'node:module';import path from 'node:path';@@ -597,3 +598,74 @@ describe('per-job scope step', () => {});});});++describe('the temporary policy folder', () => {+ // os.tmpdir() reads TMPDIR on each call, so each case gets its own folder.+ async function inFreshTemp(run) {+ const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'scope-tmp-test-'));+ const runnerTemp = fs.mkdtempSync(path.join(os.tmpdir(), 'scope-runner-'));+ const saved = {+ TMPDIR: process.env.TMPDIR,+ RUNNER_TEMP: process.env.RUNNER_TEMP,+ };+ process.env.TMPDIR = temp;+ process.env.RUNNER_TEMP = runnerTemp;+ try {+ const result = await run();+ return {result, left: fs.readdirSync(temp)};+ } finally {+ for (const [key, value] of Object.entries(saved)) {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+ }+ fs.rmSync(temp, {recursive: true, force: true});+ fs.rmSync(runnerTemp, {recursive: true, force: true});+ }+ }++ it('is removed after a decided scope', async () => {+ const {result, left} = await inFreshTemp(() =>+ decide({files: changed('packages/core/src/Button/Button.tsx')}),+ );+ expect(result.outputs.decided).toBe('true');+ expect(left).toEqual([]);+ });++ it('is removed when a base policy file cannot be read', async () => {+ const {result, left} = await inFreshTemp(() =>+ decide({+ files: changed('packages/core/src/Button/Button.tsx'),+ truncated: ['scripts/component-packages.cjs'],+ }),+ );+ expect(result.outputs.decided).toBe('false');+ expect(left).toEqual([]);+ });++ it('is removed when the trusted classifier rejects the change', async () => {+ const {result, left} = await inFreshTemp(() =>+ decide({+ files: changed('packages/core/src/Button/Button.tsx'),+ baseOverrides: {+ '.github/scripts/change-scope.cjs': 'process.exit(1);\n',+ },+ }),+ );+ expect(result.messages.failed).toHaveLength(1);+ expect(left).toEqual([]);+ });++ it('is removed after a component-audit scope, while the changed-file list stays for later steps', async () => {+ const {result, left} = await inFreshTemp(() =>+ decide({+ files: changed('packages/core/src/Button/Button.tsx'),+ inputs: {'component-audit': 'true'},+ }).then(r => ({+ ...r,+ listed: fs.existsSync(r.outputs.changed_files_path),+ })),+ );+ expect(result.listed).toBe(true);+ expect(left).toEqual([]);+ });+});