Skip to content

Commit 3be0dbf

Browse files
authored
Merge pull request #76 from wp-cli/release-aware-notes
Let contrib-list and release-notes target a specific bundle release
2 parents 96713a0 + 433a200 commit 3be0dbf

9 files changed

Lines changed: 647 additions & 210 deletions

File tree

‎.github/workflows/testing.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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
13+
with:
14+
# composer.json still allows PHP 7.2, but the development environment
15+
# cannot be installed below 7.4: johnpbloch/wordpress-core dev-master
16+
# requires it, so `composer install` fails on the 7.2 and 7.3 legs.
17+
minimum-php: '7.4'

‎.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: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
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/mustangostang-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+
$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 ) {
141+
$release = $release ? ltrim( $release, 'v' ) : null;
142+
$previous = null;
143+
144+
foreach ( $milestones as $milestone ) {
145+
$title = ltrim( $milestone->title, 'v' );
146+
147+
if ( ! self::is_version( $title ) ) {
148+
continue;
149+
}
150+
151+
if ( $release && ! version_compare( $title, $release, '<' ) ) {
152+
continue;
153+
}
154+
155+
if ( null === $previous || version_compare( $title, $previous, '>' ) ) {
156+
$previous = $title;
157+
}
158+
}
159+
160+
return $previous;
161+
}
162+
163+
/**
164+
* Fetches and decodes the composer.lock of wp-cli/wp-cli-bundle at a ref.
165+
*
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.
171+
*
172+
* @return array|false Decoded lockfile, or false when the ref has none
173+
* and that is not fatal.
174+
*/
175+
public static function get_lock( $ref, $missing_is_fatal = true ) {
176+
$url = sprintf( 'https://raw.githubusercontent.com/%s/%s/composer.lock', self::REPO, $ref );
177+
$response = Utils\http_request( 'GET', $url );
178+
179+
if ( 404 === (int) $response->status_code && ! $missing_is_fatal ) {
180+
return false;
181+
}
182+
183+
if ( 200 !== (int) $response->status_code ) {
184+
WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) );
185+
}
186+
187+
return json_decode( $response->body, true );
188+
}
189+
190+
/**
191+
* Lists the bundled packages of a lockfile with their versions.
192+
*
193+
* @param array $lock Decoded composer.lock.
194+
*
195+
* @return array<string, string> Package name => version without leading 'v',
196+
* sorted by name.
197+
*/
198+
public static function get_packages( array $lock ) {
199+
$packages = [];
200+
201+
foreach ( [ 'packages', 'packages-dev' ] as $section ) {
202+
if ( empty( $lock[ $section ] ) ) {
203+
continue;
204+
}
205+
206+
foreach ( $lock[ $section ] as $package ) {
207+
if ( ! self::is_bundled_package( $package['name'] ) ) {
208+
continue;
209+
}
210+
211+
$packages[ $package['name'] ] = ltrim( $package['version'], 'v' );
212+
}
213+
}
214+
215+
ksort( $packages );
216+
217+
return $packages;
218+
}
219+
220+
/**
221+
* Gets the closed milestones of a package that shipped in a bundle
222+
* release: newer than the version the previous release locked, and not
223+
* newer than the version this release locks. A package without a
224+
* previous version is newly bundled, so all of its releases count.
225+
*
226+
* @param string $package Package name.
227+
* @param string|null $previous_version Version locked by the previous release.
228+
* @param string $current_version Version locked by this release.
229+
*
230+
* @return array
231+
*/
232+
public static function get_shipped_milestones( $package, $previous_version, $current_version ) {
233+
return self::filter_shipped_milestones(
234+
GitHub::get_project_milestones( $package, [ 'state' => 'closed' ] ),
235+
$previous_version,
236+
$current_version
237+
);
238+
}
239+
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 ) {
252+
return array_values(
253+
array_filter(
254+
$milestones,
255+
static function ( $milestone ) use ( $previous_version, $current_version ) {
256+
$title = ltrim( $milestone->title, 'v' );
257+
258+
if ( ! self::is_version( $title ) ) {
259+
return false;
260+
}
261+
262+
if ( self::is_version( $previous_version )
263+
&& ! version_compare( $title, $previous_version, '>' ) ) {
264+
return false;
265+
}
266+
267+
if ( self::is_version( $current_version )
268+
&& version_compare( $title, $current_version, '>' ) ) {
269+
return false;
270+
}
271+
272+
return true;
273+
}
274+
)
275+
);
276+
}
277+
278+
/**
279+
* Checks whether a package's contributors and release notes belong to a
280+
* bundle release.
281+
*
282+
* @param string $name Package name.
283+
*
284+
* @return bool
285+
*/
286+
public static function is_bundled_package( $name ) {
287+
return (bool) preg_match( '#^wp-cli/.+-command$#', $name )
288+
|| in_array( $name, self::OTHER_PACKAGES, true );
289+
}
290+
291+
/**
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)`.
294+
*
295+
* @param string|null $version Version string, or null when unknown.
296+
*
297+
* @return bool
298+
*/
299+
public static function is_version( $version ) {
300+
return null !== $version && (bool) preg_match( '/^\d+(\.\d+)*(-[0-9A-Za-z.]+)?$/', $version );
301+
}
302+
}

0 commit comments

Comments
 (0)