Skip to content

Commit 2426b0c

Browse files
schlesseraclaude
andcommitted
Address review findings
Map bundled packages to their GitHub repository, keep the milestone order in release notes, preserve argument quoting in foreach-bundle, and make the refresh and clone scripts report git and API failures instead of silently carrying on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 86e3907 commit 2426b0c

12 files changed

Lines changed: 160 additions & 22 deletions

‎.maintenance/clone-all-repositories.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ fetch_repositories() {
7777
return
7878
fi
7979

80-
jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv' <<< "${response}"
80+
if ! jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv' <<< "${response}"; then
81+
echo "GitHub API returned repositories in an unexpected format." >&2
82+
return 1
83+
fi
8184
page=$((page + 1))
8285
done
8386
}

‎.maintenance/refresh-repository.php‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
/**
33
* Brings a clone up to date with its remote, without ever touching work in
44
* progress: a clone with uncommitted changes, or one on another branch than
5-
* the default one, is only fetched.
5+
* the default one, is only fetched. Exits with an error code when git fails.
66
*/
77

88
if ( ! isset( $argv[1] ) || ! is_dir( $argv[1] ) ) {
99
exit( "Usage: php refresh-repository.php <repository-folder>\n" );
1010
}
1111

1212
$repository = $argv[1];
13-
$git = 'git -C ' . escapeshellarg( (string) realpath( $repository ) );
13+
$path = (string) realpath( $repository );
14+
$git = 'git -C ' . escapeshellarg( $path );
1415

1516
/**
1617
* Runs a git command and returns its output, or null when it fails.
@@ -26,9 +27,30 @@ function git_output( $git, $command ) {
2627
return 0 === $exit_code ? trim( implode( "\n", $output ) ) : null;
2728
}
2829

30+
/**
31+
* Prints an error and exits with an error code.
32+
*
33+
* @param string $repository Repository folder.
34+
* @param string $message What went wrong.
35+
*/
36+
function fail( $repository, $message ) {
37+
fwrite( STDERR, sprintf( "Could not refresh \033[31m%s\033[0m: %s\n", $repository, $message ) );
38+
exit( 1 );
39+
}
40+
2941
printf( "--- Refreshing repository \033[32m%s\033[0m ---\n", $repository );
3042

31-
passthru( "{$git} fetch --quiet origin" );
43+
// Git looks for a repository in parent folders too, so a folder that is not a
44+
// clone would otherwise refresh the repository around it.
45+
$toplevel = git_output( $git, 'rev-parse --show-toplevel' );
46+
if ( null === $toplevel || realpath( $toplevel ) !== $path ) {
47+
fail( $repository, 'it is not a Git clone.' );
48+
}
49+
50+
passthru( "{$git} fetch --quiet origin", $exit_code );
51+
if ( 0 !== $exit_code ) {
52+
fail( $repository, 'fetching from origin failed.' );
53+
}
3254

3355
$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' );
3456
if ( null === $default_branch ) {
@@ -38,8 +60,13 @@ function git_output( $git, $command ) {
3860
}
3961
$default_branch = null === $default_branch ? null : preg_replace( '#^origin/#', '', $default_branch );
4062
$current_branch = git_output( $git, 'rev-parse --abbrev-ref HEAD' );
63+
$changes = git_output( $git, 'status --porcelain' );
4164

42-
if ( '' !== (string) git_output( $git, 'status --porcelain' ) ) {
65+
if ( null === $changes ) {
66+
fail( $repository, 'its status could not be read.' );
67+
}
68+
69+
if ( '' !== $changes ) {
4370
printf( "Leaving \033[33m%s\033[0m alone, it has uncommitted changes.\n", $repository );
4471
exit;
4572
}
@@ -50,4 +77,7 @@ function git_output( $git, $command ) {
5077
}
5178

5279
printf( "Pulling latest changes into %s...\n", $default_branch );
53-
passthru( "{$git} pull --quiet --ff-only" );
80+
passthru( "{$git} pull --quiet --ff-only", $exit_code );
81+
if ( 0 !== $exit_code ) {
82+
fail( $repository, 'pulling the latest changes failed.' );
83+
}

‎.maintenance/src/Bundle.php‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ final class Bundle {
3232
'wp-cli/mustangostang-spyc',
3333
];
3434

35+
/**
36+
* Bundled packages whose GitHub repository is named differently.
37+
*/
38+
const PACKAGE_REPOS = [
39+
'wp-cli/mustangostang-spyc' => 'wp-cli/spyc',
40+
];
41+
42+
/**
43+
* Gets the GitHub repository a bundled package is developed in.
44+
*
45+
* @param string $package Package name.
46+
*
47+
* @return string
48+
*/
49+
public static function get_repo( $package ) {
50+
return isset( self::PACKAGE_REPOS[ $package ] ) ? self::PACKAGE_REPOS[ $package ] : $package;
51+
}
52+
3553
/**
3654
* Gets the milestone of a release repository that belongs to a bundle
3755
* release: the one titled after the release, or the lowest open one when
@@ -231,7 +249,7 @@ public static function get_packages( array $lock ) {
231249
*/
232250
public static function get_shipped_milestones( $package, $previous_version, $current_version ) {
233251
return self::filter_shipped_milestones(
234-
GitHub::get_project_milestones( $package, [ 'state' => 'closed' ] ),
252+
GitHub::get_project_milestones( self::get_repo( $package ), [ 'state' => 'closed' ] ),
235253
$previous_version,
236254
$current_version
237255
);

‎.maintenance/src/Contrib_List_Command.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static function ( $milestone ) {
116116
WP_CLI::debug( "Closed {$package_name} milestone(s): " . implode( ', ', $milestone_titles ), 'contrib-list' );
117117

118118
foreach ( $milestones as $milestone ) {
119-
$pull_requests = GitHub::get_project_milestone_pull_requests( $package_name, $milestone->number );
119+
$pull_requests = GitHub::get_project_milestone_pull_requests( Bundle::get_repo( $package_name ), $milestone->number );
120120
$repo_contributors = GitHub::parse_contributors_from_pull_requests( $pull_requests );
121121
WP_CLI::debug( count( $repo_contributors ) . ' contributors, ' . count( $pull_requests ) . " pull requests in '{$package_name}' milestone '{$milestone->title}'", 'contrib-list' );
122122
$pull_request_count += count( $pull_requests );

‎.maintenance/src/GitHub.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,22 @@ public static function delete_label( $project, $label ) {
270270
* @return array
271271
*/
272272
public static function get_project_milestone_pull_requests( $project, $milestone_id ) {
273+
return self::filter_merged_pull_requests(
274+
self::get_issues( $project, [ 'milestone' => $milestone_id ] )
275+
);
276+
}
277+
278+
/**
279+
* Keeps the merged pull requests out of a list of issues.
280+
*
281+
* @param object[] $issues Issues as returned by the issues API.
282+
*
283+
* @return object[]
284+
*/
285+
public static function filter_merged_pull_requests( array $issues ) {
273286
return array_values(
274287
array_filter(
275-
self::get_issues( $project, [ 'milestone' => $milestone_id ] ),
288+
$issues,
276289
static function ( $issue ) {
277290
return ! empty( $issue->pull_request->merged_at );
278291
}

‎.maintenance/src/Release_Command.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ static function ( $repo ) {
163163
private function get_bundle_repos() {
164164
$default_branch = GitHub::get_default_branch( Bundle::REPO );
165165

166-
return array_keys( Bundle::get_packages( Bundle::get_lock( $default_branch ) ) );
166+
return array_map(
167+
[ Bundle::class, 'get_repo' ],
168+
array_keys( Bundle::get_packages( Bundle::get_lock( $default_branch ) ) )
169+
);
167170
}
168171
}

‎.maintenance/src/Release_Notes.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,17 @@ public static function render( array $sections, $format, $headings ) {
142142
);
143143
}
144144

145-
$entries = [];
146145
foreach ( $section['milestones'] as $notes ) {
147146
if ( null !== $notes['body'] ) {
148147
$lines[] = $notes['body'];
148+
continue;
149149
}
150150

151+
if ( ! $notes['pull_requests'] ) {
152+
continue;
153+
}
154+
155+
$entries = [];
151156
foreach ( $notes['pull_requests'] as $pull_request ) {
152157
$entries[] = sprintf(
153158
$html ? '<li>%1$s [<a href="%3$s">#%2$d</a>]</li>' : '- %1$s [[#%2$d](%3$s)]',
@@ -156,9 +161,7 @@ public static function render( array $sections, $format, $headings ) {
156161
$pull_request['url']
157162
);
158163
}
159-
}
160164

161-
if ( $entries ) {
162165
$lines[] = $html
163166
? '<ul>' . implode( '', $entries ) . '</ul>'
164167
: implode( PHP_EOL, $entries ) . PHP_EOL;

‎.maintenance/src/Release_Notes_Command.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function get_bundle_release_notes( $source, $release, $bundle_ref ) {
114114
continue;
115115
}
116116

117-
$sections[] = Release_Notes::get( $package_name, $milestones, $source );
117+
$sections[] = Release_Notes::get( Bundle::get_repo( $package_name ), $milestones, $source );
118118
}
119119

120120
return $sections;

‎foreach-bundle‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env bash
22
#
3-
# Runs a shell command in the folder of every bundled package, for example:
3+
# Runs a command in the folder of every bundled package. Several arguments are
4+
# run as they are; a single argument is run as a shell command line:
45
#
5-
# ./foreach-bundle git status
6+
# ./foreach-bundle git commit -m "Fix the build"
67
# ./foreach-bundle 'git pull && composer update'
78
#
89
# Keeps going when the command fails in a folder, and exits with an error code
@@ -16,15 +17,19 @@ if [[ $# -eq 0 ]]; then
1617
exit 1
1718
fi
1819

19-
COMMAND="$*"
20-
2120
get_bundle_dependencies()
2221
{
2322
echo "wp-cli/wp-cli-bundle"
2423
curl -fsS "https://raw.githubusercontent.com/wp-cli/wp-cli-bundle/main/composer.json" \
2524
| jq --raw-output '."require" | keys[] | select(startswith("wp-cli/"))'
2625
}
2726

27+
if [[ $# -eq 1 ]]; then
28+
RUN=(bash -c "$1")
29+
else
30+
RUN=("$@")
31+
fi
32+
2833
DEPENDENCIES=$(get_bundle_dependencies)
2934
FAILED=()
3035

@@ -35,7 +40,7 @@ while read -r dependency; do
3540
fi
3641

3742
echo "--- ${repo_path}: ---"
38-
if ! ( cd "${repo_path}" && bash -c "${COMMAND}" ); then
43+
if ! ( cd "${repo_path}" && "${RUN[@]}" ); then
3944
FAILED+=("${repo_path}")
4045
fi
4146
done <<< "${DEPENDENCIES}"

‎tests/BundleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,9 @@ public function test_filter_shipped_milestones_drops_titles_that_are_not_version
191191
$this->titles( Bundle::filter_shipped_milestones( $milestones, '2.1.0', '2.1.3' ) )
192192
);
193193
}
194+
195+
public function test_get_repo_maps_packages_to_their_repository(): void {
196+
$this->assertSame( 'wp-cli/spyc', Bundle::get_repo( 'wp-cli/mustangostang-spyc' ) );
197+
$this->assertSame( 'wp-cli/cache-command', Bundle::get_repo( 'wp-cli/cache-command' ) );
198+
}
194199
}

0 commit comments

Comments
 (0)