2022-04-01 01:42:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Tool to list whether projects have been touched so as to need a changelog entry.
|
|
|
|
*
|
|
|
|
* Based on the tool from the Jetpack repository:
|
|
|
|
* https://github.com/Automattic/jetpack/blob/master/tools/check-changelogger-use.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.PHP.DiscouragedPHPFunctions, WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.GlobalVariablesOverride
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display usage information and exit.
|
|
|
|
*/
|
|
|
|
function usage() {
|
|
|
|
global $argv;
|
|
|
|
echo <<<EOH
|
2022-04-01 17:43:18 +00:00
|
|
|
USAGE: {$argv[0]} [--debug|-v] [--list] [-p <path>|--path=<path>] <base-ref> <head-ref>
|
2022-04-01 01:42:54 +00:00
|
|
|
Checks that a monorepo commit contains a Changelogger change entry for each
|
|
|
|
project touched.
|
2022-04-01 17:43:18 +00:00
|
|
|
--debug, -v Display verbose output.
|
|
|
|
--list Just list projects, no explanatory output.
|
|
|
|
--path=<path>, Project path to check for changed files.
|
|
|
|
-p <path>
|
|
|
|
<base-ref> Base git ref to compare for changed files.
|
|
|
|
<head-ref> Head git ref to compare for changed files.
|
2022-04-01 01:42:54 +00:00
|
|
|
EOH;
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:43:18 +00:00
|
|
|
// Options followed by a single colon have a required value.
|
|
|
|
$short_options = 'vhp:';
|
|
|
|
$long_options = array(
|
|
|
|
'debug',
|
|
|
|
'list',
|
|
|
|
'help',
|
|
|
|
'path:',
|
|
|
|
);
|
|
|
|
$options = getopt( $short_options, $long_options, $remain_index );
|
|
|
|
$arg_count = count( $argv ) - $remain_index;
|
|
|
|
|
|
|
|
if ( isset( $options['h'] ) || isset( $options['help'] ) ) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = isset( $options['l'] ) || isset( $options['list'] );
|
|
|
|
$verbose = isset( $options['v'] ) || isset( $options['debug'] );
|
|
|
|
$path = false;
|
|
|
|
if ( isset( $options['p'] ) || isset( $options['path'] ) ) {
|
|
|
|
$path = isset( $options['path'] ) ? $options['path'] : $options['p'];
|
2022-04-01 01:42:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:43:18 +00:00
|
|
|
if ( $arg_count > 2 ) {
|
|
|
|
fprintf( STDERR, "\e[1;31mToo many arguments.\e[0m\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $arg_count < 2 ) {
|
2022-04-01 01:42:54 +00:00
|
|
|
fprintf( STDERR, "\e[1;31mBase and head refs are required.\e[0m\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:43:18 +00:00
|
|
|
$base = $argv[ count( $argv ) - 2 ];
|
|
|
|
$head = $argv[ count( $argv ) - 1 ];
|
|
|
|
|
2022-04-01 01:42:54 +00:00
|
|
|
if ( $verbose ) {
|
|
|
|
/**
|
|
|
|
* Output debug info.
|
|
|
|
*
|
|
|
|
* @param array ...$args Arguments to printf. A newline is automatically appended.
|
|
|
|
*/
|
|
|
|
function debug( ...$args ) {
|
|
|
|
if ( getenv( 'CI' ) ) {
|
|
|
|
$args[0] = "\e[34m${args[0]}\e[0m\n";
|
|
|
|
} else {
|
|
|
|
$args[0] = "\e[1;30m${args[0]}\e[0m\n";
|
|
|
|
}
|
|
|
|
fprintf( STDERR, ...$args );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Do not output debug info.
|
|
|
|
*/
|
|
|
|
function debug() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$base_path = dirname( dirname( __DIR__ ) );
|
|
|
|
|
2022-06-10 20:23:14 +00:00
|
|
|
$workspace_paths = array();
|
|
|
|
$workspace_yaml = file_get_contents( $base_path . '/pnpm-workspace.yaml' );
|
|
|
|
if ( preg_match( '/^packages:((\n\s+.+)+)/', $workspace_yaml, $matches ) ) {
|
|
|
|
$packages_config = $matches[1];
|
|
|
|
if ( preg_match_all( "/^\s+-\s?'([^']+)'/m", $packages_config, $matches ) ) {
|
|
|
|
$workspace_paths = $matches[1];
|
|
|
|
}
|
2022-04-01 01:42:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 20:23:14 +00:00
|
|
|
$composer_files = array_map( function( $path ) {
|
|
|
|
return glob( $path . '/composer.json' );
|
|
|
|
}, $workspace_paths );
|
|
|
|
$composer_files = array_merge( ...$composer_files );
|
|
|
|
$composer_projects = array_map( 'dirname', $composer_files );
|
2022-04-01 01:42:54 +00:00
|
|
|
|
2022-04-01 17:43:18 +00:00
|
|
|
if ( $path && ! count( $composer_projects ) ) {
|
|
|
|
debug( sprintf( 'The provided project path, %s, did not contain a composer file.', $path ) );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
2022-04-01 01:42:54 +00:00
|
|
|
// Find projects that use changelogger, and read the relevant config.
|
|
|
|
$changelogger_projects = array();
|
|
|
|
foreach ( $composer_projects as $project_path ) {
|
|
|
|
try {
|
|
|
|
$data = json_decode( file_get_contents( $base_path . '/' . $project_path . '/composer.json' ), true, 512, JSON_THROW_ON_ERROR );
|
|
|
|
if (
|
|
|
|
! isset( $data['require']['automattic/jetpack-changelogger'] ) &&
|
|
|
|
! isset( $data['require-dev']['automattic/jetpack-changelogger'] )
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$data = isset( $data['extra']['changelogger'] ) ? $data['extra']['changelogger'] : array();
|
|
|
|
$data += array(
|
2022-04-01 02:53:12 +00:00
|
|
|
'changelog' => $project_path . '/CHANGELOG.md',
|
2022-04-01 17:43:18 +00:00
|
|
|
'changes-dir' => $project_path . '/changelog',
|
2022-04-01 01:42:54 +00:00
|
|
|
);
|
|
|
|
$changelogger_projects[ $project_path ] = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the diff.
|
|
|
|
debug( 'Checking diff from %s...%s.', $base, $head );
|
|
|
|
$pipes = null;
|
|
|
|
$p = proc_open(
|
|
|
|
sprintf( 'git -c core.quotepath=off diff --no-renames --name-only %s...%s', escapeshellarg( $base ), escapeshellarg( $head ) ),
|
|
|
|
array( array( 'pipe', 'r' ), array( 'pipe', 'w' ), STDERR ),
|
|
|
|
$pipes
|
|
|
|
);
|
|
|
|
if ( ! $p ) {
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
fclose( $pipes[0] );
|
|
|
|
|
|
|
|
$ok_projects = array();
|
|
|
|
$touched_projects = array();
|
|
|
|
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
|
|
|
|
while ( ( $line = fgets( $pipes[1] ) ) ) {
|
|
|
|
$line = trim( $line );
|
|
|
|
|
|
|
|
$project_match = false;
|
|
|
|
foreach( $composer_projects as $path ) {
|
|
|
|
if ( substr( $line, 0, strlen( $path ) + 1 ) === $path . '/' ) {
|
|
|
|
$project_match = $path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( false === $project_match ) {
|
|
|
|
debug( 'Ignoring non-project file %s.', $line );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset( $changelogger_projects[ $project_match ] ) ) {
|
|
|
|
debug( 'Ignoring file %s, project %s does not use changelogger.', $line, $project_match );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( basename( $line ) === $changelogger_projects[ $project_match ]['changelog'] ) {
|
|
|
|
debug( 'Ignoring changelog file %s.', $line );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( dirname( $line ) === $changelogger_projects[ $project_match ]['changes-dir'] ) {
|
|
|
|
if ( '.' === basename( $line )[0] ) {
|
|
|
|
debug( 'Ignoring changes dir dotfile %s.', $line );
|
|
|
|
} else {
|
|
|
|
debug( 'PR touches file %s, marking %s as having a change file.', $line, $project_match );
|
|
|
|
$ok_projects[ $project_match ] = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug( 'PR touches file %s, marking %s as touched.', $line, $project_match );
|
|
|
|
if ( ! isset( $touched_projects[ $project_match ] ) ) {
|
|
|
|
$touched_projects[ $project_match ][] = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( $pipes[1] );
|
|
|
|
$status = proc_close( $p );
|
|
|
|
if ( $status ) {
|
|
|
|
exit( $status );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output.
|
|
|
|
ksort( $touched_projects );
|
|
|
|
$exit = 0;
|
|
|
|
foreach ( $touched_projects as $slug => $files ) {
|
|
|
|
if ( empty( $ok_projects[ $slug ] ) ) {
|
|
|
|
if ( $list ) {
|
|
|
|
echo "$slug\n";
|
|
|
|
} elseif ( getenv( 'CI' ) ) {
|
|
|
|
printf( "---\n" ); // Bracket message containing newlines for better visibility in GH's logs.
|
|
|
|
printf(
|
2022-06-09 21:40:07 +00:00
|
|
|
"::error::Project %s is being changed, but no change file in %s is touched!%%0A%%0AUse `pnpm changelog --filter=%s` to add a change file.\n",
|
2022-04-01 01:42:54 +00:00
|
|
|
$slug,
|
|
|
|
"$slug/{$changelogger_projects[ $slug ]['changes-dir']}/",
|
|
|
|
$slug
|
|
|
|
);
|
|
|
|
printf( "---\n" );
|
|
|
|
$exit = 1;
|
|
|
|
} else {
|
|
|
|
printf(
|
|
|
|
"\e[1;31mProject %s is being changed, but no change file in %s is touched!\e[0m\n",
|
|
|
|
$slug,
|
|
|
|
"$slug/{$changelogger_projects[ $slug ]['changes-dir']}/"
|
|
|
|
);
|
|
|
|
$exit = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $exit && ! getenv( 'CI' ) && ! $list ) {
|
2022-06-09 21:40:07 +00:00
|
|
|
printf( "\e[32mUse `pnpm changelog --filter={project}` to add a change file for each project.\e[0m\n" );
|
2022-04-01 01:42:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 18:12:16 +00:00
|
|
|
exit( $exit );
|