Skip to content

Commit 35b1673

Browse files
isaacsclaude
andcommitted
ci: Require a test in fix PRs that change SDK source
A `fix` PR that changes files under `packages/*/src/` now fails the "Fix needs a test" check unless it also adds or changes a unit, integration or E2E test. `fix(deps)` PRs are exempt. The `Accept Missing Test` label skips the check for fixes that cannot have a test, such as type-only changes. The workflow uses `pull_request_target` and checks out only the script from the base branch, as the team review gate does. Of the last 20 merged fix PRs, the check would have flagged one, a type-only fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 99465e1 commit 35b1673

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: 'PR: Fix needs a test'
2+
3+
on:
4+
# pull_request_target so a PR can't change the check. It only checks out the base branch, never the PR's code.
5+
pull_request_target:
6+
types:
7+
- opened
8+
- reopened
9+
- synchronize
10+
- edited
11+
- labeled
12+
- unlabeled
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
16+
cancel-in-progress: true
17+
18+
permissions:
19+
contents: read
20+
pull-requests: read
21+
22+
jobs:
23+
fix-pr-test-check:
24+
name: Fix needs a test
25+
runs-on: ubuntu-24.04
26+
steps:
27+
- name: Checkout base branch
28+
uses: actions/checkout@v7
29+
with:
30+
sparse-checkout: |
31+
scripts/fix-pr-test-check.mjs
32+
sparse-checkout-cone-mode: false
33+
34+
- name: Check that a fix PR adds or changes a test
35+
uses: actions/github-script@v9
36+
with:
37+
script: |
38+
const { checkFixPrHasTest } = await import('${{ github.workspace }}/scripts/fix-pr-test-check.mjs');
39+
await checkFixPrHasTest({ github, context, core });

‎scripts/fix-pr-test-check.mjs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const OVERRIDE_LABEL = 'Accept Missing Test';
2+
3+
// `fix(deps)` PRs that only bump a dependency have nothing to test here.
4+
const FIX_TITLE = /^fix(\((?!deps\))[^)]*\))?!?:/;
5+
const SOURCE_FILE = /^packages\/[^/]+\/src\//;
6+
const TEST_FILE = /(^dev-packages\/|\/(test|tests|__tests__)\/|\.(test|spec)\.[cm]?[jt]sx?$)/;
7+
8+
/**
9+
* Returns why a PR needs a test, or undefined when it does not: a `fix`
10+
* PR that changes SDK source but adds or changes no test.
11+
*/
12+
export function missingTestReason({ title, labels, files }) {
13+
if (!FIX_TITLE.test(title) || labels.includes(OVERRIDE_LABEL)) {
14+
return undefined;
15+
}
16+
const changed = files.filter(file => file.status !== 'removed').map(file => file.filename);
17+
if (!changed.some(name => SOURCE_FILE.test(name)) || changed.some(name => TEST_FILE.test(name))) {
18+
return undefined;
19+
}
20+
return (
21+
'This `fix` PR changes SDK source but no test. Add a unit, integration or E2E test that fails ' +
22+
`without the fix, or add the "${OVERRIDE_LABEL}" label and explain in the PR why no test is possible.`
23+
);
24+
}
25+
26+
export async function checkFixPrHasTest({ github, context, core }) {
27+
const { owner, repo } = context.repo;
28+
const pull_number = context.payload.pull_request.number;
29+
const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number });
30+
const files = await github.paginate(github.rest.pulls.listFiles, { owner, repo, pull_number, per_page: 100 });
31+
const reason = missingTestReason({ title: pr.title, labels: pr.labels.map(label => label.name), files });
32+
if (reason) {
33+
core.setFailed(reason);
34+
}
35+
}

‎scripts/fix-pr-test-check.test.mjs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { checkFixPrHasTest, missingTestReason, OVERRIDE_LABEL } from './fix-pr-test-check.mjs';
4+
import { setup } from './github-test-utils.mjs';
5+
6+
const src = { filename: 'packages/core/src/client.ts', status: 'modified' };
7+
const unitTest = { filename: 'packages/core/test/lib/client.test.ts', status: 'modified' };
8+
const check = (title, files, labels = []) => missingTestReason({ title, labels, files });
9+
10+
describe('missingTestReason', () => {
11+
it('flags a fix PR that changes source without a test', () => {
12+
expect(check('fix(core): Do a thing', [src])).toContain(OVERRIDE_LABEL);
13+
expect(check('fix: Do a thing', [src])).toBeDefined();
14+
expect(check('fix(core)!: Do a thing', [src])).toBeDefined();
15+
});
16+
17+
it.each([
18+
['a unit test', unitTest],
19+
['an integration test', { filename: 'dev-packages/node-integration-tests/suites/x/test.ts', status: 'added' }],
20+
['a spec file', { filename: 'packages/core/src/x.spec.ts', status: 'added' }],
21+
])('accepts a fix PR with %s', (_name, testFile) => {
22+
expect(check('fix(core): Do a thing', [src, testFile])).toBeUndefined();
23+
});
24+
25+
it('does not count a removed test', () => {
26+
expect(check('fix(core): Do a thing', [src, { ...unitTest, status: 'removed' }])).toBeDefined();
27+
});
28+
29+
it.each([
30+
['a feat PR', 'feat(core): Add a thing', [src]],
31+
['a fix(deps) PR', 'fix(deps): Bump @sentry/cli', [src]],
32+
['a fix PR without source changes', 'fix(ci): Repair workflow', [{ filename: '.github/x.yml', status: 'modified' }]],
33+
])('ignores %s', (_name, title, files) => {
34+
expect(check(title, files)).toBeUndefined();
35+
});
36+
37+
it('ignores a PR with the override label', () => {
38+
expect(check('fix(core): Do a thing', [src], [OVERRIDE_LABEL])).toBeUndefined();
39+
});
40+
});
41+
42+
describe('checkFixPrHasTest', () => {
43+
it('fails the check for a fix PR without a test', async () => {
44+
const { github, core, run } = setup(checkFixPrHasTest, { title: 'fix(core): Do a thing' });
45+
github.paginate.mockResolvedValue([src]);
46+
await run();
47+
expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining(OVERRIDE_LABEL));
48+
});
49+
50+
it('passes a fix PR with a test', async () => {
51+
const { github, core, run } = setup(checkFixPrHasTest, { title: 'fix(core): Do a thing' });
52+
github.paginate.mockResolvedValue([src, unitTest]);
53+
await run();
54+
expect(core.setFailed).not.toHaveBeenCalled();
55+
});
56+
});

0 commit comments

Comments
 (0)