|
5 | 5 | use WP_CLI\Utils; |
6 | 6 |
|
7 | 7 | /** |
8 | | - * Checks the locked dependencies of the repositories that commit a |
| 8 | + * Checks and updates the locked dependencies of the repositories that commit a |
9 | 9 | * composer.lock, such as wp-cli/wp-cli-bundle. |
10 | 10 | */ |
11 | 11 | final class Dependencies_Command { |
@@ -114,6 +114,182 @@ public function audit( $args, $assoc_args ) { |
114 | 114 | } |
115 | 115 | } |
116 | 116 |
|
| 117 | + /** |
| 118 | + * Updates locked dependencies and proposes the new composer.lock as a pull request. |
| 119 | + * |
| 120 | + * By default, updates the packages that `wp maintenance dependencies |
| 121 | + * audit` reports security advisories for. The update runs within the |
| 122 | + * constraints of composer.json, which stays unchanged, and moves the |
| 123 | + * dependencies of the updated packages only as far as needed. |
| 124 | + * |
| 125 | + * The new composer.lock is audited again, then committed on top of the |
| 126 | + * default branch, and a pull request is opened from that commit. The branch |
| 127 | + * is named after the update: `update-dependencies-security` by default, |
| 128 | + * and after the packages with --packages. A later run of the same update |
| 129 | + * replaces that commit and refreshes the pull request, but never |
| 130 | + * overwrites a branch that someone else pushed to. |
| 131 | + * |
| 132 | + * Without --packages, exits with an error code when advisories are left |
| 133 | + * that the update cannot fix, as fixing them needs a change to |
| 134 | + * composer.json. For each of them, it says which version fixes it, and |
| 135 | + * what `composer why-not` says holds that version back, both here and in |
| 136 | + * the pull request. |
| 137 | + * |
| 138 | + * Needs Composer 2.7 or later. Works from the files on GitHub, so no clone |
| 139 | + * of the repository is needed. |
| 140 | + * |
| 141 | + * ## OPTIONS |
| 142 | + * |
| 143 | + * [<repo>...] |
| 144 | + * : Name(s) of the repository to update. If no user/org was provided, |
| 145 | + * 'wp-cli' org is assumed. Defaults to wp-cli/wp-cli-bundle. Repositories |
| 146 | + * without a composer.lock are skipped with a warning. |
| 147 | + * |
| 148 | + * [--packages=<packages>] |
| 149 | + * : Comma-separated list of packages to update instead, with their |
| 150 | + * dependencies. `*` matches any characters, so `symfony/*` updates all |
| 151 | + * locked Symfony packages and `*` refreshes the whole lock. |
| 152 | + * |
| 153 | + * [--dry-run] |
| 154 | + * : Show what the update changes, without pushing anything. |
| 155 | + * |
| 156 | + * [--yes] |
| 157 | + * : Push and open the pull request without asking for confirmation. |
| 158 | + * |
| 159 | + * ## EXAMPLES |
| 160 | + * |
| 161 | + * # Fix what the audit reports. |
| 162 | + * $ wp maintenance dependencies update --dry-run |
| 163 | + * +--------------------+---------+---------+-----+ |
| 164 | + * | package | from | to | dev | |
| 165 | + * +--------------------+---------+---------+-----+ |
| 166 | + * | symfony/config | v5.4.46 | v5.4.56 | yes | |
| 167 | + * | symfony/filesystem | v5.4.54 | v5.4.56 | yes | |
| 168 | + * | symfony/yaml | v5.4.54 | v5.4.56 | yes | |
| 169 | + * +--------------------+---------+---------+-----+ |
| 170 | + * Fixes 3 security advisories. |
| 171 | + * Dry run: not pushing the new composer.lock of wp-cli/wp-cli-bundle. |
| 172 | + * |
| 173 | + * # Move the bundle to the latest commit of the framework. |
| 174 | + * $ wp maintenance dependencies update --packages=wp-cli/wp-cli --yes |
| 175 | + * |
| 176 | + * @when before_wp_load |
| 177 | + */ |
| 178 | + public function update( $args, $assoc_args ) { |
| 179 | + $dry_run = (bool) Utils\get_flag_value( $assoc_args, 'dry-run', false ); |
| 180 | + $confirmed = (bool) Utils\get_flag_value( $assoc_args, 'yes', false ); |
| 181 | + $packages = array_values( array_filter( array_map( 'trim', explode( ',', (string) Utils\get_flag_value( $assoc_args, 'packages', '' ) ) ), 'strlen' ) ); |
| 182 | + $repos = $args ? array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $args ) ) ) : [ Bundle::REPO ]; |
| 183 | + |
| 184 | + foreach ( $packages as $package ) { |
| 185 | + if ( '-' === $package[0] ) { |
| 186 | + WP_CLI::error( "'{$package}' is not a package name." ); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + $updated = false; |
| 191 | + $still_found = []; |
| 192 | + foreach ( $repos as $repo ) { |
| 193 | + $plan = Dependency_Update::prepare( $repo, $packages ); |
| 194 | + |
| 195 | + if ( null === $plan ) { |
| 196 | + WP_CLI::warning( "{$repo} has no composer.lock, skipping it." ); |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + $updated = true; |
| 201 | + |
| 202 | + if ( $plan['remaining'] ) { |
| 203 | + $still_found[] = $repo; |
| 204 | + } |
| 205 | + |
| 206 | + foreach ( $plan['unmatched'] as $pattern ) { |
| 207 | + WP_CLI::warning( "{$pattern} matches no package locked in {$repo}." ); |
| 208 | + } |
| 209 | + |
| 210 | + if ( ! $plan['packages'] ) { |
| 211 | + if ( ! $plan['requested'] ) { |
| 212 | + WP_CLI::success( "No locked package of {$repo} has a security advisory, so there is nothing to update." ); |
| 213 | + } |
| 214 | + continue; |
| 215 | + } |
| 216 | + |
| 217 | + if ( ! $plan['changes'] ) { |
| 218 | + WP_CLI::warning( 'Updating ' . implode( ', ', $plan['packages'] ) . " changes no locked version of {$repo}." ); |
| 219 | + $this->report_remaining( $plan ); |
| 220 | + continue; |
| 221 | + } |
| 222 | + |
| 223 | + Utils\format_items( |
| 224 | + 'table', |
| 225 | + array_map( |
| 226 | + static function ( $change ) { |
| 227 | + return [ |
| 228 | + 'package' => $change['package'], |
| 229 | + 'from' => null === $change['from'] ? '(new)' : $change['from'], |
| 230 | + 'to' => null === $change['to'] ? '(removed)' : $change['to'], |
| 231 | + 'dev' => $change['dev'] ? 'yes' : 'no', |
| 232 | + ]; |
| 233 | + }, |
| 234 | + $plan['changes'] |
| 235 | + ), |
| 236 | + [ 'package', 'from', 'to', 'dev' ] |
| 237 | + ); |
| 238 | + |
| 239 | + if ( $plan['fixed'] ) { |
| 240 | + WP_CLI::log( sprintf( 'Fixes %d security %s.', count( $plan['fixed'] ), 1 === count( $plan['fixed'] ) ? 'advisory' : 'advisories' ) ); |
| 241 | + } |
| 242 | + $this->report_remaining( $plan ); |
| 243 | + |
| 244 | + if ( $dry_run ) { |
| 245 | + WP_CLI::log( "Dry run: not pushing the new composer.lock of {$repo}." ); |
| 246 | + continue; |
| 247 | + } |
| 248 | + |
| 249 | + if ( ! $confirmed && ! Prompt::confirm( "Push the new composer.lock to the {$plan['head']} branch of {$repo} and open a pull request?" ) ) { |
| 250 | + WP_CLI::log( "Skipping {$repo}." ); |
| 251 | + continue; |
| 252 | + } |
| 253 | + |
| 254 | + list( $pull_request, $pushed ) = Dependency_Update::publish( $plan ); |
| 255 | + |
| 256 | + if ( ! $pushed ) { |
| 257 | + WP_CLI::log( "The {$plan['head']} branch already holds this composer.lock, so only the pull request was refreshed." ); |
| 258 | + } |
| 259 | + WP_CLI::success( "Pull request for {$repo}: {$pull_request->html_url}" ); |
| 260 | + } |
| 261 | + |
| 262 | + if ( ! $updated ) { |
| 263 | + WP_CLI::error( 'None of the repositories has a composer.lock to update.' ); |
| 264 | + } |
| 265 | + |
| 266 | + // An explicit list of packages may leave advisories alone on purpose. |
| 267 | + if ( $still_found && ! $packages ) { |
| 268 | + WP_CLI::error( 'Locked packages are still affected by security advisories in ' . implode( ', ', $still_found ) . '. Fixing them needs a change to composer.json.' ); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + private function report_remaining( array $plan ) { |
| 273 | + foreach ( $plan['remaining'] as $advisory ) { |
| 274 | + $message = "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}"; |
| 275 | + |
| 276 | + if ( ! empty( $advisory['fixed_in'] ) ) { |
| 277 | + $message .= "\nIt is fixed in {$advisory['fixed_in']}"; |
| 278 | + |
| 279 | + if ( ! empty( $advisory['held_back_by'] ) ) { |
| 280 | + $message .= ', which is held back by:'; |
| 281 | + foreach ( $advisory['held_back_by'] as $reason ) { |
| 282 | + $message .= "\n {$reason}"; |
| 283 | + } |
| 284 | + } else { |
| 285 | + $message .= '.'; |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + WP_CLI::warning( $message ); |
| 290 | + } |
| 291 | + } |
| 292 | + |
117 | 293 | private function render_table( array $audits ) { |
118 | 294 | $rows = []; |
119 | 295 | foreach ( $audits as $audit ) { |
|
0 commit comments