11<?php namespace WP_CLI \Maintenance ;
22
3+ use cli \Shell ;
34use WP_CLI ;
45use 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