Skip to content

Commit 49fc7d1

Browse files
authored
Merge pull request #72 from wp-cli/copilot/parallelize-repository-cloning
2 parents f7962bf + a7630d5 commit 49fc7d1

File tree

4 files changed

+126
-58
lines changed

4 files changed

+126
-58
lines changed

.maintenance/clone-all-repositories.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
7+
if ! command -v jq &>/dev/null; then
8+
echo "Required command 'jq' is not installed or not available in PATH." >&2
9+
exit 1
10+
fi
11+
12+
SKIP_LIST=(
13+
"autoload-splitter"
14+
"composer-changelogs"
15+
"dash-docset-generator"
16+
"ideas"
17+
"package-index"
18+
"regenerate-readme"
19+
"sample-plugin"
20+
"wp-cli-dev"
21+
"wp-cli-roadmap"
22+
)
23+
24+
# Detect number of CPU cores, defaulting to 4.
25+
if command -v nproc &>/dev/null; then
26+
DETECTED_CORES=$(nproc)
27+
elif command -v sysctl &>/dev/null; then
28+
DETECTED_CORES=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
29+
else
30+
DETECTED_CORES=4
31+
fi
32+
33+
MAX_CORES=8
34+
CORES="${CLONE_JOBS:-${WPCLI_DEV_JOBS:-${DETECTED_CORES}}}"
35+
36+
if ! [[ "${CORES}" =~ ^[1-9][0-9]*$ ]]; then
37+
CORES=4
38+
elif [[ -z "${CLONE_JOBS:-}" && -z "${WPCLI_DEV_JOBS:-}" && "${CORES}" -gt "${MAX_CORES}" ]]; then
39+
CORES=${MAX_CORES}
40+
fi
41+
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
52+
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
62+
fi
63+
exit 1
64+
fi
65+
66+
is_skipped() {
67+
local name="$1"
68+
for skip in "${SKIP_LIST[@]}"; do
69+
[[ "${skip}" == "${name}" ]] && return 0
70+
done
71+
return 1
72+
}
73+
74+
get_destination() {
75+
local name="$1"
76+
if [[ "${name}" == ".github" ]]; then
77+
echo "dot-github"
78+
else
79+
echo "${name}"
80+
fi
81+
}
82+
83+
CLONE_LIST=()
84+
UPDATE_FOLDERS=()
85+
86+
while IFS=$'\t' read -r name clone_url ssh_url; do
87+
if is_skipped "${name}"; then
88+
continue
89+
fi
90+
91+
destination=$(get_destination "${name}")
92+
93+
if [[ ! -d "${destination}" ]]; then
94+
if [[ -n "${GITHUB_ACTION:-}" ]]; then
95+
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
96+
else
97+
CLONE_LIST+=("${destination}"$'\t'"${ssh_url}")
98+
fi
99+
fi
100+
101+
UPDATE_FOLDERS+=("${destination}")
102+
done < <(echo "${RESPONSE}" | jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv')
103+
104+
if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then
105+
printf '%s\n' "${CLONE_LIST[@]}" | xargs -n2 -P"${CORES}" bash "${SCRIPT_DIR}/clone-repository.sh"
106+
fi
107+
108+
if [[ ${#UPDATE_FOLDERS[@]} -gt 0 ]]; then
109+
printf '%s\n' "${UPDATE_FOLDERS[@]}" | xargs -P"${CORES}" -I% php "${SCRIPT_DIR}/refresh-repository.php" %
110+
fi

.maintenance/clone-repository.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
if [[ $# -ne 2 ]]; then
6+
echo "Usage: clone-repository.sh <destination> <clone_url>" >&2
7+
exit 1
8+
fi
9+
10+
destination="$1"
11+
clone_url="$2"
12+
13+
printf "Fetching \033[32m%s\033[0m...\n" "${destination}"
14+
git clone "${clone_url}" "${destination}"

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@
319319
"minimum-stability": "dev",
320320
"prefer-stable": true,
321321
"scripts": {
322-
"pre-install-cmd": "php .maintenance/clone-all-repositories.php",
323-
"pre-update-cmd": "php .maintenance/clone-all-repositories.php",
322+
"pre-install-cmd": "bash .maintenance/clone-all-repositories.sh",
323+
"pre-update-cmd": "bash .maintenance/clone-all-repositories.sh",
324324
"post-install-cmd": [
325325
"php .maintenance/symlink-vendor-folders.php",
326326
"php .maintenance/phpstorm.exclude-recursive-folders.php"

0 commit comments

Comments
 (0)