Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions .maintenance/src/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php namespace WP_CLI\Maintenance;

use WP_CLI;
use WP_CLI\Process;
use WP_CLI\Utils;

/**
* Runs Composer against the composer.json and composer.lock of a repository,
* in a throwaway directory and without installing anything.
*/
final class Composer {

/**
* Environment variables that would change what Composer checks, resolves
* or reports, such as COMPOSER_NO_DEV dropping the dev packages, or
* COMPOSER pointing it at another project.
*/
const ENV_OVERRIDES = [
'COMPOSER',
'COMPOSER_AUDIT_ABANDONED',
'COMPOSER_AUTH',
'COMPOSER_IGNORE_PLATFORM_REQ',
'COMPOSER_IGNORE_PLATFORM_REQS',
'COMPOSER_MINIMAL_CHANGES',
'COMPOSER_NO_AUDIT',
'COMPOSER_NO_DEV',
'COMPOSER_NO_SECURITY_BLOCKING',
'COMPOSER_PREFER_LOWEST',
'COMPOSER_PREFER_STABLE',
'COMPOSER_WITH_ALL_DEPENDENCIES',
'COMPOSER_WITH_DEPENDENCIES',
];

/**
* Runs a Composer command in a temporary directory holding the given
* files. Plugins and scripts stay disabled, so the repository's code
* never runs.
*
* @param string[] $arguments Command and its arguments, such
* as [ 'audit', '--locked' ].
* @param array<string, string> $files File name => contents.
* @param string[] $read_back Files to read back afterwards.
* @param array<string, string> $env Extra environment variables.
*
* @return array{return_code: int, stdout: string, stderr: string, files: array<string, string|null>}
*/
public static function run( array $arguments, array $files, array $read_back = [], array $env = [] ) {
$dir = Utils\get_temp_dir() . uniqid( 'wp-cli-maintenance-composer-', true );

$written = mkdir( $dir, 0700 ) && mkdir( "{$dir}/home", 0700 );
foreach ( $files as $name => $contents ) {
$written = $written && false !== file_put_contents( "{$dir}/{$name}", $contents );
}

if ( ! $written ) {
self::remove_directory( $dir );
WP_CLI::error( "Could not write the files for Composer to the temporary directory {$dir}." );
}

$arguments = array_merge(
$arguments,
[ '--no-interaction', '--no-plugins', '--no-scripts', "--working-dir={$dir}" ]
);

$command = 'composer ' . implode( ' ', array_map( 'escapeshellarg', $arguments ) );
$auth = GitHub::get_auth();
$result = Process::create( $command, null, array_merge( self::get_env( (array) getenv(), "{$dir}/home", $auth['token'] ), $env ) )->run();

$contents = [];
foreach ( $read_back as $name ) {
$contents[ $name ] = is_file( "{$dir}/{$name}" ) ? (string) file_get_contents( "{$dir}/{$name}" ) : null;
}

self::remove_directory( $dir );

if ( '' !== trim( $result->stderr ) ) {
WP_CLI::debug( trim( $result->stderr ), 'composer' );
}

return [
'return_code' => (int) $result->return_code,
'stdout' => (string) $result->stdout,
'stderr' => (string) $result->stderr,
'files' => $contents,
];
}

/**
* Builds the environment for Composer: the current one, without the
* variables that change its behavior, and with a Composer home of its
* own, so that no global configuration, such as `audit.ignore` or extra
* repositories, applies.
*
* @param array<string, string> $env Current environment.
* @param string $composer_home Empty directory to use as
* Composer home.
* @param string|null $github_token Token to raise GitHub's API
* rate limit for Composer.
*
* @return array<string, string>
*/
public static function get_env( array $env, $composer_home, $github_token = null ) {
foreach ( self::ENV_OVERRIDES as $variable ) {
unset( $env[ $variable ] );
}

$env['COMPOSER_HOME'] = $composer_home;

if ( $github_token ) {
$env['COMPOSER_AUTH'] = (string) json_encode( [ 'github-oauth' => [ 'github.com' => $github_token ] ] );
}

return $env;
}

private static function remove_directory( $dir ) {
if ( ! is_dir( $dir ) ) {
return;
}

$entries = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ( $entries as $entry ) {
if ( $entry->isDir() && ! $entry->isLink() ) {
rmdir( $entry->getPathname() );
} else {
unlink( $entry->getPathname() );
}
}

rmdir( $dir );
}
}
178 changes: 177 additions & 1 deletion .maintenance/src/Dependencies_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use WP_CLI\Utils;

/**
* Checks the locked dependencies of the repositories that commit a
* Checks and updates the locked dependencies of the repositories that commit a
* composer.lock, such as wp-cli/wp-cli-bundle.
*/
final class Dependencies_Command {
Expand Down Expand Up @@ -114,6 +114,182 @@ public function audit( $args, $assoc_args ) {
}
}

/**
* Updates locked dependencies and proposes the new composer.lock as a pull request.
*
* By default, updates the packages that `wp maintenance dependencies
* audit` reports security advisories for. The update runs within the
* constraints of composer.json, which stays unchanged, and moves the
* dependencies of the updated packages only as far as needed.
*
* The new composer.lock is audited again, then committed on top of the
* default branch, and a pull request is opened from that commit. The branch
* is named after the update: `update-dependencies-security` by default,
* and after the packages with --packages. A later run of the same update
* replaces that commit and refreshes the pull request, but never
* overwrites a branch that someone else pushed to.
*
* Without --packages, exits with an error code when advisories are left
* that the update cannot fix, as fixing them needs a change to
* composer.json. For each of them, it says which version fixes it, and
* what `composer why-not` says holds that version back, both here and in
* the pull request.
*
* Needs Composer 2.7 or later. Works from the files on GitHub, so no clone
* of the repository is needed.
*
* ## OPTIONS
*
* [<repo>...]
* : Name(s) of the repository to update. If no user/org was provided,
* 'wp-cli' org is assumed. Defaults to wp-cli/wp-cli-bundle. Repositories
* without a composer.lock are skipped with a warning.
*
* [--packages=<packages>]
* : Comma-separated list of packages to update instead, with their
* dependencies. `*` matches any characters, so `symfony/*` updates all
* locked Symfony packages and `*` refreshes the whole lock.
*
* [--dry-run]
* : Show what the update changes, without pushing anything.
*
* [--yes]
* : Push and open the pull request without asking for confirmation.
*
* ## EXAMPLES
*
* # Fix what the audit reports.
* $ wp maintenance dependencies update --dry-run
* +--------------------+---------+---------+-----+
* | package | from | to | dev |
* +--------------------+---------+---------+-----+
* | symfony/config | v5.4.46 | v5.4.56 | yes |
* | symfony/filesystem | v5.4.54 | v5.4.56 | yes |
* | symfony/yaml | v5.4.54 | v5.4.56 | yes |
* +--------------------+---------+---------+-----+
* Fixes 3 security advisories.
* Dry run: not pushing the new composer.lock of wp-cli/wp-cli-bundle.
*
* # Move the bundle to the latest commit of the framework.
* $ wp maintenance dependencies update --packages=wp-cli/wp-cli --yes
*
* @when before_wp_load
*/
public function update( $args, $assoc_args ) {
$dry_run = (bool) Utils\get_flag_value( $assoc_args, 'dry-run', false );
$confirmed = (bool) Utils\get_flag_value( $assoc_args, 'yes', false );
$packages = array_values( array_filter( array_map( 'trim', explode( ',', (string) Utils\get_flag_value( $assoc_args, 'packages', '' ) ) ), 'strlen' ) );
$repos = $args ? array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $args ) ) ) : [ Bundle::REPO ];

foreach ( $packages as $package ) {
if ( '-' === $package[0] ) {
WP_CLI::error( "'{$package}' is not a package name." );
}
}

$updated = false;
$still_found = [];
foreach ( $repos as $repo ) {
$plan = Dependency_Update::prepare( $repo, $packages );

if ( null === $plan ) {
WP_CLI::warning( "{$repo} has no composer.lock, skipping it." );
continue;
}

$updated = true;

if ( $plan['remaining'] ) {
$still_found[] = $repo;
}

foreach ( $plan['unmatched'] as $pattern ) {
WP_CLI::warning( "{$pattern} matches no package locked in {$repo}." );
}

if ( ! $plan['packages'] ) {
if ( ! $plan['requested'] ) {
WP_CLI::success( "No locked package of {$repo} has a security advisory, so there is nothing to update." );
}
continue;
}

if ( ! $plan['changes'] ) {
WP_CLI::warning( 'Updating ' . implode( ', ', $plan['packages'] ) . " changes no locked version of {$repo}." );
$this->report_remaining( $plan );
continue;
}

Utils\format_items(
'table',
array_map(
static function ( $change ) {
return [
'package' => $change['package'],
'from' => null === $change['from'] ? '(new)' : $change['from'],
'to' => null === $change['to'] ? '(removed)' : $change['to'],
'dev' => $change['dev'] ? 'yes' : 'no',
];
},
$plan['changes']
),
[ 'package', 'from', 'to', 'dev' ]
);

if ( $plan['fixed'] ) {
WP_CLI::log( sprintf( 'Fixes %d security %s.', count( $plan['fixed'] ), 1 === count( $plan['fixed'] ) ? 'advisory' : 'advisories' ) );
}
$this->report_remaining( $plan );

if ( $dry_run ) {
WP_CLI::log( "Dry run: not pushing the new composer.lock of {$repo}." );
continue;
}

if ( ! $confirmed && ! Prompt::confirm( "Push the new composer.lock to the {$plan['head']} branch of {$repo} and open a pull request?" ) ) {
WP_CLI::log( "Skipping {$repo}." );
continue;
}

list( $pull_request, $pushed ) = Dependency_Update::publish( $plan );

if ( ! $pushed ) {
WP_CLI::log( "The {$plan['head']} branch already holds this composer.lock, so only the pull request was refreshed." );
}
WP_CLI::success( "Pull request for {$repo}: {$pull_request->html_url}" );
}

if ( ! $updated ) {
WP_CLI::error( 'None of the repositories has a composer.lock to update.' );
}

// An explicit list of packages may leave advisories alone on purpose.
if ( $still_found && ! $packages ) {
WP_CLI::error( 'Locked packages are still affected by security advisories in ' . implode( ', ', $still_found ) . '. Fixing them needs a change to composer.json.' );
}
}

private function report_remaining( array $plan ) {
foreach ( $plan['remaining'] as $advisory ) {
$message = "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}";

if ( ! empty( $advisory['fixed_in'] ) ) {
$message .= "\nIt is fixed in {$advisory['fixed_in']}";

if ( ! empty( $advisory['held_back_by'] ) ) {
$message .= ', which is held back by:';
foreach ( $advisory['held_back_by'] as $reason ) {
$message .= "\n {$reason}";
}
} else {
$message .= '.';
}
}

WP_CLI::warning( $message );
}
}

private function render_table( array $audits ) {
$rows = [];
foreach ( $audits as $audit ) {
Expand Down
Loading
Loading