Skip to content
Draft
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ linkStyle default opacity:0.5
changesets_lint --> changesets_utils;
changesets_lint --> auto_changelog;
changesets_utils --> auto_changelog;
create_release_branch --> apply_release_plan;
create_release_branch --> auto_changelog;
create_release_branch --> changesets_utils;
```
Expand Down
1 change: 1 addition & 0 deletions packages/changesets-changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add Keep a Changelog categories and repository option validation ([#87](https://github.com/MetaMask/release-tools/pull/87))
- Generate categorized changelog entries ([#88](https://github.com/MetaMask/release-tools/pull/88))
- Generate Keep a Changelog version headings ([#90](https://github.com/MetaMask/release-tools/pull/90))
- Write a changelog entry without a pull request link if the commit subject of its changeset has no pull request number ([#169](https://github.com/MetaMask/release-tools/pull/169))

[Unreleased]: https://github.com/MetaMask/release-tools/
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ describe('getReleaseLine', () => {
'- **BREAKING:** Add a feature ([#1234](https://github.com/MetaMask/core/pull/1234))\n - Preserve this detail',
);
});

it('leaves out the pull request link if the commit has no pull request number', async () => {
mockedGetPullRequestLink.mockResolvedValue(undefined);

expect(await getReleaseLine(changeset, 'minor', options)).toBe(
'- Add a feature\n - Preserve this detail',
);
});
});

describe('categorizeReleaseLine', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/changesets-changelog/src/changelog-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { getGitHubRepository } from './options.js';
import { getPullRequestLink } from './resolve-pull-request.js';

/**
* Formats one categorized changeset as a Markdown changelog line.
* Formats one categorized changeset as a Markdown changelog line. The line
* has no pull request link if the commit of the changeset has no pull request
* number in its subject.
*
* @param changeset - The changeset to format.
* @param type - The release's version bump type.
Expand All @@ -29,9 +31,11 @@ export const getReleaseLine: GetReleaseLine = async (
);
const [firstLine, ...remainingLines] = description.split('\n');
const breakingPrefix = type === 'major' ? '**BREAKING:** ' : '';
const linkSuffix =
pullRequestLink === undefined ? '' : ` (${pullRequestLink})`;

return [
`- ${breakingPrefix}${firstLine} (${pullRequestLink})`,
`- ${breakingPrefix}${firstLine}${linkSuffix}`,
...remainingLines,
].join('\n');
};
Expand Down
14 changes: 10 additions & 4 deletions packages/changesets-changelog/src/resolve-pull-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ describe('extractPullRequestNumber', () => {
).toBe('1234');
});

it('throws when a subject has no pull request number', () => {
expect(() => extractPullRequestNumber('Add a feature')).toThrow(
'does not contain a pull request number',
);
it('returns undefined when a subject has no pull request number', () => {
expect(extractPullRequestNumber('Add a feature')).toBeUndefined();
});
});

Expand All @@ -57,6 +55,14 @@ describe('getPullRequestLink', () => {
]);
});

it('returns undefined when the commit subject has no pull request number', async () => {
mockedExeca.mockResolvedValue({ stdout: 'Add a feature' } as never);

expect(
await getPullRequestLink(changeset, 'MetaMask/core'),
).toBeUndefined();
});

it('throws when the changeset has no commit', async () => {
const withoutCommit = { ...changeset };
delete withoutCommit.commit;
Expand Down
21 changes: 9 additions & 12 deletions packages/changesets-changelog/src/resolve-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@ const MERGE_COMMIT_PR_PATTERN = /Merge pull request #(\d+) from/iu;
* Extracts a pull request number from a Git commit subject.
*
* @param subject - The Git commit subject.
* @returns The pull request number.
* @returns The pull request number, or undefined if the subject has none.
*/
export function extractPullRequestNumber(subject: string): string {
export function extractPullRequestNumber(subject: string): string | undefined {
const match =
subject.match(SQUASH_MERGE_PR_PATTERN) ??
subject.match(MERGE_COMMIT_PR_PATTERN);

if (!match) {
throw new Error(
`Commit subject does not contain a pull request number: ${subject}`,
);
}

return match[1];
return match?.[1];
}

/**
* Resolves the pull request link for a changeset using its local Git commit.
*
* @param changeset - The changeset whose commit should be inspected.
* @param repository - The GitHub repository in `owner/repo` form.
* @returns The Markdown pull request link.
* @returns The Markdown pull request link, or undefined if the commit subject
* has no pull request number.
*/
export async function getPullRequestLink(
changeset: NewChangesetWithCommit,
repository: string,
): Promise<string> {
): Promise<string | undefined> {
if (!changeset.commit) {
throw new Error(`Changeset ${changeset.id} has no commit`);
}
Expand All @@ -61,5 +56,7 @@ export async function getPullRequestLink(
}

const pullRequestNumber = extractPullRequestNumber(subject.trim());
return `[#${pullRequestNumber}](https://github.com/${repository}/pull/${pullRequestNumber})`;
return pullRequestNumber === undefined
? undefined
: `[#${pullRequestNumber}](https://github.com/${repository}/pull/${pullRequestNumber})`;
}
1 change: 1 addition & 0 deletions packages/create-release-branch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@clack/prompts": "^1.8.1",
"@manypkg/get-packages": "^3.1.0",
"@metamask/action-utils": "^2.0.0",
"@metamask/apply-release-plan": "^0.0.0",
"@metamask/auto-changelog": "^7.1.0",
"@metamask/changesets-utils": "^0.0.0",
"@metamask/utils": "^11.12.0",
Expand Down
27 changes: 27 additions & 0 deletions packages/create-release-branch/src/core/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
hasChangesInDirectorySinceGitTag,
getCurrentBranchName,
branchExists,
hasUncommittedChanges,
restoreFiles,
} from './repo.js';

Expand Down Expand Up @@ -220,6 +221,32 @@ describe('repo', () => {
});
});

describe('hasUncommittedChanges', () => {
it('returns true when git status lists a change under the path', async () => {
vi.when(vi.spyOn(miscUtils, 'getLinesFromCommand'))
.calledWith('git', ['status', '--porcelain', '--', '.changeset'], {
cwd: '/path/to/repo',
})
.thenResolve(['?? .changeset/brave-cats-sing.md']);

expect(await hasUncommittedChanges('/path/to/repo', '.changeset')).toBe(
true,
);
});

it('returns false when git status lists no change under the path', async () => {
vi.when(vi.spyOn(miscUtils, 'getLinesFromCommand'))
.calledWith('git', ['status', '--porcelain', '--', '.changeset'], {
cwd: '/path/to/repo',
})
.thenResolve([]);

expect(await hasUncommittedChanges('/path/to/repo', '.changeset')).toBe(
false,
);
});
});

describe('restoreFiles', () => {
it('should call runCommand with the correct arguments', async () => {
const getStdoutFromCommandSpy = vi.spyOn(
Expand Down
20 changes: 20 additions & 0 deletions packages/create-release-branch/src/core/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ export async function branchExists(
return branchNames.length > 0;
}

/**
* Checks whether a path in the given repository has uncommitted changes,
* including untracked files.
*
* @param repositoryDirectoryPath - The path to the repository directory.
* @param pathspec - The path to check, relative to the repository directory.
* @returns Whether `git status` reports any change under the path.
*/
export async function hasUncommittedChanges(
repositoryDirectoryPath: string,
pathspec: string,
): Promise<boolean> {
const statusLines = await getLinesFromGitCommandWithin(
repositoryDirectoryPath,
'status',
['--porcelain', '--', pathspec],
);
return statusLines.length > 0;
}

/**
* Retrieves the names of the tags in the given repo, sorted by ascending
* semantic version order. As this fetches tags from the remote first, you are
Expand Down
Loading
Loading