|
| 1 | +<?php namespace WP_CLI\Maintenance; |
| 2 | + |
| 3 | +use WP_CLI; |
| 4 | +use WP_CLI\Utils; |
| 5 | + |
| 6 | +/** |
| 7 | + * What a wp-cli/wp-cli-bundle release ships, read from its composer.lock. |
| 8 | + */ |
| 9 | +final class Bundle { |
| 10 | + |
| 11 | + const REPO = 'wp-cli/wp-cli-bundle'; |
| 12 | + |
| 13 | + /** |
| 14 | + * Repositories whose milestones follow the bundle release itself rather |
| 15 | + * than their own package releases. |
| 16 | + */ |
| 17 | + const RELEASE_REPOS = [ |
| 18 | + 'wp-cli/wp-cli-bundle', |
| 19 | + 'wp-cli/wp-cli', |
| 20 | + 'wp-cli/handbook', |
| 21 | + ]; |
| 22 | + |
| 23 | + /** |
| 24 | + * Bundled packages that are not named `*-command`. |
| 25 | + */ |
| 26 | + const OTHER_PACKAGES = [ |
| 27 | + 'wp-cli/wp-cli-tests', |
| 28 | + 'wp-cli/regenerate-readme', |
| 29 | + 'wp-cli/autoload-splitter', |
| 30 | + 'wp-cli/wp-config-transformer', |
| 31 | + 'wp-cli/php-cli-tools', |
| 32 | + 'wp-cli/spyc', |
| 33 | + ]; |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the milestone of a release repository that belongs to a bundle |
| 37 | + * release: the one titled after the release, or the lowest open one when |
| 38 | + * no release is given. |
| 39 | + * |
| 40 | + * @param string $repo Repository name. |
| 41 | + * @param string|null $release Bundle release version, e.g. '3.0.0'. |
| 42 | + * |
| 43 | + * @return object|null |
| 44 | + */ |
| 45 | + public static function get_release_milestone( $repo, $release = null ) { |
| 46 | + if ( $release ) { |
| 47 | + $release = ltrim( $release, 'v' ); |
| 48 | + |
| 49 | + foreach ( GitHub::get_project_milestones( $repo, [ 'state' => 'all' ] ) as $milestone ) { |
| 50 | + if ( ltrim( $milestone->title, 'v' ) === $release ) { |
| 51 | + return $milestone; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + WP_CLI::warning( "Couldn't find milestone '{$release}' in repository '{$repo}'." ); |
| 56 | + |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return array_reduce( |
| 61 | + GitHub::get_project_milestones( $repo ), |
| 62 | + static function ( $lowest, $milestone ) { |
| 63 | + if ( null === $lowest ) { |
| 64 | + return $milestone; |
| 65 | + } |
| 66 | + |
| 67 | + return version_compare( $milestone->title, $lowest->title, '<' ) ? $milestone : $lowest; |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Gets the ref and composer.lock that describe the packages of a bundle |
| 74 | + * release. |
| 75 | + * |
| 76 | + * @param string|null $release Bundle release version, e.g. '3.0.0'. Its |
| 77 | + * tag is used when it exists already. |
| 78 | + * @param string|null $bundle_ref Explicit ref; wins over the release tag. |
| 79 | + * |
| 80 | + * @return array{0: string, 1: array} Ref and decoded lockfile. |
| 81 | + */ |
| 82 | + public static function get_release_lock( $release = null, $bundle_ref = null ) { |
| 83 | + if ( $bundle_ref ) { |
| 84 | + return [ $bundle_ref, self::get_lock( $bundle_ref ) ]; |
| 85 | + } |
| 86 | + |
| 87 | + if ( $release ) { |
| 88 | + $tag = 'v' . ltrim( $release, 'v' ); |
| 89 | + $lock = self::get_lock( $tag, false ); |
| 90 | + |
| 91 | + if ( $lock ) { |
| 92 | + return [ $tag, $lock ]; |
| 93 | + } |
| 94 | + |
| 95 | + WP_CLI::debug( "Tag {$tag} does not exist in " . self::REPO . ' yet, using the default branch.', 'bundle' ); |
| 96 | + } |
| 97 | + |
| 98 | + $branch = GitHub::get_default_branch( self::REPO ); |
| 99 | + |
| 100 | + return [ $branch, self::get_lock( $branch ) ]; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the ref and composer.lock of the bundle release before the given |
| 105 | + * one, i.e. the highest closed milestone below it. |
| 106 | + * |
| 107 | + * @param string|null $release Bundle release version, e.g. '3.0.0'. Without |
| 108 | + * it, the highest closed milestone is used. |
| 109 | + * |
| 110 | + * @return array{0: string|null, 1: array|null} Ref and decoded lockfile, |
| 111 | + * both null without a |
| 112 | + * previous release. |
| 113 | + */ |
| 114 | + public static function get_previous_release_lock( $release = null ) { |
| 115 | + $release = $release ? ltrim( $release, 'v' ) : null; |
| 116 | + $previous = null; |
| 117 | + |
| 118 | + foreach ( GitHub::get_project_milestones( self::REPO, [ 'state' => 'closed' ] ) as $milestone ) { |
| 119 | + $title = ltrim( $milestone->title, 'v' ); |
| 120 | + |
| 121 | + if ( $release && ! version_compare( $title, $release, '<' ) ) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + if ( null === $previous || version_compare( $title, $previous, '>' ) ) { |
| 126 | + $previous = $title; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if ( null === $previous ) { |
| 131 | + return [ null, null ]; |
| 132 | + } |
| 133 | + |
| 134 | + $tag = "v{$previous}"; |
| 135 | + |
| 136 | + return [ $tag, self::get_lock( $tag ) ]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Fetches and decodes the composer.lock of wp-cli/wp-cli-bundle at a ref. |
| 141 | + * |
| 142 | + * @param string $ref Branch, tag or commit. |
| 143 | + * @param bool $throw_errors Whether a missing lockfile is fatal. |
| 144 | + * |
| 145 | + * @return array|false |
| 146 | + */ |
| 147 | + public static function get_lock( $ref, $throw_errors = true ) { |
| 148 | + $url = sprintf( 'https://raw.githubusercontent.com/%s/%s/composer.lock', self::REPO, $ref ); |
| 149 | + $response = Utils\http_request( 'GET', $url ); |
| 150 | + |
| 151 | + if ( 200 !== $response->status_code ) { |
| 152 | + if ( ! $throw_errors ) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) ); |
| 157 | + } |
| 158 | + |
| 159 | + return json_decode( $response->body, true ); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Lists the bundled packages of a lockfile with their versions. |
| 164 | + * |
| 165 | + * @param array $lock Decoded composer.lock. |
| 166 | + * |
| 167 | + * @return array<string, string> Package name => version without leading 'v', |
| 168 | + * sorted by name. |
| 169 | + */ |
| 170 | + public static function get_packages( array $lock ) { |
| 171 | + $packages = []; |
| 172 | + |
| 173 | + foreach ( [ 'packages', 'packages-dev' ] as $section ) { |
| 174 | + if ( empty( $lock[ $section ] ) ) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + foreach ( $lock[ $section ] as $package ) { |
| 179 | + if ( ! self::is_bundled_package( $package['name'] ) ) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + $packages[ $package['name'] ] = ltrim( $package['version'], 'v' ); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + ksort( $packages ); |
| 188 | + |
| 189 | + return $packages; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Gets the closed milestones of a package that shipped in a bundle |
| 194 | + * release: newer than the version the previous release locked, and not |
| 195 | + * newer than the version this release locks. A package without a |
| 196 | + * previous version is newly bundled, so all of its releases count. |
| 197 | + * |
| 198 | + * @param string $package Package name. |
| 199 | + * @param string|null $previous_version Version locked by the previous release. |
| 200 | + * @param string $current_version Version locked by this release. |
| 201 | + * |
| 202 | + * @return array |
| 203 | + */ |
| 204 | + public static function get_shipped_milestones( $package, $previous_version, $current_version ) { |
| 205 | + $milestones = GitHub::get_project_milestones( $package, [ 'state' => 'closed' ] ); |
| 206 | + |
| 207 | + return array_values( |
| 208 | + array_filter( |
| 209 | + $milestones, |
| 210 | + static function ( $milestone ) use ( $previous_version, $current_version ) { |
| 211 | + $title = ltrim( $milestone->title, 'v' ); |
| 212 | + |
| 213 | + if ( ! self::is_numeric_version( $title ) ) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + if ( self::is_numeric_version( $previous_version ) |
| 218 | + && ! version_compare( $title, $previous_version, '>' ) ) { |
| 219 | + return false; |
| 220 | + } |
| 221 | + |
| 222 | + if ( self::is_numeric_version( $current_version ) |
| 223 | + && version_compare( $title, $current_version, '>' ) ) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + return true; |
| 228 | + } |
| 229 | + ) |
| 230 | + ); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Checks whether a package's contributors and release notes belong to a |
| 235 | + * bundle release. |
| 236 | + * |
| 237 | + * @param string $name Package name. |
| 238 | + * |
| 239 | + * @return bool |
| 240 | + */ |
| 241 | + public static function is_bundled_package( $name ) { |
| 242 | + return (bool) preg_match( '#^wp-cli/.+-command$#', $name ) |
| 243 | + || in_array( $name, self::OTHER_PACKAGES, true ); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Checks whether a version can be compared, as opposed to `dev-main`. |
| 248 | + * |
| 249 | + * @param string|null $version Version string, or null when unknown. |
| 250 | + * |
| 251 | + * @return bool |
| 252 | + */ |
| 253 | + private static function is_numeric_version( $version ) { |
| 254 | + return null !== $version && (bool) preg_match( '/^\d+(\.\d+)*/', $version ); |
| 255 | + } |
| 256 | +} |
0 commit comments