Repository navigation
feat: add changesets changelog pull request resolver #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
packages/changesets-changelog/src/resolve-pull-request.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { NewChangesetWithCommit } from '@changesets/types'; | ||
| import { execa } from 'execa'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { | ||
| extractPullRequestNumber, | ||
| getPullRequestLink, | ||
| } from './resolve-pull-request.js'; | ||
|
|
||
| vi.mock('execa', () => ({ | ||
| execa: vi.fn(), | ||
| })); | ||
|
|
||
| const mockedExeca = vi.mocked(execa); | ||
|
|
||
| const changeset = { | ||
| id: 'quick-lions-devour', | ||
| summary: '## Added\n\n- Add a feature', | ||
| releases: [{ name: 'pkg-a', type: 'minor' }], | ||
| commit: 'abc1234', | ||
| } as NewChangesetWithCommit; | ||
|
|
||
| describe('extractPullRequestNumber', () => { | ||
| it('extracts a number from a squash-merge subject', () => { | ||
| expect(extractPullRequestNumber('Add a feature (#1234)')).toBe('1234'); | ||
| }); | ||
|
|
||
| it('extracts a number from a merge-commit subject', () => { | ||
| expect( | ||
| extractPullRequestNumber('Merge pull request #1234 from owner/feature'), | ||
| ).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', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getPullRequestLink', () => { | ||
| beforeEach(() => { | ||
| mockedExeca.mockReset(); | ||
| }); | ||
|
|
||
| it('reads the commit subject and returns a pull request link', async () => { | ||
| mockedExeca.mockResolvedValue({ stdout: 'Add a feature (#1234)' } as never); | ||
|
|
||
| expect(await getPullRequestLink(changeset, 'MetaMask/core')).toBe( | ||
| '[#1234](https://github.com/MetaMask/core/pull/1234)', | ||
| ); | ||
| expect(mockedExeca).toHaveBeenCalledWith('git', [ | ||
| 'show', | ||
| '-s', | ||
| '--format=%s', | ||
| 'abc1234', | ||
| ]); | ||
| }); | ||
|
|
||
| it('throws when the changeset has no commit', async () => { | ||
| const withoutCommit = { ...changeset }; | ||
| delete withoutCommit.commit; | ||
| await expect( | ||
| getPullRequestLink(withoutCommit, 'MetaMask/core'), | ||
| ).rejects.toThrow('quick-lions-devour has no commit'); | ||
| }); | ||
|
|
||
| it('throws when git cannot read the commit', async () => { | ||
| mockedExeca.mockRejectedValue(new Error('unknown revision')); | ||
| await expect( | ||
| getPullRequestLink(changeset, 'MetaMask/core'), | ||
| ).rejects.toThrow( | ||
| 'Could not read commit abc1234 for changeset quick-lions-devour', | ||
| ); | ||
| }); | ||
|
|
||
| it('throws when git returns an empty subject', async () => { | ||
| mockedExeca.mockResolvedValue({ stdout: ' ' } as never); | ||
| await expect( | ||
| getPullRequestLink(changeset, 'MetaMask/core'), | ||
| ).rejects.toThrow( | ||
| 'Could not read commit abc1234 for changeset quick-lions-devour', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { NewChangesetWithCommit } from '@changesets/types'; | ||
| import { execa } from 'execa'; | ||
|
|
||
| const SQUASH_MERGE_PR_PATTERN = /\(#(\d+)\)$/u; | ||
| 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. | ||
| */ | ||
| export function extractPullRequestNumber(subject: string): string { | ||
| 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]; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the pull request link for a changeset using its local Git commit. | ||
| * | ||
| * @param changeset - The changeset whose commit should be inspected. | ||
| * @param repo - The GitHub repository in `owner/repo` form. | ||
| * @returns The Markdown pull request link. | ||
| */ | ||
| export async function getPullRequestLink( | ||
| changeset: NewChangesetWithCommit, | ||
| repo: string, | ||
| ): Promise<string> { | ||
| if (!changeset.commit) { | ||
| throw new Error(`Changeset ${changeset.id} has no commit`); | ||
| } | ||
|
|
||
| let subject: string; | ||
| try { | ||
| ({ stdout: subject } = await execa('git', [ | ||
| 'show', | ||
| '-s', | ||
| '--format=%s', | ||
| changeset.commit, | ||
| ])); | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Could not read commit ${changeset.commit} for changeset ${changeset.id}`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
|
|
||
| if (!subject.trim()) { | ||
| throw new Error( | ||
| `Could not read commit ${changeset.commit} for changeset ${changeset.id}: empty subject`, | ||
| ); | ||
| } | ||
|
|
||
| const pullRequestNumber = extractPullRequestNumber(subject.trim()); | ||
| return `[#${pullRequestNumber}](https://github.com/${repo}/pull/${pullRequestNumber})`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.packages.json", | ||
| "compilerOptions": { | ||
| "types": ["node"] | ||
| }, | ||
| "references": [], | ||
| "include": ["./src"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to export these at index.ts ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All right it looks like from PR description this will come up in a future PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No these will only be used internally. this is the changesets plugin so I would keep the exports to only the plugin required functions (they'll come after)