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

misc(changelog-generator): Generate changelogs #3632

Merged
merged 13 commits into from
Nov 11, 2017
74 changes: 74 additions & 0 deletions conventional-changelog-lighthouse/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* 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.
*/
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs license header

const readFileSync = require('fs').readFileSync;
const resolve = require('path').resolve;
const mainTemplate = readFileSync(resolve(__dirname, 'templates/template.hbs')).toString();
const headerPartial = readFileSync(resolve(__dirname, 'templates/header.hbs')).toString();
const commitPartial = readFileSync(resolve(__dirname, 'templates/commit.hbs')).toString();

const parserOpts = {
headerPattern: /^(\w*)(?:\((.*)\))?: (.*)$/,
headerCorrespondence: [
'type',
'scope',
'message',
],
};

const writerOpts = {
mainTemplate,
headerPartial,
commitPartial,
transform: commit => {
if (typeof commit.hash === 'string') {
commit.hash = commit.hash.substring(0, 7);
}

if (commit.type === 'test') {
commit.type = 'tests';
}

if (commit.type) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also de-dupe test and tests while we're in here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine by me, I just copy pasted it 😊

commit.type = commit.type.replace(/_/g, ' ');
commit.type = commit.type.substring(0, 1).toUpperCase() + commit.type.substring(1);
} else {
commit.type = 'Misc';
}

let pullRequestMatch = commit.header.match(/\(#(\d+)\)/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between this case and the message one below? Is header the initial line of the commit message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we be checking that the PR # is at the end of the string? (the default positioning from the squash and merge button). A #XXXX elsewhere could be e.g. a reference to an issue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header is indeed the first line and message is the rest. When I only ran it on message I didn't get all the PR references if I check both I do.

For the regex, i checked the last 50 commits and we never refer an issue in the commit header but better save than sorry so edited the regex!

// if header does not provide a PR we try the message
if (!pullRequestMatch && commit.message) {
pullRequestMatch = commit.message.match(/\(#(\d+)\)/);
}

if (pullRequestMatch) {
commit.header = commit.header.replace(pullRequestMatch[0], '');
if (commit.message) {
commit.message = commit.message.replace(pullRequestMatch[0], '');
}

commit.PR = pullRequestMatch[1];
}

return commit;
},
groupBy: 'type',
commitGroupsSort: (a, b) => {
// put new audit on the top
if (b.title === 'New audit') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't working for me. I think you also need to handle the other case, too (if (a.title === 'New audit') return -1)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake! it works now!

return 1;
}

return a.title.localeCompare(b.title);
},
commitsSort: ['type', 'scope', 'message'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past we've only sorted by type and scope (if it exists) and then by commit order, but not sure if that's possible with this

};

module.exports = {
writerOpts,
parserOpts,
};
28 changes: 28 additions & 0 deletions conventional-changelog-lighthouse/templates/commit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{!--
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* 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.
*/
--}}
* {{#if message ~}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license header for these templates, too (need to convert to handlebars comments, I guess)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include scope?

{{~message~}}
{{~else~}}
{{~header~}}
{{~/if ~}}
{{~!-- commit hash --}}
{{~#if @root.linkReferences~}}
{{~#if PR ~}}
([#{{PR}}](
{{~#if @root.host}}{{~@root.host}}/{{/if~}}
{{~#if @root.owner ~}}{{@root.owner}}/{{/if~}}
{{~@root.repository}}/pull/{{PR}}))
{{~else~}}
([{{hash}}](
{{~#if @root.host}}{{~@root.host}}/{{/if~}}
{{~#if @root.owner ~}}{{@root.owner}}/{{/if~}}
{{~@root.repository}}/{{@root.commit}}/{{hash}}))
{{~/if~}}
{{~else~}}
{{~hash~}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to preserve a single space here, the formatting looks off compared to ones with a PR link

image

{{~/if~}}
12 changes: 12 additions & 0 deletions conventional-changelog-lighthouse/templates/header.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{!--
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* 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.
*/
--}}
<a name="{{version}}"></a>
# {{version}} ({{date}})
{{#if @root.linkCompare~}}
[Full Changelog]({{~@root.repoUrl}}/compare/{{previousTag}}...{{currentTag}})
{{~/if}}
20 changes: 20 additions & 0 deletions conventional-changelog-lighthouse/templates/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{!--
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* 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.
*/
--}}
{{> header}}

{{#each commitGroups}}

{{#if title~}}
### {{title}}

{{/if}}
{{#each commits}}
{{~> commit root=@root}}

{{/each}}
{{/each}}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@
"smokehouse": "node lighthouse-cli/test/smokehouse/smokehouse.js",
"deploy-viewer": "cd lighthouse-viewer && gulp deploy",
"bundlesize": "bundlesize",
"plots-smoke": "bash plots/test/smoke.sh"
"plots-smoke": "bash plots/test/smoke.sh",
"changelog:generate": "conventional-changelog -n ./conventional-changelog-lighthouse/index.js -i changelog.md -s"
},
"devDependencies": {
"@types/node": "^6.0.45",
"babel-core": "^6.16.0",
"bundlesize": "^0.14.4",
"codecov": "^2.2.0",
"commitizen": "^2.9.6",
"conventional-changelog-cli": "^1.3.4",
"coveralls": "^2.11.9",
"cz-customizable": "^5.2.0",
"eslint": "^4.8.0",
Expand Down
Loading