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
1 change: 1 addition & 0 deletions docs/src/content/docs/changelogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
24 changes: 21 additions & 3 deletions docs/src/content/docs/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -1258,6 +1264,7 @@ export function getChangelogConfig(): NormalizedChangelogConfig {
policy,
scopeGrouping,
includePaths,
includeLabels,
unownedPaths,
aiSummary:
typeof config.changelog === 'object'
Expand Down
11 changes: 10 additions & 1 deletion src/schemas/project_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<CraftProjectConfigObject, string>(
config =>
Expand All @@ -748,6 +756,7 @@ export const CraftProjectConfigSchema = pipe(
['github', 'projectPath'],
),
workspaceOnlyChangelogField('includePaths'),
workspaceOnlyChangelogField('includeLabels'),
workspaceOnlyChangelogField('unownedPaths'),
);

Expand Down
93 changes: 92 additions & 1 deletion src/utils/__tests__/changelog-generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('generateChangesetFromGit', () => {
title: commit.title,
body: commit.body,
pr: commit.pr?.local || null,
outsideWorkspace: commit.outsideWorkspace,
})),
);

Expand Down Expand Up @@ -218,6 +219,7 @@ describe('generateChangesetFromGit', () => {
policy: ChangelogPolicy.Auto,
scopeGrouping: true,
includePaths: [],
includeLabels: [],
unownedPaths: 'include' as const,
aiSummary: {
llm: {
Expand Down Expand Up @@ -1675,6 +1677,7 @@ changelog:
policy: ChangelogPolicy.Auto,
scopeGrouping: false,
includePaths: [],
includeLabels: [],
unownedPaths: 'include',
aiSummary: false,
});
Expand Down Expand Up @@ -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[] = [
{
Expand Down Expand Up @@ -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);
Expand All @@ -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' },
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/fixtures/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/__tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 16 additions & 1 deletion src/utils/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }] : [];
});
}

Expand Down
Loading