Skip to content

Commit

Permalink
fix: handle issue links in BREAKING_CHANGE notes section (#1757)
Browse files Browse the repository at this point in the history
* test: failing test for breaking change section using extended link format

* test: add more tests for issue links

* fix: handle issue links in BREAKING_CHANGE notes section
  • Loading branch information
chingor13 committed Nov 22, 2022
1 parent 596ce4e commit cd8c04b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
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

0 comments on commit cd8c04b

Please sign in to comment.