Forked from muhammaddadu/github-add-colaborator
Last active
October 20, 2024 13:53
-
-
Save jonico/d002e30391e8b53919409690704ecdb7 to your computer and use it in GitHub Desktop.
List, add and remove multiple collaborators from multiple repositories
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 | |
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 |
Very useful script, the only thing missing is permissions. We need to add collaborators with Read access.
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
First, download the gist to a local file and make it executable
Second, generate a personal access token (PAT)
Select
repo
andadmin:org
scopes for the PAT and authorize it if you like to use it in SSO enabled orgs.Example how to add collaborators
user1
anduser2
toorg1/repo1
,org1/repo2
andorg2/repo3
with write accessUsers have to accept the invitation first, before they get access.
Example how to delete collaborator
user2
fromorg1/repo1
andorg2/repo3
You can only remove collaborators with this script if they already accepted the invitation.
Example how to list all outside collaborators from
org1/repo1
andorg2/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.