Skip to content

Commit 70f5cc8

Browse files
schlesseraclaude
andcommitted
Make release generate safe to run unattended
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5a1de0d commit 70f5cc8

1 file changed

Lines changed: 155 additions & 29 deletions

File tree

‎.maintenance/src/Release_Command.php‎

Lines changed: 155 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ public function close_released( $args, $assoc_args ) {
4848
/**
4949
* Generate a new release out of an open milestone
5050
*
51+
* Tags the default branch with the version of the milestone, publishes a
52+
* release whose notes list the pull requests merged for that milestone,
53+
* and closes the milestone.
54+
*
55+
* A milestone is skipped, with a warning, when it still has open issues or
56+
* pull requests, has no merged pull requests, is not a version newer than
57+
* the latest tag, or was already tagged. When a repository has several
58+
* milestones that could be released, it is skipped unless --milestone says
59+
* which one, as they would all tag the same commit.
60+
*
61+
* Run `wp maintenance release plan` first to see what is ready.
62+
*
5163
* ## OPTIONS
5264
*
5365
* [<repo>...]
@@ -60,52 +72,87 @@ public function close_released( $args, $assoc_args ) {
6072
* [--all]
6173
* : Generate releases for all repositories in the wp-cli organization.
6274
*
75+
* [--milestone=<milestone>]
76+
* : Only release the milestone with this title. Needs a single repository.
77+
*
78+
* [--notes-file=<file>]
79+
* : Use the contents of this file as release notes, instead of the
80+
* generated list of pull requests. Useful to publish notes that were
81+
* reviewed and tidied up first. Needs --milestone.
82+
*
83+
* [--yes]
84+
* : Publish without asking for confirmation.
85+
*
86+
* ## EXAMPLES
87+
*
88+
* # Review the generated notes, then publish them after fixing a typo.
89+
* $ wp maintenance release-notes cache-command 3.0.1 --source=pull-request > notes.md
90+
* $ wp maintenance release generate cache-command --milestone=3.0.1 --notes-file=notes.md --yes
91+
*
6392
* @when before_wp_load
6493
*/
6594
public function generate( $args, $assoc_args ) {
95+
$only_milestone = Utils\get_flag_value( $assoc_args, 'milestone' );
96+
$notes_file = Utils\get_flag_value( $assoc_args, 'notes-file' );
97+
$confirmed = Utils\get_flag_value( $assoc_args, 'yes', false );
98+
$repos = $this->get_repos( $args, $assoc_args );
6699

67-
foreach ( $this->get_repos( $args, $assoc_args ) as $repo ) {
100+
if ( null !== $only_milestone && 1 !== count( $repos ) ) {
101+
WP_CLI::error( '--milestone needs exactly one repository.' );
102+
}
103+
104+
if ( null !== $notes_file ) {
105+
if ( null === $only_milestone ) {
106+
WP_CLI::error( '--notes-file needs --milestone.' );
107+
}
108+
109+
$custom_notes = is_readable( $notes_file ) ? trim( (string) file_get_contents( $notes_file ) ) : '';
110+
if ( '' === $custom_notes ) {
111+
WP_CLI::error( "Could not read release notes from '{$notes_file}'." );
112+
}
113+
}
114+
115+
foreach ( $repos as $repo ) {
68116

69117
WP_CLI::log( "--- {$repo} ---" );
70118

71-
$releases = GitHub::get_project_releases( $repo );
72-
$milestones = GitHub::get_project_milestones( $repo );
119+
$candidates = $this->get_releasable_milestones( $repo, $only_milestone );
73120

74-
foreach ( $milestones as $milestone ) {
75-
WP_CLI::log( "Checking milestone '{$milestone->title}'..." );
76-
foreach ( $releases as $release ) {
77-
if ( $release->tag_name === $milestone->title || "v{$milestone->title}" === $release->tag_name ) {
78-
WP_CLI::log( "Found matching release '{$release->tag_name}', skipping milestone '{$milestone->title}'..." );
79-
continue 2;
80-
}
81-
}
82-
WP_CLI::log( "Milestone '{$milestone->title}' does not have a matching release, generating one..." );
121+
if ( count( $candidates ) > 1 ) {
122+
WP_CLI::warning(
123+
"Skipping {$repo} as several milestones could be released: "
124+
. implode( ', ', array_keys( $candidates ) ) . '. Pass --milestone to pick one.'
125+
);
126+
continue;
127+
}
83128

84-
if ( $this->has_open_items_on_milestone( $repo, $milestone->number ) ) {
85-
WP_CLI::warning( "Skipping milestone '{$milestone->title}' as it has open issues/PRs assigned to it." );
86-
continue;
87-
}
129+
foreach ( $candidates as $candidate ) {
130+
list( $milestone, $notes ) = $candidate;
88131

89132
$title = "Version {$milestone->title}";
90-
$tag = "v{$milestone->title}";
91-
$release_notes = Release_Notes::render(
92-
[ Release_Notes::get( $repo, [ $milestone ], 'pull-request' ) ],
93-
'markdown',
94-
false
95-
);
133+
$tag = 'v' . ltrim( $milestone->title, 'v' );
134+
$release_notes = isset( $custom_notes )
135+
? $custom_notes . "\n"
136+
: Release_Notes::render( [ $notes ], 'markdown', false );
96137

97138
WP_CLI::log( 'Generating the following release:' );
98139
WP_CLI::log( '-----' );
99140
WP_CLI::log( "{$title} ({$tag})\n{$release_notes}" );
100141
WP_CLI::log( '-----' );
101142

102-
fwrite( STDOUT, 'Is the above correct? [y/n] ' );
103-
$answer = fgets( STDIN );
104-
if ( false === $answer ) {
105-
WP_CLI::error( 'No answer received. This command needs an interactive terminal.' );
106-
}
107-
if ( 'y' !== strtolower( trim( $answer ) ) ) {
108-
continue;
143+
if ( ! $confirmed ) {
144+
fwrite( STDOUT, 'Is the above correct? [y/n] ' );
145+
$answer = fgets( STDIN );
146+
if ( false === $answer ) {
147+
WP_CLI::error( 'No answer received. Run this command in an interactive terminal, or pass --yes.' );
148+
}
149+
// A piped answer is not echoed, so end the prompt's line here.
150+
if ( ! stream_isatty( STDIN ) ) {
151+
fwrite( STDOUT, "\n" );
152+
}
153+
if ( 'y' !== strtolower( trim( $answer ) ) ) {
154+
continue;
155+
}
109156
}
110157

111158
$default_branch = GitHub::get_default_branch( $repo );
@@ -119,6 +166,85 @@ public function generate( $args, $assoc_args ) {
119166
}
120167
}
121168

169+
/**
170+
* Finds the open milestones of a repository that can be released, and
171+
* warns about the ones that cannot.
172+
*
173+
* @param string $repo Repository name, including the org.
174+
* @param string|null $only_milestone Title of the only milestone to look at.
175+
*
176+
* @return array<string, array> Milestone title => milestone and its notes.
177+
*/
178+
private function get_releasable_milestones( $repo, $only_milestone ) {
179+
$releases = array_map(
180+
static function ( $release ) {
181+
return $release->tag_name;
182+
},
183+
GitHub::get_project_releases( $repo )
184+
);
185+
$tags = array_map(
186+
static function ( $tag ) {
187+
return $tag->name;
188+
},
189+
GitHub::get_tags( $repo )
190+
);
191+
$latest = Release_Plan::latest_version_tag( $tags );
192+
193+
$milestones = GitHub::get_project_milestones( $repo );
194+
195+
if ( null !== $only_milestone ) {
196+
$milestones = array_filter(
197+
$milestones,
198+
static function ( $milestone ) use ( $only_milestone ) {
199+
return $milestone->title === $only_milestone;
200+
}
201+
);
202+
203+
if ( ! $milestones ) {
204+
WP_CLI::warning( "No open milestone '{$only_milestone}' in {$repo}." );
205+
}
206+
}
207+
208+
$candidates = [];
209+
foreach ( $milestones as $milestone ) {
210+
$version = ltrim( $milestone->title, 'v' );
211+
212+
WP_CLI::log( "Checking milestone '{$milestone->title}'..." );
213+
214+
if ( Release_Plan::has_tag( $releases, $version ) ) {
215+
WP_CLI::log( "Found a matching release, skipping milestone '{$milestone->title}'..." );
216+
continue;
217+
}
218+
219+
if ( Release_Plan::has_tag( $tags, $version ) ) {
220+
WP_CLI::warning( "Skipping milestone '{$milestone->title}' as it was already tagged. Its pull requests need a new milestone." );
221+
continue;
222+
}
223+
224+
if ( ! Bundle::is_version( $version )
225+
|| ( null !== $latest && ! version_compare( $version, ltrim( $latest, 'v' ), '>' ) ) ) {
226+
WP_CLI::warning( "Skipping milestone '{$milestone->title}' as it is not a version newer than {$latest}." );
227+
continue;
228+
}
229+
230+
if ( $this->has_open_items_on_milestone( $repo, $milestone->number ) ) {
231+
WP_CLI::warning( "Skipping milestone '{$milestone->title}' as it has open issues/PRs assigned to it." );
232+
continue;
233+
}
234+
235+
$notes = Release_Notes::get( $repo, [ $milestone ], 'pull-request' );
236+
237+
if ( empty( $notes['milestones'][0]['pull_requests'] ) ) {
238+
WP_CLI::warning( "Skipping milestone '{$milestone->title}' as it has no merged pull requests." );
239+
continue;
240+
}
241+
242+
$candidates[ $milestone->title ] = [ $milestone, $notes ];
243+
}
244+
245+
return $candidates;
246+
}
247+
122248
/**
123249
* Checks which repositories have unreleased changes, and whether their
124250
* milestones are ready to release them.

0 commit comments

Comments
 (0)