An open source design system that's fully customizable and agent ready
TypeScript
ci: lint checks the comment-provenance baseline (#10250)
Cindy Zhang11940703c558
diff --git a/.github/scripts/ci-lint-scope.test.mjs b/.github/scripts/ci-lint-scope.test.mjsindex 56dc6b2daa..99ed72c005 100644--- a/.github/scripts/ci-lint-scope.test.mjs+++ b/.github/scripts/ci-lint-scope.test.mjs@@ -21,6 +21,8 @@ import path from 'node:path';import {afterAll, beforeAll, describe, expect, it} from 'vitest';import yaml from 'yaml';+import {canSkipBaselineCheck} from './provenance-baseline-scope.mjs';+import {createRepository,importCommits,@@ -196,4 +198,184 @@ describe('required lint routing', () => {full_suite: 'false',});});++ it('checks the comment-provenance baseline in full lint, and a baseline-only change runs it', () => {+ const check = steps.find(+ step => step.name === 'Check the comment-provenance baseline',+ );+ expect(check.run).toContain(+ 'node internal/eslint-plugin-astryx/no-comment-provenance.baseline.mjs --check',+ );+ expect(check.run).toContain(+ 'node .github/scripts/provenance-baseline-scope.mjs "$RUNNER_TEMP/changed-files"',+ );+ expect(check.env.NODE_OPTIONS).toBe(+ steps.find(step => step.name === 'Run ESLint').env.NODE_OPTIONS,+ );+ // The same lanes as ESLint: everything but the docs and spec fast paths.+ expect(check.if).toBe(steps.find(step => step.name === 'Run ESLint').if);+ expect(steps.indexOf(check)).toBeGreaterThan(+ steps.findIndex(step => step.uses === './.github/actions/setup'),+ );+ expect(+ classifyPullRequest({+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.json':+ '{}\n',+ }),+ ).toMatchObject({spec_only: 'false', docsite_only: 'false'});+ });++ it('skips the baseline check only for a known change that cannot make the baseline stale', () => {+ const baseline = {'packages/core/src/Button/Button.tsx': []};+ for (const file of [+ 'packages/core/src/Button/Button.tsx',+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.json',+ 'internal/eslint-plugin-astryx/no-comment-provenance.js',+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.mjs',+ 'internal/eslint-plugin-astryx/rule-baseline.mjs',+ 'eslint.config.js',+ ]) {+ expect(canSkipBaselineCheck([file], baseline), file).toBe(false);+ }+ expect(+ canSkipBaselineCheck(['packages/core/src/Badge/Badge.tsx'], baseline),+ ).toBe(true);+ // A rename lists both paths, so moving a baselined file runs the check.+ expect(+ canSkipBaselineCheck(+ [+ 'packages/core/src/Button/Button.tsx',+ 'packages/core/src/Button/BaseButton.tsx',+ ],+ baseline,+ ),+ ).toBe(false);+ // An empty list is not a known change.+ expect(canSkipBaselineCheck([], baseline)).toBe(false);+ // ESLint skips what an ignore file names, and reads the rule from the+ // plugin entry.+ for (const file of [+ '.gitignore',+ 'packages/core/.gitignore',+ 'internal/eslint-plugin-astryx/index.js',+ ]) {+ expect(canSkipBaselineCheck([file], baseline), file).toBe(false);+ }+ });++ // Runs the step's own script in a scratch repository whose merge commit+ // changes `files`, with a stub baseline script that prints a marker.+ function runBaselineStep({event = 'pull_request', files, baseline = '{}'}) {+ const check = steps.find(+ step => step.name === 'Check the comment-provenance baseline',+ );+ const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'provenance-step-'));+ scratch.push(repo);+ const write = (file, text) => {+ fs.mkdirSync(path.dirname(path.join(repo, file)), {recursive: true});+ fs.writeFileSync(path.join(repo, file), text);+ };+ write(+ '.github/scripts/provenance-baseline-scope.mjs',+ fs.readFileSync(+ path.join(root, '.github/scripts/provenance-baseline-scope.mjs'),+ 'utf8',+ ),+ );+ write(+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.json',+ baseline,+ );+ write(+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.mjs',+ "console.log('BASELINE CHECK RAN');\n",+ );+ write('docs/README.md', 'base\n');+ write('packages/core/src/Button/Button.tsx', 'base\n');+ const g = (...args) =>+ execFileSync(+ 'git',+ ['-C', repo, '-c', 'user.name=t', '-c', 'user.email=t@t', ...args],+ {+ encoding: 'utf8',+ },+ );+ g('init', '-q');+ g('add', '-A');+ g('commit', '-qm', 'base', '--no-verify');+ for (const file of files) write(file, 'changed\n');+ g('add', '-A');+ g('commit', '-qm', 'change', '--allow-empty', '--no-verify');+ const merge = g('rev-parse', 'HEAD').trim();+ const runnerTemp = fs.mkdtempSync(+ path.join(os.tmpdir(), 'provenance-tmp-'),+ );+ scratch.push(runnerTemp);+ return execFileSync('bash', ['-e', '-c', check.run], {+ cwd: repo,+ encoding: 'utf8',+ env: {+ ...process.env,+ EVENT_NAME: event,+ MERGE_SHA: merge,+ RUNNER_TEMP: runnerTemp,+ },+ });+ }++ it('runs the baseline check for a push, a baselined change, a helper failure and an empty list, and skips an unrelated change', () => {+ const baseline = JSON.stringify({+ 'packages/core/src/Button/Button.tsx': [],+ });+ const ran = options =>+ runBaselineStep(options).includes('BASELINE CHECK RAN');+ expect(ran({event: 'push', files: ['docs/README.md'], baseline})).toBe(+ true,+ );+ expect(+ ran({files: ['packages/core/src/Button/Button.tsx'], baseline}),+ ).toBe(true);+ expect(ran({files: ['.gitignore'], baseline})).toBe(true);+ // A baseline the helper cannot parse: the helper fails, the check runs.+ expect(ran({files: ['docs/README.md'], baseline: '{not json'})).toBe(true);+ // A merge commit that changes nothing: an empty list runs the check.+ expect(ran({files: [], baseline})).toBe(true);+ expect(ran({files: ['docs/README.md'], baseline})).toBe(false);+ });++ it('runs the baseline check when the helper fails or the file list is empty', () => {+ const check = steps.find(+ step => step.name === 'Check the comment-provenance baseline',+ );+ expect(check.run).toContain('git diff --name-only --no-renames');+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'provenance-scope-'));+ scratch.push(dir);+ const helper = path.join(+ root,+ '.github/scripts/provenance-baseline-scope.mjs',+ );+ const exitOf = (list, baseline) => {+ fs.writeFileSync(path.join(dir, 'changed'), list);+ fs.writeFileSync(path.join(dir, 'baseline.json'), baseline);+ return spawnSync(+ 'node',+ [+ helper,+ path.join(dir, 'changed'),+ '--baseline',+ path.join(dir, 'baseline.json'),+ ],+ {encoding: 'utf8'},+ ).status;+ };+ // Exit 0, the only answer that skips: a known list that touches nothing.+ expect(exitOf('docs/README.md\n', '{}')).toBe(0);+ expect(exitOf('', '{}')).not.toBe(0);+ expect(exitOf('docs/README.md\n', '{not json')).not.toBe(0);+ expect(+ spawnSync('node', [helper, path.join(dir, 'missing')], {+ encoding: 'utf8',+ }).status,+ ).not.toBe(0);+ });});diff --git a/.github/scripts/provenance-baseline-scope.mjs b/.github/scripts/provenance-baseline-scope.mjsnew file mode 100644index 0000000000..110434d0bf--- /dev/null+++ b/.github/scripts/provenance-baseline-scope.mjs@@ -0,0 +1,66 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file provenance-baseline-scope.mjs+ * @input A file that lists changed paths, one per line (both paths of a+ * rename), and optionally `--baseline <file>`.+ * @output Exit 0 only when the change cannot make the comment-provenance+ * baseline stale: a non-empty list that touches no file the baseline lists,+ * not the baseline, not the rule or its baseline tooling, and no+ * `.gitignore` (ESLint skips the files those name). Any other+ * answer, an error included, exits non-zero, and the check runs.+ * @position The lint job's "Check the comment-provenance baseline" step reads+ * it, so a pull request that cannot change the baseline skips the check.+ */++import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';++const ROOT = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ '../..',+);+const BASELINE =+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.json';+export const RULE_FILES = [+ BASELINE,+ 'internal/eslint-plugin-astryx/no-comment-provenance.js',+ 'internal/eslint-plugin-astryx/no-comment-provenance.baseline.mjs',+ 'internal/eslint-plugin-astryx/rule-baseline.mjs',+ 'internal/eslint-plugin-astryx/index.js',+ 'eslint.config.js',+];++/**+ * Whether the check may be skipped: only for a known, non-empty file list+ * that touches nothing the baseline depends on.+ * @param {string[]} changed+ * @param {Record<string, unknown>} baseline+ */+export function canSkipBaselineCheck(changed, baseline) {+ if (changed.length === 0) return false;+ const listed = new Set([...Object.keys(baseline), ...RULE_FILES]);+ // ESLint skips what an ignore file names, so an ignore change can make a+ // listed site disappear.+ return !changed.some(+ file => listed.has(file) || path.posix.basename(file) === '.gitignore',+ );+}++if (process.argv[1] === fileURLToPath(import.meta.url)) {+ const args = process.argv.slice(2);+ const flag = args.indexOf('--baseline');+ const baselinePath =+ flag === -1 ? path.join(ROOT, BASELINE) : path.resolve(args[flag + 1]);+ const listFile = args.find(+ (arg, i) => !arg.startsWith('--') && args[i - 1] !== '--baseline',+ );+ const changed = fs+ .readFileSync(listFile, 'utf8')+ .split('\n')+ .map(line => line.trim())+ .filter(Boolean);+ const baseline = JSON.parse(fs.readFileSync(baselinePath, 'utf8'));+ process.exitCode = canSkipBaselineCheck(changed, baseline) ? 0 : 1;+}diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex d3f877c7d8..9bde85fc99 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -245,6 +245,31 @@ jobs:if: steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true'run: node packages/cli/scripts/generate-cli-readme.mjs --check+ # The comment-provenance rule allows only the sites its baseline lists.+ # A change that removes a cited site must also drop its entry, so the+ # baseline never allows a site that no longer exists. A pull request+ # runs the check when it changes a file the baseline lists, the+ # baseline, the rule or its tooling, the ESLint config, or an ignore+ # file; every push to main runs it. The check parses+ # Core with the type-aware config, so it gets the same heap as ESLint.+ - name: Check the comment-provenance baseline+ if: steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true'+ env:+ EVENT_NAME: ${{ github.event_name }}+ MERGE_SHA: ${{ github.sha }}+ NODE_OPTIONS: '--max-old-space-size=4096'+ run: |+ set -eu+ # Skip only on the helper's explicit answer: an empty list, an+ # error or a crash runs the check. Both paths of a rename count.+ if [ "$EVENT_NAME" = pull_request ] &&+ git diff --name-only --no-renames "$MERGE_SHA^1" "$MERGE_SHA" > "$RUNNER_TEMP/changed-files" &&+ node .github/scripts/provenance-baseline-scope.mjs "$RUNNER_TEMP/changed-files"; then+ echo 'This change touches no file the comment-provenance baseline lists, the baseline, the rule, the ESLint config or an ignore file.'+ exit 0+ fi+ node internal/eslint-plugin-astryx/no-comment-provenance.baseline.mjs --check+# The pre-commit hook formats staged files with Prettier, but a commit# made without it can land unformatted source. A later local merge of# main then reformats that file inside the merging pull request, whichdiff --git a/internal/eslint-plugin-astryx/no-comment-provenance.baseline.json b/internal/eslint-plugin-astryx/no-comment-provenance.baseline.jsonindex f48889ca37..300733359c 100644--- a/internal/eslint-plugin-astryx/no-comment-provenance.baseline.json+++ b/internal/eslint-plugin-astryx/no-comment-provenance.baseline.json@@ -938,7 +938,6 @@"DR2","DR3","FR17",- "FR4","FR6","FR9","INV12",@@ -946,8 +945,7 @@"component:AppShell","component:SideNav","design:reduced-motion",- "spec quote",- "spec:AST-105"+ "spec quote"],"packages/core/src/Skeleton/Skeleton.tsx": ["FR4", "spec:AST-108"],"packages/core/src/Slider/Slider.tsx": [@@ -1884,7 +1882,6 @@"spec:AST-105"],"packages/core/src/motion/animatedLayout.ts": [- "DEC-11","DR1","DR2","DR2",@@ -1900,7 +1897,6 @@"FR4","FR4","FR4",- "FR4","FR5","FR6","FR7",@@ -1939,8 +1935,6 @@"spec quote","spec quote","spec quote",- "spec:AST-094",- "spec:AST-105","spec:AST-105","spec:AST-105","spec:AST-105",