* Add changelog script

* Adapt changelog script to WooCommerce Blocks

* Minor improvements
This commit is contained in:
Albert Juhé Lluveras 2019-08-19 12:46:32 +02:00 committed by GitHub
parent f70d9021fe
commit 2122d694e7
2 changed files with 145 additions and 1 deletions

View File

@ -0,0 +1,140 @@
#!/usr/bin/env node
'use strict';
/* eslint-disable */
const requestPromise = require( 'request-promise' );
const chalk = require( 'chalk' );
const octokit = require( '@octokit/rest' )();
const promptly = require( 'promptly' );
const REPO = 'woocommerce/woocommerce-gutenberg-products-block';
const headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': `token ${process.env.GH_API_TOKEN}`,
'Accept': 'application/vnd.github.inertia-preview+json',
'User-Agent': 'request'
};
const getPullRequestType = labels => {
const typeLabel = labels.find( label => label.name.includes( 'type:' ) );
if ( ! typeLabel ) {
return 'dev';
}
return typeLabel.name.replace( 'type: ', '' );
};
const isCollaborator = async ( username ) => {
return requestPromise( {
url: `https://api.github.com/orgs/woocommerce/members/${ username }`,
headers,
resolveWithFullResponse: true
} ).then( response => {
return response.statusCode === 204;
} )
.catch( err => {
if ( err.statusCode !== 404 ) {
console.log( '🤯' );
console.log( err.message );
}
});
}
const isMergedPullRequest = async ( pullRequestUrl ) => {
const options = {
url: pullRequestUrl,
headers,
json: true,
};
return requestPromise( options )
.then( data => data.merged )
.catch( err => {
console.log( '🤯' );
console.log( err.message );
});
}
const getEntry = async ( data ) => {
if ( ! data.pull_request ) {
return;
}
const isMerged = await isMergedPullRequest( data.pull_request.url );
const skipChangelog = data.labels.find( label => label.name === 'skip-changelog' );
if ( ! isMerged || skipChangelog ) {
return;
}
const collaborator = await isCollaborator( data.user.login );
const type = getPullRequestType( data.labels );
const authorTag = collaborator ? '' : `👏 @${ data.user.login }`;
let title;
if ( /### Changelog\r\n\r\n> /.test( data.body ) ) {
const bodyParts = data.body.split( '### Changelog\r\n\r\n> ' );
const note = bodyParts[ bodyParts.length - 1 ];
title = note
// Remove comment prompt
.replace( /<!---(.*)--->/gm, '' )
// Remove new lines and whitespace
.trim();
if ( ! title.length ) {
title = `${ type }: ${ data.title }`;
}
} else {
title = `${ type }: ${ data.title }`;
}
return `- ${ title } #${ data.number } ${ authorTag }`;
};
const makeChangelog = async version => {
const results = await octokit.search.issuesAndPullRequests( {
q: `milestone:${ version }+type:pr+repo:${ REPO }`,
sort: 'reactions',
per_page: 100,
} );
const entries = await Promise.all( results.data.items.map( async pr => await getEntry( pr ) ) );
if ( ! entries || ! entries.length ) {
console.log( chalk.yellow( "This version doesn't have any associated PR." ) );
return;
}
const filteredEntries = entries.filter( Boolean );
if ( ! entries || ! entries.length ) {
console.log( chalk.yellow( "None of the PRs of this version are eligible for the changelog." ) );
return;
}
console.log( filteredEntries.join( '\n' ) );
};
( async () => {
console.log( chalk.yellow( 'This program requires an api token. You can create one here: ' ) + 'https://github.com/settings/tokens' );
console.log( '' );
console.log( chalk.yellow( 'Token scope will require read permissions on public_repo, admin:org, and user.' ) );
console.log( '' );
console.log( chalk.yellow( 'Export the token as variable called GH_API_TOKEN from your bash profile.' ) );
console.log( '' );
const ready = await promptly.confirm( 'Are you ready to continue? ' );
if ( ready ) {
console.log( '' );
console.log( chalk.yellow( 'In order to generate the changelog, you will have to provide a version number to retrieve the PRs from.' ) );
console.log( '' );
console.log( chalk.yellow( 'Write it as it appears in the milestones page: ' ) + 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/milestones' );
console.log( '' );
const version = await promptly.prompt( 'Version number: ' );
console.log( '' );
console.log( chalk.green( 'Here is the generated changelog. Be sure to remove entries ' +
'not intended for a WooCommerce Blocks release.' ) );
console.log( '' );
makeChangelog( version );
} else {
console.log( '' );
console.log( chalk.yellow( 'Ok, see you soon.' ) );
console.log( '' );
}
} )();

View File

@ -33,11 +33,13 @@
"test:help": "wp-scripts test-unit-js --help",
"test:update": "wp-scripts test-unit-js --updateSnapshot --config tests/js/jest.config.json",
"test:watch": "npm run test -- --watch",
"size-check": "bundlesize"
"size-check": "bundlesize",
"changelog": "node ./bin/changelog.js "
},
"devDependencies": {
"@babel/core": "7.5.5",
"@woocommerce/navigation": "2.1.1",
"@octokit/rest": "^16.28.7",
"@wordpress/babel-preset-default": "4.4.0",
"@wordpress/blocks": "6.5.0",
"@wordpress/browserslist-config": "2.6.0",
@ -81,7 +83,9 @@
"po2json": "1.0.0-alpha",
"postcss-loader": "3.0.0",
"progress-bar-webpack-plugin": "1.12.1",
"promptly": "^3.0.3",
"react-test-renderer": "16.8.6",
"request-promise": "^4.2.4",
"rimraf": "2.7.1",
"sass-loader": "7.2.0",
"style-loader": "1.0.0",