Skip to content

Instantly share code, notes, and snippets.

@jonico
Forked from muhammaddadu/github-add-colaborator
Last active April 24, 2023 19:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonico/d002e30391e8b53919409690704ecdb7 to your computer and use it in GitHub Desktop.
Save jonico/d002e30391e8b53919409690704ecdb7 to your computer and use it in GitHub Desktop.
List, add and remove multiple collaborators from multiple repositories
#!/bin/bash
function help {
echo "Add collaborators to one or more repositories on github"
echo ""
echo "Syntax: $0 -u user [-l] [-D] -r repo1,repo2 <collaborator id>"
echo ""
echo " -u OAuth token to access github"
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:Dl?" opt; do
case $opt in
h|\?)
help
exit 0
;;
u)
OAUTH_TOKEN=$OPTARG
;;
D)
METHOD=DELETE
;;
r)
REPOS=$OPTARG
;;
l)
LIST=yes
;;
esac
done
shift $((OPTIND-1))
COL_USER=$1
if [[ -z "$OAUTH_TOKEN" ]]; then
echo Enter your github PAT / OAuth token
read OAUTH_TOKEN
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//,/ })
arrayUser=(${COL_USER//,/ })
if [[ ! -z "$COL_USER" ]]; then
for repo in "${array[@]}"; do
for user in "${arrayUser[@]}"; do
echo "[INFO] $METHOD $user to $repo"
curl -i -H "Authorization: token $OAUTH_TOKEN" -X $METHOD -d '' "https://api.github.com/repos/$repo/collaborators/$user" 2>&1 | grep message || echo "OK, done."
done
done
fi
if [[ ! -z "$LIST" ]]; then
for repo in "${array[@]}"; do
echo "[INFO] Current list of collaborators in $repo:"
curl -sS -H "Authorization: token $OAUTH_TOKEN" -X GET -d '' "https://api.github.com/repos/$repo/collaborators?affiliation=outside" 2>&1 | jq ".[]| .login"
done
fi
exit 0
@jonico
Copy link
Author

jonico commented May 11, 2020

First, download the gist to a local file and make it executable

chmod a+x github-collaborators.sh

Second, generate a personal access token (PAT)

Select repo and admin:orgscopes for the PAT and authorize it if you like to use it in SSO enabled orgs.

Example how to add collaborators user1 and user2 to org1/repo1, org1/repo2and org2/repo3 with write access

./github-collaborators.sh -u <PAT> -r org1/repo1,org1/repo2,org2/repo3 user1,user2

Users have to accept the invitation first, before they get access.

Example how to delete collaborator user2 from org1/repo1 and org2/repo3

./github-collaborators.sh -u <PAT> -D -r org1/repo1,org2/repo3 user2

You can only remove collaborators with this script if they already accepted the invitation.

Example how to list all outside collaborators from org1/repo1 and org2/repo3

./github-collaborators.sh -u <PAT> -r org1/repo1,org2/repo3

You need the jq command installed if you like to list all outside collaborators.

Listing all repos within your org

If you need to determine a list of all your repositories within your org, have a look at this script.

@cj-tomas-jundulas-1
Copy link

Very useful script, the only thing missing is permissions. We need to add collaborators with Read access.

@tobiasehlert
Copy link

Very useful script, the only thing missing is permissions. We need to add collaborators with Read access.

@cj-tomas-jundulas-1, good point :)

You can only grant permission to collaborator on organization-owned repositories.

You could update line 66 to change permissions.
More specific the -d '' to following -d '{"permission":"triage"}' or some other based on your liking.

Permission options (which can be extended by custom roles) are:
pull, triage, push, maintain and admin

Default permission is push.

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