Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active January 10, 2022 13:11
Show Gist options
  • Save miraculixx/b4c30db06bb1f3b56057 to your computer and use it in GitHub Desktop.
Save miraculixx/b4c30db06bb1f3b56057 to your computer and use it in GitHub Desktop.
batch adding users to github accounts
#!/bin/bash
# Collaborator list, add, remove from a repository
# (c) 2015 miraculixx
# Author: github.com/miraculixx
# MIT License, see below
function help {
echo "Add collaborators to one or more repositories on github"
echo ""
echo "Syntax: $0 -u user -p password [-l] [-D] -r repo1,repo2 <collaborator id>"
echo ""
echo " -u User to access github"
echo " -p Password (optional, will be promoted otherwise)"
echo " -l list collaborators"
echo " -r repositories, list as owner/repo[,owner/repo,...]"
echo " -D remove"
echo " id the collaborator id to add or remove"
}
while getopts "h?u:p:r:D:l?" opt; do
case $opt in
h|\?)
help
exit 0
;;
u)
GH_USER=$OPTARG
;;
p)
PASSWORD=$OPTARG
;;
D)
METHOD=DELETE
;;
r)
REPOS=$OPTARG
;;
l)
LIST=yes
;;
esac
done
shift $((OPTIND-1))
COL_USER=$1
if [[ -z "$GH_USER" ]]; then
echo Enter your github user
read GH_USER
fi
if [[ -z "$PASSWORD" ]]; then
echo Enter Password
read -s PASSWORD
fi
if [[ -z "$REPOS" ]]; then
echo Enter the repositories as user/repo. Multiple repos comma separated.
read REPOS
fi
if [[ -z "$COL_USER" ]]; then
LIST=yes
fi
if [[ -z "$METHOD" ]] && [[ ! -z "$COL_USER" ]]; then
echo "[WARN] Assuming you want to add user $COL_USER. Use the -D option to delete"
METHOD=PUT
fi
array=(${REPOS//,/ })
if [[ ! -z "$COL_USER" ]]; then
for repo in "${array[@]}"; do
echo "[INFO] $METHOD $COL_USER to $repo"
curl -i -u "$GH_USER:$PASSWORD" -X $METHOD -d '' "https://api.github.com/repos/$repo/collaborators/$COL_USER" 2>&1 | grep message || echo "OK, done."
done
fi
if [[ ! -z "$LIST" ]]; then
for repo in "${array[@]}"; do
echo "[INFO] Current list of collaborators in $repo:"
curl -i -u "$GH_USER:$PASSWORD" -X GET -d '' "https://api.github.com/repos/$repo/collaborators" 2>&1 | grep login
done
fi
exit 0
: <<< 'EOF'
The MIT License (MIT)
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
EOF
@sleoj
Copy link

sleoj commented Jun 23, 2017

There's a typo on line 20, "getopts". The string should be "h?u:p:r:Dl?" without a colon following the "D". The colon makes the parser thing "D" has a following argument, and it does not.

@sleoj
Copy link

sleoj commented Jun 23, 2017

I have also found that this script can also be adapted to GitHub Enterprise by substituting "https://<gheurl>/api/<apiversion>" for "https://api.github.com", at least in the case of the GHE instance that I use. The API version I used was "v3". (Opening that URL in the browser should produce a JSON page similar to opening https://api.github.com in a browser, though it might be a whole lot shorter and may have a message about requiring authentication.)

@sophaz
Copy link

sophaz commented Jul 25, 2018

To make it work for comma separated list of collaborators, replace lines 72 to 79 with:

repos=(${REPOS//,/ })
collaborators=(${COL_USER//,/ })

if [[ ! -z "$COL_USER" ]]; then
  for repo in "${repos[@]}"; do
    for collaborator in "${collaborators[@]}"; do
      echo "[INFO] $METHOD $collaborator to $repo"
      curl -i -u "$GH_USER:$PASSWORD" -X $METHOD -d '' "https://api.github.com/repos/$repo/collaborators/$collaborator" 2>&1 | grep message || echo "OK, done."
    done
  done
fi```

@bonelifer
Copy link

Password (optional, will be promoted otherwise

Do you mean "will not be promoted otherwise"?

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