diff --git a/__snapshots__/github-changelog-notes.js b/__snapshots__/github-changelog-notes.js new file mode 100644 index 000000000..c99600410 --- /dev/null +++ b/__snapshots__/github-changelog-notes.js @@ -0,0 +1,5 @@ +exports['GitHubChangelogNotes buildNotes should build release notes from GitHub 1'] = ` +## 1.2.3 (1983-10-10) + +##Changes in Release v1.0.0 ... ##Contributors @monalisa +` diff --git a/src/changelog-notes/github.ts b/src/changelog-notes/github.ts index 2fd48cde7..a79ddeeab 100644 --- a/src/changelog-notes/github.ts +++ b/src/changelog-notes/github.ts @@ -25,10 +25,13 @@ export class GitHubChangelogNotes implements ChangelogNotes { _commits: ConventionalCommit[], options: BuildNotesOptions ): Promise { - return await this.github.generateReleaseNotes( + const body = await this.github.generateReleaseNotes( options.currentTag, options.targetBranch, options.previousTag ); + const date = new Date().toLocaleDateString('en-CA'); + const header = `## ${options.version} (${date})`; + return `${header}\n\n${body}`; } } diff --git a/test/changelog-notes/default-changelog-notes.ts b/test/changelog-notes/default-changelog-notes.ts index 5efd8b35b..e78e48d10 100644 --- a/test/changelog-notes/default-changelog-notes.ts +++ b/test/changelog-notes/default-changelog-notes.ts @@ -21,6 +21,8 @@ import { } from '../helpers'; import {DefaultChangelogNotes} from '../../src/changelog-notes/default'; import {parseConventionalCommits} from '../../src/commit'; +import {PullRequestBody} from '../../src/util/pull-request-body'; +import {Version} from '../../src/version'; describe('DefaultChangelogNotes', () => { const commits = [ @@ -283,4 +285,33 @@ describe('DefaultChangelogNotes', () => { // }); }); }); + describe('pull request compatibility', () => { + it('should build parseable notes', async () => { + const notesOptions = { + owner: 'googleapis', + repository: 'java-asset', + version: '1.2.3', + previousTag: 'v1.2.2', + currentTag: 'v1.2.3', + targetBranch: 'main', + }; + const changelogNotes = new DefaultChangelogNotes(); + const notes = await changelogNotes.buildNotes(commits, notesOptions); + const pullRequestBody = new PullRequestBody([ + { + version: Version.parse('1.2.3'), + notes, + }, + ]); + const pullRequestBodyContent = pullRequestBody.toString(); + const parsedPullRequestBody = PullRequestBody.parse( + pullRequestBodyContent + ); + expect(parsedPullRequestBody).to.not.be.undefined; + expect(parsedPullRequestBody!.releaseData).lengthOf(1); + expect(parsedPullRequestBody!.releaseData[0].version?.toString()).to.eql( + '1.2.3' + ); + }); + }); }); diff --git a/test/changelog-notes/github-changelog-notes.ts b/test/changelog-notes/github-changelog-notes.ts new file mode 100644 index 000000000..43a082dca --- /dev/null +++ b/test/changelog-notes/github-changelog-notes.ts @@ -0,0 +1,121 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import * as nock from 'nock'; +import {describe, it} from 'mocha'; +import {expect} from 'chai'; +import {safeSnapshot} from '../helpers'; +import {PullRequestBody} from '../../src/util/pull-request-body'; +import {Version} from '../../src/version'; +import {GitHubChangelogNotes} from '../../src/changelog-notes/github'; +import {GitHub} from '../../src/github'; + +nock.disableNetConnect(); + +describe('GitHubChangelogNotes', () => { + const commits = [ + { + sha: 'sha1', + message: 'feat: some feature', + files: ['path1/file1.txt'], + type: 'feat', + scope: null, + bareMessage: 'some feature', + notes: [], + references: [], + breaking: false, + }, + { + sha: 'sha2', + message: 'fix!: some bugfix', + files: ['path1/file1.rb'], + type: 'fix', + scope: null, + bareMessage: 'some bugfix', + notes: [{title: 'BREAKING CHANGE', text: 'some bugfix'}], + references: [], + breaking: true, + }, + { + sha: 'sha3', + message: 'docs: some documentation', + files: ['path1/file1.java'], + type: 'docs', + scope: null, + bareMessage: 'some documentation', + notes: [], + references: [], + breaking: false, + }, + ]; + describe('buildNotes', () => { + const notesOptions = { + owner: 'googleapis', + repository: 'java-asset', + version: '1.2.3', + previousTag: 'v1.2.2', + currentTag: 'v1.2.3', + targetBranch: 'main', + }; + let github: GitHub; + beforeEach(async () => { + github = await GitHub.create({ + owner: 'fake-owner', + repo: 'fake-repo', + defaultBranch: 'main', + token: 'fake-token', + }); + nock('https://api.github.com/') + .post('/repos/fake-owner/fake-repo/releases/generate-notes') + .reply(200, { + name: 'Release v1.0.0 is now available!', + body: '##Changes in Release v1.0.0 ... ##Contributors @monalisa', + }); + }); + it('should build release notes from GitHub', async () => { + const changelogNotes = new GitHubChangelogNotes(github); + const notes = await changelogNotes.buildNotes(commits, notesOptions); + expect(notes).to.is.string; + safeSnapshot(notes); + }); + + it('should build parseable notes', async () => { + const notesOptions = { + owner: 'googleapis', + repository: 'java-asset', + version: '1.2.3', + previousTag: 'v1.2.2', + currentTag: 'v1.2.3', + targetBranch: 'main', + }; + const changelogNotes = new GitHubChangelogNotes(github); + const notes = await changelogNotes.buildNotes(commits, notesOptions); + const pullRequestBody = new PullRequestBody([ + { + version: Version.parse('1.2.3'), + notes, + }, + ]); + const pullRequestBodyContent = pullRequestBody.toString(); + const parsedPullRequestBody = PullRequestBody.parse( + pullRequestBodyContent + ); + expect(parsedPullRequestBody).to.not.be.undefined; + expect(parsedPullRequestBody!.releaseData).lengthOf(1); + expect(parsedPullRequestBody!.releaseData[0].version?.toString()).to.eql( + '1.2.3' + ); + }); + }); +});