Skip to content

Commit 5971c02

Browse files
schlesseraclaude
andcommitted
Only release what the release plan reports as ready
Also makes the fixture recorder keep the existing fixtures when GitHub returns an unexpected status, and drops the PHP 8.5 deprecated $http_response_header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 08f53fb commit 5971c02

3 files changed

Lines changed: 156 additions & 33 deletions

File tree

‎.maintenance/src/Release_Command.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public function close_released( $args, $assoc_args ) {
5858
* milestones that could be released, it is skipped unless --milestone says
5959
* which one, as they would all tag the same commit.
6060
*
61+
* It is also skipped unless `wp maintenance release plan` reports it as
62+
* ready for that milestone: the tag ships everything merged into the
63+
* default branch since the latest tag, so all of it has to belong to the
64+
* milestone, and all of the milestone's pull requests have to be in it.
65+
*
6166
* Run `wp maintenance release plan` first to see what is ready.
6267
*
6368
* ## OPTIONS
@@ -126,6 +131,10 @@ public function generate( $args, $assoc_args ) {
126131
continue;
127132
}
128133

134+
if ( $candidates && ! $this->agrees_with_plan( $repo, $candidates ) ) {
135+
continue;
136+
}
137+
129138
foreach ( $candidates as $candidate ) {
130139
list( $milestone, $notes ) = $candidate;
131140

@@ -166,6 +175,52 @@ public function generate( $args, $assoc_args ) {
166175
}
167176
}
168177

178+
/**
179+
* Checks a milestone about to be released against the release plan of its
180+
* repository, which looks at what the default branch actually contains.
181+
*
182+
* The tag goes on the default branch, so it ships every change merged there
183+
* since the latest tag. Those changes must all belong to this milestone, and
184+
* every pull request in the release notes must be among them.
185+
*
186+
* @param string $repo Repository name, including the org.
187+
* @param array<string, array> $candidates The one milestone to release, see
188+
* get_releasable_milestones().
189+
*
190+
* @return bool Whether the release can go ahead. Warns when it can't.
191+
*/
192+
private function agrees_with_plan( $repo, array $candidates ) {
193+
$title = (string) key( $candidates );
194+
$plan = Release_Plan::fetch( $repo );
195+
196+
if ( Release_Plan::READY !== $plan['status'] ) {
197+
WP_CLI::warning( "Skipping milestone '{$title}' as {$repo} needs a maintainer's decision first:" );
198+
foreach ( $plan['blockers'] as $blocker ) {
199+
WP_CLI::log( " - {$blocker['message']}" );
200+
}
201+
return false;
202+
}
203+
204+
if ( $plan['release']['milestone'] !== $title ) {
205+
WP_CLI::warning( "Skipping milestone '{$title}' as the changes on the default branch belong to milestone '{$plan['release']['milestone']}'." );
206+
return false;
207+
}
208+
209+
$on_branch = array_column( $plan['pull_requests'], 'number' );
210+
$in_notes = array_column( $candidates[ $title ][1]['milestones'][0]['pull_requests'], 'number' );
211+
$missing = array_diff( $in_notes, $on_branch );
212+
213+
if ( $missing ) {
214+
WP_CLI::warning(
215+
"Skipping milestone '{$title}' as #" . implode( ', #', $missing )
216+
. " was not merged into the default branch since {$plan['latest_tag']}, so the release would not contain it."
217+
);
218+
return false;
219+
}
220+
221+
return true;
222+
}
223+
169224
/**
170225
* Finds the open milestones of a repository that can be released, and
171226
* warns about the ones that cannot.

‎tests/ReleaseCommandTest.php‎

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,25 @@ class ReleaseCommandTest extends GitHub_TestCase {
99
const REPO = 'repos/wp-cli/block-command';
1010

1111
/**
12-
* Sets up a repository whose latest tag is v1.0.0, with the given open
13-
* milestones, each holding the recorded merged pull requests #18 and #19.
12+
* Sets up a repository whose latest tag is the first of the given tags,
13+
* with the given open milestones, each holding the recorded merged pull
14+
* requests #18 and #19.
1415
*
1516
* @param array<int, string> $milestones Milestone number => title.
16-
* @param string[] $tags Tag names.
17+
* @param string[] $tags Tag names, latest first.
18+
* @param array<int, string> $on_branch Pull requests merged into the
19+
* default branch since the latest
20+
* tag: number => milestone title.
21+
* Defaults to #18 and #19 on the
22+
* first milestone.
23+
* @param array<int, int> $open_items Milestone number => open items.
1724
*/
18-
private function repository( array $milestones, array $tags = [ 'v1.0.0' ] ) {
25+
private function repository( array $milestones, array $tags = [ 'v1.0.0' ], $on_branch = null, array $open_items = [] ) {
26+
$comparison = $this->fixture( 'comparison' );
27+
if ( null === $on_branch ) {
28+
$on_branch = array_fill_keys( [ 18, 19 ], reset( $milestones ) );
29+
}
30+
1931
$this->respond( 'GET', self::REPO . '/releases', [] );
2032
$this->respond(
2133
'GET',
@@ -27,24 +39,45 @@ function ( $tag ) {
2739
$tags
2840
)
2941
);
42+
$this->respond( 'GET', self::REPO . "/compare/{$tags[0]}...main", $comparison );
43+
44+
$pull_requests = [];
45+
$index = 0;
46+
foreach ( $on_branch as $number => $milestone ) {
47+
$pull_requests[] = $this->fixture_object(
48+
'pull-requests',
49+
0,
50+
[
51+
'number' => $number,
52+
'updated_at' => '2026-09-01T00:00:00Z',
53+
'merged_at' => '2026-09-01T00:00:00Z',
54+
'merge_commit_sha' => $comparison->commits[ $index++ ]->sha,
55+
'milestone' => [ 'title' => $milestone ],
56+
]
57+
);
58+
}
59+
$this->respond( 'GET', self::REPO . '/pulls', $pull_requests );
3060

3161
$open = [];
3262
foreach ( $milestones as $number => $title ) {
63+
$count = isset( $open_items[ $number ] ) ? $open_items[ $number ] : 0;
3364
$open[] = $this->fixture_object(
3465
'milestone',
3566
null,
3667
[
3768
'number' => $number,
3869
'title' => $title,
3970
'state' => 'open',
40-
'open_issues' => 0,
71+
'open_issues' => $count,
4172
]
4273
);
43-
$this->respond( 'GET', self::REPO . "/issues?milestone={$number}&state=open", [] );
74+
$this->respond( 'GET', self::REPO . "/issues?milestone={$number}&state=open", array_slice( $this->fixture( 'issues' ), 0, $count ) );
4475
$this->respond( 'GET', self::REPO . "/issues?milestone={$number}&state=all", $this->fixture( 'issues' ) );
4576
$this->respond( 'PATCH', self::REPO . "/milestones/{$number}", $this->fixture( 'milestone' ) );
4677
}
4778

79+
// The plan asks for all milestones, `generate` for the open ones.
80+
$this->respond( 'GET', self::REPO . '/milestones?state=all', $open );
4881
$this->respond( 'GET', self::REPO . '/milestones', $open );
4982
$this->respond( 'GET', self::REPO, $this->fixture( 'repository' ) );
5083
$this->respond( 'POST', self::REPO . '/releases', $this->fixture( 'release' ) );
@@ -127,6 +160,37 @@ public function test_generate_needs_a_choice_between_several_milestones(): void
127160
$this->assertStringContainsString( 'several milestones could be released: 1.0.1, 1.1.0', $this->logger->stderr );
128161
}
129162

163+
public function test_generate_refuses_when_changes_of_another_milestone_would_ship(): void {
164+
$this->repository(
165+
[
166+
4 => '1.0.1',
167+
5 => '1.1.0',
168+
],
169+
[ 'v1.0.0' ],
170+
[
171+
18 => '1.0.1',
172+
19 => '1.0.1',
173+
30 => '1.1.0',
174+
],
175+
[ 5 => 1 ]
176+
);
177+
178+
( new Release_Command() )->generate( [ 'block-command' ], [ 'yes' => true ] );
179+
180+
$this->assertSame( [], $this->writes() );
181+
$this->assertStringContainsString( "Skipping milestone '1.0.1' as wp-cli/block-command needs a maintainer's decision first", $this->logger->stderr );
182+
$this->assertStringContainsString( 'spread over the milestones 1.0.1, 1.1.0', $this->logger->stdout );
183+
}
184+
185+
public function test_generate_refuses_notes_for_pull_requests_missing_from_the_branch(): void {
186+
$this->repository( [ 4 => '1.0.1' ], [ 'v1.0.0' ], [ 18 => '1.0.1' ] );
187+
188+
( new Release_Command() )->generate( [ 'block-command' ], [ 'yes' => true ] );
189+
190+
$this->assertSame( [], $this->writes() );
191+
$this->assertStringContainsString( "Skipping milestone '1.0.1' as #19 was not merged into the default branch since v1.0.0", $this->logger->stderr );
192+
}
193+
130194
public function test_plan_reports_a_ready_milestone(): void {
131195
$comparison = $this->fixture( 'comparison' );
132196

‎tests/fixtures/github/update.php‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,49 @@
4444
exit( 1 );
4545
}
4646

47+
/**
48+
* Statuses the recordings must have. Anything else, such as an expired token,
49+
* leaves the existing fixture untouched.
50+
*/
51+
const EXPECTED_STATUSES = [ 'release-absent' => 404 ];
52+
4753
foreach ( FIXTURES as $name => $path ) {
48-
$context = stream_context_create(
54+
$headers = [];
55+
$curl = curl_init( "https://api.github.com/{$path}" );
56+
curl_setopt_array(
57+
$curl,
4958
[
50-
'http' => [
51-
'header' => implode(
52-
"\r\n",
53-
[
54-
'Accept: application/vnd.github+json',
55-
"Authorization: Bearer {$token}",
56-
'User-Agent: wp-cli-dev test fixtures',
57-
'X-GitHub-Api-Version: 2022-11-28',
58-
]
59-
),
60-
'ignore_errors' => true,
59+
CURLOPT_RETURNTRANSFER => true,
60+
CURLOPT_HTTPHEADER => [
61+
'Accept: application/vnd.github+json',
62+
"Authorization: Bearer {$token}",
63+
'User-Agent: wp-cli-dev test fixtures',
64+
'X-GitHub-Api-Version: 2022-11-28',
6165
],
66+
CURLOPT_HEADERFUNCTION => static function ( $curl, $line ) use ( &$headers ) {
67+
$parts = explode( ':', $line, 2 );
68+
if ( 2 === count( $parts ) && in_array( strtolower( trim( $parts[0] ) ), KEPT_HEADERS, true ) ) {
69+
$headers[ strtolower( trim( $parts[0] ) ) ] = trim( $parts[1] );
70+
}
71+
return strlen( $line );
72+
},
6273
]
6374
);
6475

65-
$body = file_get_contents( "https://api.github.com/{$path}", false, $context );
76+
$body = curl_exec( $curl );
77+
$status = (int) curl_getinfo( $curl, CURLINFO_RESPONSE_CODE );
78+
$error = curl_error( $curl );
6679

67-
// Set by file_get_contents() for HTTP requests.
68-
$response_headers = isset( $http_response_header ) ? $http_response_header : [];
80+
$expected = array_key_exists( $name, EXPECTED_STATUSES ) ? EXPECTED_STATUSES[ $name ] : 200;
6981

70-
if ( false === $body || ! $response_headers || ! preg_match( '#^HTTP/\S+ (\d+)#', $response_headers[0], $matches ) ) {
71-
fwrite( STDERR, "Could not fetch {$path}.\n" );
82+
if ( ! is_string( $body ) || $expected !== $status ) {
83+
fwrite( STDERR, "Could not record {$name}.json: GET /{$path} returned " . ( $status ?: $error ) . " instead of {$expected}. The fixture is unchanged.\n" );
7284
exit( 1 );
7385
}
7486

75-
$headers = [];
76-
foreach ( array_slice( $response_headers, 1 ) as $header ) {
77-
list( $key, $value ) = array_map( 'trim', explode( ':', $header, 2 ) + [ 1 => '' ] );
78-
if ( in_array( strtolower( $key ), KEPT_HEADERS, true ) ) {
79-
$headers[ strtolower( $key ) ] = $value;
80-
}
81-
}
82-
8387
$fixture = [
8488
'request' => "GET /{$path}",
85-
'status' => (int) $matches[1],
89+
'status' => $status,
8690
'headers' => (object) $headers,
8791
'body' => json_decode( $body ),
8892
];
@@ -92,5 +96,5 @@
9296
json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n"
9397
);
9498

95-
echo "Recorded {$name}.json ({$fixture['status']})\n";
99+
echo "Recorded {$name}.json ({$status})\n";
96100
}

0 commit comments

Comments
 (0)