Skip to content

Commit a7630d5

Browse files
Copilotswissspidy
andauthored
Apply reviewer suggestions: jq check, curl -fsS, JSON array validation, cap cores to 8 with env override, drop redundant -n1, exact arg count in clone helper
Agent-Logs-Url: https://github.com/wp-cli/wp-cli-dev/sessions/aab74529-eccb-4e94-949e-b1062afb30b2 Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 94182ad commit a7630d5

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

.maintenance/clone-all-repositories.sh

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ set -euo pipefail
44

55
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
66

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+
712
SKIP_LIST=(
813
"autoload-splitter"
914
"composer-changelogs"
@@ -18,27 +23,43 @@ SKIP_LIST=(
1823

1924
# Detect number of CPU cores, defaulting to 4.
2025
if command -v nproc &>/dev/null; then
21-
CORES=$(nproc)
26+
DETECTED_CORES=$(nproc)
2227
elif command -v sysctl &>/dev/null; then
23-
CORES=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
28+
DETECTED_CORES=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
2429
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
2537
CORES=4
38+
elif [[ -z "${CLONE_JOBS:-}" && -z "${WPCLI_DEV_JOBS:-}" && "${CORES}" -gt "${MAX_CORES}" ]]; then
39+
CORES=${MAX_CORES}
2640
fi
2741

2842
# Fetch repository list from the GitHub API.
29-
CURL_OPTS=(-s)
43+
CURL_OPTS=(-fsS)
3044
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
3145
CURL_OPTS+=(--header "Authorization: Bearer ${GITHUB_TOKEN}")
3246
fi
3347

34-
RESPONSE=$(curl "${CURL_OPTS[@]}" 'https://api.github.com/orgs/wp-cli/repos?per_page=100')
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
3552

36-
# Detect API errors such as rate limiting.
37-
if echo "${RESPONSE}" | jq -e '.message' &>/dev/null; then
38-
MESSAGE=$(echo "${RESPONSE}" | jq -r '.message')
39-
echo "GitHub responded with: ${MESSAGE}"
40-
echo "If you are running into a rate limiting issue during large events please set GITHUB_TOKEN environment variable."
41-
echo "See https://github.com/settings/tokens"
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
4263
exit 1
4364
fi
4465

@@ -85,5 +106,5 @@ if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then
85106
fi
86107

87108
if [[ ${#UPDATE_FOLDERS[@]} -gt 0 ]]; then
88-
printf '%s\n' "${UPDATE_FOLDERS[@]}" | xargs -n1 -P"${CORES}" -I% php "${SCRIPT_DIR}/refresh-repository.php" %
109+
printf '%s\n' "${UPDATE_FOLDERS[@]}" | xargs -P"${CORES}" -I% php "${SCRIPT_DIR}/refresh-repository.php" %
89110
fi

.maintenance/clone-repository.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -euo pipefail
44

5-
if [[ $# -lt 2 ]]; then
5+
if [[ $# -ne 2 ]]; then
66
echo "Usage: clone-repository.sh <destination> <clone_url>" >&2
77
exit 1
88
fi

0 commit comments

Comments
 (0)