|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | + |
| 7 | +SKIP_LIST=( |
| 8 | + "autoload-splitter" |
| 9 | + "composer-changelogs" |
| 10 | + "dash-docset-generator" |
| 11 | + "ideas" |
| 12 | + "package-index" |
| 13 | + "regenerate-readme" |
| 14 | + "sample-plugin" |
| 15 | + "wp-cli-dev" |
| 16 | + "wp-cli-roadmap" |
| 17 | +) |
| 18 | + |
| 19 | +# Detect number of CPU cores, defaulting to 4. |
| 20 | +if command -v nproc &>/dev/null; then |
| 21 | + CORES=$(nproc) |
| 22 | +elif command -v sysctl &>/dev/null; then |
| 23 | + CORES=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4) |
| 24 | +else |
| 25 | + CORES=4 |
| 26 | +fi |
| 27 | + |
| 28 | +# Fetch repository list from the GitHub API. |
| 29 | +CURL_OPTS=(-s) |
| 30 | +if [[ -n "${GITHUB_TOKEN:-}" ]]; then |
| 31 | + CURL_OPTS+=(--header "Authorization: Bearer ${GITHUB_TOKEN}") |
| 32 | +fi |
| 33 | + |
| 34 | +RESPONSE=$(curl "${CURL_OPTS[@]}" 'https://api.github.com/orgs/wp-cli/repos?per_page=100') |
| 35 | + |
| 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" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +is_skipped() { |
| 46 | + local name="$1" |
| 47 | + for skip in "${SKIP_LIST[@]}"; do |
| 48 | + [[ "${skip}" == "${name}" ]] && return 0 |
| 49 | + done |
| 50 | + return 1 |
| 51 | +} |
| 52 | + |
| 53 | +get_destination() { |
| 54 | + local name="$1" |
| 55 | + if [[ "${name}" == ".github" ]]; then |
| 56 | + echo "dot-github" |
| 57 | + else |
| 58 | + echo "${name}" |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +CLONE_LIST=() |
| 63 | +UPDATE_FOLDERS=() |
| 64 | + |
| 65 | +while IFS=$'\t' read -r name clone_url ssh_url; do |
| 66 | + if is_skipped "${name}"; then |
| 67 | + continue |
| 68 | + fi |
| 69 | + |
| 70 | + destination=$(get_destination "${name}") |
| 71 | + |
| 72 | + if [[ ! -d "${destination}" ]]; then |
| 73 | + if [[ -n "${GITHUB_ACTION:-}" ]]; then |
| 74 | + CLONE_LIST+=("${destination}"$'\t'"${clone_url}") |
| 75 | + else |
| 76 | + CLONE_LIST+=("${destination}"$'\t'"${ssh_url}") |
| 77 | + fi |
| 78 | + fi |
| 79 | + |
| 80 | + UPDATE_FOLDERS+=("${destination}") |
| 81 | +done < <(echo "${RESPONSE}" | jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv') |
| 82 | + |
| 83 | +if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then |
| 84 | + printf '%s\n' "${CLONE_LIST[@]}" | xargs -n2 -P"${CORES}" bash "${SCRIPT_DIR}/clone-repository.sh" |
| 85 | +fi |
| 86 | + |
| 87 | +if [[ ${#UPDATE_FOLDERS[@]} -gt 0 ]]; then |
| 88 | + printf '%s\n' "${UPDATE_FOLDERS[@]}" | xargs -n1 -P"${CORES}" -I% php "${SCRIPT_DIR}/refresh-repository.php" % |
| 89 | +fi |
0 commit comments