Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ exclude-labels:
- chore
- dependencies
- test
- refactor
include-labels:
- breaking
- documentation
- feat
- feature
- fix
- refactor
- Update Dictionaries
autolabeler:
- label: 'Update Dictionaries'
Expand All @@ -93,8 +93,13 @@ autolabeler:
- label: 'fix'
title:
- '/^fix/i'
- '/^dev:/i'
- '/^refactor/i'
# A revert is published only if the change it undoes was.
- '/^revert(?::\s*|\s+")(feat|fix|docs?)(\([^)]*\))?!?:/i'
- label: 'refactor'
title:
# Not `!:`: a breaking refactor is published.
- '/^dev(\([^)]*\))?:/i'
- '/^refactor(\([^)]*\))?:/i'
- label: 'feature'
title:
- '/^feat/i'
Expand All @@ -103,13 +108,29 @@ autolabeler:
- '/^test/i'
- label: 'breaking'
title:
- '/^fix[!]/i'
- '/^feat[!]/i'
- '/^[a-z]+(\([^)]*\))?!:/i'
- label: cspell5
branch:
- '/cspell5/i'

template: |
$CHANGES

# Applied to the whole release body, which also feeds the CHANGELOG.md files.
# Tested by scripts/check-release-drafter.mts.
# Tool attribution, each only on a line of its own:
replacers:
# A `---` line directly above a "_Generated by [Tool](...)_" line.
- search: |-
/^[ \t]*-{3,}[ \t]*\r?\n(?=[ \t]*_Generated by \[)/gim
replace: ''
# "Generated with [Tool](...)" or "_Generated by [Tool](...)_", with an optional emoji.
- search: |-
/^[ \t]*(?:\p{Extended_Pictographic}\uFE0F?[ \t]*)?_?Generated (?:with|by) \[[^\]\n]+\]\([^)\s]+\)_?[ \t]*(?:\r?\n|$)/gimu
replace: ''
# A Claude Code session link.
- search: |-
/^[ \t]*https:\/\/claude\.ai\/code\/session_[A-Za-z0-9]+[ \t]*(?:\r?\n|$)/gim
replace: ''

# cspell:ignore autolabeler commitish
31 changes: 31 additions & 0 deletions .github/workflows/check-release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Check Release Drafter Config

on:
workflow_dispatch:
pull_request:
paths:
- '.github/release-drafter.yml'
- '.github/workflows/check-release-drafter.yml'
- 'scripts/check-release-drafter.mts'

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Setup Node and Pnpm
uses: ./.github/actions/setup-node

- name: Install
run: pnpm install --frozen-lockfile --filter @internal/scripts

- name: Check labels and replacers
run: node ./scripts/check-release-drafter.mts
167 changes: 167 additions & 0 deletions scripts/check-release-drafter.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Check the autolabeler rules, the published labels, and the replacers in `.github/release-drafter.yml`.
* Usage: node ./scripts/check-release-drafter.mts
*/

import fs from 'node:fs/promises';
import path from 'node:path';

import { parse } from 'yaml';

// eslint-disable-next-line n/no-unsupported-features/node-builtins
const __dirname = import.meta.dirname;

const releaseDrafterConfigFile = path.join(__dirname, '../.github/release-drafter.yml');

interface AutoLabel {
label: string;
title?: string[];
}

interface Replacer {
search: string;
replace: string;
}

interface ReleaseDrafterConfig {
'exclude-labels': string[];
'include-labels': string[];
autolabeler: AutoLabel[];
replacers?: Replacer[];
}

/** PR title, the labels it gets from its title, and whether it is in the release notes. */
const titleCases: [title: string, labels: string[], published: boolean][] = [
['feat: Add an option', ['feature'], true],
['feat(cspell-lib)!: Drop an option', ['feature', 'breaking'], true],
['fix: Report errors', ['fix'], true],
['fix!: Change a default', ['fix', 'breaking'], true],
['fix(cspell)!: Change a default', ['fix', 'breaking'], true],
['fix(cspell): Report errors', ['fix'], true],
['refactor!: Remove an API', ['breaking'], true],
['refactor(cspell-lib)!: Remove an API', ['breaking'], true],
['dev!: Change a debug flag', ['breaking'], true],
['perf!: Change the cache default', ['breaking'], true],
['chore!: Drop an old script', ['chore', 'breaking'], false],
['revert: fix: Report errors', ['fix'], true],
['Revert "fix: Report errors"', ['fix'], true],
['Revert "feat: Add an option"', ['fix'], true],
['Revert "fix(cspell)!: Change a default"', ['fix'], true],
['Revert "docs: Clarify imports"', ['fix'], true],
['Revert "chore: Update the agent setup"', [], false],
['Revert "refactor: Clean up the worker"', [], false],
['Revert "ci: Workflow Bot -- Update ALL Dependencies (main)"', [], false],
['refactor: Clean up the worker', ['refactor'], false],
['refactor(cspell-lib): Clean up', ['refactor'], false],
['dev: Add a debug flag', ['refactor'], false],
['docs: Clarify imports', ['documentation'], true],
['chore: Update the agent setup', ['chore'], false],
['ci: Update a workflow', ['chore'], false],
['ci: Workflow Bot -- Update ALL Dependencies (main)', ['dependencies', 'chore'], false],
['fix: Workflow Bot -- Update Dictionaries (main)', ['Update Dictionaries', 'fix'], true],
['test: Add a case', ['test'], false],
['perf: Add a performance test', [], false],
];

const robot = '\u{1F916}';
const sparkles = '\u2728\uFE0F';

const sessionUrl = 'https://claude.ai/code/session_01AW4tPikxJRFmuFxebeGi3Q';

/** A Claude Code footer, as found in real PR bodies, plus lines that must survive. */
const bodyBefore = [
'## Summary',
'',
'The schema is generated with `pnpm run update-schema`.',
'Generated with [cspell-tools](https://github.com/streetsidesoftware/cspell) and checked by hand.',
'See https://claude.ai/code/session_abc123 for details.',
'',
'---',
'',
'More notes after a separator.',
'',
`${robot} Generated with [Claude Code](https://claude.com/claude-code)`,
' Generated with [Some Tool](https://example.com/tool)',
`${sparkles} generated with [Other Tool](https://example.com/other)`,
'',
sessionUrl,
'https://claude.ai/code/session_019sbuS8kLLZAusx32Hnm9eD',
'',
'---',
`_Generated by [Claude Code](${sessionUrl})_`,
].join('\n');

const bodyAfter = [
'## Summary',
'',
'The schema is generated with `pnpm run update-schema`.',
'Generated with [cspell-tools](https://github.com/streetsidesoftware/cspell) and checked by hand.',
'See https://claude.ai/code/session_abc123 for details.',
'',
'---',
'',
'More notes after a separator.',
'',
'',
'',
'',
].join('\n');

/** Same conversion as release-drafter's `stringToRegex`: `/pattern/flags` is a regular expression, anything else a literal. */
function stringToRegex(search: string): RegExp {
if (!/^\/.+\/[AJUXgimsux]*$/.test(search)) {
return new RegExp(search.replaceAll(/[|\\{}()[\]^$+*?.-]/g, '\\$&'), 'g');
}
const m = search.match(/(\/?)(.+)\1([a-z]*)/i);
if (!m) throw new Error(`Bad regular expression: ${search}`);
const flags = [...new Set(m[3])].filter((f) => 'gimsuy'.includes(f)).join('');
return new RegExp(m[2], flags);
}

function labelsForTitle(config: ReleaseDrafterConfig, title: string): string[] {
return config.autolabeler
.filter((rule) => rule.title?.some((t) => stringToRegex(t).test(title)))
.map((rule) => rule.label);
}

function isPublished(config: ReleaseDrafterConfig, labels: string[]): boolean {
return (
labels.some((l) => config['include-labels'].includes(l)) &&
!labels.some((l) => config['exclude-labels'].includes(l))
);
}

function applyReplacers(config: ReleaseDrafterConfig, body: string): string {
return (config.replacers ?? []).reduce((text, r) => text.replace(stringToRegex(r.search), r.replace), body);
}

async function run() {
const config = parse(await fs.readFile(releaseDrafterConfigFile, 'utf8')) as ReleaseDrafterConfig;
const failures: string[] = [];

for (const [title, expectedLabels, expectedPublished] of titleCases) {
const labels = labelsForTitle(config, title);
if (labels.join(',') !== expectedLabels.join(',')) {
failures.push(`"${title}": labels [${labels}], expected [${expectedLabels}]`);
}
if (isPublished(config, labels) !== expectedPublished) {
failures.push(`"${title}": published is ${!expectedPublished}, expected ${expectedPublished}`);
}
}

const body = applyReplacers(config, bodyBefore);
if (body !== bodyAfter) {
failures.push(`replacers:\n--- got ---\n${body}\n--- expected ---\n${bodyAfter}`);
}

if (failures.length) {
console.error(`${path.relative(process.cwd(), releaseDrafterConfigFile)}:\n${failures.join('\n')}`);
process.exitCode = 1;
return;
}
console.log(`${titleCases.length} titles and the replacers match the expected release notes.`);
}

await run();

// cspell:ignore autolabeler Xgimsux
Loading