Skip to content

Commit bc43aca

Browse files
swissspidyclaude
andcommitted
Harden the bundle lookups and cover them with unit tests
Review follow-ups on #76: - Only a 404 counts as "the tag has no lockfile yet". Any other failure (rate limit, outage) is fatal, so a bad response can no longer make the commands silently fall back to the default branch. - Milestone titles that are not versions are ignored when picking the previous bundle release, not just when filtering package milestones, so a stray closed milestone cannot turn into a bogus tag. The version check is anchored and accepts a pre-release suffix. - The Spyc package is `wp-cli/mustangostang-spyc`, which is the name in the bundle lock; `wp-cli/spyc` never matched anything. - `close-released --bundle` now reads the same package list, which also brings in `packages-dev` (package-command, wp-cli-tests). The selection logic is split into pure functions (`find_previous_release`, `filter_shipped_milestones`, `is_version`) with PHPUnit coverage; the org testing workflow picks the tests up through phpunit.xml.dist. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016VDHkafhoHtgAPuYkvgvqh
1 parent 9dd2f6f commit bc43aca

6 files changed

Lines changed: 305 additions & 60 deletions

File tree

‎.github/workflows/testing.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Testing
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
test:
12+
uses: wp-cli/.github/.github/workflows/reusable-testing.yml@main

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
!/.github/
55
!/.maintenance/
66
!/.readme-partials/
7+
!/tests/
78

89
composer.lock
910
*.log

‎.maintenance/src/Bundle.php‎

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class Bundle {
2929
'wp-cli/autoload-splitter',
3030
'wp-cli/wp-config-transformer',
3131
'wp-cli/php-cli-tools',
32-
'wp-cli/spyc',
32+
'wp-cli/mustangostang-spyc',
3333
];
3434

3535
/**
@@ -112,12 +112,42 @@ public static function get_release_lock( $release = null, $bundle_ref = null ) {
112112
* previous release.
113113
*/
114114
public static function get_previous_release_lock( $release = null ) {
115+
$previous = self::find_previous_release(
116+
GitHub::get_project_milestones( self::REPO, [ 'state' => 'closed' ] ),
117+
$release
118+
);
119+
120+
if ( null === $previous ) {
121+
return [ null, null ];
122+
}
123+
124+
$tag = "v{$previous}";
125+
126+
return [ $tag, self::get_lock( $tag ) ];
127+
}
128+
129+
/**
130+
* Picks the highest release version below the given one out of a list of
131+
* milestones. Milestones whose title is not a version are ignored.
132+
*
133+
* @param array $milestones Milestone objects with a `title`.
134+
* @param string|null $release Bundle release version, e.g. '3.0.0'.
135+
* Without it, the highest version wins.
136+
*
137+
* @return string|null Version without leading 'v', or null when there is
138+
* no release below the given one.
139+
*/
140+
public static function find_previous_release( array $milestones, $release = null ) {
115141
$release = $release ? ltrim( $release, 'v' ) : null;
116142
$previous = null;
117143

118-
foreach ( GitHub::get_project_milestones( self::REPO, [ 'state' => 'closed' ] ) as $milestone ) {
144+
foreach ( $milestones as $milestone ) {
119145
$title = ltrim( $milestone->title, 'v' );
120146

147+
if ( ! self::is_version( $title ) ) {
148+
continue;
149+
}
150+
121151
if ( $release && ! version_compare( $title, $release, '<' ) ) {
122152
continue;
123153
}
@@ -127,32 +157,30 @@ public static function get_previous_release_lock( $release = null ) {
127157
}
128158
}
129159

130-
if ( null === $previous ) {
131-
return [ null, null ];
132-
}
133-
134-
$tag = "v{$previous}";
135-
136-
return [ $tag, self::get_lock( $tag ) ];
160+
return $previous;
137161
}
138162

139163
/**
140164
* Fetches and decodes the composer.lock of wp-cli/wp-cli-bundle at a ref.
141165
*
142-
* @param string $ref Branch, tag or commit.
143-
* @param bool $throw_errors Whether a missing lockfile is fatal.
166+
* @param string $ref Branch, tag or commit.
167+
* @param bool $missing_is_fatal Whether a ref without a lockfile (HTTP
168+
* 404) is fatal. Any other failure always
169+
* is, so that a rate limit or an outage
170+
* never passes for a missing tag.
144171
*
145-
* @return array|false
172+
* @return array|false Decoded lockfile, or false when the ref has none
173+
* and that is not fatal.
146174
*/
147-
public static function get_lock( $ref, $throw_errors = true ) {
175+
public static function get_lock( $ref, $missing_is_fatal = true ) {
148176
$url = sprintf( 'https://raw.githubusercontent.com/%s/%s/composer.lock', self::REPO, $ref );
149177
$response = Utils\http_request( 'GET', $url );
150178

151-
if ( 200 !== $response->status_code ) {
152-
if ( ! $throw_errors ) {
153-
return false;
154-
}
179+
if ( 404 === (int) $response->status_code && ! $missing_is_fatal ) {
180+
return false;
181+
}
155182

183+
if ( 200 !== (int) $response->status_code ) {
156184
WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) );
157185
}
158186

@@ -202,24 +230,41 @@ public static function get_packages( array $lock ) {
202230
* @return array
203231
*/
204232
public static function get_shipped_milestones( $package, $previous_version, $current_version ) {
205-
$milestones = GitHub::get_project_milestones( $package, [ 'state' => 'closed' ] );
233+
return self::filter_shipped_milestones(
234+
GitHub::get_project_milestones( $package, [ 'state' => 'closed' ] ),
235+
$previous_version,
236+
$current_version
237+
);
238+
}
206239

240+
/**
241+
* Keeps the milestones whose version lies in (previous, current]. A
242+
* version that cannot be compared, such as `dev-main`, does not bound;
243+
* milestones whose title is not a version are dropped.
244+
*
245+
* @param array $milestones Milestone objects with a `title`.
246+
* @param string|null $previous_version Version locked by the previous release.
247+
* @param string|null $current_version Version locked by this release.
248+
*
249+
* @return array
250+
*/
251+
public static function filter_shipped_milestones( array $milestones, $previous_version, $current_version ) {
207252
return array_values(
208253
array_filter(
209254
$milestones,
210255
static function ( $milestone ) use ( $previous_version, $current_version ) {
211256
$title = ltrim( $milestone->title, 'v' );
212257

213-
if ( ! self::is_numeric_version( $title ) ) {
258+
if ( ! self::is_version( $title ) ) {
214259
return false;
215260
}
216261

217-
if ( self::is_numeric_version( $previous_version )
262+
if ( self::is_version( $previous_version )
218263
&& ! version_compare( $title, $previous_version, '>' ) ) {
219264
return false;
220265
}
221266

222-
if ( self::is_numeric_version( $current_version )
267+
if ( self::is_version( $current_version )
223268
&& version_compare( $title, $current_version, '>' ) ) {
224269
return false;
225270
}
@@ -244,13 +289,14 @@ public static function is_bundled_package( $name ) {
244289
}
245290

246291
/**
247-
* Checks whether a version can be compared, as opposed to `dev-main`.
292+
* Checks whether a string is a version that can be compared, such as
293+
* `2.12.0` or `3.0.0-beta1`, as opposed to `dev-main` or `3.0.0 (docs)`.
248294
*
249295
* @param string|null $version Version string, or null when unknown.
250296
*
251297
* @return bool
252298
*/
253-
private static function is_numeric_version( $version ) {
254-
return null !== $version && (bool) preg_match( '/^\d+(\.\d+)*/', $version );
299+
public static function is_version( $version ) {
300+
return null !== $version && (bool) preg_match( '/^\d+(\.\d+)*(-[0-9A-Za-z.]+)?$/', $version );
255301
}
256302
}

‎.maintenance/src/Release_Command.php‎

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -289,42 +289,8 @@ static function ( $repo ) use ( $exclude ) {
289289
}
290290

291291
private function get_bundle_repos() {
292-
$repos = [];
293-
$default_branch = GitHub::get_default_branch( 'wp-cli/wp-cli-bundle' );
294-
$composer_lock_url = "https://raw.githubusercontent.com/wp-cli/wp-cli-bundle/{$default_branch}/composer.lock";
295-
$response = Utils\http_request( 'GET', $composer_lock_url );
296-
if ( 200 !== $response->status_code ) {
297-
WP_CLI::error( sprintf( 'Could not fetch composer.json (HTTP code %d)', $response->status_code ) );
298-
}
299-
$composer_json = json_decode( $response->body, true );
300-
301-
usort(
302-
$composer_json['packages'],
303-
static function ( $a, $b ) {
304-
return $a['name'] < $b['name'] ? - 1 : 1;
305-
}
306-
);
307-
308-
foreach ( $composer_json['packages'] as $package ) {
309-
$package_name = $package['name'];
310-
if ( ! preg_match( '#^wp-cli/.+-command$#', $package_name )
311-
&& ! in_array(
312-
$package_name,
313-
array(
314-
'wp-cli/wp-cli-tests',
315-
'wp-cli/regenerate-readme',
316-
'wp-cli/autoload-splitter',
317-
'wp-cli/wp-config-transformer',
318-
'wp-cli/php-cli-tools',
319-
'wp-cli/spyc',
320-
),
321-
true
322-
) ) {
323-
continue;
324-
}
325-
$repos[] = $package_name;
326-
}
292+
$default_branch = GitHub::get_default_branch( Bundle::REPO );
327293

328-
return $repos;
294+
return array_keys( Bundle::get_packages( Bundle::get_lock( $default_branch ) ) );
329295
}
330296
}

‎phpunit.xml.dist‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
beStrictAboutCoversAnnotation="true"
6+
beStrictAboutOutputDuringTests="true"
7+
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
convertErrorsToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertDeprecationsToExceptions="true"
13+
colors="true"
14+
verbose="true">
15+
<testsuites>
16+
<testsuite name="wp-cli/wp-cli-dev tests">
17+
<directory suffix="Test.php">tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<coverage processUncoveredFiles="false">
22+
<include>
23+
<directory suffix=".php">.maintenance/src</directory>
24+
</include>
25+
</coverage>
26+
</phpunit>

0 commit comments

Comments
 (0)