Skip to content

Commit 05db281

Browse files
authored
Merge pull request #83 from wp-cli/agent-ready-maintenance
Make the maintenance tooling ready for agents
2 parents 0920696 + 2426b0c commit 05db281

24 files changed

Lines changed: 1448 additions & 826 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: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,64 @@ 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+
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
84+
page=$((page + 1))
85+
done
86+
}
87+
88+
# Clone over the protocol the GitHub CLI is set up for, as its credential helper
89+
# then covers pushing. Without it, use HTTPS on CI and SSH everywhere else.
90+
if [[ -n "${GITHUB_ACTION:-}" ]]; then
91+
PROTOCOL="https"
92+
elif command -v gh &>/dev/null && gh auth status --hostname github.com &>/dev/null; then
93+
PROTOCOL=$(gh config get git_protocol --host github.com 2>/dev/null || echo "https")
94+
else
95+
PROTOCOL="ssh"
6496
fi
6597

98+
REPOSITORIES=$(fetch_repositories)
99+
66100
is_skipped() {
67101
local name="$1"
68102
for skip in "${SKIP_LIST[@]}"; do
@@ -84,22 +118,26 @@ CLONE_LIST=()
84118
UPDATE_FOLDERS=()
85119

86120
while IFS=$'\t' read -r name clone_url ssh_url; do
121+
if [[ -z "${name}" ]]; then
122+
continue
123+
fi
124+
87125
if is_skipped "${name}"; then
88126
continue
89127
fi
90128

91129
destination=$(get_destination "${name}")
92130

93131
if [[ ! -d "${destination}" ]]; then
94-
if [[ -n "${GITHUB_ACTION:-}" ]]; then
95-
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
96-
else
132+
if [[ "${PROTOCOL}" == "ssh" ]]; then
97133
CLONE_LIST+=("${destination}"$'\t'"${ssh_url}")
134+
else
135+
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
98136
fi
99137
fi
100138

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

104142
if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then
105143
printf '%s\n' "${CLONE_LIST[@]}" | xargs -n2 -P"${CORES}" bash "${SCRIPT_DIR}/clone-repository.sh"
Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,83 @@
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. Exits with an error code when git fails.
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+
$path = (string) realpath( $repository );
14+
$git = 'git -C ' . escapeshellarg( $path );
915

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

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)" );
27+
return 0 === $exit_code ? trim( implode( "\n", $output ) ) : null;
28+
}
29+
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+
}
1440

15-
printf( "Pulling latest changes...\n" );
16-
system( "git --git-dir={$path}/.git --work-tree={$path} pull" );
41+
printf( "--- Refreshing repository \033[32m%s\033[0m ---\n", $repository );
42+
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+
}
54+
55+
$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' );
56+
if ( null === $default_branch ) {
57+
// Clones made before the remote HEAD was recorded need it looked up once.
58+
git_output( $git, 'remote set-head origin --auto' );
59+
$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' );
60+
}
61+
$default_branch = null === $default_branch ? null : preg_replace( '#^origin/#', '', $default_branch );
62+
$current_branch = git_output( $git, 'rev-parse --abbrev-ref HEAD' );
63+
$changes = git_output( $git, 'status --porcelain' );
64+
65+
if ( null === $changes ) {
66+
fail( $repository, 'its status could not be read.' );
67+
}
68+
69+
if ( '' !== $changes ) {
70+
printf( "Leaving \033[33m%s\033[0m alone, it has uncommitted changes.\n", $repository );
71+
exit;
72+
}
73+
74+
if ( null === $default_branch || $current_branch !== $default_branch ) {
75+
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' );
76+
exit;
77+
}
78+
79+
printf( "Pulling latest changes into %s...\n", $default_branch );
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
);

0 commit comments

Comments
 (0)