Skip to content

Commit 86e3907

Browse files
schlesseraclaude
andcommitted
Make the maintenance tooling ready for agents
Authenticate transparently through the GitHub CLI with a token fallback, add `wp maintenance doctor`, add JSON output, let the commands run without a full install, stop the refresh script from switching branches and fix the bugs found while auditing the commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 79297c7 commit 86e3907

22 files changed

Lines changed: 1312 additions & 828 deletions

‎.maintenance/autoload.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Loads the maintenance commands.
4+
*
5+
* Within a full development environment, Composer does that. Without one, any
6+
* `wp` executable can still run them, so the maintenance tooling only needs
7+
* WP-CLI, git and the GitHub CLI, not a complete install of every package.
8+
*/
9+
10+
$composer_autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
11+
12+
if ( is_file( $composer_autoloader ) ) {
13+
require_once $composer_autoloader;
14+
return;
15+
}
16+
17+
spl_autoload_register(
18+
static function ( $class_name ) {
19+
$prefix = 'WP_CLI\\Maintenance\\';
20+
21+
if ( 0 !== strpos( $class_name, $prefix ) ) {
22+
return;
23+
}
24+
25+
$file = __DIR__ . '/src/' . str_replace( '\\', '/', substr( $class_name, strlen( $prefix ) ) ) . '.php';
26+
27+
if ( is_file( $file ) ) {
28+
require $file;
29+
}
30+
}
31+
);
32+
33+
require_once __DIR__ . '/bootstrap.php';

‎.maintenance/bootstrap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
WP_CLI::add_command( 'maintenance', 'WP_CLI\Maintenance\Maintenance_Namespace' );
88

99
WP_CLI::add_command( 'maintenance contrib-list', 'WP_CLI\Maintenance\Contrib_List_Command' );
10+
WP_CLI::add_command( 'maintenance doctor', 'WP_CLI\Maintenance\Doctor_Command' );
1011
WP_CLI::add_command( 'maintenance milestones-after', 'WP_CLI\Maintenance\Milestones_After_Command' );
1112
WP_CLI::add_command( 'maintenance milestones-since', 'WP_CLI\Maintenance\Milestones_Since_Command' );
1213
WP_CLI::add_command( 'maintenance release', 'WP_CLI\Maintenance\Release_Command' );

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

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,61 @@ elif [[ -z "${CLONE_JOBS:-}" && -z "${WPCLI_DEV_JOBS:-}" && "${CORES}" -gt "${MA
3939
CORES=${MAX_CORES}
4040
fi
4141

42-
# Fetch repository list from the GitHub API.
43-
CURL_OPTS=(-fsS)
44-
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
45-
CURL_OPTS+=(--header "Authorization: Bearer ${GITHUB_TOKEN}")
46-
fi
47-
48-
if ! RESPONSE=$(curl "${CURL_OPTS[@]}" 'https://api.github.com/orgs/wp-cli/repos?per_page=100'); then
49-
echo "Failed to fetch repository list from the GitHub API." >&2
50-
exit 1
51-
fi
42+
# Lists the repositories of the organization as "name<TAB>clone_url<TAB>ssh_url"
43+
# lines. Uses the GitHub CLI when it is logged in, and the GitHub API with
44+
# GH_TOKEN or GITHUB_TOKEN, or anonymously, otherwise.
45+
fetch_repositories() {
46+
if command -v gh &>/dev/null && gh auth status --hostname github.com &>/dev/null; then
47+
gh api --paginate 'orgs/wp-cli/repos?per_page=100' --jq '.[] | [.name, .clone_url, .ssh_url] | @tsv'
48+
return
49+
fi
5250

53-
# Validate the response shape and detect API errors such as rate limiting.
54-
if ! jq -e 'type == "array"' >/dev/null <<< "${RESPONSE}"; then
55-
if jq -e '.message' >/dev/null <<< "${RESPONSE}"; then
56-
MESSAGE=$(jq -r '.message' <<< "${RESPONSE}")
57-
echo "GitHub responded with: ${MESSAGE}" >&2
58-
echo "If you are running into a rate limiting issue during large events please set GITHUB_TOKEN environment variable." >&2
59-
echo "See https://github.com/settings/tokens" >&2
60-
else
61-
echo "GitHub API returned an unexpected response; expected a JSON array of repositories." >&2
51+
local curl_opts=(-sS)
52+
local token="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
53+
if [[ -n "${token}" ]]; then
54+
curl_opts+=(--header "Authorization: Bearer ${token}")
6255
fi
63-
exit 1
56+
57+
local page=1 response message
58+
while true; do
59+
if ! response=$(curl "${curl_opts[@]}" "https://api.github.com/orgs/wp-cli/repos?per_page=100&page=${page}"); then
60+
echo "Failed to fetch repository list from the GitHub API." >&2
61+
return 1
62+
fi
63+
64+
# Validate the response shape and detect API errors such as rate limiting.
65+
if ! jq -e 'type == "array"' >/dev/null <<< "${response}"; then
66+
if jq -e '.message' >/dev/null 2>&1 <<< "${response}"; then
67+
message=$(jq -r '.message' <<< "${response}")
68+
echo "GitHub responded with: ${message}" >&2
69+
echo "If you are running into a rate limiting issue, log in with 'gh auth login' or set the GITHUB_TOKEN environment variable." >&2
70+
else
71+
echo "GitHub API returned an unexpected response; expected a JSON array of repositories." >&2
72+
fi
73+
return 1
74+
fi
75+
76+
if [[ $(jq 'length' <<< "${response}") -eq 0 ]]; then
77+
return
78+
fi
79+
80+
jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv' <<< "${response}"
81+
page=$((page + 1))
82+
done
83+
}
84+
85+
# Clone over the protocol the GitHub CLI is set up for, as its credential helper
86+
# then covers pushing. Without it, use HTTPS on CI and SSH everywhere else.
87+
if [[ -n "${GITHUB_ACTION:-}" ]]; then
88+
PROTOCOL="https"
89+
elif command -v gh &>/dev/null && gh auth status --hostname github.com &>/dev/null; then
90+
PROTOCOL=$(gh config get git_protocol --host github.com 2>/dev/null || echo "https")
91+
else
92+
PROTOCOL="ssh"
6493
fi
6594

95+
REPOSITORIES=$(fetch_repositories)
96+
6697
is_skipped() {
6798
local name="$1"
6899
for skip in "${SKIP_LIST[@]}"; do
@@ -84,22 +115,26 @@ CLONE_LIST=()
84115
UPDATE_FOLDERS=()
85116

86117
while IFS=$'\t' read -r name clone_url ssh_url; do
118+
if [[ -z "${name}" ]]; then
119+
continue
120+
fi
121+
87122
if is_skipped "${name}"; then
88123
continue
89124
fi
90125

91126
destination=$(get_destination "${name}")
92127

93128
if [[ ! -d "${destination}" ]]; then
94-
if [[ -n "${GITHUB_ACTION:-}" ]]; then
95-
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
96-
else
129+
if [[ "${PROTOCOL}" == "ssh" ]]; then
97130
CLONE_LIST+=("${destination}"$'\t'"${ssh_url}")
131+
else
132+
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
98133
fi
99134
fi
100135

101136
UPDATE_FOLDERS+=("${destination}")
102-
done < <(echo "${RESPONSE}" | jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv')
137+
done <<< "${REPOSITORIES}"
103138

104139
if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then
105140
printf '%s\n' "${CLONE_LIST[@]}" | xargs -n2 -P"${CORES}" bash "${SCRIPT_DIR}/clone-repository.sh"
Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11
<?php
2+
/**
3+
* Brings a clone up to date with its remote, without ever touching work in
4+
* progress: a clone with uncommitted changes, or one on another branch than
5+
* the default one, is only fetched.
6+
*/
27

38
if ( ! isset( $argv[1] ) || ! is_dir( $argv[1] ) ) {
49
exit( "Usage: php refresh-repository.php <repository-folder>\n" );
510
}
611

712
$repository = $argv[1];
8-
$path = realpath( getcwd() . "/{$repository}" );
13+
$git = 'git -C ' . escapeshellarg( (string) realpath( $repository ) );
914

10-
printf( "--- Refreshing repository \033[32m{$repository}\033[0m ---\n" );
15+
/**
16+
* Runs a git command and returns its output, or null when it fails.
17+
*
18+
* @param string $git Git executable with its working directory.
19+
* @param string $command Git command and arguments.
20+
*
21+
* @return string|null
22+
*/
23+
function git_output( $git, $command ) {
24+
exec( "{$git} {$command} 2>/dev/null", $output, $exit_code );
1125

12-
printf( "Switching to latest \033[33mdefault\033[0m branch...\n" );
13-
system( "git --git-dir={$path}/.git --work-tree={$path} checkout $(git --git-dir={$path}/.git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)" );
26+
return 0 === $exit_code ? trim( implode( "\n", $output ) ) : null;
27+
}
28+
29+
printf( "--- Refreshing repository \033[32m%s\033[0m ---\n", $repository );
30+
31+
passthru( "{$git} fetch --quiet origin" );
32+
33+
$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' );
34+
if ( null === $default_branch ) {
35+
// Clones made before the remote HEAD was recorded need it looked up once.
36+
git_output( $git, 'remote set-head origin --auto' );
37+
$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' );
38+
}
39+
$default_branch = null === $default_branch ? null : preg_replace( '#^origin/#', '', $default_branch );
40+
$current_branch = git_output( $git, 'rev-parse --abbrev-ref HEAD' );
41+
42+
if ( '' !== (string) git_output( $git, 'status --porcelain' ) ) {
43+
printf( "Leaving \033[33m%s\033[0m alone, it has uncommitted changes.\n", $repository );
44+
exit;
45+
}
46+
47+
if ( null === $default_branch || $current_branch !== $default_branch ) {
48+
printf( "Leaving \033[33m%s\033[0m alone, it is on branch %s instead of %s.\n", $repository, $current_branch, $default_branch ?: 'the default branch' );
49+
exit;
50+
}
1451

15-
printf( "Pulling latest changes...\n" );
16-
system( "git --git-dir={$path}/.git --work-tree={$path} pull" );
52+
printf( "Pulling latest changes into %s...\n", $default_branch );
53+
passthru( "{$git} pull --quiet --ff-only" );

‎.maintenance/src/Contrib_List_Command.php‎

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ final class Contrib_List_Command {
88
/**
99
* Lists all contributors to this release.
1010
*
11-
* Run within the main WP-CLI project repository.
11+
* Only authors of merged pull requests count; bots are left out.
1212
*
1313
* ## OPTIONS
1414
*
1515
* [<repo>]
16-
* : Name of the repository to fetch the release notes for. If no user/org
17-
* was provided, 'wp-cli' org is assumed. If no repo is passed, release
18-
* notes for the entire org state since the last bundle release are fetched.
16+
* : Name of the repository to list the contributors for. If no user/org
17+
* was provided, 'wp-cli' org is assumed. If no repo is passed, the
18+
* contributors to the entire org since the last bundle release are listed.
1919
*
2020
* [<milestone>...]
21-
* : Name of one or more milestones to fetch the release notes for. If none
21+
* : Name of one or more milestones to list the contributors for. If none
2222
* are passed, the current open one is assumed.
2323
*
2424
* [--release=<version>]
@@ -41,6 +41,7 @@ final class Contrib_List_Command {
4141
* options:
4242
* - markdown
4343
* - html
44+
* - json
4445
* ---
4546
*
4647
* @when before_wp_load
@@ -49,18 +50,15 @@ public function __invoke( $args, $assoc_args ) {
4950
$repos = null;
5051
$use_bundle = false;
5152

52-
$ignored_contributors = [
53-
'github-actions[bot]',
54-
];
55-
5653
if ( count( $args ) > 0 ) {
57-
$repos = [ array_shift( $args ) ];
54+
$repos = [ GitHub::qualify_repo( array_shift( $args ) ) ];
5855
}
5956

6057
$milestone_names = $args;
6158

6259
$release = Utils\get_flag_value( $assoc_args, 'release' );
6360
$bundle_ref = Utils\get_flag_value( $assoc_args, 'bundle-ref' );
61+
$format = Utils\get_flag_value( $assoc_args, 'format', 'markdown' );
6462

6563
if ( empty( $repos ) ) {
6664
$use_bundle = true;
@@ -73,39 +71,7 @@ public function __invoke( $args, $assoc_args ) {
7371
// Get the contributors to the current open large project milestones
7472
foreach ( $repos as $repo ) {
7573
if ( $milestone_names ) {
76-
$milestone_names = (array) $milestone_names;
77-
78-
$potential_milestones = GitHub::get_project_milestones(
79-
$repo,
80-
array( 'state' => 'all' )
81-
);
82-
83-
$milestones = array();
84-
foreach ( $potential_milestones as $potential_milestone ) {
85-
if ( in_array(
86-
$potential_milestone->title,
87-
$milestone_names,
88-
true
89-
) ) {
90-
$milestones[] = $potential_milestone;
91-
$index = array_search(
92-
$potential_milestone->title,
93-
$milestone_names,
94-
true
95-
);
96-
unset( $milestone_names[ $index ] );
97-
}
98-
}
99-
100-
if ( ! empty( $milestone_names ) ) {
101-
WP_CLI::warning(
102-
sprintf(
103-
"Couldn't find the requested milestone(s) '%s' in repository '%s'.",
104-
implode( "', '", $milestone_names ),
105-
$repo
106-
)
107-
);
108-
}
74+
$milestones = Release_Notes::find_milestones( $repo, $milestone_names );
10975
} else {
11076
$milestone = Bundle::get_release_milestone( $repo, $use_bundle ? $release : null );
11177
$milestones = $milestone ? [ $milestone ] : [];
@@ -159,24 +125,39 @@ static function ( $milestone ) {
159125
}
160126
}
161127

162-
$contributors = array_diff( $contributors, $ignored_contributors );
128+
asort( $contributors, SORT_NATURAL | SORT_FLAG_CASE );
129+
130+
if ( 'json' === $format ) {
131+
$list = [];
132+
foreach ( $contributors as $url => $login ) {
133+
$list[] = [
134+
'login' => $login,
135+
'url' => $url,
136+
];
137+
}
138+
139+
WP_CLI::line(
140+
(string) json_encode(
141+
[
142+
'total_contributors' => count( $contributors ),
143+
'total_pull_requests' => $pull_request_count,
144+
'contributors' => $list,
145+
],
146+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
147+
)
148+
);
149+
return;
150+
}
163151

164152
WP_CLI::log( 'Total contributors: ' . count( $contributors ) );
165153
WP_CLI::log( 'Total pull requests: ' . $pull_request_count );
166154

167-
// Sort and render the contributor list
168-
asort( $contributors, SORT_NATURAL | SORT_FLAG_CASE );
169-
if ( in_array( $assoc_args['format'], array( 'markdown', 'html' ), true ) ) {
170-
$contrib_list = '';
171-
foreach ( $contributors as $url => $login ) {
172-
if ( 'markdown' === $assoc_args['format'] ) {
173-
$contrib_list .= '[@' . $login . '](' . $url . '), ';
174-
} elseif ( 'html' === $assoc_args['format'] ) {
175-
$contrib_list .= '<a href="' . $url . '">@' . $login . '</a>, ';
176-
}
177-
}
178-
$contrib_list = rtrim( $contrib_list, ', ' );
179-
WP_CLI::log( $contrib_list );
155+
$contrib_list = [];
156+
foreach ( $contributors as $url => $login ) {
157+
$contrib_list[] = 'html' === $format
158+
? '<a href="' . $url . '">@' . $login . '</a>'
159+
: '[@' . $login . '](' . $url . ')';
180160
}
161+
WP_CLI::log( implode( ', ', $contrib_list ) );
181162
}
182163
}

0 commit comments

Comments
 (0)