From 5509cc4e3bf753bfa18d0dd8b1612ef6160fcc54 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 7 Feb 2019 12:37:58 -0500 Subject: [PATCH] Tooling; Consider previous commits in CHANGELOG validation --- bin/check-changelog.js | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/bin/check-changelog.js b/bin/check-changelog.js index fb3c1482aa146..b8f7196c37045 100644 --- a/bin/check-changelog.js +++ b/bin/check-changelog.js @@ -1,20 +1,7 @@ /** - * Given a set of file paths as arguments, checks whether the file paths also - * include a requisite CHANGELOG entry. The file paths should include packages - * maintained with a CHANGELOG at, e.g. `packages/i18n/CHANGELOG.md`. - * - * Example (Error): - * - * ``` - * node bin/check-changelog.js /Gutenberg/packages/i18n/src/index.js - * ``` - * - * Example (Success): - * - * ``` - * node bin/check-changelog.js /Gutenberg/packages/i18n/src/index.js \ - * /Gutenberg/packages/i18n/CHANGELOG.md - * ``` + * Optionally given a set of file paths as arguments, checks whether all files + * modified on the current branch (including those passed as arguments) include + * a requisite CHANGELOG entry. */ /** @@ -23,6 +10,8 @@ const { relative, sep, join } = require( 'path' ); const minimatch = require( 'minimatch' ); +const { execSync } = require( 'child_process' ); +const { flow, toString, trim } = require( 'lodash' ); /** * Internal dependencies @@ -30,6 +19,15 @@ const minimatch = require( 'minimatch' ); const { ignoreChanges = [] } = require( '../lerna' ); +/** + * Utility function to trim the result of an execSync string result. + * + * @param {string} command Command to exec. + * + * @return {string} Result of exec. + */ +const exec = flow( execSync, toString, trim ); + /** * Returns true if the given path is ignored by the Lerna `ignoreChanges` * configuration, or false otherwise. @@ -42,14 +40,32 @@ function isIgnored( path ) { return ignoreChanges.some( ( pattern ) => minimatch( path, pattern ) ); } +/** + * Merge base commit SHA on which to perform git file diff. + * + * @type {string} + */ +const base = exec( 'git merge-base master HEAD' ); + +/** + * Set of files to consider as changed, as a combination of those already + * changed on the current branch and those passed in as file arguments. + * + * @type {string[]} + */ +const files = [ + ...exec( `git diff --name-only ${ base }` ).split( '\n' ), + ...process.argv.slice( 2 ), +]; + /** * An array of file paths relative the `packages` directory. * * @type {string[]} */ -const paths = process.argv - .slice( 2 ) - .map( ( path ) => relative( 'packages', path ) ); +const paths = files + .map( ( path ) => relative( 'packages', path ) ) + .filter( ( path ) => path[ 0 ] !== '.' ); /** * The set of verified package names, i.e. those which have been confirmed to