Last active
April 28, 2022 16:13
-
-
Save benjyiw/3b2f699bb933702a5fb8926c70e1a86a to your computer and use it in GitHub Desktop.
pulls or clones all repositories in a list of orgs and/or users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # pulls or clones all repositories in a list of orgs and/or users | |
| set -euo pipefail | |
| # required input | |
| GITHUB_USER="" | |
| GITHUB_PAT="" | |
| REPOS_PATH="${HOME}/git" | |
| orgs=( | |
| # orgs go here ... | |
| ) | |
| users=( | |
| # users go here ... | |
| ) | |
| GITHUB_URL="https://api.github.com/" | |
| get_repos() { | |
| # orgs or users | |
| local identity_type="${1}" | |
| # org name or user name | |
| local identity="${2}" | |
| curl -u ${GITHUB_USER}:${GITHUB_PAT} -s \ | |
| "${GITHUB_URL}${identity_type}/${identity}/repos?per_page=200" | | |
| jq -r '.[].ssh_url' | |
| } | |
| clone_or_pull() { | |
| local repo="${1}" | |
| local dir="${repo/*:}" | |
| dir="${REPOS_PATH}/${dir%.git}" | |
| if [[ -d ${dir} ]]; then | |
| git -C "${dir}" pull --quiet | |
| else | |
| git clone --quiet "${repo}" "${dir}" | |
| fi | |
| } | |
| for org in ${orgs[@]}; do | |
| repos=$(get_repos orgs $org) | |
| for repo in ${repos[@]}; do | |
| clone_or_pull "${repo}" | |
| done | |
| done | |
| for user in ${users[@]}; do | |
| repos=$(get_repos users $user) | |
| for repo in ${repos[@]}; do | |
| clone_or_pull "${repo}" | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment