Skip to content
Open
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 packages/create-release-branch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add Changesets mode: the pending Changesets set the packages and versions in the release spec, and `@metamask/changesets-changelog` writes the changelogs. Choose it with `--changesets` or with `"mode": "changesets"` in `release.config.json` ([#170](https://github.com/MetaMask/release-tools/pull/170))
- Read the release mode from an optional `mode` property in `release.config.json` ([#165](https://github.com/MetaMask/release-tools/pull/165))

### Changed
Expand Down
2 changes: 2 additions & 0 deletions packages/create-release-branch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ npm install @metamask/create-release-branch --save-dev

For more on how to use this tool, please see the [documentation](./docs).

The tool can also release a monorepo with [Changesets](https://github.com/changesets/changesets). See [Changesets mode](./docs/setup.md#changesets-mode).

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/release-tools#readme).
39 changes: 39 additions & 0 deletions packages/create-release-branch/docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,42 @@ Combined with the changes above, this might look like:
"versioningStrategy": "independent"
}
```

## Changesets mode

By default, the tool reads the Git history to write the changelogs. A monorepo with independent versions can use [Changesets](https://github.com/changesets/changesets) instead. In Changesets mode:

- The pending Changesets in the `.changeset` directory set the packages to release and their new versions.
- The release spec lists these packages, with their version specifiers already filled in. Do not change a specifier. If a specifier gives another version than the Changesets, the tool stops with an error. To keep the Changesets of a package for a later release, remove the package from the release spec. If another released package depends on it, or if it depends on a major bump, set it to `intentionally-skip` instead.
- The tool writes the changelogs from the Changesets. Then it deletes the Changesets that the release uses.
- The tool does not release the entries in the "Unreleased" section of a changelog. A package without Changesets is not released, even if it has such entries. If a package in the release has such entries, the tool stops with an error. Move the entries into Changesets and commit them, or remove the entries. You can also release them in legacy mode before you use Changesets mode.

To use Changesets mode, set `mode` to `"changesets"` in `release.config.json`:

```
# release.config.json
{
"versioningStrategy": "independent",
"mode": "changesets"
}
```

You can also run the tool with the `--changesets` flag. The flag selects Changesets mode for one run, whatever `mode` is set to.

Changesets mode has these requirements:

- The project must have a `.changeset/config.json` file that uses the `@metamask/changesets-changelog` changelog module. Give the module the GitHub repository of the project:

```json
{
"changelog": [
"@metamask/changesets-changelog",
{ "repository": "<owner>/<repo>" }
]
}
```

- Each Changeset summary must have one category heading and one entry, such as `## Added` followed by `- Add a feature`. If the subject of the commit that adds a Changeset ends with a pull request number, such as `Add a feature (#123)`, the changelog entry links to this pull request. If the subject has no pull request number, for example for a Changeset that you commit on the release branch, the entry has no link. Add the link by hand.
- Changesets pre mode must be off. To exit pre mode, run `changeset pre exit`.
- The `.changeset` directory must not have uncommitted changes. The tool checks this before it makes the first commit of the release, and again before it writes the release.
- The interactive UI (`--interactive`) does not support Changesets mode yet. Edit the release spec in your editor instead.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type CommandLineArguments = {
backport: boolean;
defaultBranch: string;
interactive: boolean;
changesets: boolean;
port: number;
formatter: string;
};
Expand Down Expand Up @@ -61,6 +62,12 @@ export async function readCommandLineArguments(
type: 'boolean',
default: false,
})
.option('changesets', {
describe:
'Release with Changesets: the pending Changesets set the packages and versions, and write the changelogs. Overrides the "mode" in release.config.json.',
type: 'boolean',
default: false,
})
.option('port', {
describe:
'Port to run the interactive web UI server (only used with --interactive)',
Expand Down
104 changes: 104 additions & 0 deletions packages/create-release-branch/src/cli/initial-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -111,6 +113,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -142,6 +145,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -173,6 +177,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -202,6 +207,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -231,6 +237,7 @@ describe('initial-parameters', () => {
backport: true,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -260,6 +267,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -289,6 +297,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: true,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand Down Expand Up @@ -318,6 +327,7 @@ describe('initial-parameters', () => {
backport: false,
defaultBranch: 'main',
interactive: false,
changesets: false,
port: 3000,
formatter: 'prettier',
});
Expand All @@ -335,5 +345,99 @@ describe('initial-parameters', () => {

expect(initialParameters.frontEndName).toBe('editor');
});

describe('when choosing the mode', () => {
/**
* Mocks the command-line arguments and the project.
*
* @param overrides - The arguments that differ from the defaults.
* @param overrides.changesets - Whether `--changesets` is given.
* @param overrides.interactive - Whether `--interactive` is given.
*/
function mockInputs(overrides: {
changesets: boolean;
interactive: boolean;
}): void {
vi.when(
vi.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'),
)
.calledWith(['arg1', 'arg2'])
.thenResolve({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: false,
backport: false,
defaultBranch: 'main',
port: 3000,
formatter: 'prettier',
...overrides,
});
vi.spyOn(envModule, 'getEnvironmentVariables').mockReturnValue({
EDITOR: undefined,
});
vi.spyOn(projectModule, 'readProject').mockResolvedValue(
buildMockProject(),
);
}

it('uses the mode of the release config', async () => {
mockInputs({ changesets: false, interactive: false });
vi.when(vi.spyOn(releaseConfigModule, 'readReleaseConfig'))
.calledWith('/path/to/project')
.thenResolve({ mode: 'changesets' });

const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
});

expect(initialParameters.modeName).toBe('changesets');
});

it('uses Changesets mode if --changesets is given, without reading the release config', async () => {
mockInputs({ changesets: true, interactive: false });
const readReleaseConfigSpy = vi.spyOn(
releaseConfigModule,
'readReleaseConfig',
);

const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
});

expect(initialParameters.modeName).toBe('changesets');
expect(readReleaseConfigSpy).not.toHaveBeenCalled();
});

it('throws if --interactive is given with --changesets', async () => {
mockInputs({ changesets: true, interactive: true });

await expect(
determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
}),
).rejects.toThrow(
'The interactive UI does not support Changesets mode yet.',
);
});

it('throws if --interactive is given and the release config uses Changesets mode', async () => {
mockInputs({ changesets: false, interactive: true });
vi.spyOn(releaseConfigModule, 'readReleaseConfig').mockResolvedValue({
mode: 'changesets',
});

await expect(
determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
}),
).rejects.toThrow(
'The interactive UI does not support Changesets mode yet.\nRun this tool without --interactive to edit the release spec in your editor.',
);
});
});
});
});
13 changes: 12 additions & 1 deletion packages/create-release-branch/src/cli/initial-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type InitialParameters = {
* @param args.argv - The arguments to this executable.
* @param args.cwd - The directory in which this executable was run.
* @returns The initial parameters.
* @throws If the interactive UI is asked for in Changesets mode.
*/
export async function determineInitialParameters({
argv,
Expand All @@ -40,7 +41,17 @@ export async function determineInitialParameters({

const projectDirectoryPath = path.resolve(cwd, args.projectDirectory);
const project = await readProject(projectDirectoryPath);
const { mode: modeName } = await readReleaseConfig(projectDirectoryPath);
const modeName: ModeName = args.changesets
? 'changesets'
: (await readReleaseConfig(projectDirectoryPath)).mode;
if (modeName === 'changesets' && args.interactive) {
throw new Error(
[
'The interactive UI does not support Changesets mode yet.',
'Run this tool without --interactive to edit the release spec in your editor.',
].join('\n'),
);
}
const tempDirectoryPath =
args.tempDirectory === undefined
? path.join(
Expand Down
23 changes: 23 additions & 0 deletions packages/create-release-branch/src/cli/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import type { FrontEnd } from '../core/front-end.js';
import type { Mode } from '../core/mode.js';
import * as editorFrontEndModule from '../frontends/editor/editor-front-end.js';
import * as uiFrontEndModule from '../frontends/ui/server.js';
import * as changesetsModeModule from '../modes/changesets/changesets-mode.js';
import * as legacyModeModule from '../modes/legacy/legacy-mode.js';
import { selectFrontEnd, selectMode } from './select.js';

vi.mock('../frontends/editor/editor-front-end');
vi.mock('../frontends/ui/server');
vi.mock('../modes/changesets/changesets-mode');
vi.mock('../modes/legacy/legacy-mode');

describe('select', () => {
Expand All @@ -34,6 +36,27 @@ describe('select', () => {
});
expect(mode).toBe(legacyMode);
});

it('selectMode returns the Changesets mode', () => {
const project = buildMockProject();
const changesetsMode = { name: 'changesets' } as Mode<never>;
const createChangesetsModeSpy = vi
.spyOn(changesetsModeModule, 'createChangesetsMode')
.mockReturnValue(changesetsMode);

const mode = selectMode('changesets', {
project,
defaultBranch: 'main',
formatter: 'prettier',
});

expect(createChangesetsModeSpy).toHaveBeenCalledWith({
project,
defaultBranch: 'main',
formatter: 'prettier',
});
expect(mode).toBe(changesetsMode);
});
});

describe('selectFrontEnd', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/create-release-branch/src/cli/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Mode, ModeName } from '../core/mode.js';
import type { Formatter } from '../core/types.js';
import { createEditorFrontEnd } from '../frontends/editor/editor-front-end.js';
import { createUIFrontEnd } from '../frontends/ui/server.js';
import { createChangesetsMode } from '../modes/changesets/changesets-mode.js';
import { createLegacyMode } from '../modes/legacy/legacy-mode.js';
import type { Project } from '../project/project.js';

Expand All @@ -30,6 +31,9 @@ export function selectMode(
case 'legacy':
return createLegacyMode(options);

case 'changesets':
return createChangesetsMode(options);

default:
return assertExhaustive(name);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/create-release-branch/src/core/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ReleaseChoiceError, ReleaseChoices } from './release-rules.js';
/**
* The names of the release modes.
*/
export const MODE_NAMES = ['legacy'] as const;
export const MODE_NAMES = ['legacy', 'changesets'] as const;

/**
* The name of a release mode.
Expand Down
Loading
Loading