Skip to content

Instantly share code, notes, and snippets.

@whiteinge
Last active June 28, 2017 16:45
Show Gist options
  • Save whiteinge/6198749 to your computer and use it in GitHub Desktop.
Save whiteinge/6198749 to your computer and use it in GitHub Desktop.
Functions to more easily work with a ton of organization repos
#!/bin/bash
# Functions to more easily work with a ton of organization repos
#
# Requires ok.sh and Git.
#
# Available commands: ${ALL_FUNCS}
# Lazyness!
ok="$(which ok.sh)"
eval "$(awk '$0 ~ /^_helptext/, /^}/ { print }' $ok)"
eval "$(awk '$0 ~ /^help/, /^}/ { print }' $ok)"
NAME=$(basename $0)
FDIR=$(dirname $0)
GH_ORG="saltstack-formulas"
GH_SSH="git@github.com"
CONTRIBUTORS_TEAM=481700
DOCS="http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html"
export OK_SH_NEXT=1
_wait_for_ssh () {
# Block until a multiplexed ssh connection is ready
#
# Useful for making a single ssh connection that can be reused for many ssh
# operations. This requires ControlMaster and ControlPath to be configured
# in your ~/.ssh/config file.
#
# Usage:
# SSH="me@example.com"
# trap 'ssh -O exit '${SSH} SIGINT SIGTERM EXIT
# ssh -N ${SSH} &
# _wait_for_ssh ${SSH}
# ...use multiplexed ssh connection here...
[[ $# -eq 1 ]] || { echo "ssh hostname required"; exit 1; }
local ssh="${1}"
echo -n "Connecting to GitHub."
while ! ssh -O check ${ssh} &>/dev/null true; do
echo -n '.' ; sleep 0.5;
done
echo -e "\nConnected!\n"
}
get_repos () {
# Return an alphebetized list of all repos in the org
#
# Usage: get_repos
ok.sh org_repos ${GH_ORG} _filter='.[] | "\(.name)"' | sort
}
get_clones () {
# Returns an alphebetized list of all clones in the formulas dir
#
# Usage: get_clones
find ${FDIR} -maxdepth 1 -mindepth 1 -type d -printf '%P\n' | sort
}
update_repo () {
# Add the correct meta data to an existing repo
#
# Usage: add_homepage reponame
local repo="${1}"
[[ $# -eq 1 ]] || { echo "Repo name required"; exit 1; }
ok.sh _format_json name="$repo" homepage="$DOCS" has_issues=true \
| ok.sh _request "/repos/${GH_ORG}/${repo}" method=PATCH \
| ok.sh _response \
| ok.sh _filter_json '"\(.name)\t\(.has_issues)"'
}
add_repo () {
# Create a new repo with the given name
#
# Usage: add_repo newreponame
ok.sh create_repo "$1" homepage="$DOCS" has_issues=true \
organization="$GH_ORG" team_id="$CONTRIBUTORS_TEAM"
}
add_hooks () {
# Enable the IRC hook for the given repo
#
# Usage: add_hooks reponame
[[ $# -eq 1 ]] || { echo "Repo name required"; exit 1; }
local repo="${1}"
printf '{
"name": "irc",
"events": [
"push",
"pull_request"
],
"config": {
"server": "irc.freenode.net",
"port": "6667",
"room": "#salt-devel",
"nick": "GHCommits",
"branch_regexes": "",
"nickserv_password": "",
"password": "",
"notice": "1"
}
}\n' \
| ok.sh _request "/repos/${GH_ORG}/${repo}/hooks" \
| ok.sh _filter_json '"\(.name)\t\(.last_response.status)\t\(.updated_at)"'
}
add_to_team() {
# Add a repo to the contributors team
#
# Usage: add_to_team reponame
[[ $# -eq 1 ]] || { echo "Repo name required"; exit 1; }
local repo="${1}"
ok.sh _request "/teams/${CONTRIBUTORS_TEAM}/repos/${GH_ORG}/${repo}" \
method=PUT < /dev/null
}
fetch_all () {
# Run git fetch on all local repos
#
# Usage fetch_all
# Start a connection and wait for it; exit when we're done
trap 'ssh -O exit '${GH_SSH} SIGINT SIGTERM EXIT
ssh -N ${GH_SSH} &
_wait_for_ssh ${GH_SSH}
# Kick off a ton of parallel fetch operations
time find ${FDIR} -type d -name .git -print0 |\
xargs -t -r -0 -P5 -I@ git --git-dir=@ fetch -a
local count=$(find ${FDIR} -type d -name .git -print | wc -l)
printf 'Fetched upstream changes for %s repositories.\n' "$count"
}
get_new () {
# Clone any repos that do not yet exist locally
#
# Usage: get_new
comm -2 -3 <(get_repos) <(get_clones) \
| xargs -r -n1 -IREPO git clone ${GH_SSH}:${GH_ORG}/REPO.git
}
_main() {
# Runs one of the functions detailed above
# Run the command
local cmd="${1}" && shift
${cmd} "$@"
}
_main "$@"
@basepi
Copy link

basepi commented Aug 10, 2013

Yay docs!

That is all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment