Skip to content

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

TypeScript

ci: keep pr-analysis for three days; pin api-changes.json's shape for merge-guard (#10647)

Cindy Zhang0f28099dedd9
diff --git a/.github/scripts/generate-pr-comment.test.mjs b/.github/scripts/generate-pr-comment.test.mjs
index abe2c235dc..e95b2e2d0c 100644
--- a/.github/scripts/generate-pr-comment.test.mjs
+++ b/.github/scripts/generate-pr-comment.test.mjs
@@ -15,8 +15,13 @@ import os from 'node:os';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {describe, it, expect} from 'vitest';
+import yaml from 'yaml';
+
+import {computeChanges} from '../../scripts/api-report/changes.mjs';
+import {changesFile} from '../../scripts/api-report/cli.mjs';
const SCRIPTS_DIR = path.dirname(fileURLToPath(import.meta.url));
+const repoRoot = path.resolve(SCRIPTS_DIR, '../..');
const SCRIPT = path.join(SCRIPTS_DIR, 'generate-pr-comment.js');
/** Run the comment generator with the given analysis.json; return stdout. */
@@ -223,6 +228,78 @@ describe('comment length budget', () => {
}
});
+ // Two readers depend on pr-analysis/api-changes.json: this PR report and
+ // merge-guard, which reads a run's file up to a day later. The shape below
+ // is what both read; a change to it breaks merge-guard as well.
+ it("keeps api-changes.json's shape for its readers, the PR report and merge-guard", () => {
+ const base = fs.readFileSync(
+ path.join(repoRoot, 'packages/build/api-report.md'),
+ 'utf8',
+ );
+ const head = base.replace(/^function postcss: .*\n/m, '');
+ const changes = computeChanges([
+ {pkg: '@astryxdesign/build', base, head, committed: base},
+ {pkg: '@astryxdesign/new', base: null, head, committed: null},
+ ]);
+ const file = changesFile(changes, {haveSurface: true, baseRecord: 'main'});
+ expect(Object.keys(file).sort()).toEqual([
+ 'baseRecord',
+ 'packages',
+ 'version',
+ ]);
+ expect(file.version).toBe(2);
+ expect(file.baseRecord).toBe('main');
+ expect(
+ changesFile(changes, {haveSurface: true, baseRecord: 'build'}).baseRecord,
+ ).toBe('build');
+ expect(changesFile(changes, {haveSurface: false}).baseRecord).toBeNull();
+ for (const pkg of file.packages) {
+ expect(Object.keys(pkg).sort()).toEqual(
+ [
+ 'baseSource',
+ 'baseline',
+ 'counts',
+ 'missing',
+ 'pkg',
+ 'rows',
+ 'stale',
+ 'staleOwnRows',
+ 'staleRows',
+ ].sort(),
+ );
+ expect(typeof pkg.pkg).toBe('string');
+ for (const key of ['baseline', 'stale', 'missing'])
+ expect(typeof pkg[key], key).toBe('boolean');
+ expect(Object.keys(pkg.counts).sort()).toEqual([
+ 'added',
+ 'changed',
+ 'removed',
+ ]);
+ for (const row of pkg.rows) {
+ expect(['added', 'changed', 'removed']).toContain(row.change);
+ for (const key of ['entry', 'section', 'item', 'line', 'usage'])
+ expect(typeof row[key], key).toBe('string');
+ }
+ }
+ const [build, added] = file.packages;
+ expect(build.counts).toEqual({added: 0, changed: 0, removed: 1});
+ expect(build.rows[0]).toMatchObject({
+ change: 'removed',
+ item: 'function postcss',
+ });
+ expect(added).toMatchObject({baseline: true, missing: true, rows: []});
+ // The artifact that carries it outlives a day, so merge-guard can read it.
+ const ci = yaml.parse(
+ fs.readFileSync(path.join(repoRoot, '.github/workflows/ci.yml'), 'utf8'),
+ );
+ const upload = ci.jobs['build-storybook'].steps.find(
+ step => step.name === 'Upload analysis artifact',
+ );
+ expect(upload.with.name).toBe('pr-analysis');
+ expect(upload.with.path.split('\n')).toContain('api-changes.json');
+ expect(upload.with['retention-days']).toBe(3);
+ });
+
it("keeps a report with 150 long API rows under GitHub's comment limit", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-budget-'));
try {
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b81c68c421..efeb5ea329 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1897,7 +1897,9 @@ jobs:
package-size.json
api-changes.json
api-changes.md
- retention-days: 1
+ # merge-guard reads this run's api-changes.json up to a day after the
+ # run, so the artifact outlives that.
+ retention-days: 3
# Typechecks run beside build-storybook instead of before its Storybook
# build, so the browser checks that wait on the Storybook artifact start
diff --git a/scripts/api-report/cli.mjs b/scripts/api-report/cli.mjs
index 392d2fff47..8c5d62dbcd 100644
--- a/scripts/api-report/cli.mjs
+++ b/scripts/api-report/cli.mjs
@@ -50,6 +50,27 @@ export const SURFACE_FILE = 'surface.json';
* and `staleOwnRows`; a reader of version 2 accepts a missing `staleOwnRows`.
*/
export const CHANGES_VERSION = 2;
+
+/**
+ * The object `api-changes.json` holds. Its readers are the pull request report
+ * and merge-guard.
+ * @param {ReturnType<typeof computeChanges>} changes
+ * @param {{haveSurface: boolean, baseRecord?: string}} base whether the merge
+ * base's surface was found, and where it came from
+ */
+export function changesFile(changes, {haveSurface, baseRecord}) {
+ return {
+ version: CHANGES_VERSION,
+ // Where the merge base's surface came from: Main's record of that commit,
+ // a build of it in this run, or neither (committed reports).
+ baseRecord: haveSurface
+ ? baseRecord === 'build'
+ ? 'build'
+ : 'main'
+ : null,
+ packages: changes,
+ };
+}
const REPO_ROOT = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../..',
@@ -304,17 +325,7 @@ async function main() {
fs.writeFileSync(
path.join(args.out, 'api-changes.json'),
JSON.stringify(
- {
- version: CHANGES_VERSION,
- // Where the merge base's surface came from: Main's record of that
- // commit, a build of it in this run, or neither (committed reports).
- baseRecord: haveSurface
- ? args.baseRecord === 'build'
- ? 'build'
- : 'main'
- : null,
- packages: changes,
- },
+ changesFile(changes, {haveSurface, baseRecord: args.baseRecord}),
null,
2,
) + '\n',