diff --git a/docs/src/content/docs/changelogs.md b/docs/src/content/docs/changelogs.md index 0702f729d..3f314138b 100644 --- a/docs/src/content/docs/changelogs.md +++ b/docs/src/content/docs/changelogs.md @@ -347,6 +347,7 @@ that touch the selected workspace. See | `changelog.policy` | Mode: `none`, `simple`, or `auto`. Default: `none` | | `changelog.scopeGrouping` | Enable scope-based grouping. Default: `true` | | `changelog.includePaths` | Extra workspace directories to include in automatic changelogs. [Release workspaces](../workspaces/#workspace-aware-changelogs) only. Default: `[]` | +| `changelog.includeLabels` | PR labels whose changes are included in automatic changelogs wherever their files are. [Release workspaces](../workspaces/#workspace-aware-changelogs) only. Default: `[]` | | `changelog.unownedPaths` | `include`, `exclude`, or `own` files outside every workspace. [Release workspaces](../workspaces/#workspace-aware-changelogs) only. Default: `include` | | `changelog.aiSummary` | Opt-in release, category, and scope [AI summaries](../ai-summaries/). Default: disabled. | diff --git a/docs/src/content/docs/workspaces.md b/docs/src/content/docs/workspaces.md index b05deb972..e897b0078 100644 --- a/docs/src/content/docs/workspaces.md +++ b/docs/src/content/docs/workspaces.md @@ -219,9 +219,27 @@ workspaces: - apps/cli-docs ``` -Most workspaces need no `includePaths` setting. Craft rejects `includePaths` and -`unownedPaths` in configurations without `workspaces`, because there is no -workspace ownership to adjust. +When a release depends on changes that no path describes, such as a package +generated from an API spec that any backend change can alter, label those PRs +and list the labels in `includeLabels`. A commit whose PR has one of them is in +the changelog wherever its files are. Workspace placeholders expand here, so a +glob key can give each package its own label: + +```yaml +workspaces: + packages/*: + changelog: + policy: auto + includeLabels: + - 'changelog: {{packageName}}' +``` + +With `includeLabels` set, Craft looks up the PRs of every commit in the release +range, not only the workspace's own, so it can read their labels. + +Most workspaces need neither setting. Craft rejects `includePaths`, +`includeLabels`, and `unownedPaths` in configurations without `workspaces`, +because there is no workspace ownership to adjust. Files outside every workspace, such as root build files or an application that lives at the repository root, count as shared by default. Use diff --git a/src/__tests__/config.test.ts b/src/__tests__/config.test.ts index 704543a4f..8a4d363be 100644 --- a/src/__tests__/config.test.ts +++ b/src/__tests__/config.test.ts @@ -397,6 +397,7 @@ describe('validateConfiguration', () => { test.each([ ['includePaths', ['apps/cli-docs']], + ['includeLabels', ['changelog: cli']], ['unownedPaths', 'exclude'], ])('rejects changelog.%s without release workspaces', (field, value) => { expect(() => diff --git a/src/config.ts b/src/config.ts index 337003955..7ef6bf0d7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1141,6 +1141,8 @@ export interface NormalizedChangelogConfig { scopeGrouping: boolean; /** Additional workspace paths to include in automatic changelogs */ includePaths: string[]; + /** PR labels that include a change in automatic changelogs, wherever it is */ + includeLabels: string[]; /** How automatic changelogs treat files outside every workspace */ unownedPaths: UnownedPathsPolicy; /** Optional summaries; omitted unless configured explicitly. */ @@ -1218,6 +1220,7 @@ export function getChangelogConfig(): NormalizedChangelogConfig { let policy = ChangelogPolicy.None; let scopeGrouping = true; let includePaths: string[] = []; + let includeLabels: string[] = []; let unownedPaths: UnownedPathsPolicy = 'include'; // Handle legacy changelogPolicy (deprecated) @@ -1247,6 +1250,9 @@ export function getChangelogConfig(): NormalizedChangelogConfig { if (config.changelog.includePaths !== undefined) { includePaths = config.changelog.includePaths; } + if (config.changelog.includeLabels !== undefined) { + includeLabels = config.changelog.includeLabels; + } if (config.changelog.unownedPaths !== undefined) { unownedPaths = config.changelog.unownedPaths; } @@ -1258,6 +1264,7 @@ export function getChangelogConfig(): NormalizedChangelogConfig { policy, scopeGrouping, includePaths, + includeLabels, unownedPaths, aiSummary: typeof config.changelog === 'object' diff --git a/src/schemas/project_config.ts b/src/schemas/project_config.ts index 22f0f9114..8c65c0fc4 100644 --- a/src/schemas/project_config.ts +++ b/src/schemas/project_config.ts @@ -446,6 +446,12 @@ export const ChangelogConfigSchema = union([ ), ), ), + /** + * PR labels that put a change in this workspace's changelog even when it + * touches no file the workspace owns, such as a backend change to a spec + * that a package is generated from. + */ + includeLabels: optional(array(pipe(string(), minLength(1)))), /** * How a workspace changelog treats files outside every workspace: * `include` keeps them as shared (default), `exclude` ignores them, and @@ -722,7 +728,9 @@ const hasWorkspaces = (config: CraftProjectConfigObject) => // Path ownership only exists between release workspaces, so these settings // would otherwise be silently ignored. -const workspaceOnlyChangelogField = (field: 'includePaths' | 'unownedPaths') => +const workspaceOnlyChangelogField = ( + field: 'includePaths' | 'includeLabels' | 'unownedPaths', +) => forward( check( config => @@ -748,6 +756,7 @@ export const CraftProjectConfigSchema = pipe( ['github', 'projectPath'], ), workspaceOnlyChangelogField('includePaths'), + workspaceOnlyChangelogField('includeLabels'), workspaceOnlyChangelogField('unownedPaths'), ); diff --git a/src/utils/__tests__/changelog-generate.test.ts b/src/utils/__tests__/changelog-generate.test.ts index d609534d7..1f11266b1 100644 --- a/src/utils/__tests__/changelog-generate.test.ts +++ b/src/utils/__tests__/changelog-generate.test.ts @@ -99,6 +99,7 @@ describe('generateChangesetFromGit', () => { title: commit.title, body: commit.body, pr: commit.pr?.local || null, + outsideWorkspace: commit.outsideWorkspace, })), ); @@ -218,6 +219,7 @@ describe('generateChangesetFromGit', () => { policy: ChangelogPolicy.Auto, scopeGrouping: true, includePaths: [], + includeLabels: [], unownedPaths: 'include' as const, aiSummary: { llm: { @@ -1675,6 +1677,7 @@ changelog: policy: ChangelogPolicy.Auto, scopeGrouping: false, includePaths: [], + includeLabels: [], unownedPaths: 'include', aiSummary: false, }); @@ -2369,6 +2372,58 @@ See incident report: https://example.com/incident/123`, }); }); + describe('included labels', () => { + it('keeps commits outside the workspace only when their PR has an included label', async () => { + vi.mocked(config.getChangelogConfig).mockReturnValue({ + filePath: 'CHANGELOG.md', + policy: ChangelogPolicy.Auto, + scopeGrouping: true, + includePaths: [], + includeLabels: ['changelog: @sentry/api'], + unownedPaths: 'include', + }); + const pr = (number: string, labels: string[] = []) => ({ + local: number, + remote: { number, author: { login: 'alice' }, labels }, + }); + setup( + [ + { + hash: 'abc123', + title: 'feat(api): Add a client option (#1)', + body: '', + pr: pr('1'), + }, + { + hash: 'def456', + title: 'feat(issues): Add an endpoint field (#2)', + body: '', + outsideWorkspace: true, + pr: pr('2', ['changelog: @sentry/api']), + }, + { + hash: 'ghi789', + title: 'fix(ui): Fix a button (#3)', + body: '', + outsideWorkspace: true, + pr: pr('3', ['changelog: @sentry/other']), + }, + ], + null, + ); + + const { changelog } = await generateChangesetFromGit( + dummyGit, + '1.0.0', + 10, + ); + + expect(changelog).toContain('Add a client option'); + expect(changelog).toContain('Add an endpoint field'); + expect(changelog).not.toContain('Fix a button'); + }); + }); + describe('GitHub lookup failures', () => { const commits: TestCommit[] = [ { @@ -2985,9 +3040,24 @@ describe('generateChangelogWithHighlight', () => { typeof getWorkspaceChangeFilter >; + const labelConfig = (includeLabels: string[]) => + vi.mocked(config.getChangelogConfig).mockReturnValue({ + filePath: 'CHANGELOG.md', + policy: ChangelogPolicy.Auto, + scopeGrouping: true, + includePaths: [], + includeLabels, + unownedPaths: 'include', + }); + it('leaves out a PR that changes no workspace files', async () => { + labelConfig(['changelog: @sentry/api']); setup({ - currentPR: { number: 5, title: 'feat(server): Add endpoint' }, + currentPR: { + number: 5, + title: 'feat(server): Add endpoint', + labels: ['changelog: @sentry/other'], + }, existingCommits: [], }); const belongsToWorkspace = vi.fn(() => false); @@ -3007,6 +3077,27 @@ describe('generateChangelogWithHighlight', () => { expect(dummyGit.fetch).not.toHaveBeenCalled(); }); + it('keeps a PR outside the workspace that has an included label', async () => { + labelConfig(['changelog: @sentry/api']); + setup({ + currentPR: { + number: 5, + title: 'feat(server): Add endpoint', + labels: ['changelog: @sentry/api'], + }, + existingCommits: [], + }); + mockFilter.mockResolvedValue(vi.fn(() => false)); + mockRestClient.pulls.listFiles.mockResolvedValueOnce({ + data: [{ filename: 'server/src/main.rs' }], + }); + + const result = await generateChangelogWithHighlight(dummyGit, '1.0.0', 5); + + expect(result.prOutsideWorkspace).toBeUndefined(); + expect(result.changelog).toContain('Add endpoint'); + }); + it('checks every page of changed files, including renamed paths', async () => { setup({ currentPR: { number: 5, title: 'feat(py): Add client option' }, diff --git a/src/utils/__tests__/fixtures/changelog.ts b/src/utils/__tests__/fixtures/changelog.ts index c8f9b1ba7..74022771b 100644 --- a/src/utils/__tests__/fixtures/changelog.ts +++ b/src/utils/__tests__/fixtures/changelog.ts @@ -59,6 +59,8 @@ export interface TestCommit { hash: string; title: string; body: string; + /** Marks a commit whose paths are outside the selected workspace */ + outsideWorkspace?: boolean; pr?: { local?: string; remote?: { diff --git a/src/utils/__tests__/git.test.ts b/src/utils/__tests__/git.test.ts index a25c403e5..82ad3ccc3 100644 --- a/src/utils/__tests__/git.test.ts +++ b/src/utils/__tests__/git.test.ts @@ -454,6 +454,24 @@ describe('getChangesSince', () => { 'cli change', ]); + // Included labels are read from PRs later, so commits outside the + // workspace are only marked here. + writeFileSync( + join(dir, '.craft.yml'), + "minVersion: 2.29.0\nworkspaces:\n packages/cli:\n changelog:\n policy: auto\n includeLabels:\n - 'changelog: cli'\n packages/mcp: {}\n", + ); + setActiveWorkspace('packages/cli'); + expect( + (await getChangesSince(client, 'cli@0.1.0')) + .filter(change => change.outsideWorkspace) + .map(({ title }) => title), + ).toEqual(['other docs change', 'cli docs change', 'mcp change']); + writeFileSync( + join(dir, '.craft.yml'), + 'minVersion: 2.29.0\nworkspaces:\n packages/cli:\n changelog:\n policy: auto\n includePaths:\n - apps/cli-docs\n packages/mcp: {}\n', + ); + setActiveWorkspace('packages/cli'); + git('mv', 'packages/cli/cli.ts', 'packages/mcp/moved.ts'); git( '-c', diff --git a/src/utils/changelog.ts b/src/utils/changelog.ts index f1b99c535..cafb94e37 100644 --- a/src/utils/changelog.ts +++ b/src/utils/changelog.ts @@ -1816,7 +1816,8 @@ export async function generateChangelogWithHighlight( const belongsToWorkspace = await getWorkspaceChangeFilter(); if ( belongsToWorkspace && - !belongsToWorkspace(await fetchPRFiles(currentPRNumber)) + !belongsToWorkspace(await fetchPRFiles(currentPRNumber)) && + !hasIncludedLabel(prInfo.labels, getChangelogConfig().includeLabels) ) { return { changelog: '', @@ -1918,6 +1919,11 @@ export async function generateChangelogWithHighlight( }; } +/** Whether a PR has one of the workspace's `changelog.includeLabels` */ +function hasIncludedLabel(labels: string[], includeLabels: string[]): boolean { + return labels.some(label => includeLabels.includes(label)); +} + /** * Fetches raw commit/PR info from git history and GitHub. * This is the first step - just gathering data, no categorization. @@ -1952,6 +1958,15 @@ async function fetchRawCommitInfo( for (const gitCommit of gitCommits) { const githubCommit = githubCommits[gitCommit.hash]; + if ( + gitCommit.outsideWorkspace && + !hasIncludedLabel( + githubCommit?.labels ?? [], + getChangelogConfig().includeLabels, + ) + ) { + continue; + } const pr = githubCommit?.pr ?? gitCommit.pr ?? undefined; // Deduplicate by PR number, but keep all commits without PR association. diff --git a/src/utils/git.ts b/src/utils/git.ts index fa144ad39..94993ca32 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -32,6 +32,11 @@ export interface GitChange { title: string; body: string; pr: string | null; + /** + * Set when the commit's paths are outside the selected workspace. Such a + * commit stays only if its PR has one of `changelog.includeLabels`. + */ + outsideWorkspace?: boolean; } // This regex relies on the default GitHub behavior where it appends the PR @@ -276,12 +281,18 @@ export async function getChangesSince( pathsByHash.set(hash, names.slice(0, -1).split('\0')); } - return changes.filter(commit => { + // Labels are only known after the PR lookup, so with included labels the + // commits outside the workspace are marked instead of dropped. + const keepOutside = getChangelogConfig().includeLabels.length > 0; + return changes.flatMap(commit => { const files = pathsByHash.get(commit.hash); if (!files) { throw new Error(`Missing changelog paths for commit ${commit.hash}`); } - return belongsToWorkspace(files); + if (belongsToWorkspace(files)) { + return [commit]; + } + return keepOutside ? [{ ...commit, outsideWorkspace: true }] : []; }); }