Skip to content

Commit a6f91dd

Browse files
authored
Merge pull request #86 from wp-cli/release-plan
Add a read-only release plan
2 parents 1476f8a + 4c71dca commit a6f91dd

6 files changed

Lines changed: 579 additions & 20 deletions

File tree

‎.maintenance/src/GitHub.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,78 @@ public static function get_organization_repos() {
345345
);
346346
}
347347

348+
/**
349+
* Gets the tags of a repository.
350+
*
351+
* @param string $project
352+
*
353+
* @return array
354+
*/
355+
public static function get_tags( $project ) {
356+
return self::request_all( self::API_ROOT . "repos/{$project}/tags" );
357+
}
358+
359+
/**
360+
* Gets the pull requests merged into a branch since a given tag.
361+
*
362+
* @param string $project
363+
* @param string $tag Tag to compare the branch against.
364+
* @param string $branch Branch the pull requests were merged into.
365+
*
366+
* @return array Pull requests, most recently updated first.
367+
*/
368+
public static function get_pull_requests_merged_since( $project, $tag, $branch ) {
369+
list( $comparison ) = self::request(
370+
self::API_ROOT . "repos/{$project}/compare/" . rawurlencode( $tag ) . '...' . rawurlencode( $branch )
371+
);
372+
373+
$commits = [];
374+
foreach ( $comparison->commits as $commit ) {
375+
$commits[ $commit->sha ] = true;
376+
}
377+
378+
$tagged_at = $comparison->base_commit->commit->committer->date;
379+
380+
// The comparison lists at most 250 commits. Past that, the merge date
381+
// alone has to tell which pull requests are unreleased.
382+
$complete = count( $commits ) >= $comparison->total_commits;
383+
384+
$pull_requests = [];
385+
$url = self::API_ROOT . "repos/{$project}/pulls";
386+
$args = [
387+
'state' => 'closed',
388+
'base' => $branch,
389+
'sort' => 'updated',
390+
'direction' => 'desc',
391+
'per_page' => 100,
392+
];
393+
394+
// Pull requests are sorted by their last update, which is never older
395+
// than their merge, so the listing can stop at the tag.
396+
while ( $url ) {
397+
list( $page, $headers ) = self::request( $url, $args );
398+
399+
foreach ( $page as $pull_request ) {
400+
if ( $pull_request->updated_at < $tagged_at ) {
401+
break 2;
402+
}
403+
404+
if ( ! $pull_request->merged_at || $pull_request->merged_at < $tagged_at ) {
405+
continue;
406+
}
407+
408+
if ( ! $complete || isset( $commits[ $pull_request->merge_commit_sha ] ) ) {
409+
$pull_requests[] = $pull_request;
410+
}
411+
}
412+
413+
$url = self::get_next_page_url( isset( $headers['link'] ) ? $headers['link'] : '' );
414+
$args = [];
415+
}
416+
417+
return $pull_requests;
418+
}
419+
348420
/**
349421
* Gets the default branch of a repository.
350422
*

‎.maintenance/src/Release_Command.php‎

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace WP_CLI\Maintenance;
22

3+
use cli\Shell;
34
use WP_CLI;
45
use WP_CLI\Utils;
56

@@ -25,16 +26,7 @@ final class Release_Command {
2526
*/
2627
public function close_released( $args, $assoc_args ) {
2728

28-
$repos = (array) $args;
29-
30-
if ( Utils\get_flag_value( $assoc_args, 'all', false ) ) {
31-
$repos = array_unique( array_merge( $repos, $this->get_all_repos() ) );
32-
} elseif ( Utils\get_flag_value( $assoc_args, 'bundle', false ) ) {
33-
$repos = array_unique( array_merge( $repos, $this->get_bundle_repos() ) );
34-
}
35-
36-
foreach ( $repos as $repo ) {
37-
$repo = GitHub::qualify_repo( $repo );
29+
foreach ( $this->get_repos( $args, $assoc_args ) as $repo ) {
3830

3931
WP_CLI::log( "--- {$repo} ---" );
4032

@@ -72,16 +64,7 @@ public function close_released( $args, $assoc_args ) {
7264
*/
7365
public function generate( $args, $assoc_args ) {
7466

75-
$repos = (array) $args;
76-
77-
if ( Utils\get_flag_value( $assoc_args, 'all', false ) ) {
78-
$repos = array_unique( array_merge( $repos, $this->get_all_repos() ) );
79-
} elseif ( Utils\get_flag_value( $assoc_args, 'bundle', false ) ) {
80-
$repos = array_unique( array_merge( $repos, $this->get_bundle_repos() ) );
81-
}
82-
83-
foreach ( $repos as $repo ) {
84-
$repo = GitHub::qualify_repo( $repo );
67+
foreach ( $this->get_repos( $args, $assoc_args ) as $repo ) {
8568

8669
WP_CLI::log( "--- {$repo} ---" );
8770

@@ -136,6 +119,85 @@ public function generate( $args, $assoc_args ) {
136119
}
137120
}
138121

122+
/**
123+
* Checks which repositories have unreleased changes, and whether their
124+
* milestones are ready to release them.
125+
*
126+
* Compares the default branch of each repository against its latest
127+
* version tag and looks at the pull requests merged since. A repository is
128+
* `up_to_date` when none of them calls for a release, `ready` when they
129+
* all sit on one open milestone that `release generate` can release, and
130+
* `needs_human` when a maintainer has to decide something first, such as
131+
* which milestone a pull request belongs to. The blockers say what.
132+
*
133+
* Routine pull requests by Dependabot or GitHub Actions only count when
134+
* they are on a milestone.
135+
*
136+
* Makes no changes.
137+
*
138+
* ## OPTIONS
139+
*
140+
* [<repo>...]
141+
* : Name(s) of the repository to check. If no user/org was provided,
142+
* 'wp-cli' org is assumed.
143+
*
144+
* [--bundle]
145+
* : Check every package of the bundle.
146+
*
147+
* [--all]
148+
* : Check all repositories in the wp-cli organization.
149+
*
150+
* [--format=<format>]
151+
* : Render output in a specific format. Defaults to a table in a terminal
152+
* and to JSON otherwise. The JSON lists the unreleased pull requests and
153+
* the blockers of each repository as well.
154+
* ---
155+
* options:
156+
* - table
157+
* - json
158+
* ---
159+
*
160+
* ## EXAMPLES
161+
*
162+
* $ wp maintenance release plan cache-command db-command eval-command
163+
* +----------------------+-------------+------------+---------+------------+
164+
* | repo | status | latest_tag | release | blockers |
165+
* +----------------------+-------------+------------+---------+------------+
166+
* | wp-cli/cache-command | ready | v3.0.0 | v3.0.1 | |
167+
* | wp-cli/db-command | needs_human | v3.0.1 | | open_items |
168+
* | wp-cli/eval-command | up_to_date | v3.0.1 | | |
169+
* +----------------------+-------------+------------+---------+------------+
170+
*
171+
* @when before_wp_load
172+
*/
173+
public function plan( $args, $assoc_args ) {
174+
$format = Utils\get_flag_value( $assoc_args, 'format', Shell::isPiped() ? 'json' : 'table' );
175+
176+
$plans = array_map( [ Release_Plan::class, 'fetch' ], $this->get_repos( $args, $assoc_args ) );
177+
178+
if ( 'json' === $format ) {
179+
WP_CLI::line( (string) json_encode( $plans, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
180+
return;
181+
}
182+
183+
Utils\format_items(
184+
'table',
185+
array_map(
186+
static function ( $plan ) {
187+
return [
188+
'repo' => $plan['repo'],
189+
'status' => $plan['status'],
190+
'latest_tag' => $plan['latest_tag'],
191+
'release' => $plan['release'] ? $plan['release']['tag'] : '',
192+
'blockers' => implode( ', ', array_unique( array_column( $plan['blockers'], 'code' ) ) ),
193+
];
194+
},
195+
$plans
196+
),
197+
[ 'repo', 'status', 'latest_tag', 'release', 'blockers' ]
198+
);
199+
}
200+
139201
private function has_open_items_on_milestone( $repo, $milestone ) {
140202
return GitHub::get_issues(
141203
$repo,
@@ -146,6 +208,26 @@ private function has_open_items_on_milestone( $repo, $milestone ) {
146208
);
147209
}
148210

211+
/**
212+
* Gets the repositories a command applies to, fully qualified.
213+
*
214+
* @param string[] $args Repositories passed by name.
215+
* @param array $assoc_args `--bundle` and `--all` flags.
216+
*
217+
* @return string[]
218+
*/
219+
private function get_repos( $args, $assoc_args ) {
220+
$repos = (array) $args;
221+
222+
if ( Utils\get_flag_value( $assoc_args, 'all', false ) ) {
223+
$repos = array_merge( $repos, $this->get_all_repos() );
224+
} elseif ( Utils\get_flag_value( $assoc_args, 'bundle', false ) ) {
225+
$repos = array_merge( $repos, $this->get_bundle_repos() );
226+
}
227+
228+
return array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $repos ) ) );
229+
}
230+
149231
private function get_all_repos() {
150232
return array_map(
151233
static function ( $repo ) {

0 commit comments

Comments
 (0)