Skip to content

Commit

Permalink
Improve generated changelog (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jun 11, 2019
1 parent 58cf05b commit 61f9936
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"check": "flow check",
"check:cover": "node resources/check-cover.js && nyc report --nycrc-path .nycflowrc.yml",
"build": "node resources/build.js",
"changelog": "node resources/gen-changelog.js",
"preversion": ". ./resources/checkgit.sh",
"version": "node resources/gen-version.js && npm test",
"prepublishOnly": ". ./resources/prepublish.sh",
Expand Down
30 changes: 22 additions & 8 deletions resources/gen-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,36 @@ const labelsConfig = {
fold: true,
},
};
const lastTag = `v${packageJSON.version}`;
const GH_TOKEN = process.env['GH_TOKEN'];

if (!GH_TOKEN) {
console.error('Must provide GH_TOKEN as enviroment variable!');
process.exit(1);
}

getCommitsInfo(lastTag)
.then(genChangeLog)
getChangeLog()
.then(changelog => process.stdout.write(changelog))
.catch(error => console.error(error));

function genChangeLog(commitsInfo) {
function getChangeLog() {
const { version } = packageJSON;

let tag = null;
let commitsList = exec(`git rev-list --reverse v${version}..`);
if (commitsList === '') {
const parentPackageJSON = exec('git cat-file blob HEAD~1:package.json');
const parentVersion = JSON.parse(parentPackageJSON).version;
commitsList = exec(`git rev-list --reverse v${parentVersion}..HEAD~1`);
tag = `v${version}`;
}

const date = exec('git log -1 --format=%cd --date=short');
return getCommitsInfo(commitsList.split('\n')).then(commitsInfo =>
genChangeLog(tag, date, commitsInfo),
);
}

function genChangeLog(tag, date, commitsInfo) {
const allPRs = commitsInfoToPRs(commitsInfo);
const byLabel = {};
const commitersByLogin = {};
Expand All @@ -62,7 +78,7 @@ function genChangeLog(commitsInfo) {
commitersByLogin[pr.author.login] = pr.author;
}

let changelog = '';
let changelog = `## ${tag || 'Unreleased'} (${date})\n`;
for (const [label, config] of Object.entries(labelsConfig)) {
const prs = byLabel[label];
if (prs) {
Expand Down Expand Up @@ -239,9 +255,7 @@ function commitsInfoToPRs(commits) {
return prs;
}

async function getCommitsInfo(tag) {
const commits = exec(`git rev-list --reverse ${tag}..`).split('\n');

async function getCommitsInfo(commits) {
// Split commits into batches of 50 to prevent timeouts
const commitInfoPromises = [];
for (let i = 0; i < commits.length; i += 50) {
Expand Down

0 comments on commit 61f9936

Please sign in to comment.