Skip to content
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

fix: handle issue links in BREAKING_CHANGE notes section #1757

Merged
merged 3 commits into from Nov 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions __snapshots__/default-changelog-notes.js
Expand Up @@ -109,6 +109,19 @@ exports['DefaultChangelogNotes buildNotes with commit parsing should handle a br
* some bugfix ([05670cf](https://github.com/googleapis/java-asset/commit/05670cf2e850beffe53bb2691f8701c7))
`

exports['DefaultChangelogNotes buildNotes with commit parsing should handle a breaking change with reference 1'] = `
## [1.2.3](https://github.com/googleapis/java-asset/compare/v1.2.2...v1.2.3) (1983-10-10)


### ⚠ BREAKING CHANGES

* some bugfix ([#1234](https://github.com/googleapis/java-asset/issues/1234))

### Bug Fixes

* some bugfix ([#1234](https://github.com/googleapis/java-asset/issues/1234)) ([749cd8b](https://github.com/googleapis/java-asset/commit/749cd8b9edc6103a2f40a34ca45c31c5))
`

exports['DefaultChangelogNotes buildNotes with commit parsing should handle bug links 1'] = `
## [1.2.3](https://github.com/googleapis/java-asset/compare/v1.2.2...v1.2.3) (1983-10-10)

Expand Down Expand Up @@ -140,6 +153,15 @@ exports['DefaultChangelogNotes buildNotes with commit parsing should handle html
* render all imagesets as <picture> ([383fb14](https://github.com/googleapis/java-asset/commit/383fb14708ae91f7bb7e64bf0bacab38))
`

exports['DefaultChangelogNotes buildNotes with commit parsing should handle inline bug links 1'] = `
## [1.2.3](https://github.com/googleapis/java-asset/compare/v1.2.2...v1.2.3) (1983-10-10)


### Bug Fixes

* some bugfix ([#1234](https://github.com/googleapis/java-asset/issues/1234)) ([6f2163b](https://github.com/googleapis/java-asset/commit/6f2163be093d8a8dd90232d06b45c07e))
`

exports['DefaultChangelogNotes buildNotes with commit parsing should handle meta commits 1'] = `
## [1.2.3](https://github.com/googleapis/java-asset/compare/v1.2.2...v1.2.3) (1983-10-10)

Expand Down
31 changes: 29 additions & 2 deletions src/changelog-notes/default.ts
Expand Up @@ -31,6 +31,11 @@ interface DefaultChangelogNotesOptions {
mainTemplate?: string;
}

interface Note {
title: string;
text: string;
}

export class DefaultChangelogNotes implements ChangelogNotes {
// allow for customized commit template.
private commitPartial?: string;
Expand Down Expand Up @@ -68,14 +73,23 @@ export class DefaultChangelogNotes implements ChangelogNotes {
this.headerPartial || preset.writerOpts.headerPartial;
preset.writerOpts.mainTemplate =
this.mainTemplate || preset.writerOpts.mainTemplate;

const changelogCommits = commits.map(commit => {
const notes = commit.notes
.filter(note => note.title === 'BREAKING CHANGE')
.map(note =>
replaceIssueLink(
note,
context.host,
context.owner,
context.repository
)
);
return {
body: '', // commit.body,
subject: htmlEscape(commit.bareMessage),
type: commit.type,
scope: commit.scope,
notes: commit.notes.filter(note => note.title === 'BREAKING CHANGE'),
notes,
references: commit.references,
mentions: [],
merge: null,
Expand All @@ -95,6 +109,19 @@ export class DefaultChangelogNotes implements ChangelogNotes {
}
}

function replaceIssueLink(
note: Note,
host: string,
owner: string,
repo: string
): Note {
note.text = note.text.replace(
/\(#(\d+)\)/,
`([#$1](${host}/${owner}/${repo}/issues/$1))`
);
return note;
}

function htmlEscape(message: string): string {
return message.replace('<', '&lt;').replace('>', '&gt;');
}
20 changes: 20 additions & 0 deletions test/changelog-notes/default-changelog-notes.ts
Expand Up @@ -135,6 +135,16 @@ describe('DefaultChangelogNotes', () => {
expect(notes).to.is.string;
safeSnapshot(notes);
});
it('should handle a breaking change with reference', async () => {
const commits = [buildMockCommit('fix!: some bugfix (#1234)')];
const changelogNotes = new DefaultChangelogNotes();
const notes = await changelogNotes.buildNotes(
parseConventionalCommits(commits),
notesOptions
);
expect(notes).to.is.string;
safeSnapshot(notes);
});
it('should parse multiple commit messages from a single commit', async () => {
const commits = [buildCommitFromFixture('multiple-messages')];
const changelogNotes = new DefaultChangelogNotes();
Expand Down Expand Up @@ -165,6 +175,16 @@ describe('DefaultChangelogNotes', () => {
expect(notes).to.is.string;
safeSnapshot(notes);
});
it('should handle inline bug links', async () => {
const commits = [buildMockCommit('fix: some bugfix (#1234)')];
const changelogNotes = new DefaultChangelogNotes();
const notes = await changelogNotes.buildNotes(
parseConventionalCommits(commits),
notesOptions
);
expect(notes).to.is.string;
safeSnapshot(notes);
});
it('should handle git trailers', async () => {
const commits = [buildCommitFromFixture('git-trailers-with-breaking')];
const changelogNotes = new DefaultChangelogNotes();
Expand Down