Skip to content

Commit 5b80089

Browse files
doc(contributing): improve guidance and examples (#427)
Co-authored-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com>
1 parent 49ccc17 commit 5b80089

7 files changed

Lines changed: 218 additions & 7 deletions

File tree

.github/dependabot.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ updates:
1111

1212
- package-ecosystem: github-actions
1313
commit-message:
14-
prefix: ci
14+
prefix: dep
1515
directory: /
1616
labels:
17+
- "dependencies"
1718
- "gh actions"
1819
schedule:
1920
interval: monthly
2021

2122
- package-ecosystem: npm # See documentation for possible values
2223
commit-message:
23-
prefix: deps
24+
prefix: dep
2425
directories:
2526
- / # Location of package manifests
2627
- /recipies/*
2728
labels:
29+
- "dependencies"
2830
- "npm"
2931
schedule:
3032
interval: weekly

.github/scripts/pr-prefixes.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* The subset of conventional commit prefixes used in this project.
3+
*/
4+
export const SUPPORTED_PREFIXES = [
5+
'doc',
6+
'dep',
7+
'fix',
8+
'feat',
9+
'setup',
10+
'test',
11+
] as const;
12+
13+
export const SCOPE_RGX = new RegExp(`^(${SUPPORTED_PREFIXES.join('|')})\\([\\w\\-\\d]*\\)\\: `);

.github/scripts/pr-title.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { equal, match } from 'node:assert/strict';
2+
import { execPath } from 'node:process';
3+
import { describe, it } from 'node:test';
4+
import { fileURLToPath } from 'node:url';
5+
6+
import { spawnPromisified } from '../../utils/src/spawn-promisified.ts';
7+
8+
import { SUPPORTED_PREFIXES } from './pr-prefixes.ts';
9+
10+
describe('Pull Request checks', { concurrency: true }, async () => {
11+
const encoding = 'utf8';
12+
const prTestPath = fileURLToPath(import.meta.resolve('./pr-title.ts'));
13+
14+
for (const prefix of SUPPORTED_PREFIXES) {
15+
describe(prefix, () => {
16+
it('should pass when valid', async () => {
17+
const { code, stderr } = await spawnPromisified(
18+
execPath,
19+
[prTestPath],
20+
{
21+
encoding,
22+
env: { PR_TITLE: `${prefix}(DEP0000): update …` },
23+
},
24+
);
25+
26+
equal(stderr, '');
27+
equal(code, 0);
28+
});
29+
30+
it('should fail when missing scope', async () => {
31+
const { code, stderr } = await spawnPromisified(
32+
execPath,
33+
[prTestPath],
34+
{
35+
encoding,
36+
env: { PR_TITLE: `${prefix}: update …` },
37+
},
38+
);
39+
40+
match(stderr, /AssertionError/);
41+
match(stderr, new RegExp(prefix));
42+
match(stderr, /pull request title/i);
43+
equal(code, 1);
44+
});
45+
46+
it('should fail scope is misformatted', async () => {
47+
const { code, stderr } = await spawnPromisified(
48+
execPath,
49+
[prTestPath],
50+
{
51+
encoding,
52+
env: { PR_TITLE: `${prefix}(DEP0000) update …` },
53+
},
54+
);
55+
56+
match(stderr, /AssertionError/);
57+
match(stderr, new RegExp(prefix));
58+
match(stderr, /pull request title/i);
59+
equal(code, 1);
60+
});
61+
});
62+
}
63+
64+
describe('unsupported prefix', () => {
65+
it('should fail', async () => {
66+
const prefix = 'foo';
67+
const { code, stderr } = await spawnPromisified(execPath, [prTestPath], {
68+
encoding,
69+
env: { PR_TITLE: `${prefix}(DEP0000): update …` },
70+
});
71+
72+
match(stderr, /AssertionError/);
73+
match(stderr, new RegExp(prefix));
74+
match(stderr, /pull request title/i);
75+
for (const p of SUPPORTED_PREFIXES) match(stderr, new RegExp(p));
76+
equal(code, 1);
77+
});
78+
})
79+
});

.github/scripts/pr-title.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import assert from 'node:assert/strict';
2+
import { env } from 'node:process';
3+
4+
import { SCOPE_RGX, SUPPORTED_PREFIXES } from './pr-prefixes.ts';
5+
6+
declare global {
7+
namespace NodeJS {
8+
interface ProcessEnv {
9+
PR_TITLE: string;
10+
}
11+
}
12+
}
13+
14+
const { PR_TITLE } = env;
15+
16+
const INVALID_PR_TITLE_ERR = '\
17+
The pull request title did not match the required format; see CONTRIBUTING.md for details.\
18+
';
19+
20+
assert.match(PR_TITLE, SCOPE_RGX, INVALID_PR_TITLE_ERR);
21+
const firstParen = PR_TITLE.indexOf('(');
22+
const prefix = PR_TITLE.slice(0, firstParen);
23+
24+
assert.ok(
25+
SUPPORTED_PREFIXES.includes(prefix),
26+
`The pull request title prefix '${prefix}' is not in the supported list: '${SUPPORTED_PREFIXES.join("', '")}'`,
27+
);

.github/workflows/pr.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
2+
3+
name: PR Checks
4+
5+
on:
6+
pull_request:
7+
branches:
8+
- main
9+
types:
10+
- edited
11+
- opened
12+
- reopened
13+
- synchronize
14+
15+
permissions:
16+
pull-requests: read
17+
18+
jobs:
19+
check-title:
20+
name: PR title (Conventional Commit)
21+
runs-on: ubuntu-slim
22+
23+
if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }}
24+
25+
steps:
26+
# FIXME https://github.com/step-security/harden-runner/issues/627
27+
# - name: Harden Runner
28+
# uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
29+
# with:
30+
# egress-policy: audit
31+
32+
- name: Checkout
33+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
34+
35+
- name: Check prefix
36+
env:
37+
PR_TITLE: ${{ github.event.pull_request.title }}
38+
run: node .github/scripts/pr-title.ts
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
2+
3+
# For more information see: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow
4+
5+
name: Workflow Quality Assurance
6+
7+
on:
8+
push:
9+
branches: ["*"]
10+
paths:
11+
- ".github/workflows/*.yml"
12+
- ".github/scripts/*"
13+
pull_request:
14+
branches: ["*"]
15+
paths:
16+
- ".github/workflows/*.yml"
17+
- ".github/scripts/*"
18+
types:
19+
- opened
20+
- ready_for_review
21+
- reopened
22+
- synchronize
23+
24+
permissions:
25+
contents: read
26+
27+
jobs:
28+
29+
run-workflow-script-tests:
30+
name: test scripts
31+
runs-on: ubuntu-slim
32+
33+
if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }}
34+
35+
steps:
36+
# FIXME https://github.com/step-security/harden-runner/issues/627
37+
# - name: Harden the runner (Audit all outbound calls)
38+
# uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
39+
# with:
40+
# egress-policy: audit
41+
42+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
43+
with:
44+
persist-credentials: false
45+
show-progress: false
46+
47+
- run: node --test ./github/scripts/*.test.ts

CONTRIBUTING.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,18 @@ Format:
191191
```
192192

193193
- **`type`**: The type of change (e.g., `feat`, `fix`, `docs`, `chore`, etc.)
194-
- **`scope`**: A short, lowercase description of the section of the codebase affected (e.g., `tmpDir-to-tmpdir`, `esm-migration`)
195-
- **`description`**: A concise summary of the change
194+
- **`scope`**: A short, description of the section of the codebase affected;
195+
- if introducing a migration handling
196+
- a deprecation, the deprecation code (e.g. `DEP0000`)
197+
- a non-deprecation, such as facilitating adoption of a node API from a 3rd party, `adopt`
198+
- if adjusting an existing migration, the migration's name (e.g. `tmpDir-to-tmpdir`)
199+
- **`description`**: A concise summary of the change, citing relevant APIs (e.g. `jest-to-node-test-runner`)
196200

197201
Examples:
198-
- `feat(tmpDir-to-tmpdir): add new node.js 18 migration codemod`
199-
- `fix(esm-migration): correct type checking in ESM migration`
200-
- `docs(codemod-usage): improve usage examples`
202+
- ``feat(DEP0022): migrate `tmpDir` to `tmpdir` ``
203+
- ``fix(`tmpDir-to-tmpdir`): correct type checking in …``
204+
- ``docs(`tmpDir-to-tmpdir`): correct usage example``
205+
- `docs(CONTRIBUTING): improve good test examples`
201206

202207
## Pull Request Process
203208

0 commit comments

Comments
 (0)