Skip to content

Commit

Permalink
fix(github-changelog-notes): prepend header to create parseable relea…
Browse files Browse the repository at this point in the history
…se notes (#1912)

* test: failing test for parsing generated github changelog notes

* fix(github-changelog-notes): prepend header to create parseable release notes
  • Loading branch information
chingor13 committed Apr 11, 2023
1 parent f860829 commit 3f53d40
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
5 changes: 5 additions & 0 deletions __snapshots__/github-changelog-notes.js
Original file line number Diff line number Diff line change
@@ -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
`
5 changes: 4 additions & 1 deletion src/changelog-notes/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ export class GitHubChangelogNotes implements ChangelogNotes {
_commits: ConventionalCommit[],
options: BuildNotesOptions
): Promise<string> {
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}`;
}
}
31 changes: 31 additions & 0 deletions test/changelog-notes/default-changelog-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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'
);
});
});
});
121 changes: 121 additions & 0 deletions test/changelog-notes/github-changelog-notes.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
});

0 comments on commit 3f53d40

Please sign in to comment.