Skip to content

Commit

Permalink
misc(changelog-generator): Generate changelogs (GoogleChrome#3632)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and christhompson committed Nov 17, 2017
1 parent 0bce362 commit 2e825af
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ echo "Test the lighthouse-viewer build"
echo "Branch and commit the version bump."
git co -b bumpv240
git c "2.4.0"
yarn changelog
git tag -a v2.4.0 -m "v2.4.0"
echo "Generate a PR and get it merged."

Expand All @@ -162,7 +163,7 @@ npm publish
yarn deploy-viewer

echo "Use the GitHub web interface to tag the release"
echo "Generate the release notes, and update the release page"
echo "Copy changelog to release notes and update the release page"

# * Tell the world!!! *
echo "Inform various peoples"
Expand Down
86 changes: 86 additions & 0 deletions build/changelog-generator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @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';
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 pullRequestRegex = /\(#(\d+)\)$/;
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) {
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(pullRequestRegex);
// if header does not provide a PR we try the message
if (!pullRequestMatch && commit.message) {
pullRequestMatch = commit.message.match(pullRequestRegex);
}

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

commit.PR = pullRequestMatch[1];
}

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

// put misc on the bottom
if (a.title === 'Misc') {
return 1;
}
if (b.title === 'Misc') {
return -1;
}

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

module.exports = {
writerOpts,
parserOpts,
};
27 changes: 27 additions & 0 deletions build/changelog-generator/templates/commit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{!--
/**
* @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 scope}}{{scope}}: {{/if~}}
{{#if message ~}}
{{~message~}}
{{~else~}}
{{~header~}}
{{~/if ~}}
{{~!-- PR number/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~}}
{{~/if~}}
12 changes: 12 additions & 0 deletions build/changelog-generator/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 build/changelog-generator/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": "conventional-changelog -n ./build/changelog-generator/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

0 comments on commit 2e825af

Please sign in to comment.