Last active
April 16, 2019 05:12
-
-
Save os1ma/cf25ceb590147e9a07619b7ab639dc91 to your computer and use it in GitHub Desktop.
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 -eu -o pipefail | |
# | |
# ある組織の全リポジトリをPull (CloneしていなければClone) するシェルスクリプト | |
# | |
# 前提: GitHubのSSH鍵が設定済であること | |
# 依存関係: git, jq | |
# 注意: GitHub APIの制限に注意してください | |
# カレントディレクトリではなく、このスクリプトが存在するディレクトリで実行する | |
cd `dirname $0` | |
# 組織、ユーザ、パスワードの設定 | |
read -p "Organizatin: " ORGANIZATION | |
read -p "Username: " USERNAME | |
read -sp "Password: " PASSWORD | |
# | |
# GitHub APIを叩き、組織のリポジトリ一覧を取得する関数 | |
# | |
function get-organization-repositories () { | |
local ORGANIZATION=$1 | |
local USERNAME=$2 | |
local PASSWORD=$3 | |
curl -s -u $USERNAME:$PASSWORD https://api.github.com/orgs/$ORGANIZATION/repos | |
} | |
# | |
# リポジトリ一覧をリポジトリ名一覧に変換する関数 | |
# | |
function repositories-to-repository-names () { | |
cat - | jq -r '.[].name' | |
} | |
# | |
# 全リポジトリをPullする関数 | |
# | |
function pull-repositories () { | |
while read LINE | |
do | |
pull-repository $LINE | |
done | |
} | |
# | |
# 引数のリポジトリをPullする関数 | |
# | |
function pull-repository () { | |
set -x | |
local REPOSITORY=$1 | |
if [ -e $REPOSITORY ]; then | |
cd $REPOSITORY | |
DEFAULT_BRANCH=`get-default-branch-of-current-directory` | |
git checkout $DEFAULT_BRANCH | |
git pull | |
cd .. | |
else | |
git clone [email protected]:$ORGANIZATION/$REPOSITORY.git | |
fi | |
set +x | |
} | |
# | |
# カレントディレクトリのデフォルトのブランチを取得する関数 | |
# | |
function get-default-branch-of-current-directory () { | |
git remote show origin | grep 'HEAD' | awk '{print $NF}' | |
} | |
# #### # | |
# Main # | |
# #### # | |
get-organization-repositories $ORGANIZATION $USERNAME $PASSWORD \ | |
| repositories-to-repository-names \ | |
| pull-repositories |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment