|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2021 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}") |
| 22 | +source "${SCRIPT_ROOT}/repos.sh" |
| 23 | + |
| 24 | +if [ "$#" = 0 ] || [ "$#" -gt 2 ]; then |
| 25 | + echo "usage: $0 [source-github-user-name] dest-github-user-name" |
| 26 | + echo |
| 27 | + echo "This connects to [email protected]:<from>/<repo>. Set GITHUB_HOST to access git@<GITHUB_HOST>:<from>/<repo> instead." |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +FROM="kubernetes" |
| 32 | +TO="${1}" |
| 33 | +if [ "$#" -ge 2 ]; then |
| 34 | + FROM="${TO}" |
| 35 | + TO="${2}" |
| 36 | +fi |
| 37 | +GITHUB_HOST=${GITHUB_HOST:-github.com} |
| 38 | + |
| 39 | +repo_count=${#repos[@]} |
| 40 | + |
| 41 | +# safety check |
| 42 | +if [ "${TO}" = "kubernetes" ]; then |
| 43 | + echo "Cannot operate on kubernetes directly" 1>&2 |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +destination_repos=( $(curl -ks https://api.github.com/orgs/${TO}/repos | jq ".[].name" | tr -d '"') ) |
| 48 | +destination_repo_count=${#destination_repos[@]} |
| 49 | + |
| 50 | +if ! command -v gh > /dev/null; then |
| 51 | + echo "Can't find 'gh' tool in PATH, please install from https://github.com/cli/cli" |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Checks if you are logged in. Will error/bail if you are not. |
| 56 | +gh auth status |
| 57 | + |
| 58 | +echo "=======================" |
| 59 | +echo " create repos if needed" |
| 60 | +echo "=======================" |
| 61 | +for (( i=0; i<${repo_count}; i++ )); do |
| 62 | + found=0 |
| 63 | + for (( j=0; j<${destination_repo_count}; j++ )); do |
| 64 | + if [[ "${repos[i]}" == ${destination_repos[j]} ]]; then |
| 65 | + found=1 |
| 66 | + fi |
| 67 | + done |
| 68 | + if [[ $found -eq 1 ]]; then |
| 69 | + echo "repository found: ${repos[i]}" |
| 70 | + else |
| 71 | + echo "repository not found: ${repos[i]}" |
| 72 | + gh repo fork "kubernetes/${repos[i]}" --org "${TO}" --remote --clone=false |
| 73 | + fi |
| 74 | +done |
0 commit comments